diet-guard/app/test/services/meal_schedule_history_test.dart
Krzysztof kuhy Rudnicki d2e62bf210 Read the meal schedule from a forward-only history (Dart)
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
2026-08-16 14:41:37 +02:00

147 lines
4.4 KiB
Dart

/// Tests for the pure meal-schedule history: parsing, encoding, forDay,
/// upsert and seeding.
///
/// Split from `meal_schedule_service_test.dart` (250-line cap); that file
/// keeps the persistence half. Mirrors
/// `diet_guard/tests/test_meal_schedule_store.py`.
library;
import 'package:diet_guard_app/models/meal_schedule.dart';
import 'package:diet_guard_app/services/meal_schedule_history.dart';
import 'package:flutter_test/flutter_test.dart';
DateTime _at(String day) => DateTime.parse('${day}T12:00:00');
void main() {
group('scheduleEntryFromJson', () {
test('round trips', () {
const entry = ScheduleEntry(
effectiveFrom: '2026-08-16',
schedule: MealSchedule(first: 8, last: 20, count: 5),
editedAt: '2026-08-16T12:00:00.000',
);
expect(
scheduleEntryFromJson('2026-08-16', scheduleEntryToJson(entry)),
entry,
);
});
test('normalizes on the way in', () {
final entry = scheduleEntryFromJson('2026-08-16', {
'f': 8,
'l': 20,
'n': 99,
});
expect(entry!.schedule, const MealSchedule(first: 8, last: 20, count: 6));
});
test('falls back to the epoch when the stamp is missing', () {
final entry = scheduleEntryFromJson('2026-08-16', {
'f': 8,
'l': 20,
'n': 4,
});
expect(entry!.editedAt, '1970-01-01T00:00:00.000Z');
});
test('rejects a non-mapping', () {
expect(scheduleEntryFromJson('2026-08-16', 'nonsense'), isNull);
});
test('rejects non-integer fields', () {
expect(
scheduleEntryFromJson('2026-08-16', {'f': '8', 'l': 20, 'n': 4}),
isNull,
);
});
});
group('MealScheduleHistory', () {
test('parse degrades on anything unreadable', () {
expect(MealScheduleHistory.parse('nonsense'), isEmpty);
expect(MealScheduleHistory.parse({'e': 'nonsense'}), isEmpty);
expect(MealScheduleHistory.parse(const <String, Object?>{}), isEmpty);
});
test('parse skips only the bad entry', () {
final parsed = MealScheduleHistory.parse(const {
'e': {
'2026-01-01': {'f': 8, 'l': 20, 'n': 4},
'2026-02-01': 7,
},
});
expect(parsed.map((e) => e.effectiveFrom), ['2026-01-01']);
});
test('parse ignores a non-string key', () {
final parsed = MealScheduleHistory.parse(const {
'e': {
7: {'f': 8, 'l': 20, 'n': 4},
},
});
expect(parsed, isEmpty);
});
test('forDay defaults when the history is silent', () {
final history = MealScheduleHistory(const [
ScheduleEntry(
effectiveFrom: '2026-08-16',
schedule: MealSchedule(first: 8, last: 20, count: 5),
editedAt: 't',
),
]);
expect(history.forDay('2026-08-15'), kDefaultSchedule);
});
test('forDay uses the newest applicable entry', () {
final history = MealScheduleHistory(const [
ScheduleEntry(
effectiveFrom: '2026-08-16',
schedule: MealSchedule(first: 8, last: 20, count: 5),
editedAt: 't1',
),
ScheduleEntry(
effectiveFrom: '2026-01-01',
schedule: kDefaultSchedule,
editedAt: 't0',
),
]);
expect(
history.forDay('2026-08-16'),
const MealSchedule(first: 8, last: 20, count: 5),
);
expect(history.forDay('2026-05-01'), kDefaultSchedule);
});
test('upsert replaces a same-day re-edit', () {
final once = MealScheduleHistory.empty.upsert(
const MealSchedule(first: 8, last: 20, count: 5),
when: _at('2026-08-16'),
);
final twice = once.upsert(
const MealSchedule(first: 9, last: 21, count: 3),
when: _at('2026-08-16'),
);
expect(twice.entries, hasLength(1));
expect(
twice.entries.single.schedule,
const MealSchedule(first: 9, last: 21, count: 3),
);
});
test('upsert defaults to now', () {
final history = MealScheduleHistory.empty.upsert(
const MealSchedule(first: 8, last: 20, count: 5),
);
expect(history.entries, hasLength(1));
});
test('seedDefault pins the default at the epoch and is idempotent', () {
final seeded = MealScheduleHistory.empty.seedDefault();
expect(seeded.entries.first.effectiveFrom, kScheduleEpochDay);
expect(seeded.entries.first.schedule, kDefaultSchedule);
expect(seeded.seedDefault().entries, hasLength(1));
});
});
}