Files
workouts/Workouts Watch AppTests/WatchConnectivityBridgeTests.swift
T
rzen 1b399ee7ba Add a watchOS test target
New Workouts Watch AppTests bundle wired into the watch scheme. Extracts
the phone-to-watch cache apply/prune into a pure, session-free
WatchCacheApplier seam and makes the HR-zone bucketing a nonisolated
static, so both can be unit-tested off the main actor without a live
WatchConnectivity session.

Claude-Session: https://claude.ai/code/session_01HJDQQDA9QdP8zByg43H5v3
2026-07-08 07:57:24 -04:00

89 lines
4.2 KiB
Swift

import Foundation
import SwiftData
import Testing
@testable import Workouts_Watch_App
/// Exercises the real `WatchConnectivityBridge` — constructed against an in-memory cache with no
/// live `WCSession` (its `init` never touches WatchConnectivity; only `activate()` does, which we
/// don't call). This pins the two things reachable without a session:
/// • the optimistic local update: `update(workout:)` writes straight to the read-through cache
/// (the forward-to-phone step no-ops with no session), so the UI sees the edit immediately; and
/// • the fresh-bridge state contract (no sync date, no edit lock, no follower frame).
///
/// It deliberately does not exercise updatedAt/version ordering on the apply path.
@MainActor
struct WatchConnectivityBridgeTests {
private static let ts = Date(timeIntervalSince1970: 1_700_000_000)
private func makeContainer() throws -> ModelContainer {
let schema = Schema([Split.self, Exercise.self, Workout.self, WorkoutLog.self])
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true, cloudKitDatabase: .none)
return try ModelContainer(for: schema, configurations: [config])
}
private func workout(id: String, name: String, splitName: String) -> WorkoutDocument {
WorkoutDocument(
schemaVersion: WorkoutDocument.currentSchemaVersion, id: id, splitID: "S", splitName: splitName,
start: Self.ts, end: nil, status: WorkoutStatus.inProgress.rawValue,
createdAt: Self.ts, updatedAt: Self.ts,
logs: [WorkoutLogDocument(id: "L-\(id)", exerciseName: name, order: 0, sets: 4,
reps: 10, weight: 135, loadType: LoadType.weight.rawValue,
durationSeconds: 0, currentStateIndex: 0,
status: WorkoutStatus.notStarted.rawValue, notes: nil, date: Self.ts)],
metrics: nil
)
}
@Test func updateWritesWorkoutOptimisticallyIntoTheCache() throws {
let container = try makeContainer()
let bridge = WatchConnectivityBridge(container: container)
let doc = workout(id: "01WKOPT", name: "Bench Press", splitName: "Push")
#expect(CacheMapper.fetchWorkout(id: doc.id, in: container.mainContext) == nil)
bridge.update(workout: doc)
let fetched = try #require(CacheMapper.fetchWorkout(id: doc.id, in: container.mainContext))
#expect(fetched.id == "01WKOPT")
#expect(fetched.splitName == "Push")
#expect(fetched.logsArray.first?.exerciseName == "Bench Press")
}
@Test func updateAppliesAnEditInPlaceOnASecondCall() throws {
let container = try makeContainer()
let bridge = WatchConnectivityBridge(container: container)
bridge.update(workout: workout(id: "01WKEDIT", name: "Bench Press", splitName: "Push"))
var edited = workout(id: "01WKEDIT", name: "Incline Press", splitName: "Push Day")
edited.status = WorkoutStatus.completed.rawValue
bridge.update(workout: edited)
let all = try container.mainContext.fetch(FetchDescriptor<Workout>())
#expect(all.count == 1) // updated in place, not duplicated
let fetched = try #require(CacheMapper.fetchWorkout(id: "01WKEDIT", in: container.mainContext))
#expect(fetched.splitName == "Push Day")
#expect(fetched.status == .completed)
#expect(fetched.logsArray.first?.exerciseName == "Incline Press")
}
@Test func freshBridgeHasNoSyncDateEditLockOrFollowerFrame() throws {
let bridge = try WatchConnectivityBridge(container: makeContainer())
#expect(bridge.lastSyncDate == nil)
#expect(bridge.editingWorkoutID == nil)
#expect(bridge.editingSplitID == nil)
#expect(bridge.liveIncoming == nil)
#expect(bridge.presentable == nil)
#expect(bridge.navigatedRunID == nil)
}
/// `muteLive()` with no incoming frame is a harmless no-op — `presentable` stays nil rather
/// than trapping on the optional it reads.
@Test func muteLiveWithoutIncomingFrameIsHarmless() throws {
let bridge = try WatchConnectivityBridge(container: makeContainer())
bridge.muteLive()
#expect(bridge.presentable == nil)
}
}