Keep no Disconnect flag when Disconnect fails
Fable's nit 4: the flag was set before the POST and kept whatever it answered, so an offline or refused Disconnect left Ask ready but held renewal off for good. A Disconnect that fails now puts the flag back as it was (while the account is still the one).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+27
−1
native/apple/Sources/BaseKit/Session/BaseSession.swift+10 −1native/apple/Tests/BaseKitTests/SessionTests.swift+17 −0
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift| index 92d3162..8a88761 100644 |
| --- a/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| +++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| @@ -227,12 +227,21 @@ import RastrilloApple |
| /// set-up running, and keeps one from running again by itself. |
| public func disconnectAsk() async { |
| guard let client else { return } |
| + let before = askDisconnected |
| askDisconnected = true |
| settingUp?.cancel() |
| settingUp = nil |
| askSetup?.invalidate() |
| let epoch = generation |
| - try? await client.disconnectAsk() |
| + do { |
| + try await client.disconnectAsk() |
| + } catch { |
| + // Nothing was disconnected (offline, a 502): as Base says, the |
| + // flag stays as it was, and renewal goes on. |
| + guard generation == epoch else { return } |
| + askDisconnected = before |
| + return |
| + } |
| guard generation == epoch else { return } |
| await ask?.load() |
| } |
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift| index b62a29d..e0e8199 100644 |
| --- a/native/apple/Tests/BaseKitTests/SessionTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/SessionTests.swift |
| @@ -317,3 +317,20 @@ extension SessionTests { |
| phone.end() |
| } |
| } |
| + |
| +extension SessionTests { |
| + /// Fable's nit 4: a Disconnect Base didn't do leaves no flag behind. |
| + func testAFailedDisconnectKeepsNoFlag() async throws { |
| + let session = BaseSession(oficina: memoryOficina(), defaults: freshDefaults()) |
| + let t = gated("alice", "Alice's", "Hello") |
| + t.answer("/ask/state", #"{"state":"ready","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":7}"#) |
| + t.answer("/ask/disconnect", status: 502, #"{"state":"unavailable"}"#) |
| + for path in ["/api/v1/me", "/api/v1/home", "/ask/state", "/ask/disconnect"] { t.release(path) } |
| + session.begin(identity: "id-alice", origin: URL(string: "https://alice.example")!, transport: t) |
| + await session.refreshing?.value |
| + await session.disconnectAsk() |
| + XCTAssertFalse(session.askDisconnected) |
| + XCTAssertEqual(session.ask?.phase, .ready) |
| + session.end() |
| + } |
| +} |