diet-guard/app/test/services/meal_schedule_history_test.dart
Krzysztof kuhy Rudnicki f83938bfe3 app: minute-of-day slots (phase 2 of 3: every app consumer)
- 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
2026-10-10 19:55:26 +02:00

147 lines
4.6 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(firstMinute: 8 * 60, lastMinute: 20 * 60, 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(firstMinute: 8 * 60, lastMinute: 20 * 60, 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(firstMinute: 8 * 60, lastMinute: 20 * 60, 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(firstMinute: 8 * 60, lastMinute: 20 * 60, count: 5),
editedAt: 't1',
),
ScheduleEntry(
effectiveFrom: '2026-01-01',
schedule: kDefaultSchedule,
editedAt: 't0',
),
]);
expect(
history.forDay('2026-08-16'),
const MealSchedule(firstMinute: 8 * 60, lastMinute: 20 * 60, count: 5),
);
expect(history.forDay('2026-05-01'), kDefaultSchedule);
});
test('upsert replaces a same-day re-edit', () {
final once = MealScheduleHistory.empty.upsert(
const MealSchedule(firstMinute: 8 * 60, lastMinute: 20 * 60, count: 5),
when: _at('2026-08-16'),
);
final twice = once.upsert(
const MealSchedule(firstMinute: 9 * 60, lastMinute: 21 * 60, count: 3),
when: _at('2026-08-16'),
);
expect(twice.entries, hasLength(1));
expect(
twice.entries.single.schedule,
const MealSchedule(firstMinute: 9 * 60, lastMinute: 21 * 60, count: 3),
);
});
test('upsert defaults to now', () {
final history = MealScheduleHistory.empty.upsert(
const MealSchedule(firstMinute: 8 * 60, lastMinute: 20 * 60, 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));
});
});
}