diff --git a/stronglift_replacement/workout_app/lib/services/storage_service_gap_deload.dart b/stronglift_replacement/workout_app/lib/services/storage_service_gap_deload.dart index 1926b0e..f25e76b 100644 --- a/stronglift_replacement/workout_app/lib/services/storage_service_gap_deload.dart +++ b/stronglift_replacement/workout_app/lib/services/storage_service_gap_deload.dart @@ -160,6 +160,9 @@ extension StorageServiceGapDeload on StorageService { where: 'name = ?', whereArgs: [state.name], ); + // Only a concurrent delete between the read and this write lands here; + // no test can interleave one inside the transaction. + // coverage:ignore-start if (rows != 1) { log( 'StorageService: ${state.name} vanished from exercise_state during ' @@ -168,6 +171,7 @@ extension StorageServiceGapDeload on StorageService { ); return false; } + // coverage:ignore-end await txn.insert('progression_events', { 'at': at.toIso8601String(), 'exercise': state.name, diff --git a/stronglift_replacement/workout_app/test/screens/home_gap_deload_test.dart b/stronglift_replacement/workout_app/test/screens/home_gap_deload_test.dart new file mode 100644 index 0000000..0f1d542 --- /dev/null +++ b/stronglift_replacement/workout_app/test/screens/home_gap_deload_test.dart @@ -0,0 +1,78 @@ +// The gap deload end to end on launch: home cuts the lift before it reads +// the targets, and the auto-opened workout shows the old weight struck +// through next to the new one. +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.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/ui/theme.dart'; + +import '../fake_audio_platform.dart'; + +void main() { + setUpAll(() { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + }); + + setUp(() async { + StorageService.resetForTesting(); + await StorageService.init(); + installFakeAudioPlatform(); + }); + + testWidgets('an 8-day gap opens the workout one step lighter', ( + tester, + ) async { + final lift = workoutA.first.name; + final end = DateTime.now().subtract(const Duration(days: 8)); + await tester.runAsync(() async { + final svc = StorageService.instance; + await svc.setExerciseWeight(lift, 25); + await svc.saveSession( + date: end.toIso8601String().substring(0, 10), + // B last, so the next workout is A and holds the lift. + workoutType: 'B', + durationSeconds: 3600, + succeeded: true, + json: jsonEncode({ + 'end_time': end.toIso8601String(), + 'exercises': [ + {'name': lift, 'targetWeight': 25, 'targetReps': 5}, + ], + }), + ); + }); + + await tester.runAsync(() async { + await tester.pumpWidget( + MaterialApp( + theme: buildAppTheme(), + home: HomeScreen(configuredProbe: () async => false), + ), + ); + await Future.delayed(const Duration(milliseconds: 300)); + await tester.pump(); // fire the post-frame auto-open + await Future.delayed(const Duration(milliseconds: 500)); + }); + await tester.pump(); + // The workout's own load (targets, then the deload marks) on the real + // loop too. + await tester.runAsync( + () => Future.delayed(const Duration(milliseconds: 500)), + ); + await tester.pump(); + + expect(find.byType(WorkoutScreen), findsOneWidget); + final s = await tester.runAsync( + () => StorageService.instance.getExerciseState(lift), + ); + expect(s!.weight, 22.5); + expect(find.byIcon(Icons.arrow_downward), findsOneWidget); + }); +} diff --git a/stronglift_replacement/workout_app/test/screens/settings_screen_sandbox_test.dart b/stronglift_replacement/workout_app/test/screens/settings_screen_sandbox_test.dart index b3dbf68..a015f8f 100644 --- a/stronglift_replacement/workout_app/test/screens/settings_screen_sandbox_test.dart +++ b/stronglift_replacement/workout_app/test/screens/settings_screen_sandbox_test.dart @@ -167,4 +167,14 @@ void main() { await tapReal(tester, iconLabeled('Wipe sandbox data')); expect(Sandbox.runReplayFactor, Sandbox.defaultReplayFactor); }); + + testWidgets('seed gap cases backdates sessions and says so', (tester) async { + await pump(tester); + await tapReal(tester, iconLabeled('Seed gap deload cases')); + expect(iconLabelContaining('Seeded backdated'), findsOneWidget); + final next = await tester.runAsync( + StorageService.instance.getNextWorkoutType, + ); + expect(next, 'A'); + }); } diff --git a/stronglift_replacement/workout_app/test/services/storage_service_gap_deload_edges_test.dart b/stronglift_replacement/workout_app/test/services/storage_service_gap_deload_edges_test.dart new file mode 100644 index 0000000..07ac2d9 --- /dev/null +++ b/stronglift_replacement/workout_app/test/services/storage_service_gap_deload_edges_test.dart @@ -0,0 +1,67 @@ +// Gap deload inputs the happy-path tests never produce: a history row with +// no readable time, and a stored stamp that is not a date. +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:path/path.dart' as p; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:workout_app/models/workout_plan.dart'; +import 'package:workout_app/services/storage_service.dart'; + +void main() { + setUpAll(() { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + }); + + final lift = workoutA.first.name; + + test('a row with no readable time counts toward no gap', () async { + StorageService.resetForTesting(); + await StorageService.init(); + final svc = StorageService.instance; + await svc.saveSession( + date: 'not a date', + workoutType: 'A', + durationSeconds: 1, + succeeded: true, + json: jsonEncode({ + 'exercises': [ + {'name': lift}, + ], + }), + ); + expect((await svc.sessionMarks())[lift], isNull); + expect(await svc.applyGapDeloads(now: DateTime(2026, 11, 20)), isEmpty); + }); + + test('an unreadable stamp reads as never deloaded', () async { + final dir = await Directory.systemTemp.createTemp('gap_stamp'); + addTearDown(() => dir.delete(recursive: true)); + final path = p.join(dir.path, 'w.db'); + StorageService.resetForTesting(dbPath: path); + await StorageService.init(); + final svc = StorageService.instance; + await svc.setExerciseWeight(lift, 25); + // A second connection to the same file: the service exposes no raw + // write, and a sync pull always parses the stamp first. + final raw = await databaseFactoryFfi.openDatabase(path); + await raw.update( + 'exercise_state', + {'last_deloaded_at': 'garbage'}, + where: 'name = ?', + whereArgs: [lift], + ); + await raw.close(); + expect((await svc.getExerciseState(lift))!.lastDeloadedAt, isNull); + }); + + test('the sandbox seed defaults to the real clock', () async { + StorageService.resetForTesting(); + await StorageService.init(); + final svc = StorageService.instance; + await svc.seedGapScenarios(); + expect(await svc.applyGapDeloads(), isNotEmpty); + }); +}