diff --git a/Sources/BetterFit/Features/EquipmentSwap/EquipmentSwapManager.swift b/Sources/BetterFit/Features/EquipmentSwap/EquipmentSwapManager.swift index fd45cda..a74d4f5 100644 --- a/Sources/BetterFit/Features/EquipmentSwap/EquipmentSwapManager.swift +++ b/Sources/BetterFit/Features/EquipmentSwap/EquipmentSwapManager.swift @@ -94,7 +94,25 @@ public class EquipmentSwapManager { return suggestions } - /// Apply equipment swap to workout + /// Replace only the exercise in the entry identified by `WorkoutExercise.id`. + /// Preserves the entry's ID, sets and notes and all other workout data. + /// Returns false without changing the workout when the entry is missing. + /// The caller selects the replacement; equipment and muscle matching are not validated here. + public func applySwap( + workout: inout Workout, + workoutExerciseId: UUID, + newExercise: Exercise + ) -> Bool { + guard let index = workout.exercises.firstIndex(where: { $0.id == workoutExerciseId }) else { + return false + } + + workout.exercises[index].exercise = newExercise + return true + } + + /// Replace the exercise in the first entry matching `Exercise.id`. + /// Use `applySwap(workout:workoutExerciseId:newExercise:)` to target a specific repeated entry. public func applySwap( workout: inout Workout, originalExerciseId: UUID, diff --git a/Tests/BetterFitTests/WorkoutEntrySwapTests.swift b/Tests/BetterFitTests/WorkoutEntrySwapTests.swift new file mode 100644 index 0000000..59c5bbd --- /dev/null +++ b/Tests/BetterFitTests/WorkoutEntrySwapTests.swift @@ -0,0 +1,110 @@ +import Foundation +import Testing +@testable import BetterFit + +struct WorkoutEntrySwapTests { + private let press = Exercise( + name: "Barbell Bench Press", equipmentRequired: .barbell, muscleGroups: [.chest, .triceps]) + private let dumbbellPress = Exercise( + name: "Dumbbell Bench Press", equipmentRequired: .dumbbell, + muscleGroups: [.chest, .triceps], imageURL: "dumbbell-press.png") + + // MARK: - Entry Selection + + @Test func replacesOnlySelectedRepeatedEntryAndPreservesWorkoutData() throws { + let manager = EquipmentSwapManager(availableEquipment: [.dumbbell]) + let timestamp = Date(timeIntervalSince1970: 1_700_000_000) + let warmUp = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 12)], notes: "Warm up") + let working = WorkoutExercise(exercise: press, sets: [ + ExerciseSet(reps: 8, weight: 20, isCompleted: true, timestamp: timestamp, autoTracked: true), + ExerciseSet(reps: 6, weight: 22) + ], notes: "Controlled tempo") + var workout = Workout( + name: "Push", exercises: [warmUp, working], date: timestamp, + duration: 600, isCompleted: true, templateId: UUID()) + let snapshot = workout + let replacement = try #require(manager.findAlternatives(for: working.exercise, from: [dumbbellPress]).first) + + #expect(manager.applySwap(workout: &workout, workoutExerciseId: working.id, newExercise: replacement)) + + var expected = snapshot + expected.exercises[1] = WorkoutExercise( + id: working.id, exercise: dumbbellPress, sets: working.sets, notes: working.notes) + #expect(workout == expected) + #expect(snapshot.exercises == [warmUp, working]) + } + + @Test func selectsEntryByIdentityAfterReordering() { + let manager = EquipmentSwapManager() + let first = WorkoutExercise(exercise: press) + let second = WorkoutExercise(exercise: press) + var workout = Workout(name: "Push", exercises: [first, second]) + workout.exercises.reverse() + + #expect(manager.applySwap(workout: &workout, workoutExerciseId: first.id, newExercise: dumbbellPress)) + + #expect(workout.exercises[0] == second) + #expect(workout.exercises[1].id == first.id) + #expect(workout.exercises[1].exercise == dumbbellPress) + } + + @Test func keepsEntryAddressableAfterAReplacement() { + let manager = EquipmentSwapManager() + let entry = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 8)], notes: "Working sets") + var workout = Workout(name: "Push", exercises: [entry]) + let snapshot = workout + + #expect(manager.applySwap(workout: &workout, workoutExerciseId: entry.id, newExercise: dumbbellPress)) + #expect(manager.applySwap(workout: &workout, workoutExerciseId: entry.id, newExercise: press)) + + #expect(workout == snapshot) + } + + // MARK: - Missing Entries and Compatibility + + @Test(arguments: [false, true]) + func leavesWorkoutUnchangedWhenEntryIsMissing(empty: Bool) { + let manager = EquipmentSwapManager() + var workout = Workout(name: "Push", exercises: empty ? [] : [WorkoutExercise(exercise: press)]) + let snapshot = workout + + #expect(!manager.applySwap(workout: &workout, workoutExerciseId: UUID(), newExercise: dumbbellPress)) + + #expect(workout == snapshot) + } + + @Test func doesNotUseExerciseDefinitionIdAsWorkoutEntryId() { + let manager = EquipmentSwapManager() + var workout = Workout(name: "Push", exercises: [WorkoutExercise(exercise: press)]) + let snapshot = workout + + #expect(!manager.applySwap(workout: &workout, workoutExerciseId: press.id, newExercise: dumbbellPress)) + + #expect(workout == snapshot) + } + + @Test func legacyOverloadStillReplacesFirstMatchingExercise() { + let manager = EquipmentSwapManager() + let first = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 12)], notes: "Warm up") + let second = WorkoutExercise(exercise: press, sets: [ExerciseSet(reps: 6)], notes: "Working sets") + var workout = Workout(name: "Push", exercises: [first, second]) + let snapshot = workout + + #expect(manager.applySwap(workout: &workout, originalExerciseId: press.id, newExercise: dumbbellPress)) + + var expected = snapshot + expected.exercises[0] = WorkoutExercise( + id: first.id, exercise: dumbbellPress, sets: first.sets, notes: first.notes) + #expect(workout == expected) + } + + @Test func legacyOverloadLeavesWorkoutUnchangedWhenExerciseIsMissing() { + let manager = EquipmentSwapManager() + var workout = Workout(name: "Push", exercises: [WorkoutExercise(exercise: press)]) + let snapshot = workout + + #expect(!manager.applySwap(workout: &workout, originalExerciseId: UUID(), newExercise: dumbbellPress)) + + #expect(workout == snapshot) + } +} diff --git a/docs/api.md b/docs/api.md index d23e2bb..4d195bd 100644 --- a/docs/api.md +++ b/docs/api.md @@ -413,6 +413,7 @@ public class EquipmentSwapManager - `suggestSwaps(for:) -> [(original: Exercise, alternatives: [Exercise])]` - `suggestSwaps(for:from:) -> [(original: Exercise, alternatives: [Exercise])]` - Select catalogue alternatives for a workout - `applySwap(workout:originalExerciseId:newExercise:) -> Bool` +- `applySwap(workout:workoutExerciseId:newExercise:) -> Bool` - Replace a specific workout entry `findAlternatives(for:from:)` returns catalogue exercises only when the original equipment is unavailable. Candidates must use available equipment listed by @@ -433,6 +434,23 @@ metadata. Entries with available equipment or no eligible alternative are omitte Repeated workout entries remain separate suggestions. Neither the workout nor its sets are changed until the caller applies a selected swap with `applySwap`. +Use `applySwap(workout:workoutExerciseId:newExercise:)` with a `WorkoutExercise.id` +to replace one occurrence of a repeated exercise. It changes only that entry's +exercise, preserving its ID, sets and notes and all other workout data. It returns +false without mutation if the entry is missing. The existing `originalExerciseId` +overload continues to replace the first entry matching an `Exercise.id`. + +```swift +if let entry = workout.exercises.last, + let replacement = swaps.findAlternatives(for: entry.exercise, from: exerciseCatalogue).first { + swaps.applySwap(workout: &workout, workoutExerciseId: entry.id, newExercise: replacement) +} +``` + +Both application methods use the caller's selected exercise without validating +equipment availability or muscle matching. Choose an eligible replacement before +applying it; sets and loads are preserved without adjustment. + Muscle matching does not establish movement equivalence. Supply a catalogue appropriate for the workout. This opt-in lookup does not change the generated alternatives returned by `findAlternatives(for:)` or `suggestSwaps(for:)`.