Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Trim device health test scaffolding

Neeraj Gupta2cbe31f7fa55
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_health.dart b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_health.dart
index fe5068381a..af31c397cf 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_health.dart
+++ b/mobile/apps/photos/plugins/ente_photos_platform/lib/src/device_health.dart
@@ -260,7 +260,7 @@ class EntePhotosDeviceHealth implements DeviceHealthSource, DeviceMemorySource {
throw const FormatException('Device health event is not a map');
}
return DeviceHealthSnapshot.fromMap(event);
- }).asBroadcastStream();
+ });
}
Map<dynamic, dynamic> _requiredMap(Object? value, String name) {
diff --git a/mobile/apps/photos/plugins/ente_photos_platform/test/device_health_test.dart b/mobile/apps/photos/plugins/ente_photos_platform/test/device_health_test.dart
index 67264331d3..777b8ecba6 100644
--- a/mobile/apps/photos/plugins/ente_photos_platform/test/device_health_test.dart
+++ b/mobile/apps/photos/plugins/ente_photos_platform/test/device_health_test.dart
@@ -15,13 +15,6 @@ void main() {
expect(snapshot.battery.temperatureCelsius, 31.5);
expect(snapshot.battery.health, DeviceBatteryHealth.good);
expect(snapshot.thermal.state, DeviceThermalState.moderate);
- expect(
- snapshot.isFreshAt(
- snapshot.observedAt.add(const Duration(seconds: 30)),
- const Duration(minutes: 1),
- ),
- isTrue,
- );
});
test('accepts iOS battery observations without private metrics', () {
@@ -81,23 +74,20 @@ void main() {
);
});
- test('future observations are not fresh', () {
- final snapshot = DeviceHealthSnapshot.fromMap(_snapshotMap());
- expect(
- snapshot.isFreshAt(
- snapshot.observedAt.subtract(const Duration(milliseconds: 1)),
- const Duration(minutes: 1),
- ),
- isFalse,
- );
- });
-
- test('source requests and parses the typed native snapshot', () async {
- const channel = MethodChannel('device-health-test');
+ test('source requests typed health and memory snapshots', () async {
+ const channel = MethodChannel('device-health-source-test');
+ final methods = <String>[];
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (call) async {
- expect(call.method, 'deviceHealth.getSnapshot');
- return _snapshotMap();
+ methods.add(call.method);
+ return switch (call.method) {
+ 'deviceHealth.getSnapshot' => _snapshotMap(),
+ 'deviceHealth.getMemorySnapshot' => {
+ 'status': 'available',
+ 'totalBytes': 8 * 1024 * 1024 * 1024,
+ },
+ _ => throw MissingPluginException(),
+ };
});
addTearDown(
() => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
@@ -106,25 +96,14 @@ void main() {
final source = EntePhotosDeviceHealth(methodChannel: channel);
expect((await source.getSnapshot()).battery.levelPercent, 64);
- });
-
- test('source requests and parses physical memory separately', () async {
- const channel = MethodChannel('device-memory-test');
- TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
- .setMockMethodCallHandler(channel, (call) async {
- expect(call.method, 'deviceHealth.getMemorySnapshot');
- return {'status': 'available', 'totalBytes': 8 * 1024 * 1024 * 1024};
- });
- addTearDown(
- () => TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
- .setMockMethodCallHandler(channel, null),
- );
-
- final source = EntePhotosDeviceHealth(methodChannel: channel);
expect(
(await source.getMemorySnapshot()).totalBytes,
8 * 1024 * 1024 * 1024,
);
+ expect(methods, [
+ 'deviceHealth.getSnapshot',
+ 'deviceHealth.getMemorySnapshot',
+ ]);
});
}
diff --git a/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart b/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart
index 5ac371285c..1a63d2eba3 100644
--- a/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart
+++ b/mobile/apps/photos/test/services/machine_learning/device_health_policy_test.dart
@@ -5,148 +5,121 @@ import 'package:photos/services/machine_learning/device_health_policy.dart';
void main() {
const policy = DeviceHealthPolicy();
final now = DateTime(2026, 8, 6, 12);
+ Set<DeviceHealthIssue> issues(DeviceHealthSnapshot snapshot) =>
+ policy.evaluate(snapshot, now: now).issues;
test('accepts fresh healthy Android evidence', () {
- expect(policy.evaluate(_snapshot(now), now: now).issues, isEmpty);
+ expect(issues(_snapshot(now)), isEmpty);
});
test('rejects stale and future observations', () {
- expect(
- policy
- .evaluate(
- _snapshot(now.subtract(const Duration(minutes: 3))),
- now: now,
- )
- .issues,
- {DeviceHealthIssue.staleObservation},
- );
- expect(
- policy
- .evaluate(
- _snapshot(now.add(const Duration(milliseconds: 1))),
- now: now,
- )
- .issues,
- {DeviceHealthIssue.staleObservation},
- );
+ for (final observedAt in [
+ now.subtract(const Duration(minutes: 3)),
+ now.add(const Duration(milliseconds: 1)),
+ ]) {
+ expect(issues(_snapshot(observedAt)), {
+ DeviceHealthIssue.staleObservation,
+ });
+ }
});
test('rejects unavailable or low battery evidence', () {
expect(
- policy
- .evaluate(
- _snapshot(
- now,
- battery: const DeviceBatterySnapshot(
- status: DeviceSignalStatus.unavailable,
- errorCode: 'battery_read_failed',
- ),
- ),
- now: now,
- )
- .issues,
+ issues(
+ _snapshot(
+ now,
+ battery: const DeviceBatterySnapshot(
+ status: DeviceSignalStatus.unavailable,
+ errorCode: 'battery_read_failed',
+ ),
+ ),
+ ),
contains(DeviceHealthIssue.batteryUnavailable),
);
expect(
- policy
- .evaluate(
- _snapshot(now, battery: _androidBattery(level: 19)),
- now: now,
- )
- .issues,
+ issues(_snapshot(now, battery: _androidBattery(level: 19))),
contains(DeviceHealthIssue.lowBattery),
);
});
test('rejects hot or unknown Android battery evidence', () {
expect(
- policy
- .evaluate(
- _snapshot(now, battery: _androidBattery(temperature: 42.1)),
- now: now,
- )
- .issues,
+ issues(_snapshot(now, battery: _androidBattery(temperature: 42.1))),
contains(DeviceHealthIssue.batteryTooHot),
);
expect(
- policy
- .evaluate(
- _snapshot(
- now,
- battery: _androidBattery(health: DeviceBatteryHealth.unknown),
- ),
- now: now,
- )
- .issues,
+ issues(
+ _snapshot(
+ now,
+ battery: _androidBattery(health: DeviceBatteryHealth.unknown),
+ ),
+ ),
contains(DeviceHealthIssue.batteryHealthUnavailable),
);
});
test('rejects serious thermal state', () {
expect(
- policy
- .evaluate(
- _snapshot(
- now,
- thermal: const DeviceThermalSnapshot(
- status: DeviceSignalStatus.available,
- state: DeviceThermalState.serious,
- ),
- ),
- now: now,
- )
- .issues,
+ issues(
+ _snapshot(
+ now,
+ thermal: const DeviceThermalSnapshot(
+ status: DeviceSignalStatus.available,
+ state: DeviceThermalState.serious,
+ ),
+ ),
+ ),
contains(DeviceHealthIssue.thermalTooHigh),
);
});
test('uses Android battery evidence when thermal APIs are unsupported', () {
- final evaluation = policy.evaluate(
- _snapshot(
- now,
- thermal: const DeviceThermalSnapshot(
- status: DeviceSignalStatus.unsupported,
+ expect(
+ issues(
+ _snapshot(
+ now,
+ thermal: const DeviceThermalSnapshot(
+ status: DeviceSignalStatus.unsupported,
+ ),
),
),
- now: now,
+ isEmpty,
);
-
- expect(evaluation.issues, isEmpty);
});
test('accepts iOS without Android-only battery metrics', () {
- final evaluation = policy.evaluate(
- _snapshot(
- now,
- platform: DeviceHealthPlatform.ios,
- battery: const DeviceBatterySnapshot(
- status: DeviceSignalStatus.available,
- levelPercent: 80,
+ expect(
+ issues(
+ _snapshot(
+ now,
+ platform: DeviceHealthPlatform.ios,
+ battery: const DeviceBatterySnapshot(
+ status: DeviceSignalStatus.available,
+ levelPercent: 80,
+ ),
),
),
- now: now,
+ isEmpty,
);
-
- expect(evaluation.issues, isEmpty);
});
test('does not treat unsupported iOS thermal evidence as healthy', () {
- final evaluation = policy.evaluate(
- _snapshot(
- now,
- platform: DeviceHealthPlatform.ios,
- battery: const DeviceBatterySnapshot(
- status: DeviceSignalStatus.available,
- levelPercent: 80,
- ),
- thermal: const DeviceThermalSnapshot(
- status: DeviceSignalStatus.unsupported,
+ expect(
+ issues(
+ _snapshot(
+ now,
+ platform: DeviceHealthPlatform.ios,
+ battery: const DeviceBatterySnapshot(
+ status: DeviceSignalStatus.available,
+ levelPercent: 80,
+ ),
+ thermal: const DeviceThermalSnapshot(
+ status: DeviceSignalStatus.unsupported,
+ ),
),
),
- now: now,
+ contains(DeviceHealthIssue.thermalUnavailable),
);
-
- expect(evaluation.issues, contains(DeviceHealthIssue.thermalUnavailable));
});
}