diet-guard/app/test/services
Krzysztof kuhy Rudnicki 24dbffc432 Sync the meal schedule as sched: fields on the budget record
Without this the schedule would be device-local, which is the failure this
whole feature has to avoid: the phone sets five meals, the PC still derives
four, and the PC's gate nags for a 12:00 checkpoint the phone never offered
and can never satisfy. So sync lands before either edit surface, not after.

The history rides the existing `budget` CRDT record as one
`sched:<YYYY-MM-DD>` field per entry, exactly like the budget's own `hist:`
fields. merge_record is per-field LWW over the *union* of field names and both
devices push the merged record, so a device that predates the feature relays
the fields untouched -- no coordinated release. There is a test for precisely
that: merging our push with a budget-only peer keeps both schedule entries.

Verified the two languages agree on the wire, not just in their own suites --
Python and Dart both emit
  {"sched:1970-01-01":{"f":8,"l":20,"n":4},"sched:2026-08-16":{"f":8,"l":20,"n":5}}
The Hlc is derived from the *parsed* timestamp on both sides, which matters
because the epoch fallback formats differently in each language
(1970-01-01T00:00:00+00:00 vs ...000Z) and a string comparison would diverge.

Two guards worth naming:

- A device with no history contributes no `sched:` fields at all, so an unset
  default can never outrank a peer's real schedule; and an empty merge result
  is never written back, so a pre-feature peer cannot erase a real history.
- Dart's `_syncBudget` synthesises a local record when the schedule has been
  edited but the budget never has -- `budgetToLog(null, ...)` returns an empty
  Log and would otherwise drop the schedule fields too. Its budget value
  carries an epoch stamp, so it loses every LWW race against a real edit.

sync_merge/_schedule.py and sync_merge_schedule.dart are separate modules
rather than additions to the budget adapters, which were at 186 and 170 lines
against the 250 cap. Each declares its own record-id constant so the Dart
pair does not import each other.

772 Python tests at 100% branch coverage, 619 Dart.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159b4E5hh512jYUeaJVNxfM
2026-08-16 14:49:08 +02:00
..
app_settings_service_sync_test.dart Split the remaining seven Dart test files 2026-08-15 20:53:55 +02:00
app_settings_service_test.dart Split the remaining seven Dart test files 2026-08-15 20:53:55 +02:00
average_service_periods_test.dart Split the remaining seven Dart test files 2026-08-15 20:53:55 +02:00
average_service_test.dart Split the remaining seven Dart test files 2026-08-15 20:53:55 +02:00
background_sync_service_test.dart Read the meal schedule from a forward-only history (Dart) 2026-08-16 14:41:37 +02:00
budget_history_service_test.dart Remove unused features; fix reminder false alarms; share all state 2026-07-26 16:39:52 +02:00
budget_schedule_test.dart Remove unused features; fix reminder false alarms; share all state 2026-07-26 16:39:52 +02:00
day_status_service_streaks_test.dart Split the remaining seven Dart test files 2026-08-15 20:53:55 +02:00
day_status_service_test.dart Split the remaining seven Dart test files 2026-08-15 20:53:55 +02:00
due_slot_check_test.dart Read the meal schedule from a forward-only history (Dart) 2026-08-16 14:41:37 +02:00
foodbank_service_manual_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
foodbank_service_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
fuzzy_test.dart Add food bank, edit-entry, and app settings screens; history redesign 2026-07-04 05:18:32 +02:00
indexeddb_stores_test.dart Remove unused features; fix reminder false alarms; share all state 2026-07-26 16:39:52 +02:00
log_storage_service_edit_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
log_storage_service_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
meal_schedule_history_test.dart Read the meal schedule from a forward-only history (Dart) 2026-08-16 14:41:37 +02:00
meal_schedule_service_test.dart Read the meal schedule from a forward-only history (Dart) 2026-08-16 14:41:37 +02:00
notification_service_test.dart Read the meal schedule from a forward-only history (Dart) 2026-08-16 14:41:37 +02:00
sync_device_id_test.dart Push under a persisted per-install uuid, not a fixed role id 2026-08-09 17:18:42 +02:00
sync_health_test.dart Make the phone publish on every tick, and fail closed on Firebase errors 2026-08-10 14:35:37 +02:00
sync_merge_banks_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_merge_budget_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_merge_curated_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_merge_schedule_test.dart Sync the meal schedule as sched: fields on the budget record 2026-08-16 14:49:08 +02:00
sync_merge_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_merge_wire_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_service_budget_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_service_test_support.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_service_test.dart Split six Dart test files; share the sync fixtures via a support sibling 2026-08-15 20:49:54 +02:00
sync_settings_test.dart Point sync at the consolidated utils/syncs monorepos 2026-07-10 14:12:16 +02:00
sync_state_factory_test.dart Split the last four Dart lib files; file-length gate now reports zero 2026-08-15 21:07:24 +02:00