From 87828d9ef735a1ee9399cac7b2da72b88246b893 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Sat, 8 Aug 2026 21:28:43 -0700 Subject: [PATCH] fix(swift-ios): close remaining review gaps Use an explicit cancellation receipt instead of a long blocking sleep in the aggregate refresh test. Treat a disconnected snapshot as a failed onboarding connection so activation and pairing cannot advance to the success screen while the selected computer is unreachable. --- .../Features/Root/FeatureRootModel.swift | 18 ++++++- .../FeatureTests/FeatureRootModelTests.swift | 48 +++++++++++++++++++ .../NativeMultiEnvironmentTests.swift | 35 +++++++++++++- 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/apps/swift-ios/Features/Root/FeatureRootModel.swift b/apps/swift-ios/Features/Root/FeatureRootModel.swift index 8ca539095f..dc854bc9a4 100644 --- a/apps/swift-ios/Features/Root/FeatureRootModel.swift +++ b/apps/swift-ios/Features/Root/FeatureRootModel.swift @@ -1,6 +1,12 @@ import Foundation import Observation +private struct FeatureConnectionUnavailableError: LocalizedError { + var errorDescription: String? { + "Could not connect to the selected computer." + } +} + enum FeatureDetailRenderChange: Equatable { case full case delta(FeatureDetailDelta) @@ -101,8 +107,12 @@ public final class FeatureRootModel { public func pair(endpoint: String, token: String?) async -> Bool { await perform { try await client.pair(endpoint: endpoint, token: token) + let next = try await client.initialSnapshot() clearDetails() - install(try await client.initialSnapshot()) + install(next) + guard next.connection.state != .disconnected else { + throw FeatureConnectionUnavailableError() + } } } @@ -110,8 +120,12 @@ public final class FeatureRootModel { public func activateEnvironment(_ id: String) async -> Bool { await perform { try await client.activateEnvironment(id: id) - install(try await client.initialSnapshot()) + let next = try await client.initialSnapshot() clearDetails() + install(next) + guard next.connection.state != .disconnected else { + throw FeatureConnectionUnavailableError() + } } } diff --git a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift index 47560f325b..7e19fb9a68 100644 --- a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift @@ -250,6 +250,54 @@ struct FeatureRootModelTests { #expect(model.snapshot.environments.first(where: { $0.id == "target" })?.isActive == false) } + @Test + func disconnectedActivationDoesNotReportConnectionSuccess() async { + let client = FeatureClientStub() + client.snapshot = FeatureSnapshot( + connection: .init( + state: .disconnected, + environmentName: "Target studio", + endpoint: "https://target.example" + ), + environments: [ + .init( + id: "target", + name: "Target studio", + endpoint: "https://target.example", + isActive: true, + connectionState: .disconnected + ), + ] + ) + let model = testRootModel(client: client) + + let activated = await model.activateEnvironment("target") + + #expect(!activated) + #expect(client.activatedEnvironmentID == "target") + #expect(model.snapshot.connection.state == .disconnected) + #expect(model.errorMessage?.contains("Could not connect") == true) + } + + @Test + func disconnectedPairDoesNotReportConnectionSuccess() async { + let client = FeatureClientStub() + client.snapshotAfterPair = FeatureSnapshot( + connection: .init( + state: .disconnected, + environmentName: "New studio", + endpoint: "https://new.example" + ) + ) + let model = testRootModel(client: client) + + let paired = await model.pair(endpoint: "https://new.example", token: "pair-token") + + #expect(!paired) + #expect(model.snapshot.connection.state == .disconnected) + #expect(model.errorMessage?.contains("Could not connect") == true) + } + @Test func testCreateThreadOptimisticallyUpsertsIt() async { let client = FeatureClientStub() diff --git a/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift b/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift index 8bb387ee89..9a7be78c50 100644 --- a/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/NativeMultiEnvironmentTests.swift @@ -251,6 +251,7 @@ final class NativeMultiEnvironmentTests: XCTestCase { _ = try await fixture.client.initialSnapshot() + await loader.waitForFirstLoadCancellation() await loader.waitForCallCount(2) let restartedCallCount = await loader.callCount XCTAssertGreaterThanOrEqual(restartedCallCount, 2) @@ -546,12 +547,26 @@ private actor BlockingFirstAggregateEnvironmentLoader { target: Int, continuation: CheckedContinuation )] = [] + private var firstLoadContinuation: CheckedContinuation? + private var firstLoadCancellationObserved = false + private var firstLoadCancellationWaiters: [CheckedContinuation] = [] func load(from runtime: EnvironmentRuntime) async throws -> [Environment] { callCount += 1 resumeSatisfiedWaiters() if callCount == 1 { - try await Task.sleep(for: .seconds(60)) + await withTaskCancellationHandler { + await withCheckedContinuation { continuation in + firstLoadContinuation = continuation + if Task.isCancelled { + firstLoadContinuation = nil + continuation.resume() + } + } + } onCancel: { + Task { await self.recordFirstLoadCancellation() } + } + try Task.checkCancellation() } return try await runtime.environments() } @@ -563,6 +578,24 @@ private actor BlockingFirstAggregateEnvironmentLoader { } } + func waitForFirstLoadCancellation() async { + guard !firstLoadCancellationObserved else { return } + await withCheckedContinuation { continuation in + firstLoadCancellationWaiters.append(continuation) + } + } + + private func recordFirstLoadCancellation() { + firstLoadCancellationObserved = true + firstLoadContinuation?.resume() + firstLoadContinuation = nil + let waiters = firstLoadCancellationWaiters + firstLoadCancellationWaiters.removeAll() + for waiter in waiters { + waiter.resume() + } + } + private func resumeSatisfiedWaiters() { let satisfied = callCountWaiters.filter { callCount >= $0.target } callCountWaiters.removeAll { callCount >= $0.target }