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.
This commit is contained in:
Theo Browne committed 2026-08-09 20:55:49 -07:00
1 parent e94f59baa4
commit 87828d9ef7
3 files changed
+98 -3

No files matched your search

@@ -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()
}
}
}
@@ -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()
@@ -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<Void, Never>
)] = []
private var firstLoadContinuation: CheckedContinuation<Void, Never>?
private var firstLoadCancellationObserved = false
private var firstLoadCancellationWaiters: [CheckedContinuation<Void, Never>] = []
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 }