diff --git a/test/routine/gym_mode/gym_mode_test.dart b/test/routine/gym_mode/gym_mode_test.dart index 6cca4793f..92553f558 100644 --- a/test/routine/gym_mode/gym_mode_test.dart +++ b/test/routine/gym_mode/gym_mode_test.dart @@ -25,6 +25,7 @@ import 'package:mockito/mockito.dart'; import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; +import 'package:wger/models/workouts/log.dart'; import 'package:wger/models/workouts/repetition_unit.dart'; import 'package:wger/models/workouts/session.dart'; import 'package:wger/models/workouts/weight_unit.dart'; @@ -33,6 +34,7 @@ import 'package:wger/providers/exercises_notifier.dart'; import 'package:wger/providers/network_provider.dart'; import 'package:wger/providers/routines_notifier.dart'; import 'package:wger/providers/routines_repository.dart'; +import 'package:wger/providers/workout_logs_repository.dart'; import 'package:wger/providers/workout_session_repository.dart'; import 'package:wger/screens/gym_mode.dart'; import 'package:wger/screens/routine_screen.dart'; @@ -50,7 +52,12 @@ import '../../../test_data/routines.dart'; import '../../fake_connectivity.dart'; import 'gym_mode_test.mocks.dart'; -@GenerateMocks([WorkoutSessionRepository, ExerciseRepository, RoutinesRepository]) +@GenerateMocks([ + WorkoutSessionRepository, + ExerciseRepository, + RoutinesRepository, + WorkoutLogRepository, +]) void main() { installFakeConnectivity(); @@ -62,9 +69,11 @@ void main() { final mockSessionRepo = MockWorkoutSessionRepository(); final mockExerciseRepo = MockExerciseRepository(); final mockRoutinesRepo = MockRoutinesRepository(); + final mockWorkoutLogRepo = MockWorkoutLogRepository(); setUp(() { SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty(); + when(mockWorkoutLogRepo.addLocalDrift(any)).thenAnswer((_) async {}); when(mockSessionRepo.watchAllDrift()).thenAnswer( (_) => Stream>.multi((controller) { controller.add(testRoutine.sessions); @@ -94,6 +103,7 @@ void main() { routinesRepositoryProvider.overrideWithValue(mockRoutinesRepo), exerciseRepositoryProvider.overrideWithValue(mockExerciseRepo), workoutSessionRepositoryProvider.overrideWithValue(mockSessionRepo), + workoutLogRepositoryProvider.overrideWithValue(mockWorkoutLogRepo), // The repetition + weight unit catalogues are tiny direct-Drift // stream providers, overriding them inline is the established // pattern (see also [exerciseCategoriesProvider] etc.). @@ -330,6 +340,42 @@ void main() { semanticsEnabled: false, ); + testWidgets('saving a log persists the entered reps/weight with the slot/routine/iteration', ( + WidgetTester tester, + ) async { + await withClock(Clock.fixed(DateTime(2025, 3, 29, 14, 33)), () async { + await tester.pumpWidget(renderGymMode()); + await tester.pumpAndSettle(); + await tester.tap(find.byType(TextButton)); + await tester.pumpAndSettle(); + + // Start page -> bench press overview -> its log page. + await tester.tap(find.byIcon(Icons.chevron_right)); + await tester.pumpAndSettle(); + await tester.drag(find.byType(ExerciseOverview), const Offset(-500, 0)); + await tester.pumpAndSettle(); + expect(find.byType(LogPage), findsOneWidget); + + // Overwrite the pre-filled values so the assertion proves the user's + // edits flow through to the saved log, not just the set-config defaults. + final fields = find.byType(TextFormField); + await tester.enterText(fields.at(0), '12'); // reps + await tester.enterText(fields.at(1), '34'); // weight + await tester.pump(); + + await tester.tap(find.byKey(const ValueKey('save-log-button'))); + await tester.pumpAndSettle(); + + final saved = verify(mockWorkoutLogRepo.addLocalDrift(captureAny)).captured.single as Log; + expect(saved.repetitions, 12); + expect(saved.weight, 34); + expect(saved.routineId, testRoutine.id); + expect(saved.iteration, 1); + // Linked to the currently shown slot entry (bench press), not left null. + expect(saved.slotEntryId, isNotNull); + }); + }); + testWidgets('loads offline from the cached routine', (WidgetTester tester) async { // Offline, gym mode uses the already-downloaded (hydrated) routine. when(mockRoutinesRepo.watchAllDrift()).thenAnswer( diff --git a/test/routine/gym_mode/gym_mode_test.mocks.dart b/test/routine/gym_mode/gym_mode_test.mocks.dart index ed6df6819..3a0d77ced 100644 --- a/test/routine/gym_mode/gym_mode_test.mocks.dart +++ b/test/routine/gym_mode/gym_mode_test.mocks.dart @@ -14,6 +14,7 @@ import 'package:wger/models/exercises/exercise_filters.dart' as _i12; import 'package:wger/models/exercises/muscle.dart' as _i16; import 'package:wger/models/workouts/base_config.dart' as _i6; import 'package:wger/models/workouts/day.dart' as _i3; +import 'package:wger/models/workouts/log.dart' as _i22; import 'package:wger/models/workouts/repetition_unit.dart' as _i20; import 'package:wger/models/workouts/routine.dart' as _i2; import 'package:wger/models/workouts/session.dart' as _i9; @@ -23,6 +24,7 @@ import 'package:wger/models/workouts/weight_unit.dart' as _i19; import 'package:wger/providers/exercise_repository.dart' as _i10; import 'package:wger/providers/exercises_notifier.dart' as _i14; import 'package:wger/providers/routines_repository.dart' as _i18; +import 'package:wger/providers/workout_logs_repository.dart' as _i21; import 'package:wger/providers/workout_session_repository.dart' as _i7; // ignore_for_file: type=lint @@ -409,3 +411,39 @@ class MockRoutinesRepository extends _i1.Mock implements _i18.RoutinesRepository ) as _i8.Future); } + +/// A class which mocks [WorkoutLogRepository]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockWorkoutLogRepository extends _i1.Mock implements _i21.WorkoutLogRepository { + MockWorkoutLogRepository() { + _i1.throwOnMissingStub(this); + } + + @override + _i8.Future deleteLocalDrift(String? id) => + (super.noSuchMethod( + Invocation.method(#deleteLocalDrift, [id]), + returnValue: _i8.Future.value(), + returnValueForMissingStub: _i8.Future.value(), + ) + as _i8.Future); + + @override + _i8.Future updateLocalDrift(_i22.Log? log) => + (super.noSuchMethod( + Invocation.method(#updateLocalDrift, [log]), + returnValue: _i8.Future.value(), + returnValueForMissingStub: _i8.Future.value(), + ) + as _i8.Future); + + @override + _i8.Future addLocalDrift(_i22.Log? log) => + (super.noSuchMethod( + Invocation.method(#addLocalDrift, [log]), + returnValue: _i8.Future.value(), + returnValueForMissingStub: _i8.Future.value(), + ) + as _i8.Future); +} diff --git a/test/widgets/routines/gym_mode/log_page_test.dart.TODO b/test/widgets/routines/gym_mode/log_page_test.dart.TODO deleted file mode 100644 index ed11aaa0d..000000000 --- a/test/widgets/routines/gym_mode/log_page_test.dart.TODO +++ /dev/null @@ -1,360 +0,0 @@ -/* - * This file is part of wger Workout Manager . - * Copyright (c) 2025 - 2026 wger Team - * - * wger Workout Manager is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; -import 'package:provider/provider.dart' as provider; -import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; -import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; -import 'package:wger/l10n/generated/app_localizations.dart'; -import 'package:wger/models/exercises/exercise.dart'; -import 'package:wger/models/workouts/day_data.dart'; -import 'package:wger/models/workouts/log.dart'; -import 'package:wger/models/workouts/set_config_data.dart'; -import 'package:wger/models/workouts/slot_data.dart'; -import 'package:wger/providers/gym_state.dart'; -import 'package:wger/providers/routines.dart'; -import 'package:wger/widgets/routines/gym_mode/log_page.dart'; - -import '../../../../test_data/exercises.dart'; -import '../../../../test_data/routines.dart' as testdata; -import 'log_page_test.mocks.dart'; - -@GenerateMocks([ExercisesProvider, RoutinesProvider]) -void main() { - TestWidgetsFlutterBinding.ensureInitialized(); - - group('LogPage tests', () { - late List testExercises; - late ProviderContainer container; - - setUp(() { - SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty(); - testExercises = getTestExercises(); - container = ProviderContainer.test(); - }); - - Future pumpLogPage(WidgetTester tester, {RoutinesProvider? routinesProvider}) async { - final providerValue = routinesProvider ?? MockRoutinesProvider(); - - await tester.pumpWidget( - UncontrolledProviderScope( - container: container, - child: provider.ChangeNotifierProvider.value( - value: providerValue, - child: MaterialApp( - locale: const Locale('en'), - localizationsDelegates: AppLocalizations.localizationsDelegates, - supportedLocales: AppLocalizations.supportedLocales, - home: Scaffold( - // Provide a PageView so the PageController used by LogPage is attached - body: Builder( - builder: (context) { - final controller = PageController(); - return PageView( - controller: controller, - children: [LogPage(controller)], - ); - }, - ), - ), - ), - ), - ), - ); - - await tester.pumpAndSettle(); - } - - testWidgets('handles null values', (tester) async { - // Arrange - final notifier = container.read(gymStateProvider.notifier); - final routine = testdata.getTestRoutine(); - routine.dayDataGym = [ - DayData( - iteration: 1, - date: DateTime(2024, 11, 01), - label: '', - day: routine.dayDataGym.first.day, - slots: [ - SlotData( - isSuperset: false, - exerciseIds: [testExercises[0].id!], - setConfigs: [ - SetConfigData( - exerciseId: testExercises[0].id!, - exercise: testExercises[0], - slotEntryId: 1, - nrOfSets: 1, - repetitions: null, - repetitionsUnit: null, - weight: null, - weightUnit: null, - restTime: 120, - rir: 1.5, - rpe: 8, - textRepr: '3x100kg', - ), - ], - ), - ], - ), - ]; - notifier.state = notifier.state.copyWith( - dayId: routine.days.first.id, - routine: routine, - iteration: 1, - currentPage: 2, - ); - - // Act - notifier.calculatePages(); - notifier.setCurrentPage(2); - - // Assert - expect(notifier.state.getSlotEntryPageByIndex()!.type, SlotPageType.log); - await pumpLogPage(tester); - expect(find.byType(LogPage), findsOneWidget); - }); - - testWidgets('renders without crashing for default slotEntryPage', (tester) async { - final notifier = container.read(gymStateProvider.notifier); - final routine = testdata.getTestRoutine(); - notifier.state = notifier.state.copyWith( - dayId: routine.days.first.id, - routine: routine, - iteration: 1, - ); - notifier.calculatePages(); - await pumpLogPage(tester); - - expect(find.byType(LogPage), findsOneWidget); - }); - - testWidgets('copy from past log updates form fields and shows SnackBar', (tester) async { - // Arrange - final notifier = container.read(gymStateProvider.notifier); - final routine = testdata.getTestRoutine(); - notifier.state = notifier.state.copyWith( - dayId: routine.days.first.id, - routine: routine, - iteration: 1, - ); - notifier.calculatePages(); - notifier.setCurrentPage(2); - - // Act - // Log page is at index 2 - notifier.state = notifier.state.copyWith(currentPage: 2); - expect(notifier.state.getSlotEntryPageByIndex()!.type, SlotPageType.log); - await pumpLogPage(tester); - - // Assert - final pastLogTile = find.byKey(const ValueKey('past-log-1')); - expect(pastLogTile, findsOneWidget); - await tester.tap(pastLogTile); - await tester.pumpAndSettle(); - - final editableFields = find.byType(EditableText); - expect(editableFields, findsWidgets); - - // Get controller texts - final repControllerText = tester.widget(editableFields.at(0)).controller.text; - final weightControllerText = tester - .widget(editableFields.at(1)) - .controller - .text; - - expect(repControllerText, contains('10')); - expect(weightControllerText, contains('10')); - expect(find.byType(SnackBar), findsOneWidget); - }); - - testWidgets('save button calls addLog on RoutinesProvider', (tester) async { - // Arrange - final notifier = container.read(gymStateProvider.notifier); - final routine = testdata.getTestRoutine(); - notifier.state = notifier.state.copyWith( - dayId: routine.days.first.id, - routine: routine, - iteration: 1, - ); - notifier.calculatePages(); - notifier.setCurrentPage(2); - notifier.state = notifier.state.copyWith(currentPage: 2); - final mockRoutines = MockRoutinesProvider(); - - // Act - await pumpLogPage(tester, routinesProvider: mockRoutines); - - final editableFields = find.byType(EditableText); - expect(editableFields, findsWidgets); - - await tester.enterText(editableFields.at(0), '12'); // Reps - await tester.enterText(editableFields.at(1), '34'); // Weight - await tester.pump(); - - Log? capturedLog; - when(mockRoutines.addLog(any)).thenAnswer((invocation) async { - capturedLog = invocation.positionalArguments[0] as Log; - capturedLog!.id = '42'; - return capturedLog!; - }); - - final saveButton = find.byKey(const ValueKey('save-log-button')); - expect(saveButton, findsOneWidget); - - await tester.tap(saveButton); - await tester.pumpAndSettle(); - - // Assert - verify(mockRoutines.addLog(any)).called(1); - expect(capturedLog, isNotNull); - expect(capturedLog!.repetitions, equals(12)); - expect(capturedLog!.weight, equals(34)); - - final currentSlotPage = notifier.state.getSlotEntryPageByIndex()!; - expect(capturedLog!.slotEntryId, equals(currentSlotPage.setConfigData!.slotEntryId)); - expect(capturedLog!.routineId, equals(notifier.state.routine.id)); - expect(capturedLog!.iteration, equals(notifier.state.iteration)); - }); - - testWidgets('LogsRepsWidget quick buttons update values', (tester) async { - // Arrange - final notifier = container.read(gymStateProvider.notifier); - final routine = testdata.getTestRoutine(); - routine.dayDataGym[0].slots[0].setConfigs[0].repetitions = 0; - notifier.state = notifier.state.copyWith( - dayId: routine.days.first.id, - routine: routine, - iteration: 1, - ); - notifier.calculatePages(); - notifier.setCurrentPage(2); - notifier.state = notifier.state.copyWith(currentPage: 2); - final mockRoutines = MockRoutinesProvider(); - await pumpLogPage(tester, routinesProvider: mockRoutines); - - // Act - final repsWidgetFinder = find.byKey(const ValueKey('logs-reps-widget')); - expect(repsWidgetFinder, findsOneWidget); - final addBtn = find.descendant( - of: repsWidgetFinder, - matching: find.byIcon(Icons.add), - ); - final removeBtn = find.descendant( - of: repsWidgetFinder, - matching: find.byIcon(Icons.remove), - ); - - // Assert - // Increment 0 -> 1 - await tester.tap(addBtn); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); - await tester.pumpAndSettle(); - expect( - find.descendant(of: repsWidgetFinder, matching: find.text('1')), - findsOneWidget, - ); - - // Increment 1 -> 2 - await tester.tap(addBtn); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); - await tester.pumpAndSettle(); - expect( - find.descendant(of: repsWidgetFinder, matching: find.text('2')), - findsOneWidget, - ); - - // Decrement 2 -> 1 - await tester.tap(removeBtn); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); - await tester.pumpAndSettle(); - expect( - find.descendant(of: repsWidgetFinder, matching: find.text('1')), - findsOneWidget, - ); - }); - - testWidgets('LogsWeightWidget quick buttons update values', (tester) async { - // Arrange - final notifier = container.read(gymStateProvider.notifier); - final routine = testdata.getTestRoutine(); - routine.dayDataGym[0].slots[0].setConfigs[0].weight = 0; - notifier.state = notifier.state.copyWith( - dayId: routine.days.first.id, - routine: routine, - iteration: 1, - ); - notifier.calculatePages(); - notifier.setCurrentPage(2); - notifier.state = notifier.state.copyWith(currentPage: 2); - final mockRoutines = MockRoutinesProvider(); - await pumpLogPage(tester, routinesProvider: mockRoutines); - - // Act - final weightWidgetFinder = find.byKey(const ValueKey('logs-weight-widget')); - expect(weightWidgetFinder, findsOneWidget); - final addBtn = find.descendant( - of: weightWidgetFinder, - matching: find.byIcon(Icons.add), - ); - final removeBtn = find.descendant( - of: weightWidgetFinder, - matching: find.byIcon(Icons.remove), - ); - - // Assert - // Increment 0 -> 1.25 - await tester.tap(addBtn); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); - await tester.pumpAndSettle(); - expect( - find.descendant(of: weightWidgetFinder, matching: find.text('1.25')), - findsOneWidget, - ); - - // Increment 1.25 -> 2.5 - await tester.tap(addBtn); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); - await tester.pumpAndSettle(); - expect( - find.descendant(of: weightWidgetFinder, matching: find.text('2.5')), - findsOneWidget, - ); - - // Decrement 2.5 -> 1.25 - await tester.tap(removeBtn); - await tester.pump(); - await tester.pump(const Duration(milliseconds: 100)); - await tester.pumpAndSettle(); - expect( - find.descendant(of: weightWidgetFinder, matching: find.text('1.25')), - findsOneWidget, - ); - }); - }); -}