Forget Disconnect when the identity leaves the device
Fable's re-review finding 1: only Base's own Sign out cleared the person's Disconnect, and only for the team in front of them. Signed out from another Oficina app (or by Home ending the grant), or with Disconnect kept in another team, the next fresh sign-in stayed held off and Ask wasn't set up. Now, whenever the kit's step leaves a session and the kit no longer holds that identity, every team's Disconnect for it goes; a team switch or a relaunch, where the identity stays, keeps them. Regressions: a sign-out past Base's own, a sign-out after Disconnect in two teams (with the switch keeping the first), and an ended session keeping its flag.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+73
−3
native/apple/Sources/BaseKit/Session/BaseSession.swift+17 −3native/apple/Tests/BaseKitTests/SessionTests.swift+56 −0
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift| index 9479745..d56beef 100644 |
| --- a/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| +++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| @@ -57,9 +57,13 @@ import RastrilloApple |
| // Loading is the kit reading its store, not a sign-out. |
| if step == .loading { return } |
| guard case .signedIn(let state) = step else { |
| - if account != nil { end() } |
| + if let left = account { |
| + end() |
| + forgetDisconnectIfGone(left.identity) |
| + } |
| return |
| } |
| + if let previous = account, previous.identity != state.identity.id { forgetDisconnectIfGone(previous.identity) } |
| begin(identity: state.identity.id, origin: state.origin, transport: state.client(), state: state) |
| } |
| @@ -200,6 +204,16 @@ import RastrilloApple |
| } |
| static func disconnectedKey(_ account: Account) -> String { "askDisconnected:\(account.identity)@\(account.origin.absoluteString)" } |
| + /// The identity has left this device (a sign-out here, in another |
| + /// Oficina app, or Home ending its grant): its Disconnect goes for every |
| + /// team, so the next sign-in is a fresh one. A team switch or a |
| + /// relaunch keeps it: the identity is still here. |
| + func forgetDisconnectIfGone(_ identity: String) { |
| + guard (try? oficina.store.hasIdentity(identity)) != true else { return } |
| + let prefix = "askDisconnected:\(identity)@" |
| + for key in defaults.dictionaryRepresentation().keys where key.hasPrefix(prefix) { defaults.removeObject(forKey: key) } |
| + } |
| + |
| /// Settings' Disconnect for Ask, then Ask as it is now. It stops any |
| /// set-up running, and keeps one from running again by itself. |
| public func disconnectAsk() async { |
| @@ -233,8 +247,8 @@ import RastrilloApple |
| /// Sign out (confirmed by the caller): the kit's sign-out of this |
| /// Oficina ID, everywhere on this device. |
| public func signOut() { |
| - // The next sign-in is a fresh one: Ask may set itself up again. |
| - askDisconnected = false |
| + // The kit's step change clears Disconnect for every team of the |
| + // identity (forgetDisconnectIfGone), whoever signed it out. |
| oficina.signOut() |
| } |
| } |
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift| index 2525e28..3683113 100644 |
| --- a/native/apple/Tests/BaseKitTests/SessionTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/SessionTests.swift |
| @@ -234,3 +234,59 @@ extension SessionTests { |
| session.end() |
| } |
| } |
| + |
| +extension SessionTests { |
| + func disconnectKeys(_ defaults: UserDefaults) -> [String] { |
| + defaults.dictionaryRepresentation().keys.filter { $0.hasPrefix("askDisconnected:") && defaults.bool(forKey: $0) }.sorted() |
| + } |
| + |
| + /// Fable's finding 1, scenario A: Disconnect, then the Oficina ID is |
| + /// signed out somewhere other than Base's own Sign out (another app; |
| + /// here the kit's own sign-out, past BaseSession): the flag goes, so |
| + /// the next sign-in sets Ask up. |
| + func testASignOutFromElsewhereClearsDisconnect() async throws { |
| + let (oficina, _) = try await signedInOficina() |
| + let defaults = freshDefaults() |
| + let session = BaseSession(oficina: oficina, defaults: defaults) |
| + try await waitUntil { session.signedIn != nil } |
| + session.askDisconnected = true |
| + XCTAssertEqual(disconnectKeys(defaults).count, 1) |
| + oficina.signOut() |
| + try await waitUntil { session.signedIn == nil } |
| + XCTAssertEqual(disconnectKeys(defaults), [], "signed out elsewhere: Disconnect doesn't outlive the identity") |
| + } |
| + |
| + /// Scenario B: Disconnect in one team, switch (the flag stays, it's |
| + /// per team), Disconnect in the other, sign out: both go. |
| + func testSigningOutClearsDisconnectForEveryTeam() async throws { |
| + let (oficina, _) = try await signedInOficina() |
| + let defaults = freshDefaults() |
| + let session = BaseSession(oficina: oficina, defaults: defaults) |
| + try await waitUntil { session.signedIn != nil } |
| + session.askDisconnected = true |
| + let teams = await session.teams() |
| + let other = try XCTUnwrap(teams.first { $0.sqid != session.signedIn?.team.sqid }) |
| + await session.switchTo(other) |
| + try await waitUntil { session.signedIn?.team.sqid == other.sqid } |
| + XCTAssertEqual(disconnectKeys(defaults).count, 1, "a team switch keeps the first team's Disconnect") |
| + XCTAssertFalse(session.askDisconnected, "and the other team has its own") |
| + session.askDisconnected = true |
| + XCTAssertEqual(disconnectKeys(defaults).count, 2) |
| + session.signOut() |
| + try await waitUntil { session.signedIn == nil } |
| + XCTAssertEqual(disconnectKeys(defaults), [], "every team of the identity is cleared") |
| + } |
| + |
| + /// Ending a session without the identity leaving (a team switch, a |
| + /// relaunch) keeps Disconnect. |
| + func testEndingWithoutSigningOutKeepsDisconnect() async throws { |
| + let defaults = freshDefaults() |
| + let session = BaseSession(oficina: memoryOficina(), defaults: defaults) |
| + session.begin(identity: "id-alice", origin: URL(string: "https://alice.example")!, transport: GatedTransport()) |
| + session.askDisconnected = true |
| + session.end() |
| + session.begin(identity: "id-alice", origin: URL(string: "https://alice.example")!, transport: GatedTransport()) |
| + XCTAssertTrue(session.askDisconnected) |
| + session.end() |
| + } |
| +} |