mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 18:32:25 +01:00
- FoodEntry.slot -> slotMinute: read via entrySlotMinute, written via slotFields (whole-hour stays byte-identical, else slot_min), kept by copyWithDeleted and the edit screen, so the phone never republishes a stripped copy of a PC entry. Out-of-range peer values echo, never throw. - Satisfaction is nearest-slot snapping (satisfiedSlots) everywhere: loggedSlotsToday, due check, kuchnia occupancy, future-day logged set (that day's schedule), slot selector. Schedule edits no longer un-log. - Future pre-log builds DateTime from slot ~/ 60, slot % 60. - Schedule history + sync go through scheduleToWire/scheduleFromWire. - Notifications: ids are slot minutes; syncToSlots cancels whatever the platform reports active (getActiveNotifications, reminder channel) that is not due, covering legacy hour ids; full 0..1439 sweep only if the lookup throws. - Settings: 15-minute dropdowns + "Custom..." (24h showTimePicker), off-grid values always selectable, counts capped by grid capacity; editor moved to widgets/meal_schedule_editor.dart + meal_time_dropdown.dart. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015npsYVB2PdtWkG7MPAi4tn
212 lines
7.3 KiB
Dart
212 lines
7.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/models/local_time.dart';
|
|
import 'package:diet_guard_app/models/meal_component.dart';
|
|
import 'package:diet_guard_app/models/nutrition.dart';
|
|
import 'package:diet_guard_app/services/document_store_io.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
const _manual = Nutrition(
|
|
kcal: 150,
|
|
proteinG: 5,
|
|
carbsG: 20,
|
|
fatG: 3,
|
|
grams: 50,
|
|
source: 'manual',
|
|
);
|
|
|
|
void main() {
|
|
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_test_');
|
|
LogStorageService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
});
|
|
|
|
tearDown(() async {
|
|
LogStorageService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
group('readLog', () {
|
|
test('returns an empty log when no file exists yet', () async {
|
|
expect(await LogStorageService.instance.readLog(), isEmpty);
|
|
});
|
|
|
|
test('returns an empty log for unparsable JSON', () async {
|
|
final file = File('${tempDir.path}/food_log.json');
|
|
await file.writeAsString('not json');
|
|
expect(await LogStorageService.instance.readLog(), isEmpty);
|
|
});
|
|
|
|
test('returns an empty log when the JSON root is not a map', () async {
|
|
final file = File('${tempDir.path}/food_log.json');
|
|
await file.writeAsString('[]');
|
|
expect(await LogStorageService.instance.readLog(), isEmpty);
|
|
});
|
|
|
|
test('skips a date key whose value is not a list', () async {
|
|
final file = File('${tempDir.path}/food_log.json');
|
|
await file.writeAsString('{"2026-06-22": "not a list"}');
|
|
expect(await LogStorageService.instance.readLog(), isEmpty);
|
|
});
|
|
});
|
|
group('logMeal', () {
|
|
test('assigns a fresh id and never an hmac', () async {
|
|
final entry = await LogStorageService.instance.logMeal(
|
|
'toast',
|
|
_manual,
|
|
slotMinute: 480,
|
|
);
|
|
expect(entry.id, isNotEmpty);
|
|
expect(entry.hmac, isNull);
|
|
expect(entry.slotMinute, 480);
|
|
expect(entry.desc, 'toast');
|
|
});
|
|
|
|
test('persists components when given', () async {
|
|
const components = [
|
|
MealComponent(
|
|
name: 'bread',
|
|
kcal: 100,
|
|
proteinG: 3,
|
|
carbsG: 18,
|
|
fatG: 1,
|
|
grams: 40,
|
|
),
|
|
];
|
|
final entry = await LogStorageService.instance.logMeal(
|
|
'toast meal',
|
|
_manual,
|
|
components: components,
|
|
);
|
|
expect(entry.components, hasLength(1));
|
|
final reloaded = await LogStorageService.instance.todayEntries();
|
|
expect(reloaded.single.components!.single.name, 'bread');
|
|
});
|
|
|
|
test("two logged meals both persist under today's date key", () async {
|
|
await LogStorageService.instance.logMeal('a', _manual);
|
|
await LogStorageService.instance.logMeal('b', _manual);
|
|
final entries = await LogStorageService.instance.todayEntries();
|
|
expect(entries, hasLength(2));
|
|
});
|
|
|
|
test('a future `when` buckets the entry under that future date', () async {
|
|
final future = DateTime.now().add(const Duration(days: 1, hours: 1));
|
|
final entry = await LogStorageService.instance.logMeal(
|
|
'future meal',
|
|
_manual,
|
|
slotMinute: 720,
|
|
when: future,
|
|
);
|
|
final log = await LogStorageService.instance.readLog();
|
|
expect(log[localDateKey(future)]!.single.id, entry.id);
|
|
expect(await LogStorageService.instance.todayEntries(), isEmpty);
|
|
});
|
|
|
|
test('rejects a `when` before now', () {
|
|
final past = DateTime.now().subtract(const Duration(days: 1));
|
|
expect(
|
|
() => LogStorageService.instance.logMeal('late', _manual, when: past),
|
|
throwsArgumentError,
|
|
);
|
|
});
|
|
});
|
|
group('undoLastToday', () {
|
|
test('returns null when today has no entries', () async {
|
|
expect(await LogStorageService.instance.undoLastToday(), isNull);
|
|
});
|
|
|
|
test('tombstones the most recent entry in place', () async {
|
|
await LogStorageService.instance.logMeal('first', _manual);
|
|
final second = await LogStorageService.instance.logMeal(
|
|
'second',
|
|
_manual,
|
|
);
|
|
final undone = await LogStorageService.instance.undoLastToday();
|
|
expect(undone!.id, second.id);
|
|
expect(undone.deleted, isTrue);
|
|
});
|
|
|
|
test('tombstoned entries are excluded from todayEntries', () async {
|
|
await LogStorageService.instance.logMeal('only', _manual);
|
|
await LogStorageService.instance.undoLastToday();
|
|
expect(await LogStorageService.instance.todayEntries(), isEmpty);
|
|
});
|
|
|
|
test("never touches a previous day's entries", () async {
|
|
final yesterdayKey = localDateKey(
|
|
DateTime.now().subtract(const Duration(days: 1)),
|
|
);
|
|
final yesterday = FoodEntry(
|
|
id: 'yesterday-1',
|
|
time: '${yesterdayKey}T08:00:00+02:00',
|
|
desc: 'yesterday meal',
|
|
grams: 50,
|
|
kcal: 150,
|
|
proteinG: 5,
|
|
carbsG: 20,
|
|
fatG: 3,
|
|
source: 'manual',
|
|
);
|
|
await LogStorageService.instance.writeLog({
|
|
yesterdayKey: [yesterday],
|
|
});
|
|
await LogStorageService.instance.logMeal('today', _manual);
|
|
|
|
expect(await LogStorageService.instance.undoLastToday(), isNotNull);
|
|
|
|
final log = await LogStorageService.instance.readLog();
|
|
expect(log[yesterdayKey]!.single.deleted, isFalse);
|
|
});
|
|
|
|
test(
|
|
'skips an already-tombstoned entry and undoes the one before it',
|
|
() async {
|
|
final first = await LogStorageService.instance.logMeal(
|
|
'first',
|
|
_manual,
|
|
);
|
|
await LogStorageService.instance.logMeal('second', _manual);
|
|
await LogStorageService.instance.undoLastToday();
|
|
final undoneAgain = await LogStorageService.instance.undoLastToday();
|
|
expect(undoneAgain!.id, first.id);
|
|
expect(await LogStorageService.instance.undoLastToday(), isNull);
|
|
},
|
|
);
|
|
});
|
|
group('todayTotalKcal', () {
|
|
test("sums kcal across today's non-tombstoned entries", () async {
|
|
await LogStorageService.instance.logMeal('a', _manual);
|
|
await LogStorageService.instance.logMeal('b', _manual);
|
|
expect(await LogStorageService.instance.todayTotalKcal(), 300.0);
|
|
});
|
|
|
|
test('excludes a tombstoned entry from the total', () async {
|
|
await LogStorageService.instance.logMeal('a', _manual);
|
|
await LogStorageService.instance.logMeal('b', _manual);
|
|
await LogStorageService.instance.undoLastToday();
|
|
expect(await LogStorageService.instance.todayTotalKcal(), 150.0);
|
|
});
|
|
});
|
|
group('loggedSlotsToday', () {
|
|
test('returns only the slots with a logged entry', () async {
|
|
await LogStorageService.instance.logMeal('a', _manual, slotMinute: 480);
|
|
await LogStorageService.instance.logMeal('b', _manual);
|
|
expect(await LogStorageService.instance.loggedSlotsToday(), {480});
|
|
});
|
|
|
|
test('snaps an off-slot minute to the nearest slot', () async {
|
|
// 07:30 under the default 08/12/16/20 schedule satisfies 08:00, and
|
|
// two meals snapping onto one slot satisfy only that slot.
|
|
await LogStorageService.instance.logMeal('a', _manual, slotMinute: 450);
|
|
await LogStorageService.instance.logMeal('b', _manual, slotMinute: 500);
|
|
expect(await LogStorageService.instance.loggedSlotsToday(), {480});
|
|
});
|
|
});
|
|
}
|