mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 05:14:22 +01:00
Flutter's Linux embedder manages ~20fps at 3840x2160 -- measured on a stock `flutter create` counter app, so it is the toolkit rather than this code -- where the same Dart rendered by Chrome sustains ~144fps (evidence in ~/todo/docs/desktop-performance-findings.md). The desktop target therefore becomes a Flutter *web* build; Android is untouched. `dart:io` does not fail a web *compile* -- it resolves to a stub that throws at runtime -- so every platform edge moves behind a conditional export rather than being discovered later as a blank white window: - storage: `document_store*.dart`, files on Android and IndexedDB in the browser (mirrored to the desktop wrapper, which is what makes a wiped Chrome profile recoverable). Document names stay the exact pre-web filenames so an installed phone keeps reading its existing log. - photos: `blob_store*.dart` plus `attached_image*.dart`, since `Image.file` cannot resolve an IndexedDB key. The picker itself has web support; what changed is the storage and display model. Camera *capture* is genuinely lost on desktop -- and photos never crossed devices anyway, `imagePath` being stripped before push. - scheduling: `background_tasks*.dart`. A browser has no WorkManager equivalent -- a closed tab runs nothing -- so the desktop runs an in-page 5-minute check while its window is open. That loses nothing on the PC, where `diet-guard-gate.timer` already locks the screen on an unlogged slot, which is strictly stronger than a notification. - notifications: `notification_backend*.dart`, the browser Notifications API in place of flutter_local_notifications. - the GitHub token: `token_vault*.dart`. On web it is *not* held by the browser at all -- flutter_secure_storage's web backend is WebCrypto-wrapped localStorage with the wrap key beside the ciphertext, which is obfuscation, not encryption at rest -- so the desktop wrapper owns it and proxies the API (`github_client_factory*.dart`). `Platform.isAndroid` itself throws on web, so every platform branch now tests `kIsWeb` first; the battery-optimization row (permission_handler, no web implementation) is hidden off Android. The desktop pushes under device id `desktop`, not `phone`: sharing an id would have made the two devices overwrite each other's pushed log on every tick. The Python side enumerates device directories generically, so it needs no change. Tests move onto the same seams -- the file-backed stores keep the atomicity and unreadable-file guarantees where they belong. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
305 lines
10 KiB
Dart
305 lines
10 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/services/document_store_io.dart';
|
|
import 'package:diet_guard_app/services/app_settings_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp(
|
|
'diet_guard_settings_test_',
|
|
);
|
|
AppSettingsService.resetForTesting();
|
|
});
|
|
|
|
tearDown(() async {
|
|
AppSettingsService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
group('dailyKcalGoal static getter', () {
|
|
test('returns 2200 when singleton is uninitialised', () {
|
|
// Singleton is null after resetForTesting() — exercises the ?? 2200 branch.
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
});
|
|
|
|
group('resetForTesting', () {
|
|
test('with testDir creates a working instance', () {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
expect(AppSettingsService.instance, isNotNull);
|
|
});
|
|
|
|
test('without testDir nulls the singleton', () {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
AppSettingsService.resetForTesting();
|
|
// instance getter throws when null — verify via dailyKcalGoal fallback.
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
});
|
|
|
|
group('init early-return', () {
|
|
test('returns existing instance without re-initialising', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
final first = AppSettingsService.instance;
|
|
// init() sees _instance != null and returns early (no platform channel).
|
|
final second = await AppSettingsService.init();
|
|
expect(identical(first, second), isTrue);
|
|
});
|
|
});
|
|
|
|
group('saveDailyKcalGoal', () {
|
|
test('updates in-memory value and persists to file', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.instance.saveDailyKcalGoal(1800);
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 1800);
|
|
|
|
final raw = await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).readAsString();
|
|
final data = jsonDecode(raw) as Map;
|
|
expect(data['daily_kcal_goal'], 1800);
|
|
});
|
|
|
|
test('stamps a fresh dailyKcalGoalUpdatedAt', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
final before = DateTime.now();
|
|
await AppSettingsService.instance.saveDailyKcalGoal(1800);
|
|
final after = DateTime.now();
|
|
|
|
final updatedAt = AppSettingsService.dailyKcalGoalUpdatedAt;
|
|
expect(updatedAt, isNotNull);
|
|
expect(updatedAt!.isBefore(before), isFalse);
|
|
expect(updatedAt.isAfter(after), isFalse);
|
|
});
|
|
});
|
|
|
|
group('applySyncedBudget', () {
|
|
test(
|
|
'persists the given value and updatedAt verbatim, not "now"',
|
|
() async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
final winningEdit = DateTime.utc(2020);
|
|
|
|
await AppSettingsService.instance.applySyncedBudget(
|
|
1700,
|
|
updatedAt: winningEdit,
|
|
);
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 1700);
|
|
expect(AppSettingsService.dailyKcalGoalUpdatedAt, winningEdit);
|
|
|
|
final raw = await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).readAsString();
|
|
final data = jsonDecode(raw) as Map;
|
|
expect(data['daily_kcal_goal'], 1700);
|
|
expect(
|
|
data['daily_kcal_goal_updated_at'],
|
|
winningEdit.toIso8601String(),
|
|
);
|
|
},
|
|
);
|
|
|
|
test('a null updatedAt is accepted and persisted as null', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.instance.applySyncedBudget(1700);
|
|
expect(AppSettingsService.dailyKcalGoalUpdatedAt, isNull);
|
|
});
|
|
});
|
|
|
|
group('initForTesting (_load paths)', () {
|
|
test('loads daily_kcal_goal from an existing file', () async {
|
|
await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).writeAsString(jsonEncode({'daily_kcal_goal': 1600}));
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 1600);
|
|
});
|
|
|
|
test('keeps default 2200 when file does not exist', () async {
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
|
|
test('keeps default 2200 on unparsable JSON', () async {
|
|
await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).writeAsString('not json at all');
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
|
|
test('keeps default 2200 when JSON root is not a Map', () async {
|
|
await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).writeAsString(jsonEncode([1, 2, 3]));
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
|
|
test('keeps default 2200 when daily_kcal_goal is not an int', () async {
|
|
await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).writeAsString(jsonEncode({'daily_kcal_goal': 'two thousand'}));
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
|
|
test('loads daily_kcal_goal_updated_at from an existing file', () async {
|
|
await File('${tempDir.path}/app_settings.json').writeAsString(
|
|
jsonEncode({
|
|
'daily_kcal_goal': 1600,
|
|
'daily_kcal_goal_updated_at': '2026-01-01T00:00:00.000Z',
|
|
}),
|
|
);
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(
|
|
AppSettingsService.dailyKcalGoalUpdatedAt,
|
|
DateTime.parse('2026-01-01T00:00:00.000Z'),
|
|
);
|
|
});
|
|
|
|
test(
|
|
'dailyKcalGoalUpdatedAt stays null when file has no such key',
|
|
() async {
|
|
await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).writeAsString(jsonEncode({'daily_kcal_goal': 1600}));
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoalUpdatedAt, isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'dailyKcalGoalUpdatedAt stays null when the field is not a string',
|
|
() async {
|
|
await File('${tempDir.path}/app_settings.json').writeAsString(
|
|
jsonEncode({
|
|
'daily_kcal_goal': 1600,
|
|
'daily_kcal_goal_updated_at': 12345,
|
|
}),
|
|
);
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.dailyKcalGoalUpdatedAt, isNull);
|
|
},
|
|
);
|
|
|
|
test('loads reward_label and reward_url from an existing file', () async {
|
|
await File('${tempDir.path}/app_settings.json').writeAsString(
|
|
jsonEncode({
|
|
'daily_kcal_goal': 1600,
|
|
'reward_label': 'Podcast',
|
|
'reward_url': 'https://example.com/podcast',
|
|
}),
|
|
);
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.rewardLabel, 'Podcast');
|
|
expect(AppSettingsService.rewardUrl, 'https://example.com/podcast');
|
|
});
|
|
|
|
test('reward fields stay null when the file has no such keys', () async {
|
|
await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).writeAsString(jsonEncode({'daily_kcal_goal': 1600}));
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.rewardLabel, isNull);
|
|
expect(AppSettingsService.rewardUrl, isNull);
|
|
});
|
|
|
|
test('reward fields stay null when not strings', () async {
|
|
await File('${tempDir.path}/app_settings.json').writeAsString(
|
|
jsonEncode({
|
|
'daily_kcal_goal': 1600,
|
|
'reward_label': 123,
|
|
'reward_url': false,
|
|
}),
|
|
);
|
|
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
|
|
expect(AppSettingsService.rewardLabel, isNull);
|
|
expect(AppSettingsService.rewardUrl, isNull);
|
|
});
|
|
});
|
|
|
|
group('rewardLabel / rewardUrl static getters', () {
|
|
test('default to null when singleton is uninitialised', () {
|
|
expect(AppSettingsService.rewardLabel, isNull);
|
|
expect(AppSettingsService.rewardUrl, isNull);
|
|
});
|
|
});
|
|
|
|
group('saveReward', () {
|
|
test('persists label and url, readable back via static getters', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.instance.saveReward(
|
|
label: 'Podcast',
|
|
url: 'https://example.com/podcast',
|
|
);
|
|
|
|
expect(AppSettingsService.rewardLabel, 'Podcast');
|
|
expect(AppSettingsService.rewardUrl, 'https://example.com/podcast');
|
|
|
|
final raw = await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).readAsString();
|
|
final data = jsonDecode(raw) as Map;
|
|
expect(data['reward_label'], 'Podcast');
|
|
expect(data['reward_url'], 'https://example.com/podcast');
|
|
});
|
|
|
|
test('clears fields back to null when saved as null', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.instance.saveReward(
|
|
label: 'Podcast',
|
|
url: 'https://example.com/podcast',
|
|
);
|
|
await AppSettingsService.instance.saveReward(label: null, url: null);
|
|
|
|
expect(AppSettingsService.rewardLabel, isNull);
|
|
expect(AppSettingsService.rewardUrl, isNull);
|
|
});
|
|
|
|
test('does not disturb the existing kcal goal fields', () async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.instance.saveDailyKcalGoal(1800);
|
|
await AppSettingsService.instance.saveReward(
|
|
label: 'Podcast',
|
|
url: 'https://example.com/podcast',
|
|
);
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 1800);
|
|
|
|
final raw = await File(
|
|
'${tempDir.path}/app_settings.json',
|
|
).readAsString();
|
|
final data = jsonDecode(raw) as Map;
|
|
expect(data['daily_kcal_goal'], 1800);
|
|
});
|
|
});
|
|
}
|