fix(swift-ios): complete activation recovery
Treat a connected recovered target as a successful activation, preserve cached thread details when the active environment did not change, and require the requested environment to be active before reporting success.
This commit is contained in:
2 files changed
+76
-5
No files matched your search
@@ -120,14 +120,14 @@ public final class FeatureRootModel {
|
||||
public func activateEnvironment(_ id: String) async -> Bool {
|
||||
isPerformingAction = true
|
||||
defer { isPerformingAction = false }
|
||||
let previousActiveID = snapshot.environments.first(where: \.isActive)?.id
|
||||
var installedActivationSnapshot = false
|
||||
do {
|
||||
try await client.activateEnvironment(id: id)
|
||||
let next = try await client.initialSnapshot()
|
||||
clearDetails()
|
||||
install(next)
|
||||
installActivationSnapshot(next, previousActiveID: previousActiveID)
|
||||
installedActivationSnapshot = true
|
||||
guard next.connection.state != .disconnected else {
|
||||
guard Self.isUsableActivation(next, targetID: id) else {
|
||||
throw FeatureConnectionUnavailableError()
|
||||
}
|
||||
return true
|
||||
@@ -137,8 +137,11 @@ public final class FeatureRootModel {
|
||||
// environment while the client already targets the new one.
|
||||
if !installedActivationSnapshot,
|
||||
let recovered = try? await client.initialSnapshot() {
|
||||
clearDetails()
|
||||
install(recovered)
|
||||
installActivationSnapshot(recovered, previousActiveID: previousActiveID)
|
||||
if Self.isUsableActivation(recovered, targetID: id) {
|
||||
errorMessage = nil
|
||||
return true
|
||||
}
|
||||
}
|
||||
if !Self.isBenignCancellation(error) {
|
||||
errorMessage = error.localizedDescription
|
||||
@@ -147,6 +150,25 @@ public final class FeatureRootModel {
|
||||
}
|
||||
}
|
||||
|
||||
private func installActivationSnapshot(
|
||||
_ next: FeatureSnapshot,
|
||||
previousActiveID: String?
|
||||
) {
|
||||
let nextActiveID = next.environments.first(where: \.isActive)?.id
|
||||
if nextActiveID != previousActiveID {
|
||||
clearDetails()
|
||||
}
|
||||
install(next)
|
||||
}
|
||||
|
||||
private static func isUsableActivation(
|
||||
_ next: FeatureSnapshot,
|
||||
targetID: String
|
||||
) -> Bool {
|
||||
next.connection.state != .disconnected
|
||||
&& next.environments.contains { $0.id == targetID && $0.isActive }
|
||||
}
|
||||
|
||||
public func removeEnvironment(_ id: String) async {
|
||||
await stopOutboxDrain()
|
||||
await perform {
|
||||
|
||||
@@ -238,8 +238,13 @@ struct FeatureRootModelTests {
|
||||
]
|
||||
)
|
||||
client.activateEnvironmentError = FeatureCapabilityUnavailable("Activation")
|
||||
client.threadDetail = FeatureThreadDetail(
|
||||
thread: FeatureThread(id: "old-thread", projectID: "old-project", title: "Old"),
|
||||
messages: [FeatureMessage(id: "message", role: .assistant, text: "Keep me")]
|
||||
)
|
||||
let model = testRootModel(client: client)
|
||||
await model.reload()
|
||||
_ = await model.detail(for: "old-thread")
|
||||
|
||||
let activated = await model.activateEnvironment("target")
|
||||
|
||||
@@ -248,6 +253,7 @@ struct FeatureRootModelTests {
|
||||
#expect(model.snapshot.connection.state == .connected)
|
||||
#expect(model.snapshot.environments.first(where: { $0.id == "old" })?.isActive == true)
|
||||
#expect(model.snapshot.environments.first(where: { $0.id == "target" })?.isActive == false)
|
||||
#expect(model.details["old-thread"] != nil)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -293,6 +299,49 @@ struct FeatureRootModelTests {
|
||||
#expect(model.snapshot.environments.first?.isActive == true)
|
||||
}
|
||||
|
||||
@Test
|
||||
func connectedRecoveryCompletesPartialActivation() 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: .connected,
|
||||
environmentName: "Target studio",
|
||||
endpoint: "https://target.example"
|
||||
),
|
||||
environments: [
|
||||
.init(
|
||||
id: "target",
|
||||
name: "Target studio",
|
||||
endpoint: "https://target.example",
|
||||
isActive: true,
|
||||
connectionState: .connected
|
||||
),
|
||||
]
|
||||
)
|
||||
client.activateEnvironmentError = FeatureCapabilityUnavailable("Activation refresh")
|
||||
let model = testRootModel(client: client)
|
||||
await model.reload()
|
||||
|
||||
let activated = await model.activateEnvironment("target")
|
||||
|
||||
#expect(activated)
|
||||
#expect(model.errorMessage == nil)
|
||||
#expect(model.snapshot.connection.state == .connected)
|
||||
#expect(model.snapshot.environments.first?.id == "target")
|
||||
}
|
||||
|
||||
@Test
|
||||
func disconnectedActivationDoesNotReportConnectionSuccess() async {
|
||||
let client = FeatureClientStub()
|
||||
|
||||
Reference in new issue
Block a user