mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-11 22:53:51 +01:00
Eleventh batch of the 250-line sweep (over-cap count 12 -> 5). Every test file in the repo, Python and Dart, is now under cap; the five left are all in app/lib/. wrapper_server_test.dart 349 -> 145 (+ proxy, account siblings) app_settings_service_test.dart 321 -> 162 (+ sync sibling) average_service_test.dart 299 -> 158 (+ periods sibling) settings_screen_test.dart 274 -> 189 (+ battery sibling) day_status_service_test.dart 270 -> 161 (+ streaks sibling) edit_entry_screen_test.dart 269 -> 159 (+ foodbank sibling) day_status_calendar_test.dart 256 -> 237 (+ _test_support sibling) day_status_calendar_test is a single group whose tests close over group-local state, so it could not be split by block. Its ~25 lines of file-level fixtures moved to `day_status_calendar_test_support.dart` instead, which required the same publicisation as the sync support file (`_theme` -> `appTheme`, `_wrap` -> `wrapInApp`, `_colorOfDay` -> `colorOfDay`) with doc comments. Block counts were compared against HEAD per family rather than trusting the suite total, since the duplication bug in the previous batch passed green at an inflated count. All seven families match exactly (29/26/34/11/34/8/14). Verified: flutter test 555 (unchanged), flutter analyze clean, pre-commit clean. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
183 lines
5.6 KiB
Dart
183 lines
5.6 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:diet_guard_app/services/budget_history_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();
|
|
BudgetHistoryService.resetForTesting();
|
|
});
|
|
|
|
tearDown(() async {
|
|
AppSettingsService.resetForTesting();
|
|
BudgetHistoryService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
group('applySyncedBudget', () {
|
|
test(
|
|
'persists the given value and updatedAt verbatim, not "now"',
|
|
() async {
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
BudgetHistoryService.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));
|
|
BudgetHistoryService.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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: FileDocumentStore(tempDir),
|
|
);
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 1600);
|
|
});
|
|
|
|
test('keeps default 2200 when file does not exist', () async {
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: 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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: 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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: 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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: 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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: 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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: 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));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: FileDocumentStore(tempDir),
|
|
);
|
|
|
|
expect(AppSettingsService.dailyKcalGoalUpdatedAt, isNull);
|
|
},
|
|
);
|
|
});
|
|
}
|