From 1b87fd2120a5daccd3fcae13f311e54e2ad28a99 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 30 Jul 2026 21:30:16 +0200 Subject: [PATCH] Show the weight form bounds rounded inwards --- lib/features/weight/widgets/forms.dart | 7 ++- test/features/weight/widgets/forms_test.dart | 58 ++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/lib/features/weight/widgets/forms.dart b/lib/features/weight/widgets/forms.dart index e256a7b2f..83ba9ec64 100644 --- a/lib/features/weight/widgets/forms.dart +++ b/lib/features/weight/widgets/forms.dart @@ -202,10 +202,11 @@ class _WeightFormState extends riverpod.ConsumerState { return i18n.enterValidNumber; } if (!_isInRange(parsed)) { - // The bounds are defined in kg, show them in the entered unit + // The bounds are defined in kg; show them in the entered unit, + // rounded inwards so every displayed bound is itself accepted return i18n.formMinMaxValues( - convertWeight(_minWeightKg, from: 'kg', to: _unit).round(), - convertWeight(_maxWeightKg, from: 'kg', to: _unit).round(), + convertWeight(_minWeightKg, from: 'kg', to: _unit).ceil(), + convertWeight(_maxWeightKg, from: 'kg', to: _unit).floor(), ); } return null; diff --git a/test/features/weight/widgets/forms_test.dart b/test/features/weight/widgets/forms_test.dart index fd479996b..c15addbae 100644 --- a/test/features/weight/widgets/forms_test.dart +++ b/test/features/weight/widgets/forms_test.dart @@ -203,6 +203,64 @@ void main() { expect(saved.extraData, {'unit': 'lb'}); }); + testWidgets('Accepts imperial weights over the 300 kg display bound', ( + WidgetTester tester, + ) async { + when( + mockProfileRepo.watchDrift(), + ).thenAnswer((_) => Stream.value(UserProfile(id: 1, weightUnitStr: 'lb'))); + + final container = ProviderContainer.test( + overrides: [ + measurementRepositoryProvider.overrideWithValue(mockRepo), + userProfileRepositoryProvider.overrideWithValue(mockProfileRepo), + ], + ); + container.listen(userProfileProvider, (_, _) {}); + await tester.runAsync(pumpEventQueue); + + await tester.pumpWidget( + UncontrolledProviderScope( + container: container, + child: MaterialApp( + localizationsDelegates: AppLocalizations.localizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: Scaffold( + body: SingleChildScrollView( + child: WeightForm(getBodyWeightCategory(const [])), + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); + + // 365 lb are over the numeric kg bound but a perfectly valid weight + await tester.enterText(find.byKey(const Key('weightInput')), '365'); + await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); + await tester.pumpAndSettle(); + + final saved = verify(mockRepo.addLocalDrift(captureAny)).captured.single as MeasurementEntry; + expect(saved.value, 365); + expect(saved.extraData, {'unit': 'lb'}); + }); + + testWidgets('Rejects implausible weights with bounds in the entered unit', ( + WidgetTester tester, + ) async { + await tester.pumpWidget(createWeightForm(entry: testWeightEntryLb)); + await tester.pumpAndSettle(); + + // 35 lb are below the plausible minimum of 30 kg + await tester.enterText(find.byKey(const Key('weightInput')), '35'); + await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); + await tester.pumpAndSettle(); + + verifyNever(mockRepo.updateLocalDrift(any)); + // Bounds are rounded inwards, so the displayed minimum is acceptable + expect(find.text('Please enter a value between 67 and 661'), findsOneWidget); + }); + testWidgets('Editing shows the stored value in its stored unit, unconverted', ( WidgetTester tester, ) async {