diet-guard/app/test/services/app_settings_service_test.dart
Krzysztof kuhy Rudnicki 4c6083b768 Add food bank, edit-entry, and app settings screens; history redesign
Work-in-progress feature set accumulated ahead of the log-meal compact
layout change: a food bank browser, a shared entry-edit screen, an
app-wide settings service, and a substantially reworked history screen
with filtering/sorting.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018UorgLvWJ4huH55tmXoUAZ
2026-07-04 05:18:32 +02:00

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 unparseable 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);
});
});
}