mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-10 19:07:15 +01:00
CI on main was already red before the crdt_sync migration. Make the pre-commit --all-files run and the test job green: - requirements.txt: add crdt-sync (now a runtime dep) and mcp so CI's `pip install -r requirements.txt` can import diet_guard._sync and collect test_mcp.py (both were ModuleNotFoundError in CI). - .pre-commit-config.yaml: add mcp + pydantic to the pylint hook deps so it can resolve diet_guard._mcp's imports (was E0401). - pyproject.toml: raise pylint min-similarity-lines to 8 — the shared nutrition-field keys in _foodbank/_mcp mirror one domain schema, not real duplicate code. - scripts/setup_mcp.sh: split declare/assign for SCRIPT_DIR/REPO_DIR (shellcheck SC2155). - codespell: fix "packge"/"unparseable" typos; trailing-whitespace auto-fixes across app/.agents/skills docs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RRCdR4D8SaVrN2xraMQxM
116 lines
3.5 KiB
Dart
116 lines
3.5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
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(testDir: tempDir);
|
|
expect(AppSettingsService.instance, isNotNull);
|
|
});
|
|
|
|
test('without testDir nulls the singleton', () {
|
|
AppSettingsService.resetForTesting(testDir: 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(testDir: 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(testDir: 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);
|
|
});
|
|
});
|
|
|
|
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(tempDir);
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 1600);
|
|
});
|
|
|
|
test('keeps default 2200 when file does not exist', () async {
|
|
await AppSettingsService.initForTesting(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(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(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(tempDir);
|
|
|
|
expect(AppSettingsService.dailyKcalGoal, 2200);
|
|
});
|
|
});
|
|
}
|