mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 22:02:17 +01:00
Three independent cleanups ahead of the configurable meal schedule.
Pills: the row was a `Wrap`, so pills dropped onto a second line as soon as
they outgrew the width -- which they do even at four. Measured: a logged chip
is ~124px at Material defaults, so four need 522px against ~330px of phone
width. Now a `FittedBox(scaleDown)` over a `Row`, which scales the row down
uniformly, plus compact chip density so there is less to scale away.
A `Row` of `Expanded` children was the obvious alternative and is wrong: it
imposes a tight width, and a Chip narrower than avatar+padding+label clips its
label rather than shrinking -- measured at 10px per label, about one character,
while throwing no overflow error. The new regression test therefore asserts
both a shared baseline and a legible label width; it was confirmed to fail on
the old `Wrap` ("chip wrapped to a second line at 320.0") rather than passing
vacuously.
Card: the `Logged "<meal>".` line restated the description the user had just
typed. Removed, along with `TodayProgress.justLogged`, which nothing else read.
The kcal position, macros and streak stay.
refactor_claude_todo.md: the 250-line file-splitting refactor it tracked is
done -- `scripts/check_file_length.sh --all` exits 0 and an independent sweep
finds nothing over the cap. DESIGN_AUDIT_TODO.md is a separate, still-open
effort and stays.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159b4E5hh512jYUeaJVNxfM
246 lines
9.1 KiB
Dart
246 lines
9.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/screens/log_meal_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/document_store_io.dart';
|
|
import 'package:diet_guard_app/services/foodbank_service.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:diet_guard_app/widgets/today_progress_card.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
|
|
import 'package:url_launcher_platform_interface/link.dart';
|
|
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
|
|
|
|
/// Stub launcher that records the URL instead of opening it -- same pattern
|
|
/// as settings_screen_test.dart's fake, duplicated locally per this
|
|
/// codebase's convention of small per-file fakes over a shared mock.
|
|
class _FakeUrlLauncher extends UrlLauncherPlatform
|
|
with MockPlatformInterfaceMixin {
|
|
String? launched;
|
|
|
|
@override
|
|
final LinkDelegate? linkDelegate = null;
|
|
|
|
@override
|
|
Future<bool> supportsMode(PreferredLaunchMode mode) async => true;
|
|
|
|
@override
|
|
Future<bool> launchUrl(String url, LaunchOptions options) async {
|
|
launched = url;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// Returns a fixed [XFile] without touching any real platform channel.
|
|
|
|
/// A minimal valid 1x1 transparent PNG, so the thumbnail preview can decode
|
|
/// it as a real image instead of throwing on bogus bytes.
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_screen_');
|
|
LogStorageService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
FoodBankService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
AppSettingsService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
BudgetHistoryService.resetForTesting(
|
|
store: FileDocumentStore(tempDir),
|
|
);
|
|
});
|
|
|
|
tearDown(() async {
|
|
LogStorageService.resetForTesting();
|
|
FoodBankService.resetForTesting();
|
|
AppSettingsService.resetForTesting();
|
|
BudgetHistoryService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
final logMealButton = find.byTooltip('Log meal');
|
|
|
|
// The screen's button handlers and description-field listener trigger
|
|
// real `dart:io` file I/O as fire-and-forget Futures that Flutter's frame
|
|
// scheduler does not track -- pumpAndSettle() can return *before* that
|
|
// I/O (and its eventual setState) actually finishes. Every interaction
|
|
// that can reach a service call therefore runs inside a single
|
|
// tester.runAsync() per test, with a short real delay before each
|
|
// pumpAndSettle() to let the in-flight I/O actually complete first.
|
|
Future<void> settle(WidgetTester tester) async {
|
|
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets('logging a manually-typed meal persists it as source manual', (
|
|
tester,
|
|
) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.enterText(find.byType(TextField).at(0), 'toast');
|
|
await settle(tester);
|
|
await tester.enterText(find.byType(TextField).at(2), '150');
|
|
await tester.enterText(find.byType(TextField).at(3), '5');
|
|
await tester.enterText(find.byType(TextField).at(4), '20');
|
|
await tester.enterText(find.byType(TextField).at(5), '3');
|
|
await settle(tester);
|
|
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
|
|
// The card itself is the "it logged" signal; the `Logged "<meal>".`
|
|
// line it used to open with was removed 2026-08-16.
|
|
expect(find.byType(TodayProgressCard), findsOneWidget);
|
|
expect(find.textContaining('Logged'), findsNothing);
|
|
final entries = await LogStorageService.instance.todayEntries();
|
|
expect(entries.single.source, 'manual');
|
|
expect(entries.single.kcal, 150);
|
|
});
|
|
});
|
|
testWidgets('the progress card summarises today after a log', (
|
|
tester,
|
|
) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
expect(find.byType(TodayProgressCard), findsNothing);
|
|
|
|
await tester.enterText(find.byType(TextField).at(0), 'toast');
|
|
await settle(tester);
|
|
await tester.enterText(find.byType(TextField).at(2), '150');
|
|
await settle(tester);
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
|
|
expect(find.byType(TodayProgressCard), findsOneWidget);
|
|
// Default budget is 2200 with the settings singleton uninitialised.
|
|
expect(find.text('150 / 2200'), findsOneWidget);
|
|
expect(find.text('2050 left'), findsOneWidget);
|
|
});
|
|
});
|
|
testWidgets('typing a new description dismisses the progress card', (
|
|
tester,
|
|
) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.enterText(find.byType(TextField).at(0), 'toast');
|
|
await settle(tester);
|
|
await tester.enterText(find.byType(TextField).at(2), '150');
|
|
await settle(tester);
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
expect(find.byType(TodayProgressCard), findsOneWidget);
|
|
|
|
// Starting the next meal clears the previous one's summary, so the
|
|
// card never describes a stale log.
|
|
await tester.enterText(find.byType(TextField).at(0), 'eggs');
|
|
await settle(tester);
|
|
|
|
expect(find.byType(TodayProgressCard), findsNothing);
|
|
});
|
|
});
|
|
testWidgets('refuses to log with an empty description', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
|
|
expect(find.text('Type what you ate first.'), findsOneWidget);
|
|
expect(await LogStorageService.instance.todayEntries(), isEmpty);
|
|
});
|
|
});
|
|
testWidgets(
|
|
'per-grams and amount-eaten fields scale macros to the eaten portion',
|
|
(tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.enterText(find.byType(TextField).at(0), 'label food');
|
|
await settle(tester);
|
|
await tester.enterText(find.byType(TextField).at(1), '100');
|
|
await tester.enterText(find.byType(TextField).at(2), '200');
|
|
await tester.enterText(find.byType(TextField).at(3), '10');
|
|
await tester.enterText(find.byType(TextField).at(4), '20');
|
|
await tester.enterText(find.byType(TextField).at(5), '5');
|
|
await tester.enterText(find.byType(TextField).at(6), '150');
|
|
await settle(tester);
|
|
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
|
|
final entry = (await LogStorageService.instance.todayEntries()).single;
|
|
expect(entry.kcal, 300);
|
|
expect(entry.proteinG, 15);
|
|
expect(entry.carbsG, 30);
|
|
expect(entry.fatG, 7.5);
|
|
expect(entry.grams, 150);
|
|
});
|
|
},
|
|
);
|
|
testWidgets(
|
|
'selecting a food-bank suggestion stamps source food bank, but '
|
|
'editing a macro afterward reverts it to manual',
|
|
(tester) async {
|
|
await tester.runAsync(() async {
|
|
const seed = FoodEntry(
|
|
id: 'seed-1',
|
|
time: '2026-06-01T08:00:00+02:00',
|
|
desc: 'seeded food',
|
|
grams: 100,
|
|
kcal: 250,
|
|
proteinG: 10,
|
|
carbsG: 30,
|
|
fatG: 8,
|
|
source: 'manual',
|
|
);
|
|
await FoodBankService.instance.rebuildAndPersist({
|
|
'2026-06-01': [seed],
|
|
});
|
|
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
// The empty-query suggestion list shows the only banked food.
|
|
await tester.tap(find.text('seeded food · 250 kcal'));
|
|
await settle(tester);
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
|
|
final firstEntry =
|
|
(await LogStorageService.instance.todayEntries()).single;
|
|
expect(firstEntry.source, 'food bank');
|
|
expect(firstEntry.kcal, 250);
|
|
|
|
await tester.tap(find.text('seeded food · 250 kcal'));
|
|
await settle(tester);
|
|
await tester.enterText(find.byType(TextField).at(2), '999');
|
|
await settle(tester);
|
|
await tester.ensureVisible(logMealButton);
|
|
await tester.tap(logMealButton);
|
|
await settle(tester);
|
|
|
|
final secondEntry =
|
|
(await LogStorageService.instance.todayEntries()).last;
|
|
expect(secondEntry.source, 'manual');
|
|
expect(secondEntry.kcal, 999);
|
|
});
|
|
},
|
|
);
|
|
}
|