fix(swift-ios): keep activation state consistent

Reconcile the root snapshot after a partial environment activation failure, and make deep-link routing honor the activation result before navigating. This keeps the UI selection aligned with the runtime even when the target computer is unreachable.
This commit is contained in:
Theo Browne committed 2026-08-09 20:55:49 -07:00
1 parent 87828d9ef7
commit c08d206455
3 files changed
+67 -3

No files matched your search

@@ -304,8 +304,7 @@ struct PlatformRootView: View {
return false
}
guard !environment.isActive else { return true }
await model.activateEnvironment(id)
return model.snapshot.environments.contains { $0.id == id && $0.isActive }
return await model.activateEnvironment(id)
}
/// Home revisions are coalesced by FeatureRootModel, so this performs one
@@ -118,14 +118,32 @@ public final class FeatureRootModel {
@discardableResult
public func activateEnvironment(_ id: String) async -> Bool {
await perform {
isPerformingAction = true
defer { isPerformingAction = false }
var installedActivationSnapshot = false
do {
try await client.activateEnvironment(id: id)
let next = try await client.initialSnapshot()
clearDetails()
install(next)
installedActivationSnapshot = true
guard next.connection.state != .disconnected else {
throw FeatureConnectionUnavailableError()
}
return true
} catch {
// Activation can persist the new selection before its first refresh
// fails. Reconcile once so UI routing never keeps the previous
// environment while the client already targets the new one.
if !installedActivationSnapshot,
let recovered = try? await client.initialSnapshot() {
clearDetails()
install(recovered)
}
if !Self.isBenignCancellation(error) {
errorMessage = error.localizedDescription
}
return false
}
}
@@ -250,6 +250,49 @@ struct FeatureRootModelTests {
#expect(model.snapshot.environments.first(where: { $0.id == "target" })?.isActive == false)
}
@Test
func partialActivationFailureReconcilesTheRuntimeSelection() async {
let client = FeatureClientStub()
client.snapshot = FeatureSnapshot(
connection: .init(state: .connected),
environments: [
.init(
id: "old",
name: "Old studio",
endpoint: "https://old.example",
isActive: true,
connectionState: .connected
),
]
)
client.snapshotAfterActivation = 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
),
]
)
client.activateEnvironmentError = FeatureCapabilityUnavailable("Activation refresh")
let model = testRootModel(client: client)
await model.reload()
let activated = await model.activateEnvironment("target")
#expect(!activated)
#expect(model.snapshot.connection.state == .disconnected)
#expect(model.snapshot.environments.first?.id == "target")
#expect(model.snapshot.environments.first?.isActive == true)
}
@Test
func disconnectedActivationDoesNotReportConnectionSuccess() async {
let client = FeatureClientStub()
@@ -1236,6 +1279,7 @@ private final class FeatureClientStub: FeatureClient {
private let eventContinuation: AsyncStream<FeatureEvent>.Continuation
var snapshot = FeatureSnapshot()
var backgroundSnapshotValue: FeatureSnapshot?
var snapshotAfterActivation: FeatureSnapshot?
var initialSnapshotCallCount = 0
var backgroundSnapshotCallCount = 0
var snapshotAfterPair: FeatureSnapshot?
@@ -1292,6 +1336,9 @@ private final class FeatureClientStub: FeatureClient {
if pairEndpoint != nil, let snapshotAfterPair {
return snapshotAfterPair
}
if activatedEnvironmentID != nil, let snapshotAfterActivation {
return snapshotAfterActivation
}
return snapshot
}