mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 14:32:26 +01:00
The phone/desktop half of the previous commit, so both devices now derive their slots the same way. Still no edit surface, so `MealScheduleService` returns the default and the derived hours are unchanged at 08/12/16/20. `slot.dart` takes the schedule as a required argument, mirroring `_slots.py`, and `slot_test.dart` gains the same every-hour x several-schedules sweep the Python suite runs. That sweep is the contract: the two must agree on which checkpoint a meal satisfies for every cell, not merely on the endpoints. Also fixes a real bug this feature would otherwise have shipped on top of. `syncToSlots` used the slot hour as the notification id but only cancelled ids in the *current* schedule, so changing 08/12/16/20 -> 08/11/14/17/20 would leave ids 12 and 16 posted with nothing that could ever cancel them: permanent reminders for checkpoints that no longer exist. It now sweeps the whole 0..23 id space. The regression test was confirmed to fail against the old loop (4 cancels vs 24) rather than passing vacuously. MealScheduleService.current degrades to the default when the singleton is absent, so `due_slot_check` deliberately does not init() it -- that would reach path_provider from widget tests and from the WorkManager isolate before main() runs. main.dart owns the real init, and background_sync_service.dart does its own, since a fresh isolate has its own static state and would otherwise nag against the default four meals. 609 Dart tests, 761 Python, both suites green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0159b4E5hh512jYUeaJVNxfM
148 lines
5.0 KiB
Dart
148 lines
5.0 KiB
Dart
/// Tests for meal-schedule persistence.
|
|
///
|
|
/// Split from `meal_schedule_history_test.dart` (250-line cap); that file
|
|
/// keeps the pure parsing/derivation half. Mirrors the storage half of
|
|
/// `diet_guard/tests/test_meal_schedule_store.py`.
|
|
library;
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:diet_guard_app/models/meal_schedule.dart';
|
|
import 'package:diet_guard_app/services/document_store.dart';
|
|
import 'package:diet_guard_app/services/meal_schedule_history.dart';
|
|
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// In-memory store, so these tests need no plugin channel.
|
|
class _MemoryStore implements DocumentStore {
|
|
final Map<String, String> documents = {};
|
|
|
|
@override
|
|
Future<String?> read(String name) async => documents[name];
|
|
|
|
@override
|
|
Future<void> write(String name, String contents) async {
|
|
documents[name] = contents;
|
|
}
|
|
}
|
|
|
|
DateTime _at(String day) => DateTime.parse('${day}T12:00:00');
|
|
|
|
void main() {
|
|
late _MemoryStore store;
|
|
|
|
setUp(() {
|
|
store = _MemoryStore();
|
|
MealScheduleService.resetForTesting(store: store);
|
|
});
|
|
|
|
tearDown(MealScheduleService.resetForTesting);
|
|
|
|
group('MealScheduleService', () {
|
|
test('current is the default before any edit', () {
|
|
expect(MealScheduleService.current, kDefaultSchedule);
|
|
expect(MealScheduleService.updatedAt, isNull);
|
|
});
|
|
|
|
test('current degrades to the default when uninitialised', () {
|
|
MealScheduleService.resetForTesting();
|
|
expect(MealScheduleService.current, kDefaultSchedule);
|
|
expect(MealScheduleService.history.entries, isEmpty);
|
|
expect(MealScheduleService.isInitialized, isFalse);
|
|
});
|
|
|
|
test(
|
|
'recordChange persists, reloads, and grandfathers past days',
|
|
() async {
|
|
await MealScheduleService.initForTesting(store);
|
|
await MealScheduleService.instance.recordChange(
|
|
const MealSchedule(first: 8, last: 20, count: 5),
|
|
);
|
|
|
|
await MealScheduleService.initForTesting(store);
|
|
expect(
|
|
MealScheduleService.current,
|
|
const MealSchedule(first: 8, last: 20, count: 5),
|
|
);
|
|
// The whole point of the history: a day before the edit keeps the
|
|
// four-meal schedule it was actually judged against.
|
|
expect(
|
|
MealScheduleService.history.forDay('2020-01-01'),
|
|
kDefaultSchedule,
|
|
);
|
|
expect(MealScheduleService.updatedAt, isNotNull);
|
|
},
|
|
);
|
|
|
|
test('a corrupt document degrades to the default', () async {
|
|
store.documents[MealScheduleService.documentName] = '{not json';
|
|
await MealScheduleService.initForTesting(store);
|
|
expect(MealScheduleService.current, kDefaultSchedule);
|
|
});
|
|
|
|
test('an absent document loads as no history', () async {
|
|
await MealScheduleService.initForTesting(store);
|
|
expect(MealScheduleService.history.entries, isEmpty);
|
|
});
|
|
|
|
test(
|
|
'updatedAt stays null when the stored stamp is not a string',
|
|
() async {
|
|
store.documents[MealScheduleService.documentName] = jsonEncode({
|
|
'v': 1,
|
|
'e': <String, Object?>{},
|
|
't': 7,
|
|
});
|
|
await MealScheduleService.initForTesting(store);
|
|
expect(MealScheduleService.updatedAt, isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'applyMerged replaces the history but ignores an empty merge',
|
|
() async {
|
|
await MealScheduleService.initForTesting(store);
|
|
await MealScheduleService.instance.recordChange(
|
|
const MealSchedule(first: 8, last: 20, count: 5),
|
|
);
|
|
|
|
// A pre-feature peer contributes no `sched:` fields; writing that back
|
|
// would discard this device's own history.
|
|
await MealScheduleService.instance.applyMerged(const []);
|
|
expect(
|
|
MealScheduleService.current,
|
|
const MealSchedule(first: 8, last: 20, count: 5),
|
|
);
|
|
|
|
final stamp = DateTime.parse('2026-08-16T09:00:00.000');
|
|
await MealScheduleService.instance.applyMerged([
|
|
ScheduleEntry(
|
|
effectiveFrom: kScheduleEpochDay,
|
|
schedule: const MealSchedule(first: 7, last: 19, count: 3),
|
|
editedAt: stamp.toIso8601String(),
|
|
),
|
|
], updatedAt: stamp);
|
|
expect(
|
|
MealScheduleService.current,
|
|
const MealSchedule(first: 7, last: 19, count: 3),
|
|
);
|
|
// The winner's stamp is kept verbatim, so re-syncing is idempotent.
|
|
expect(MealScheduleService.updatedAt, stamp);
|
|
},
|
|
);
|
|
|
|
test('the stored document is plain readable JSON', () async {
|
|
await MealScheduleService.initForTesting(store);
|
|
await MealScheduleService.instance.recordChange(
|
|
const MealSchedule(first: 8, last: 20, count: 5),
|
|
when: _at('2026-08-16'),
|
|
);
|
|
final decoded =
|
|
jsonDecode(store.documents[MealScheduleService.documentName]!)
|
|
as Map<String, Object?>;
|
|
expect(decoded['v'], 1);
|
|
expect((decoded['e']! as Map).keys, contains('2026-08-16'));
|
|
});
|
|
});
|
|
}
|