mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 22:22:18 +01:00
Flutter's Linux embedder manages ~20fps at 3840x2160 -- measured on a stock `flutter create` counter app, so it is the toolkit rather than this code -- where the same Dart rendered by Chrome sustains ~144fps (evidence in ~/todo/docs/desktop-performance-findings.md). The desktop target therefore becomes a Flutter *web* build; Android is untouched. `dart:io` does not fail a web *compile* -- it resolves to a stub that throws at runtime -- so every platform edge moves behind a conditional export rather than being discovered later as a blank white window: - storage: `document_store*.dart`, files on Android and IndexedDB in the browser (mirrored to the desktop wrapper, which is what makes a wiped Chrome profile recoverable). Document names stay the exact pre-web filenames so an installed phone keeps reading its existing log. - photos: `blob_store*.dart` plus `attached_image*.dart`, since `Image.file` cannot resolve an IndexedDB key. The picker itself has web support; what changed is the storage and display model. Camera *capture* is genuinely lost on desktop -- and photos never crossed devices anyway, `imagePath` being stripped before push. - scheduling: `background_tasks*.dart`. A browser has no WorkManager equivalent -- a closed tab runs nothing -- so the desktop runs an in-page 5-minute check while its window is open. That loses nothing on the PC, where `diet-guard-gate.timer` already locks the screen on an unlogged slot, which is strictly stronger than a notification. - notifications: `notification_backend*.dart`, the browser Notifications API in place of flutter_local_notifications. - the GitHub token: `token_vault*.dart`. On web it is *not* held by the browser at all -- flutter_secure_storage's web backend is WebCrypto-wrapped localStorage with the wrap key beside the ciphertext, which is obfuscation, not encryption at rest -- so the desktop wrapper owns it and proxies the API (`github_client_factory*.dart`). `Platform.isAndroid` itself throws on web, so every platform branch now tests `kIsWeb` first; the battery-optimization row (permission_handler, no web implementation) is hidden off Android. The desktop pushes under device id `desktop`, not `phone`: sharing an id would have made the two devices overwrite each other's pushed log on every tick. The Python side enumerates device directories generically, so it needs no change. Tests move onto the same seams -- the file-backed stores keep the atomicity and unreadable-file guarantees where they belong. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
177 lines
6.0 KiB
Dart
177 lines
6.0 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/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));
|
|
});
|
|
|
|
tearDown(() async {
|
|
LogStorageService.resetForTesting();
|
|
AppSettingsService.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);
|
|
});
|
|
}
|