mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-20 16:52:28 +01:00
- 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
187 lines
5.6 KiB
Dart
187 lines
5.6 KiB
Dart
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/models/meal_component.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
group('FoodEntry.fromJson', () {
|
|
test('parses a fully-populated entry', () {
|
|
final entry = FoodEntry.fromJson({
|
|
'id': 'abc-123',
|
|
'time': '2026-06-22T17:41:17+02:00',
|
|
'desc': 'label_food',
|
|
'grams': 150.0,
|
|
'kcal': 300.0,
|
|
'protein_g': 15.0,
|
|
'carbs_g': 30.0,
|
|
'fat_g': 7.5,
|
|
'source': 'manual',
|
|
'slot': 16,
|
|
'hmac': 'deadbeef',
|
|
'components': [
|
|
{
|
|
'name': 'rice',
|
|
'kcal': 200.0,
|
|
'protein_g': 4.0,
|
|
'carbs_g': 44.0,
|
|
'fat_g': 1.0,
|
|
'grams': 150.0,
|
|
},
|
|
],
|
|
'deleted': true,
|
|
});
|
|
expect(entry.id, 'abc-123');
|
|
expect(entry.desc, 'label_food');
|
|
expect(entry.kcal, 300.0);
|
|
expect(entry.slotMinute, 960);
|
|
expect(entry.hmac, 'deadbeef');
|
|
expect(entry.components, hasLength(1));
|
|
expect(entry.components!.first.name, 'rice');
|
|
expect(entry.deleted, isTrue);
|
|
});
|
|
|
|
test('defaults missing macro fields to 0 and source to manual', () {
|
|
final entry = FoodEntry.fromJson(const {'desc': 'mystery food'});
|
|
expect(entry.id, isNull);
|
|
expect(entry.kcal, 0);
|
|
expect(entry.proteinG, 0);
|
|
expect(entry.source, 'manual');
|
|
expect(entry.slotMinute, isNull);
|
|
expect(entry.deleted, isFalse);
|
|
expect(entry.components, isNull);
|
|
});
|
|
});
|
|
|
|
group('toLocalJson vs toSyncJson', () {
|
|
test('toLocalJson matches toSyncJson; both exclude hmac', () {
|
|
const entry = FoodEntry(
|
|
id: 'id-1',
|
|
time: '2026-06-22T08:00:00+02:00',
|
|
desc: 'toast',
|
|
grams: 50,
|
|
kcal: 120,
|
|
proteinG: 3,
|
|
carbsG: 20,
|
|
fatG: 2,
|
|
source: 'manual',
|
|
hmac: 'sig',
|
|
);
|
|
final local = entry.toLocalJson();
|
|
final sync = entry.toSyncJson();
|
|
expect(local, sync);
|
|
expect(sync.containsKey('hmac'), isFalse);
|
|
expect(sync['desc'], 'toast');
|
|
});
|
|
|
|
test('omits optional fields entirely when unset', () {
|
|
const entry = FoodEntry(
|
|
time: '2026-06-22T08:00:00+02:00',
|
|
desc: 'toast',
|
|
grams: 50,
|
|
kcal: 120,
|
|
proteinG: 3,
|
|
carbsG: 20,
|
|
fatG: 2,
|
|
source: 'manual',
|
|
);
|
|
final sync = entry.toSyncJson();
|
|
expect(sync.containsKey('id'), isFalse);
|
|
expect(sync.containsKey('slot'), isFalse);
|
|
expect(sync.containsKey('components'), isFalse);
|
|
expect(sync.containsKey('deleted'), isFalse);
|
|
});
|
|
|
|
test('includes deleted: true only when tombstoned', () {
|
|
const entry = FoodEntry(
|
|
time: '2026-06-22T08:00:00+02:00',
|
|
desc: 'toast',
|
|
grams: 50,
|
|
kcal: 120,
|
|
proteinG: 3,
|
|
carbsG: 20,
|
|
fatG: 2,
|
|
source: 'manual',
|
|
deleted: true,
|
|
);
|
|
expect(entry.toSyncJson()['deleted'], isTrue);
|
|
});
|
|
});
|
|
|
|
group('copyWithImagePath / copyWithDeleted', () {
|
|
const base = FoodEntry(
|
|
id: 'id-1',
|
|
time: '2026-06-22T08:00:00+02:00',
|
|
desc: 'toast',
|
|
grams: 50,
|
|
kcal: 120,
|
|
proteinG: 3,
|
|
carbsG: 20,
|
|
fatG: 2,
|
|
source: 'manual',
|
|
components: [
|
|
MealComponent(
|
|
name: 'bread',
|
|
kcal: 120,
|
|
proteinG: 3,
|
|
carbsG: 20,
|
|
fatG: 2,
|
|
grams: 50,
|
|
),
|
|
],
|
|
);
|
|
|
|
test('copyWithDeleted sets deleted true and preserves everything else', () {
|
|
final tombstoned = base.copyWithDeleted();
|
|
expect(tombstoned.deleted, isTrue);
|
|
expect(tombstoned.id, base.id);
|
|
expect(tombstoned.kcal, base.kcal);
|
|
expect(tombstoned.components, base.components);
|
|
});
|
|
});
|
|
|
|
group('slot minute round trip', () {
|
|
Map<String, Object?> entry(Map<String, Object?> slot) => {
|
|
'id': 'id-1',
|
|
'time': '2026-06-22T07:15:00+02:00',
|
|
'desc': 'toast',
|
|
'grams': 50.0,
|
|
'kcal': 120.0,
|
|
'protein_g': 3.0,
|
|
'carbs_g': 20.0,
|
|
'fat_g': 2.0,
|
|
'source': 'manual',
|
|
...slot,
|
|
};
|
|
|
|
test('a PC entry with slot_min survives a phone round trip', () {
|
|
// The regression this guards: keeping only the hour made every phone
|
|
// edit or tombstone republish a stripped copy of the PC's entry.
|
|
final raw = entry({'slot': 7, 'slot_min': 435});
|
|
final parsed = FoodEntry.fromJson(raw);
|
|
expect(parsed.slotMinute, 435);
|
|
expect(parsed.toSyncJson(), raw);
|
|
expect(parsed.copyWithDeleted().slotMinute, 435);
|
|
expect(parsed.copyWithDeleted().toSyncJson()['slot_min'], 435);
|
|
});
|
|
|
|
test('a legacy whole-hour entry stays byte-identical', () {
|
|
final raw = entry({'slot': 8});
|
|
final parsed = FoodEntry.fromJson(raw);
|
|
expect(parsed.slotMinute, 480);
|
|
expect(parsed.toSyncJson(), raw);
|
|
expect(parsed.toSyncJson().containsKey('slot_min'), isFalse);
|
|
});
|
|
|
|
test('an out-of-range peer slot is echoed, never thrown on', () {
|
|
// slotFields throws outside the day; writeLog encodes every entry, so a
|
|
// throw here would block all logging behind one bad synced entry.
|
|
expect(FoodEntry.fromJson(entry({'slot': 24})).toSyncJson()['slot'], 24);
|
|
final late = FoodEntry.fromJson(entry({'slot_min': 1510})).toSyncJson();
|
|
expect(late['slot'], 25);
|
|
expect(late['slot_min'], 1510);
|
|
final negative = FoodEntry.fromJson(entry({'slot': -1})).toSyncJson();
|
|
expect(negative.containsKey('slot'), isFalse);
|
|
expect(negative['slot_min'], -60);
|
|
});
|
|
});
|
|
}
|