From f11cd900f138cbf1c40a3a88d95c744447ebfce7 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 26 Aug 2026 01:48:25 +0200 Subject: [PATCH] Test the workout session time validator --- test/features/routines/validators_test.dart | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 test/features/routines/validators_test.dart diff --git a/test/features/routines/validators_test.dart b/test/features/routines/validators_test.dart new file mode 100644 index 000000000..1140bcb70 --- /dev/null +++ b/test/features/routines/validators_test.dart @@ -0,0 +1,81 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 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_test/flutter_test.dart'; +import 'package:wger/features/routines/models/session.dart'; +import 'package:wger/features/routines/validators.dart'; +import 'package:wger/l10n/generated/app_localizations_en.dart'; + +void main() { + final i18n = AppLocalizationsEn(); + final start = DateTime(2026, 5, 4, 18, 0); + + String? validate(DateTime? end) => + validateWorkoutSessionTimes(datetimeStart: start, datetimeEnd: end, i18n: i18n); + + group('validateWorkoutLogCrossField', () { + test('a log needs a repetition count or a weight', () { + expect( + validateWorkoutLogCrossField(repetitions: null, weight: null, i18n: i18n), + i18n.weightOrRepsRequired, + ); + }); + + test('either one on its own is enough', () { + expect(validateWorkoutLogCrossField(repetitions: 8, weight: null, i18n: i18n), isNull); + expect(validateWorkoutLogCrossField(repetitions: null, weight: 60, i18n: i18n), isNull); + expect(validateWorkoutLogCrossField(repetitions: 8, weight: 60, i18n: i18n), isNull); + }); + }); + + group('validateWorkoutSessionTimes', () { + test('a session that is still running has nothing to compare', () { + expect(validate(null), isNull); + }); + + test('an end after the start passes', () { + expect(validate(start.add(const Duration(hours: 1))), isNull); + }); + + test('an end before the start is refused', () { + expect(validate(start.subtract(const Duration(minutes: 30))), i18n.timeStartAhead); + }); + + test('a session running past midnight ends on the next day, not before its start', () { + // 18:00 to 00:30 is six and a half hours on the following day + expect(validate(DateTime(2026, 5, 5, 0, 30)), i18n.sessionTooLong(5)); + expect(validate(DateTime(2026, 5, 4, 23, 0)), isNull); + }); + + test('the longest allowed session is exactly the maximum', () { + // The bound the server enforces as WGER_MAX_SESSION_LENGTH_HOURS + expect(validate(start.add(sessionMaxDuration)), isNull); + }); + + test('a minute over the maximum is refused', () { + expect( + validate(start.add(sessionMaxDuration + const Duration(minutes: 1))), + i18n.sessionTooLong(sessionMaxDuration.inHours), + ); + }); + + test('the message names the hours the server allows', () { + expect(validate(start.add(const Duration(hours: 9))), contains('5')); + }); + }); +}