Properly load and set the unit from the profile

This commit is contained in:
Roland Geider
2026-06-19 15:17:59 +02:00
parent 01873ecebe
commit 8fe4340949
4 changed files with 93 additions and 5 deletions

View File

@@ -93,7 +93,9 @@ class Log {
weight = setConfig.weight;
weightTarget = setConfig.weight;
weightUnitId = setConfig.weightUnitId ?? WEIGHT_UNIT_KG;
// Fall back to the resolved unit object (set during routine hydration from
// the user's metric preference) when the config carries no explicit unit.
weightUnitId = setConfig.weightUnitId ?? setConfig.weightUnit?.id ?? WEIGHT_UNIT_KG;
weightUnitObj = setConfig.weightUnit;
repetitions = setConfig.repetitions;

View File

@@ -32,7 +32,7 @@ import 'package:wger/providers/workout_session_repository.dart';
import 'routine_form_test_overrides.mocks.dart';
export 'routine_form_test_overrides.mocks.dart'
show MockExerciseRepository, MockWorkoutSessionRepository;
show MockExerciseRepository, MockWorkoutSessionRepository, MockUserProfileRepository;
@GenerateMocks([ExerciseRepository, WorkoutSessionRepository, UserProfileRepository])
/// Repository overrides for the two reference-data notifiers that
@@ -47,6 +47,7 @@ export 'routine_form_test_overrides.mocks.dart'
List<Override> exerciseAndSessionRepoOverrides({
MockExerciseRepository? exercise,
MockWorkoutSessionRepository? session,
MockUserProfileRepository? userProfile,
}) {
final exerciseRepo = exercise ?? _emptyExerciseRepoMock();
final sessionRepo = session ?? _emptySessionRepoMock();
@@ -55,7 +56,9 @@ List<Override> exerciseAndSessionRepoOverrides({
workoutSessionRepositoryProvider.overrideWithValue(sessionRepo),
// RoutinesRiverpod.build listens to the user profile to pick the default
// weight unit; stub it so the real Drift-backed repo isn't pulled in.
userProfileRepositoryProvider.overrideWithValue(_emptyUserProfileRepoMock()),
// Defaults to a null (metric) profile; pass [userProfile] to exercise the
// imperial default-unit path.
userProfileRepositoryProvider.overrideWithValue(userProfile ?? _emptyUserProfileRepoMock()),
];
}
@@ -90,10 +93,15 @@ MockWorkoutSessionRepository _emptySessionRepoMock() {
List<Override> routineFormAmbientOverrides({
MockExerciseRepository? exercise,
MockWorkoutSessionRepository? session,
MockUserProfileRepository? userProfile,
List<RepetitionUnit> repetitionUnits = const [],
List<WeightUnit> weightUnits = const [],
}) => [
...exerciseAndSessionRepoOverrides(exercise: exercise, session: session),
...exerciseAndSessionRepoOverrides(
exercise: exercise,
session: session,
userProfile: userProfile,
),
routineRepetitionUnitProvider.overrideWith(
(ref) => Stream<List<RepetitionUnit>>.value(repetitionUnits),
),

View File

@@ -1,6 +1,6 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2020, 2025 wger Team
* Copyright (c) 2020 - 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
@@ -22,6 +22,7 @@ import 'package:wger/models/exercises/category.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/models/workouts/log.dart';
import 'package:wger/models/workouts/set_config_data.dart';
import 'package:wger/models/workouts/weight_unit.dart';
void main() {
group('Log.volume', () {
@@ -178,6 +179,24 @@ void main() {
expect(log.repetitionsUnitId, REP_UNIT_TILL_FAILURE_ID);
});
test('derives the unit ID from the resolved unit object when no explicit ID is set', () {
// Imperial users: routine hydration leaves weightUnitId null and only
// resolves the weightUnit object (lb) from the profile. The log must pick
// up lb for *both* the ID and the object
const lb = WeightUnit(id: WEIGHT_UNIT_LB, name: 'lb');
final setConfig = SetConfigData(
exerciseId: 1,
slotEntryId: 1,
exercise: exercise,
weightUnit: lb,
);
final log = Log.fromSetConfigData(setConfig);
expect(log.weightUnitId, WEIGHT_UNIT_LB);
expect(log.weightUnitObj, lb);
});
test('copies weight and repetitions values', () {
final setConfig = SetConfigData(
exerciseId: 1,

View File

@@ -22,15 +22,20 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:wger/models/exercises/exercise.dart';
import 'package:wger/models/user/user_profile.dart';
import 'package:wger/models/workouts/day.dart';
import 'package:wger/models/workouts/day_data.dart';
import 'package:wger/models/workouts/log.dart';
import 'package:wger/models/workouts/routine.dart';
import 'package:wger/models/workouts/session.dart';
import 'package:wger/models/workouts/set_config_data.dart';
import 'package:wger/models/workouts/slot.dart';
import 'package:wger/models/workouts/slot_data.dart';
import 'package:wger/models/workouts/slot_entry.dart';
import 'package:wger/providers/exercises_notifier.dart';
import 'package:wger/providers/routines_notifier.dart';
import 'package:wger/providers/routines_repository.dart';
import 'package:wger/providers/user_profile_notifier.dart';
import '../../test_data/exercises.dart';
import '../../test_data/routines.dart';
@@ -359,6 +364,60 @@ void main() {
// Assert: `_fullFetchInFlight` collapses them into a single roundtrip.
verify(mockRepo.fetchAndSetRoutineFullServer(101)).called(1);
});
test('resolves the default weight unit from an imperial profile', () async {
// A set config without an explicit weight unit must inherit the unit
// implied by the profile: lb (id 2) for an imperial user, not kg.
overrideStreams(mockExerciseRepo);
final setConfig = SetConfigData(
exerciseId: 1,
slotEntryId: 1,
exercise: getTestExercises()[0],
// No weightUnit / weightUnitId: the server omits it, the default applies.
);
final routine = Routine(id: 101, name: 'Test routine')
..dayDataGym = [
DayData(
iteration: 1,
date: DateTime(2024, 11, 1),
label: '',
day: Day(id: 1, routineId: 101, name: 'Test', order: 1),
slots: [
SlotData(comment: '', isSuperset: false, exerciseIds: [1], setConfigs: [setConfig]),
],
),
];
when(mockRepo.fetchAndSetRoutineFullServer(101)).thenAnswer((_) async => routine);
final imperialProfileRepo = MockUserProfileRepository();
when(
imperialProfileRepo.watchDrift(),
).thenAnswer((_) => Stream.value(UserProfile(id: 1, weightUnitStr: 'lb')));
final container = ProviderContainer.test(
overrides: [
routinesRepositoryProvider.overrideWithValue(mockRepo),
...routineFormAmbientOverrides(
exercise: mockExerciseRepo,
session: mockSessionRepo,
userProfile: imperialProfileRepo,
repetitionUnits: testRepetitionUnits,
weightUnits: testWeightUnits,
),
],
);
// fetchAndSetRoutineFull doesn't await the profile, so keep it alive and
// let its stream emit before hydration reads it (post-sync steady state).
container.listen(userProfileProvider, (_, _) {});
await pumpEventQueue();
final notifier = container.read(routinesRiverpodProvider.notifier);
final result = await notifier.fetchAndSetRoutineFull(101);
// testWeightUnit2 has id 2 == WEIGHT_UNIT_LB.
final hydratedConfig = result.dayDataGym[0].slots[0].setConfigs[0];
expect(hydratedConfig.weightUnit, testWeightUnit2);
});
});
group('routineHydration family', () {