mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 22:22:18 +01:00
Action row 1A, due mode, catering auto-log rows with undo, relay interface, photo input. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
158 lines
6.0 KiB
Dart
158 lines
6.0 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/screens/body_screen.dart';
|
|
import 'package:diet_guard_app/screens/calendar_screen.dart';
|
|
import 'package:diet_guard_app/screens/food_bank_screen.dart';
|
|
import 'package:diet_guard_app/screens/history_screen.dart';
|
|
import 'package:diet_guard_app/screens/log_meal_screen.dart';
|
|
import 'package:diet_guard_app/screens/settings_screen.dart';
|
|
import 'package:diet_guard_app/services/app_settings_service.dart';
|
|
import 'package:diet_guard_app/services/body_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:material_ui/material_ui.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import '../label_finders.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';
|
|
|
|
import '../services/body_test_support.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 = controlByLabel('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('the history icon navigates to HistoryScreen', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.tap(find.byIcon(Icons.history));
|
|
await settle(tester);
|
|
|
|
expect(find.byType(HistoryScreen), findsOneWidget);
|
|
});
|
|
});
|
|
testWidgets('the calendar icon navigates to CalendarScreen', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.tap(find.byIcon(Icons.calendar_month));
|
|
await settle(tester);
|
|
|
|
expect(find.byType(CalendarScreen), findsOneWidget);
|
|
});
|
|
});
|
|
testWidgets('the body icon navigates to BodyScreen', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await BodyService.initForTesting(MemoryDocStore());
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.tap(controlByLabel('Body'));
|
|
await settle(tester);
|
|
|
|
expect(find.byType(BodyScreen), findsOneWidget);
|
|
BodyService.resetForTesting();
|
|
});
|
|
});
|
|
testWidgets('the settings icon navigates to SettingsScreen', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
// SettingsScreen briefly shows a perpetually-animating
|
|
// CircularProgressIndicator while its settings load; pumpAndSettle
|
|
// never settles against that, so pump explicit frames instead (see
|
|
// history_screen_test.dart's note on the same pitfall).
|
|
await tester.tap(find.byIcon(Icons.settings));
|
|
await tester.pump();
|
|
await Future<void>.delayed(const Duration(milliseconds: 200));
|
|
await tester.pump();
|
|
|
|
expect(find.byType(SettingsScreen), findsOneWidget);
|
|
});
|
|
});
|
|
testWidgets('food bank icon navigates to FoodBankScreen', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
await tester.tap(find.byIcon(Icons.restaurant_menu));
|
|
await settle(tester);
|
|
|
|
expect(find.byType(FoodBankScreen), findsOneWidget);
|
|
});
|
|
});
|
|
testWidgets('normal mode has no slot chips', (tester) async {
|
|
await tester.runAsync(() async {
|
|
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
|
|
await settle(tester);
|
|
|
|
// The slot is derived (oldest due, else nearest), never picked.
|
|
expect(find.byType(ChoiceChip), findsNothing);
|
|
expect(find.text('08:00'), findsNothing);
|
|
});
|
|
});
|
|
}
|