Log screen tests: await work, not a fixed sleep

The 200 ms settle flaked under load on origin/main too: the open
chain or a tap's I/O outlasted it. The screen keeps its open chain
in openChain; tests await it, or poll a bounded condition.

Todo: 7808ee0a

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-21 06:50:31 +01:00
parent 9d63c33fa7
commit 1190e2d079
6 changed files with 58 additions and 9 deletions

View File

@ -22,6 +22,11 @@ mixin LogMealLoggedMixin<T extends StatefulWidget>
/// The rows under the action row, oldest first.
List<LoggedMeal> loggedRows = const [];
/// The latest open-or-resume chain. Tests await it instead of guessing how
/// long real file I/O takes (a fixed sleep flaked under load).
@visibleForTesting
Future<void> openChain = Future<void>.value();
/// Replaces the rows with [meals]: a new log retires the previous ones.
void showLogged(List<LoggedMeal> meals) {
if (mounted) setState(() => loggedRows = meals);
@ -57,12 +62,15 @@ mixin LogMealLoggedMixin<T extends StatefulWidget>
// single-flight, so calling super too would let the auto-log run before
// the pull finished). Pause still pushes through the sync mixin.
if (state == AppLifecycleState.resumed) {
unawaited(onOpenedOrResumed());
startOpenChain();
return;
}
super.didChangeAppLifecycleState(state);
}
/// Starts [onOpenedOrResumed] and keeps its future in [openChain].
void startOpenChain() => openChain = onOpenedOrResumed();
/// What a fresh open or a resume does: show the screen from the local log,
/// pull what other devices logged, then auto-log what is still due.
Future<void> onOpenedOrResumed() async {

View File

@ -90,7 +90,7 @@ class _LogMealScreenState extends State<LogMealScreen>
unawaited(_onDescChanged());
// Health first, so a device that stalled earlier says so before a tick.
unawaited(refreshSyncHealth());
unawaited(onOpenedOrResumed());
startOpenChain();
}
@override

View File

@ -2,7 +2,7 @@ name: diet_guard_app
description: "Companion phone app for diet_guard: log meals on the go."
publish_to: 'none'
version: 1.0.188+1
version: 1.0.189+1
environment:
sdk: ^3.12.2

View File

@ -47,7 +47,7 @@ void main() {
Future<void> open(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(home: LogMealScreen()));
await settle(tester);
await settleOpen(tester);
}
testWidgets('opening in due mode logs the due dishes with no tap', (
@ -72,7 +72,11 @@ void main() {
await tester.runAsync(() async {
await open(tester);
await tester.tap(byIconLabel('Undo').first);
await settle(tester);
await settleUntil(
tester,
() async =>
(await LogStorageService.instance.todayEntries()).length == 1,
);
expect(find.byIcon(Icons.restaurant), findsOneWidget);
var live = await LogStorageService.instance.todayEntries();
expect(live.map((e) => e.desc), ['Danie 2']);
@ -130,7 +134,7 @@ void main() {
(state as WidgetsBindingObserver).didChangeAppLifecycleState(
AppLifecycleState.resumed,
);
await settle(tester);
await settleOpen(tester);
final entries = await LogStorageService.instance.todayEntries();
expect(entries.map((e) => e.desc), ['Danie 1', 'Danie 2']);

View File

@ -38,7 +38,7 @@ void main() {
Future<void> open(WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(home: LogMealScreen(relay: relay)));
await settle(tester);
await settleOpen(tester);
}
testWidgets('the request id is a ULID and the mode follows due mode', (
@ -75,7 +75,12 @@ void main() {
await settle(tester);
await tester.tap(controlByLabel('Log meal'));
await settle(tester);
await settleUntil(
tester,
() async =>
relay.cancelled.isNotEmpty &&
byIconLabel('Ask Claude').evaluate().isNotEmpty,
);
expect(relay.cancelled, [relay.sent.single.requestId]);
expect(byIconLabel('Ask Claude'), findsOneWidget);
@ -88,7 +93,12 @@ void main() {
_claudeEntry(claudeEntryId(relay.sent.single.requestId)),
),
);
await settle(tester);
await settleUntil(
tester,
() async =>
(await LogStorageService.instance.todayEntries()).length ==
(claimed ? 2 : 1),
);
final live = await LogStorageService.instance.todayEntries();
expect(live, hasLength(claimed ? 2 : 1));
});

View File

@ -4,6 +4,8 @@ library;
import 'dart:io';
import 'package:diet_guard_app/screens/log_meal_due_mixin.dart';
import 'package:diet_guard_app/screens/log_meal_logged_mixin.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';
@ -51,3 +53,28 @@ Future<void> settle(WidgetTester tester) async {
await Future<void>.delayed(const Duration(milliseconds: 200));
await tester.pumpAndSettle();
}
/// Waits for the screen's open-or-resume chain (sync, slots, catering
/// auto-log) to finish, then [settle]s.
Future<void> settleOpen(WidgetTester tester) async {
final state = tester.state(find.byType(LogMealScreen));
await (state as LogMealLoggedMixin).openChain;
await settle(tester);
}
/// [settle]s, then keeps pumping until [done] holds or [timeout] passes.
///
/// For chains a tap starts and nothing exposes: real file I/O outlasts the
/// fixed sleep in [settle] when the machine is loaded.
Future<void> settleUntil(
WidgetTester tester,
Future<bool> Function() done, {
Duration timeout = const Duration(seconds: 5),
}) async {
final deadline = DateTime.now().add(timeout);
await settle(tester);
while (!await done() && DateTime.now().isBefore(deadline)) {
await Future<void>.delayed(const Duration(milliseconds: 50));
await tester.pumpAndSettle();
}
}