mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-04 12:42:22 +01:00
Reject a session end before its start again
This commit is contained in:
@@ -33,16 +33,21 @@ String? validateWorkoutLogCrossField({
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Cross-field validation for a workout session, mirroring the backend rule.
|
||||
/// Cross-field validation for a workout session, mirroring the backend rules:
|
||||
/// the end must not lie before the start, and the session must not last longer
|
||||
/// than [sessionMaxDuration].
|
||||
///
|
||||
/// An end before the start is not an error, the form reads it as a session that
|
||||
/// ran past midnight. What the server does reject is a session longer than
|
||||
/// [sessionMaxDuration].
|
||||
/// A session that ran past midnight ends on the following day, so its end is
|
||||
/// after its start as well.
|
||||
String? validateWorkoutSessionTimes({
|
||||
required DateTime datetimeStart,
|
||||
required DateTime? datetimeEnd,
|
||||
required AppLocalizations i18n,
|
||||
}) {
|
||||
if (datetimeEnd != null && datetimeEnd.isBefore(datetimeStart)) {
|
||||
return i18n.timeStartAhead;
|
||||
}
|
||||
|
||||
if (datetimeEnd != null && datetimeEnd.difference(datetimeStart) > sessionMaxDuration) {
|
||||
return i18n.sessionTooLong(sessionMaxDuration.inHours);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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:wger/core/widgets/datetime_input.dart';
|
||||
import 'package:wger/features/routines/models/session.dart';
|
||||
import 'package:wger/features/routines/providers/workout_session_repository.dart';
|
||||
import 'package:wger/features/routines/widgets/forms/session.dart';
|
||||
@@ -121,6 +122,55 @@ void main() {
|
||||
expect(onSavedCalled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('refuses a start moved past the end', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
final existingSession = WorkoutSession(
|
||||
id: '1',
|
||||
routineId: 1,
|
||||
impression: WorkoutImpression.neutral,
|
||||
datetimeStart: DateTime(2026, 8, 13, 10, 0),
|
||||
datetimeEnd: DateTime(2026, 8, 13, 11, 0),
|
||||
);
|
||||
when(mockRepository.editLocalDrift(any as dynamic)).thenAnswer((_) async {});
|
||||
await pumpSessionForm(tester, session: existingSession);
|
||||
|
||||
// Act
|
||||
tester
|
||||
.widget<TimeInputWidget>(find.byKey(const ValueKey('time-start')))
|
||||
.onChanged(const TimeOfDay(hour: 12, minute: 0));
|
||||
await tester.tap(find.byKey(const ValueKey('save-button')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Assert
|
||||
verifyNever(mockRepository.editLocalDrift(any as dynamic));
|
||||
expect(find.text('Start time cannot be ahead of end time'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('saves a session that ran past midnight', (WidgetTester tester) async {
|
||||
// Arrange
|
||||
final existingSession = WorkoutSession(
|
||||
id: '1',
|
||||
routineId: 1,
|
||||
impression: WorkoutImpression.neutral,
|
||||
datetimeStart: DateTime(2026, 8, 13, 23, 0),
|
||||
);
|
||||
when(mockRepository.editLocalDrift(any as dynamic)).thenAnswer((_) async {});
|
||||
await pumpSessionForm(tester, session: existingSession);
|
||||
|
||||
// Act
|
||||
tester
|
||||
.widget<TimeInputWidget>(find.byKey(const ValueKey('time-end')))
|
||||
.onChanged(const TimeOfDay(hour: 1, minute: 0));
|
||||
await tester.tap(find.byKey(const ValueKey('save-button')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Assert
|
||||
final captured =
|
||||
verify(mockRepository.editLocalDrift(captureAny as dynamic)).captured.single
|
||||
as WorkoutSession;
|
||||
expect(captured.datetimeEnd, DateTime(2026, 8, 14, 1, 0));
|
||||
});
|
||||
|
||||
// testWidgets('shows server side error messages', (WidgetTester tester) async {
|
||||
// // Arrange
|
||||
// await pumpSessionForm(tester);
|
||||
|
||||
Reference in New Issue
Block a user