From e2721b5a298e57b34526395355910c20ad3f8114 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 15 Aug 2026 22:16:57 +0200 Subject: [PATCH] Split home_screen_test.dart (383) into three MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The file's four post-`main()` declarations were the blocker: FailingSyncService, OkSyncService, HangingSyncService and CountingSyncService all `extends WorkoutSyncService` and override `syncNow`. They moved to _home_test_fixtures.dart together with the `_pump` / `_wrap` locals, so both halves bind to one definition of each. home_screen_test.dart 185 navigation, resume, start-workout home_sync_card_test.dart 172 the sync-status card and its Retry tick _home_test_fixtures.dart 66 pumpHome, wrapHome, the four fakes Worth recording for the workout_sync_service.dart work still to come: these four fakes are exactly why `syncNow` cannot move into a `part` extension. Extension methods dispatch statically, so a subclass override of an extension member never runs — the real implementation would, and the tests would hang rather than fail. 402 tests pass; the name set gains only the new file's reporter lifecycle records. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01EveB813BQ8abAP68VSqZ5Y --- .../test/screens/_home_test_fixtures.dart | 72 ++++++ .../test/screens/home_screen_test.dart | 222 +----------------- .../test/screens/home_sync_card_test.dart | 172 ++++++++++++++ 3 files changed, 256 insertions(+), 210 deletions(-) create mode 100644 stronglift_replacement/workout_app/test/screens/_home_test_fixtures.dart create mode 100644 stronglift_replacement/workout_app/test/screens/home_sync_card_test.dart diff --git a/stronglift_replacement/workout_app/test/screens/_home_test_fixtures.dart b/stronglift_replacement/workout_app/test/screens/_home_test_fixtures.dart new file mode 100644 index 0000000..4372060 --- /dev/null +++ b/stronglift_replacement/workout_app/test/screens/_home_test_fixtures.dart @@ -0,0 +1,72 @@ +// Shared fixtures for the home-screen test files. +// +// The four fakes all subclass WorkoutSyncService and override `syncNow` — a +// real override, not an extension member, because extension methods dispatch +// statically and a fake could not intercept one. +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:workout_app/screens/home_screen.dart'; +import 'package:workout_app/services/workout_sync_service.dart'; +import 'package:workout_app/ui/theme.dart'; + +// runAsync steps outside FakeAsync so real I/O (NetworkInterface.list) completes. +Future pumpHome(WidgetTester tester, Widget w) async { + await tester.runAsync(() async { + await tester.pumpWidget(w); + // Small real delay lets sqflite + NetworkInterface.list complete. + await Future.delayed(const Duration(milliseconds: 200)); + }); + await tester.pump(); +} + +// The home screen syncs in the background on every open. Left to itself +// that reaches FlutterSecureStorage, whose platform channel throws under +// `flutter test`, so every test here would fail on a MissingPluginException +// that has nothing to do with what it is asserting. Report "not +// configured" instead: no credentials means no sync tick is attempted. +Widget wrapHome({Future Function()? configuredProbe}) => MaterialApp( + theme: buildAppTheme(), + home: HomeScreen( + configuredProbe: configuredProbe ?? () async => false, + clock: () => DateTime(2026, 8, 15, 18), + ), +); + +/// A sync service whose tick always fails, with a reason worth showing. +class FailingSyncService extends WorkoutSyncService { + @override + Future syncNow() async => + const PushResult(pushed: false, reason: 'sync failed: no network'); +} + +/// A sync service whose tick always succeeds. +class OkSyncService extends WorkoutSyncService { + @override + Future syncNow() async => + const PushResult(pushed: true, reason: 'synced'); +} + +/// A sync service whose tick never resolves, holding the card on whatever +/// the persisted state said — which is how a real slow network behaves. +class HangingSyncService extends WorkoutSyncService { + @override + Future syncNow() => Completer().future; +} + +/// A sync service that counts how many ticks it was asked for. +class CountingSyncService extends WorkoutSyncService { + CountingSyncService(this.onTick); + + /// Called once per [syncNow]. + final void Function() onTick; + + @override + Future syncNow() async { + onTick(); + // Fail, so the card keeps offering Retry instead of collapsing to the + // button-less healthy state after the first tick. + return const PushResult(pushed: false, reason: 'sync failed: offline'); + } +} diff --git a/stronglift_replacement/workout_app/test/screens/home_screen_test.dart b/stronglift_replacement/workout_app/test/screens/home_screen_test.dart index e0fc237..45926ed 100644 --- a/stronglift_replacement/workout_app/test/screens/home_screen_test.dart +++ b/stronglift_replacement/workout_app/test/screens/home_screen_test.dart @@ -12,6 +12,7 @@ import 'package:workout_app/ui/theme.dart'; import 'package:workout_app/widgets/sync_status_card.dart'; import '../fake_secure_storage.dart'; +import '_home_test_fixtures.dart'; void main() { setUpAll(() { @@ -24,46 +25,23 @@ void main() { await StorageService.init(); }); - // runAsync steps outside FakeAsync so real I/O (NetworkInterface.list) completes. - Future _pump(WidgetTester tester, Widget w) async { - await tester.runAsync(() async { - await tester.pumpWidget(w); - // Small real delay lets sqflite + NetworkInterface.list complete. - await Future.delayed(const Duration(milliseconds: 200)); - }); - await tester.pump(); - } - - // The home screen syncs in the background on every open. Left to itself - // that reaches FlutterSecureStorage, whose platform channel throws under - // `flutter test`, so every test here would fail on a MissingPluginException - // that has nothing to do with what it is asserting. Report "not - // configured" instead: no credentials means no sync tick is attempted. - Widget _wrap({Future Function()? configuredProbe}) => MaterialApp( - theme: buildAppTheme(), - home: HomeScreen( - configuredProbe: configuredProbe ?? () async => false, - clock: () => DateTime(2026, 8, 15, 18), - ), - ); - testWidgets('shows Workout Tracker app bar', (tester) async { - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); expect(find.text('Workout Tracker'), findsOneWidget); }); testWidgets('shows Next: Workout A when no workout done', (tester) async { - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); expect(find.textContaining('Next: Workout A'), findsOneWidget); }); testWidgets('shows Start Workout A button', (tester) async { - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); expect(find.text('Start Workout A'), findsOneWidget); }); testWidgets('history icon navigates to history screen', (tester) async { - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); await tester.tap(find.byIcon(Icons.history)); await tester.pump(); await tester.pump(const Duration(milliseconds: 300)); @@ -71,7 +49,7 @@ void main() { }); testWidgets('settings icon navigates to settings screen', (tester) async { - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); await tester.tap(find.byIcon(Icons.settings)); await tester.pump(); await tester.pump(const Duration(milliseconds: 300)); @@ -96,7 +74,7 @@ void main() { json: '{"exercises":[]}', ), ); - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); expect(find.text('Done for today!'), findsOneWidget); }); @@ -104,7 +82,7 @@ void main() { tester, ) async { installFakeSecureStorage(); // ManualWorkoutScreen loads its sync budget - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); await tester.tap(find.byIcon(Icons.edit_note)); await tester.pump(); await tester.pump(const Duration(milliseconds: 300)); @@ -114,7 +92,7 @@ void main() { testWidgets('starting a workout navigates to the workout screen', ( tester, ) async { - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); await tester.tap(find.text('Start Workout A')); await tester.pump(); await tester.pump(const Duration(milliseconds: 300)); @@ -131,7 +109,7 @@ void main() { 'exercises': [], }), ); - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); await tester.pump(const Duration(milliseconds: 300)); // The post-frame auto-resume pushed the workout screen. expect(find.textContaining('Workout A'), findsWidgets); @@ -156,7 +134,7 @@ void main() { // Drive the whole first-load + post-frame auto-resume push on the real loop // (auto-resume awaits getCurrentExercises, which hangs under FakeAsync). await tester.runAsync(() async { - await tester.pumpWidget(_wrap()); + await tester.pumpWidget(wrapHome()); await Future.delayed(const Duration(milliseconds: 300)); await tester.pump(); // fire the post-frame auto-resume callback await Future.delayed(const Duration(milliseconds: 300)); @@ -189,7 +167,7 @@ void main() { tester, ) async { installFakeSecureStorage(); // SettingsScreen reads the sync token on init - await _pump(tester, _wrap()); + await pumpHome(tester, wrapHome()); await tester.tap(find.byIcon(Icons.settings)); await tester.pump(); await tester.pump(const Duration(milliseconds: 300)); @@ -204,180 +182,4 @@ void main() { await tester.pump(const Duration(milliseconds: 300)); expect(find.text('Workout Tracker'), findsOneWidget); }); - - group('sync status card', () { - testWidgets('an unconfigured device is warned before it works out', ( - tester, - ) async { - await _pump(tester, _wrap(configuredProbe: () async => false)); - expect(find.byType(SyncStatusCard), findsOneWidget); - expect(find.text('Not connected to sync'), findsOneWidget); - }); - - testWidgets('the card sits ABOVE the workout card', (tester) async { - await _pump(tester, _wrap(configuredProbe: () async => false)); - // The whole point of the placement: the warning has to be read before - // the workout starts, not discovered after it failed to count. - final cardY = tester.getTopLeft(find.byType(SyncStatusCard)).dy; - final workoutY = tester.getTopLeft(find.text('Start Workout A')).dy; - expect(cardY, lessThan(workoutY)); - }); - - testWidgets('a failing sync surfaces the reason on the card', ( - tester, - ) async { - installFakeSecureStorage(); - await _pump( - tester, - MaterialApp( - theme: buildAppTheme(), - home: HomeScreen( - configuredProbe: () async => true, - syncService: _FailingSyncService(), - clock: () => DateTime(2026, 8, 15, 18), - ), - ), - ); - expect(find.text('Sync failed'), findsOneWidget); - expect(find.textContaining('no network'), findsOneWidget); - }); - - testWidgets('a phone that has not synced for days says so, before the tick', - (tester) async { - // Regression: computing the status only AFTER the tick made - // SyncState.outOfDate unreachable in the running app -- a success - // stamps the time (age 0 -> Synced) and a failure shows Sync failed, - // so a stale phone silently looked healthy. - installFakeSecureStorage(); - final now = DateTime(2026, 8, 15, 18); - await tester.runAsync(() async { - await StorageService.instance.markSyncedNow( - now.subtract(const Duration(days: 3)), - ); - }); - await _pump( - tester, - MaterialApp( - theme: buildAppTheme(), - home: HomeScreen( - configuredProbe: () async => true, - syncService: _HangingSyncService(), - clock: () => now, - ), - ), - ); - expect(find.text('Sync out of date'), findsOneWidget); - expect(find.textContaining('3d ago'), findsOneWidget); - }); - - testWidgets('Set up sync opens Settings so the user can fix it', ( - tester, - ) async { - installFakeSecureStorage(); // SettingsScreen reads the token on init - await _pump(tester, _wrap(configuredProbe: () async => false)); - await tester.tap(find.text('Set up sync')); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 300)); - // The card must lead somewhere: telling the user sync is broken and - // leaving them to find Settings is the silence this whole card fixes. - expect(find.text('Settings'), findsOneWidget); - - // Coming back re-checks sync, so connecting there clears the card - // without needing an app restart. - await tester.runAsync(() async { - tester.state(find.byType(Navigator).last).pop(); - await Future.delayed(const Duration(milliseconds: 300)); - }); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 300)); - expect(find.text('Workout Tracker'), findsOneWidget); - }); - - testWidgets('Retry runs another sync tick', (tester) async { - installFakeSecureStorage(); - var ticks = 0; - await _pump( - tester, - MaterialApp( - theme: buildAppTheme(), - home: HomeScreen( - configuredProbe: () async => true, - syncService: _CountingSyncService(() => ticks++), - clock: () => DateTime(2026, 8, 15, 18), - ), - ), - ); - expect(ticks, 1); // the automatic tick on open - await tester.runAsync(() async { - await tester.tap(find.text('Retry')); - await Future.delayed(const Duration(milliseconds: 200)); - }); - await tester.pump(); - expect(ticks, 2); - }); - - testWidgets('a successful sync stamps the time and shows Synced', ( - tester, - ) async { - installFakeSecureStorage(); - final now = DateTime(2026, 8, 15, 18); - await _pump( - tester, - MaterialApp( - theme: buildAppTheme(), - home: HomeScreen( - configuredProbe: () async => true, - syncService: _OkSyncService(), - clock: () => now, - ), - ), - ); - expect(find.textContaining('Synced'), findsOneWidget); - // The timestamp must actually persist, or the card would go stale - // again on the very next open. Read it inside runAsync: the DB is real - // I/O, and awaiting it under FakeAsync hangs the test. - DateTime? stamped; - await tester.runAsync(() async { - stamped = await StorageService.instance.getLastSyncedAt(); - }); - expect(stamped, now); - }); - }); -} - -/// A sync service whose tick always fails, with a reason worth showing. -class _FailingSyncService extends WorkoutSyncService { - @override - Future syncNow() async => - const PushResult(pushed: false, reason: 'sync failed: no network'); -} - -/// A sync service whose tick always succeeds. -class _OkSyncService extends WorkoutSyncService { - @override - Future syncNow() async => - const PushResult(pushed: true, reason: 'synced'); -} - -/// A sync service whose tick never resolves, holding the card on whatever -/// the persisted state said — which is how a real slow network behaves. -class _HangingSyncService extends WorkoutSyncService { - @override - Future syncNow() => Completer().future; -} - -/// A sync service that counts how many ticks it was asked for. -class _CountingSyncService extends WorkoutSyncService { - _CountingSyncService(this.onTick); - - /// Called once per [syncNow]. - final void Function() onTick; - - @override - Future syncNow() async { - onTick(); - // Fail, so the card keeps offering Retry instead of collapsing to the - // button-less healthy state after the first tick. - return const PushResult(pushed: false, reason: 'sync failed: offline'); - } } diff --git a/stronglift_replacement/workout_app/test/screens/home_sync_card_test.dart b/stronglift_replacement/workout_app/test/screens/home_sync_card_test.dart new file mode 100644 index 0000000..67bfa8b --- /dev/null +++ b/stronglift_replacement/workout_app/test/screens/home_sync_card_test.dart @@ -0,0 +1,172 @@ +// The home screen's sync-status card: state, Retry, and the sync tick. +// +// Split out of home_screen_test.dart, which keeps navigation and the +// workout-start paths. +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:workout_app/models/workout_plan.dart'; +import 'package:workout_app/screens/home_screen.dart'; +import 'package:workout_app/screens/workout_screen.dart'; +import 'package:workout_app/services/storage_service.dart'; +import 'package:workout_app/services/workout_sync_service.dart'; +import 'package:workout_app/ui/theme.dart'; +import 'package:workout_app/widgets/sync_status_card.dart'; + +import '../fake_secure_storage.dart'; +import '_home_test_fixtures.dart'; + +void main() { + setUpAll(() { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + }); + + setUp(() async { + StorageService.resetForTesting(); + await StorageService.init(); + }); + + group('sync status card', () { + testWidgets('an unconfigured device is warned before it works out', ( + tester, + ) async { + await pumpHome(tester, wrapHome(configuredProbe: () async => false)); + expect(find.byType(SyncStatusCard), findsOneWidget); + expect(find.text('Not connected to sync'), findsOneWidget); + }); + + testWidgets('the card sits ABOVE the workout card', (tester) async { + await pumpHome(tester, wrapHome(configuredProbe: () async => false)); + // The whole point of the placement: the warning has to be read before + // the workout starts, not discovered after it failed to count. + final cardY = tester.getTopLeft(find.byType(SyncStatusCard)).dy; + final workoutY = tester.getTopLeft(find.text('Start Workout A')).dy; + expect(cardY, lessThan(workoutY)); + }); + + testWidgets('a failing sync surfaces the reason on the card', ( + tester, + ) async { + installFakeSecureStorage(); + await pumpHome( + tester, + MaterialApp( + theme: buildAppTheme(), + home: HomeScreen( + configuredProbe: () async => true, + syncService: FailingSyncService(), + clock: () => DateTime(2026, 8, 15, 18), + ), + ), + ); + expect(find.text('Sync failed'), findsOneWidget); + expect(find.textContaining('no network'), findsOneWidget); + }); + + testWidgets( + 'a phone that has not synced for days says so, before the tick', + (tester) async { + // Regression: computing the status only AFTER the tick made + // SyncState.outOfDate unreachable in the running app -- a success + // stamps the time (age 0 -> Synced) and a failure shows Sync failed, + // so a stale phone silently looked healthy. + installFakeSecureStorage(); + final now = DateTime(2026, 8, 15, 18); + await tester.runAsync(() async { + await StorageService.instance.markSyncedNow( + now.subtract(const Duration(days: 3)), + ); + }); + await pumpHome( + tester, + MaterialApp( + theme: buildAppTheme(), + home: HomeScreen( + configuredProbe: () async => true, + syncService: HangingSyncService(), + clock: () => now, + ), + ), + ); + expect(find.text('Sync out of date'), findsOneWidget); + expect(find.textContaining('3d ago'), findsOneWidget); + }, + ); + + testWidgets('Set up sync opens Settings so the user can fix it', ( + tester, + ) async { + installFakeSecureStorage(); // SettingsScreen reads the token on init + await pumpHome(tester, wrapHome(configuredProbe: () async => false)); + await tester.tap(find.text('Set up sync')); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 300)); + // The card must lead somewhere: telling the user sync is broken and + // leaving them to find Settings is the silence this whole card fixes. + expect(find.text('Settings'), findsOneWidget); + + // Coming back re-checks sync, so connecting there clears the card + // without needing an app restart. + await tester.runAsync(() async { + tester.state(find.byType(Navigator).last).pop(); + await Future.delayed(const Duration(milliseconds: 300)); + }); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 300)); + expect(find.text('Workout Tracker'), findsOneWidget); + }); + + testWidgets('Retry runs another sync tick', (tester) async { + installFakeSecureStorage(); + var ticks = 0; + await pumpHome( + tester, + MaterialApp( + theme: buildAppTheme(), + home: HomeScreen( + configuredProbe: () async => true, + syncService: CountingSyncService(() => ticks++), + clock: () => DateTime(2026, 8, 15, 18), + ), + ), + ); + expect(ticks, 1); // the automatic tick on open + await tester.runAsync(() async { + await tester.tap(find.text('Retry')); + await Future.delayed(const Duration(milliseconds: 200)); + }); + await tester.pump(); + expect(ticks, 2); + }); + + testWidgets('a successful sync stamps the time and shows Synced', ( + tester, + ) async { + installFakeSecureStorage(); + final now = DateTime(2026, 8, 15, 18); + await pumpHome( + tester, + MaterialApp( + theme: buildAppTheme(), + home: HomeScreen( + configuredProbe: () async => true, + syncService: OkSyncService(), + clock: () => now, + ), + ), + ); + expect(find.textContaining('Synced'), findsOneWidget); + // The timestamp must actually persist, or the card would go stale + // again on the very next open. Read it inside runAsync: the DB is real + // I/O, and awaiting it under FakeAsync hangs the test. + DateTime? stamped; + await tester.runAsync(() async { + stamped = await StorageService.instance.getLastSyncedAt(); + }); + expect(stamped, now); + }); + }); +}