mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-22 04:32:23 +01:00
The phone is getting its own Kuchnia Wikinga importer so the feature works with the PC switched off. That turns a risk `_kuchnia_spread.py` called "theoretical" into a live one: two implementations must now agree exactly on which dishes are dropped, which slot each lands on, and the JSON a banked record encodes to. Disagreement means each device re-adds what the other dropped, and since `add_manual_entry` restamps `t` unconditionally, the whole curated bank republishes to every peer on every refresh. A slot mismatch is worse -- a checkpoint one device offers and the other does not can never be satisfied. So the port lands with its gate rather than after it. One shared fixture, `tests/fixtures/kuchnia_day.json`, is asserted by both `pytest` and `flutter test`: same input, same expected dishes, drops, slots, bank keys and records. Two suites written independently from the same prose is not a gate. The fixture is five real dishes (a scrubbed 2026-08-22 capture) plus synthetic meals that each pin one drop or fallback path -- a per-100 g mix-up, an absurd portion, a stringly-typed number, a non-dict entry, a missing `nutrition`, a missing `mealPriority`, and a twin-dish pair. Without those, "the same meals are dropped" is a vacuous assertion against an all-happy-path capture. Both halves were mutation-checked: flipping an expected slot, drop count or record value fails each suite, and flipping `435.0` to `435` fails the byte-identity test while the value comparison still passes, which is why both exist. Three traps the port had to survive, each caught before it shipped: - `_ENERGY_TOLERANCE` is **0.35**. The prose said "~1%", which describes how closely the captured day agreed, not the threshold; porting 0.01 would drop dishes the PC keeps. That paragraph now says so. - `jsonEncode(435)` emits `435` where Python emits `435.0`, so every Dart macro is forced to `double` and a canonical-encoding test pins it. - Dart's `List.sort` is unstable where Python's `sorted` is stable, so the Dart comparator carries the payload index as a final tiebreak. The real dishes live in their own file rather than as a literal in the generator: transcribing them by hand corrupted three of the five, and the error was invisible until the generated fixture was diffed against the capture. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
208 lines
7.5 KiB
Dart
208 lines
7.5 KiB
Dart
/// The Dart half of the cross-language catering parity gate.
|
|
///
|
|
/// `diet_guard/tests/test_kuchnia_parity.py` asserts the *same* expectations
|
|
/// against the *same* `tests/fixtures/kuchnia_day.json`. Two independently
|
|
/// written suites from the same prose is not a gate -- one shared input with
|
|
/// one shared expected result is, because a divergence has to surface as a
|
|
/// failure on one side rather than as two self-consistent implementations.
|
|
///
|
|
/// What the parity protects, per `docs/kuchnia-wikinga.md`:
|
|
///
|
|
/// * **Slot assignment.** A slot one device offers while the other does not is
|
|
/// a checkpoint that can never be satisfied -- a permanent lock.
|
|
/// * **Which dishes are dropped.** If the two sides disagree, each re-adds
|
|
/// what the other dropped, `add_manual_entry` restamps `t` unconditionally,
|
|
/// and the curated bank republishes to every peer on every refresh.
|
|
/// * **Bank keys and record values**, including their JSON *types*.
|
|
///
|
|
/// `dart:io` is fine here: the repo invariant only scans `lib/`, and
|
|
/// `flutter test` runs on the VM.
|
|
library;
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/models/kuchnia_dish.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_parse.dart';
|
|
import 'package:diet_guard_app/services/kuchnia_spread.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
/// The repo root, found by walking up from the package directory.
|
|
///
|
|
/// Mirrors `repo_invariants_test.dart`'s approach so the test works whatever
|
|
/// working directory `flutter test` picks.
|
|
Directory get _repoRoot {
|
|
var dir = Directory.current;
|
|
while (!File('${dir.path}/pubspec.yaml').existsSync()) {
|
|
final parent = dir.parent;
|
|
if (parent.path == dir.path) {
|
|
fail('could not locate app/ from ${Directory.current.path}');
|
|
}
|
|
dir = parent;
|
|
}
|
|
// `dir` is app/; the fixture is shared, so it lives at the repo root.
|
|
return dir.parent;
|
|
}
|
|
|
|
File get _fixtureFile =>
|
|
File('${_repoRoot.path}/tests/fixtures/kuchnia_day.json');
|
|
|
|
void main() {
|
|
late Map<String, dynamic> fixture;
|
|
late Map<String, dynamic> expected;
|
|
late List<KuchniaDish> dishes;
|
|
|
|
setUpAll(() {
|
|
fixture =
|
|
jsonDecode(_fixtureFile.readAsStringSync()) as Map<String, dynamic>;
|
|
expected = fixture['expected'] as Map<String, dynamic>;
|
|
dishes = parseMenu(fixture['payload']);
|
|
});
|
|
|
|
test('the shared fixture is present', () {
|
|
// Asserted explicitly so a relocation fails with a clear reason rather
|
|
// than as a confusing cast error inside another test.
|
|
expect(
|
|
_fixtureFile.existsSync(),
|
|
isTrue,
|
|
reason: 'shared parity fixture missing at ${_fixtureFile.path}. It is '
|
|
'read by both flutter test and pytest; regenerate with '
|
|
'scripts/build_kuchnia_fixture.py.',
|
|
);
|
|
});
|
|
|
|
test('parsed dishes match the shared expectation', () {
|
|
final actual = [
|
|
for (final dish in dishes)
|
|
{
|
|
'name': dish.name,
|
|
'kcal': dish.kcal,
|
|
'protein_g': dish.proteinG,
|
|
'carbs_g': dish.carbsG,
|
|
'fat_g': dish.fatG,
|
|
'grams': dish.grams,
|
|
'priority': dish.priority,
|
|
'slot_label': dish.slotLabel,
|
|
},
|
|
];
|
|
expect(actual, equals(expected['dishes']));
|
|
});
|
|
|
|
test('the same meals are dropped on both sides', () {
|
|
// Vacuous against a clean capture, which is why the fixture carries a
|
|
// per-100 g mix-up, an absurd portion, a stringly-typed number, a non-map
|
|
// entry and a meal with no nutrition at all.
|
|
final total = (fixture['payload'] as Map)['deliveryMenuMeal'] as List;
|
|
expect(total.length - dishes.length, equals(expected['dropped_count']));
|
|
expect(expected['dropped_count'], greaterThan(0));
|
|
});
|
|
|
|
test('slot assignment matches for every configured schedule', () {
|
|
final slotCases = expected['slots'] as Map<String, dynamic>;
|
|
for (final entry in slotCases.entries) {
|
|
var key = entry.key;
|
|
var subject = dishes;
|
|
if (key.startsWith('first_three_')) {
|
|
subject = dishes.take(3).toList();
|
|
key = key.substring('first_three_'.length);
|
|
}
|
|
final hours = [for (final part in key.split(',')) int.parse(part)];
|
|
final actual = [
|
|
for (final item in assignSlots(subject, hours)) item.slot,
|
|
];
|
|
expect(
|
|
actual,
|
|
equals(entry.value),
|
|
reason: 'slot assignment diverged for ${entry.key}',
|
|
);
|
|
}
|
|
});
|
|
|
|
test('slot ordering is total, so the twin-dish pair cannot reshuffle', () {
|
|
// Dart's List.sort is not stable and Python's sorted is, so two dishes
|
|
// sharing both priority and name are what force the comparator to be
|
|
// total. Run repeatedly: an unstable sort need not misbehave every time.
|
|
for (var attempt = 0; attempt < 5; attempt++) {
|
|
final ordered = assignSlots(parseMenu(fixture['payload']), const [
|
|
8,
|
|
12,
|
|
16,
|
|
20,
|
|
]);
|
|
expect(
|
|
[for (final item in ordered) item.dish.name],
|
|
equals(expected['slot_order']),
|
|
);
|
|
}
|
|
});
|
|
|
|
test('bank keys match', () {
|
|
expect(
|
|
[for (final dish in dishes) dish.bankKey],
|
|
equals(expected['bank_keys']),
|
|
);
|
|
});
|
|
|
|
test('bank records match by value', () {
|
|
// Numeric comparison, not string: `270 == 270.0` must be true here exactly
|
|
// as it is in Python's `_matches`, or every phone refresh rewrites the
|
|
// whole curated bank.
|
|
expect(
|
|
[for (final dish in dishes) dish.toBankRecord()],
|
|
equals(expected['bank_records']),
|
|
);
|
|
});
|
|
|
|
test('bank records encode byte-identically to the Python ones', () {
|
|
// The real divergence risk is int-vs-double at the JSON boundary:
|
|
// jsonEncode(435) emits `435` where Python emits `435.0`. Comparing the
|
|
// encoded text (canonical key order on both sides) is what catches it;
|
|
// the value comparison above would not.
|
|
final expectedRecords = expected['bank_records'] as List;
|
|
for (var i = 0; i < dishes.length; i++) {
|
|
final actual = dishes[i].toBankRecord();
|
|
final wanted = (expectedRecords[i] as Map).cast<String, dynamic>();
|
|
final sortedKeys = actual.keys.toList()..sort();
|
|
String canonical(Map<String, dynamic> record) => jsonEncode({
|
|
for (final key in sortedKeys) key: record[key],
|
|
});
|
|
expect(
|
|
canonical(actual),
|
|
equals(canonical(wanted)),
|
|
reason: 'record ${i + 1} (${dishes[i].name}) encodes differently to '
|
|
'the Python-banked record; check int vs double.',
|
|
);
|
|
}
|
|
});
|
|
|
|
test('macros bank as doubles and count as an int', () {
|
|
for (final dish in dishes) {
|
|
final record = dish.toBankRecord();
|
|
for (final key in ['kcal', 'protein_g', 'carbs_g', 'fat_g', 'grams']) {
|
|
expect(
|
|
record[key],
|
|
isA<double>(),
|
|
reason: '$key must bank as a double, or its JSON loses the ".0"',
|
|
);
|
|
}
|
|
expect(record['count'], isA<int>());
|
|
}
|
|
});
|
|
|
|
test('a stringly-typed number is coerced to 0, never parsed', () {
|
|
// Pins the `as_float` contract. `double.tryParse` here would keep a dish
|
|
// the PC drops, and then each device re-adds what the other removed.
|
|
expect(asDouble('435'), equals(0.0));
|
|
expect(asDouble(true), equals(0.0));
|
|
expect(asDouble(null), equals(0.0));
|
|
expect(asDouble(435), equals(435.0));
|
|
expect(asDouble(435.5), equals(435.5));
|
|
});
|
|
|
|
test('the energy tolerance is 0.35, not the "~1%" in the prose', () {
|
|
// Porting 0.01 would drop dishes the PC keeps, and each side would re-add
|
|
// what the other dropped -- the republish flood.
|
|
expect(kuchniaEnergyTolerance, equals(0.35));
|
|
});
|
|
}
|