mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-12 08:50:50 +01:00
Four asks, plus the fixes two review passes turned up. Removals (all unused): - "Repeat last meal" button, and the reward/temptation-bundling prompt it was the sole trigger for. Replaced with a post-log progress card showing kcal against budget, macros, and the adherence streak. - The multi-item meal builder, on both the gate and the phone. The stored `components` field stays: nothing writes it now, but historical composite entries and the food bank still read it and it is part of the wire format. - Meal photos entirely: 13 files, `imagePath`, the photo filter, the wrapper's /blobs/ route, image_picker, and the CAMERA permission. Reminder false alarms had three real causes, all fixed: - Logging never reconciled notifications, so a reminder sat on screen up to 15 minutes after the meal was logged -- on the phone itself. - The background check never pulled, so a PC-logged meal nagged forever until the app was opened. Now mirrors _cli_gate._should_lock: cheap local check, network only when a slot looks due, then re-check. - Meals before 08:00 / after 22:00 were attributed to no slot at all. New slot_for_log/slotForLog clamps them to the first/last slot, byte-identical across Python and Dart. Deliberately separate from elapsed_slots, which would otherwise make every slot fall due at 23:00. Budget history, so lowering the budget stops rewriting the past: - Forward-only (effective_from, kcal) entries; each day is judged against the budget in force then. Verified live: setting 2000 left a 36-day adherence streak and the whole calendar pixel-identical. - Rides as hist:<date> fields on the existing budget record. crdt_sync merges per-field over the union of field names and both devices push the merged log, so a device that predates the feature relays them untouched -- no coordinated rollout, no second document. - The seed guard tests emptiness, not file presence: _sync can write an empty history after merging with a pre-feature peer, and a presence check would then never seed again, silently reclassifying every past day. One shared source of truth for every piece of data: - Body weight now travels as its own `weight` CRDT field. Inside `value` it was collateral damage of whole-map LWW -- any phone budget edit deleted it, and the protein target with it. - Both halves of the food bank sync. The curated half (foods added without eating them) was the last device-local state. The derived half merges with count as the clock, so LWW means max-count-wins; the log stays authoritative for membership, because a CRDT union never shrinks and a peer's stale copy would otherwise resurrect a deleted food permanently. - Bank adapters honour tombstones, and Dart parses bank records defensively: a cast failure there is an Error, not an Exception, so one malformed remote record would have broken phone sync permanently. _test_guard.py blocks any pathlib write to real user state while pytest is in the process, whatever conftest is loaded. A throwaway test run outside the package conftest destroyed a real GitHub token during this work; this makes that class of accident impossible rather than merely discouraged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
182 lines
6.2 KiB
Dart
182 lines
6.2 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/services/document_store_io.dart';
|
|
import 'package:diet_guard_app/screens/calendar_screen.dart';
|
|
import 'package:diet_guard_app/screens/history_screen.dart';
|
|
import 'package:diet_guard_app/services/app_settings_service.dart';
|
|
import 'package:diet_guard_app/services/budget_history_service.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
const _monthNames = [
|
|
'January',
|
|
'February',
|
|
'March',
|
|
'April',
|
|
'May',
|
|
'June',
|
|
'July',
|
|
'August',
|
|
'September',
|
|
'October',
|
|
'November',
|
|
'December',
|
|
];
|
|
|
|
String _label(DateTime d) => '${_monthNames[d.month - 1]} ${d.year}';
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_calendar_');
|
|
LogStorageService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: FileDocumentStore(tempDir),
|
|
);
|
|
});
|
|
|
|
tearDown(() async {
|
|
LogStorageService.resetForTesting();
|
|
AppSettingsService.resetForTesting();
|
|
BudgetHistoryService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
Future<void> pumpScreen(WidgetTester tester) async {
|
|
await tester.pumpWidget(const MaterialApp(home: CalendarScreen()));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
/// `FilledButton.onPressed` only accepts a synchronous `VoidCallback`, so
|
|
/// `_onEditOrSaveBudget`'s real dart:io write is fired-and-forgotten by
|
|
/// `tester.tap()` -- `pumpAndSettle()` alone can return before it
|
|
/// resolves. Poll for the observable effect instead, mirroring
|
|
/// settings_screen_test.dart's `pumpUntil` for the same class of issue.
|
|
Future<void> pumpUntil(
|
|
WidgetTester tester,
|
|
bool Function() done, {
|
|
int maxTries = 200,
|
|
}) async {
|
|
for (var i = 0; i < maxTries && !done(); i++) {
|
|
await Future<void>.delayed(const Duration(milliseconds: 10));
|
|
await tester.pump();
|
|
}
|
|
}
|
|
|
|
testWidgets('shows the default 2200 kcal budget, read-only, with an Edit '
|
|
'button', (tester) async {
|
|
await pumpScreen(tester);
|
|
expect(find.widgetWithText(TextField, '2200'), findsOneWidget);
|
|
expect(find.widgetWithText(FilledButton, 'Edit'), findsOneWidget);
|
|
final field = tester.widget<TextField>(find.byType(TextField));
|
|
expect(field.enabled, isFalse);
|
|
});
|
|
|
|
testWidgets('tapping Edit unlocks the field and relabels the button Save', (
|
|
tester,
|
|
) async {
|
|
await pumpScreen(tester);
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Edit'));
|
|
await tester.pump();
|
|
|
|
expect(find.widgetWithText(FilledButton, 'Save'), findsOneWidget);
|
|
final field = tester.widget<TextField>(find.byType(TextField));
|
|
expect(field.enabled, isTrue);
|
|
});
|
|
|
|
testWidgets('entering an invalid value keeps editing open with an error', (
|
|
tester,
|
|
) async {
|
|
await pumpScreen(tester);
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Edit'));
|
|
await tester.pump();
|
|
await tester.enterText(find.byType(TextField), '');
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Save'));
|
|
await tester.pump();
|
|
|
|
expect(find.text('Enter a whole number of kcal.'), findsOneWidget);
|
|
expect(find.widgetWithText(FilledButton, 'Save'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a zero value is rejected and editing stays open', (
|
|
tester,
|
|
) async {
|
|
await pumpScreen(tester);
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Edit'));
|
|
await tester.pump();
|
|
await tester.enterText(find.byType(TextField), '0');
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Save'));
|
|
await tester.pump();
|
|
|
|
expect(find.text('Budget must be a positive number.'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('saving a valid value persists it and reverts to read-only', (
|
|
tester,
|
|
) async {
|
|
// Real dart:io writes (AppSettingsService._persist, LogStorageService's
|
|
// subsequent re-read) complete on the real event loop, not a microtask
|
|
// pumpAndSettle alone drains -- runAsync interleaves real waits with
|
|
// frame pumps, mirroring settings_screen_test.dart's device-flow tests.
|
|
await tester.runAsync(() async {
|
|
await pumpScreen(tester);
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Edit'));
|
|
await tester.pump();
|
|
await tester.enterText(find.byType(TextField), '1800');
|
|
await tester.tap(find.widgetWithText(FilledButton, 'Save'));
|
|
await pumpUntil(tester, () => find.text('Saved.').evaluate().isNotEmpty);
|
|
|
|
expect(find.text('Saved.'), findsOneWidget);
|
|
expect(find.widgetWithText(FilledButton, 'Edit'), findsOneWidget);
|
|
expect(AppSettingsService.dailyKcalGoal, 1800);
|
|
final field = tester.widget<TextField>(find.byType(TextField));
|
|
expect(field.enabled, isFalse);
|
|
});
|
|
});
|
|
|
|
testWidgets('shows streak and year-to-date summary text', (tester) async {
|
|
await pumpScreen(tester);
|
|
expect(find.textContaining('Logging streak:'), findsOneWidget);
|
|
expect(find.textContaining('This year:'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('month navigation steps the displayed month back and forward', (
|
|
tester,
|
|
) async {
|
|
await pumpScreen(tester);
|
|
final now = DateTime.now();
|
|
expect(find.text(_label(now)), findsOneWidget);
|
|
|
|
await tester.tap(find.byIcon(Icons.chevron_left));
|
|
await tester.pump();
|
|
expect(
|
|
find.text(_label(DateTime(now.year, now.month - 1))),
|
|
findsOneWidget,
|
|
);
|
|
|
|
await tester.tap(find.byIcon(Icons.chevron_right));
|
|
await tester.pump();
|
|
expect(find.text(_label(now)), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('tapping a day navigates to HistoryScreen filtered to that day', (
|
|
tester,
|
|
) async {
|
|
await pumpScreen(tester);
|
|
final today = DateTime.now();
|
|
await tester.tap(find.text('${today.day}'));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.byType(HistoryScreen), findsOneWidget);
|
|
final screen = tester.widget<HistoryScreen>(find.byType(HistoryScreen));
|
|
final range = screen.initialDateRange!;
|
|
expect(range.start.year, today.year);
|
|
expect(range.start.month, today.month);
|
|
expect(range.start.day, today.day);
|
|
expect(range.end, range.start);
|
|
});
|
|
}
|