diff --git a/apps/swift-ios/App/Platform/PlatformRootView.swift b/apps/swift-ios/App/Platform/PlatformRootView.swift index 163046a4bc..23a3d6e22b 100644 --- a/apps/swift-ios/App/Platform/PlatformRootView.swift +++ b/apps/swift-ios/App/Platform/PlatformRootView.swift @@ -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 diff --git a/apps/swift-ios/Features/Root/FeatureRootModel.swift b/apps/swift-ios/Features/Root/FeatureRootModel.swift index dc854bc9a4..877d7cd480 100644 --- a/apps/swift-ios/Features/Root/FeatureRootModel.swift +++ b/apps/swift-ios/Features/Root/FeatureRootModel.swift @@ -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 } } diff --git a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift index 7e19fb9a68..3ab611bfa8 100644 --- a/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift +++ b/apps/swift-ios/Tests/FeatureTests/FeatureRootModelTests.swift @@ -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.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 }