Keep a set-up's Disconnect flag to its own account
Fable's second re-review, nit 1: the set-up's heldOff read the Ask of the account it was made for but wrote the flag of whichever account was signed in, before checking it was still that one. heldOff and onDisconnected now check the session first (a gone account's set-up is held off and writes nothing), and read and write the flag of the set-up's own account only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+35
−6
native/apple/Sources/BaseKit/Session/BaseSession.swift+14 −6native/apple/Tests/BaseKitTests/SessionTests.swift+21 −0
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift| index c2b8479..785bf8b 100644 |
| --- a/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| +++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| @@ -121,16 +121,22 @@ import RastrilloApple |
| return try await self.assertion(equipo) |
| }, fallbackEquipo: { [weak self] in self?.me?.equipoOrigin }) |
| setup.isCurrent = { [weak self] in self?.generation == epoch } |
| + // Both read and write the flag of the account this set-up belongs |
| + // to, and only while it is still the one signed in: a gone |
| + // account's set-up is held off and writes nothing. |
| setup.heldOff = { [weak self, weak ask] in |
| - guard let self else { return true } |
| + guard let self, self.generation == epoch else { return true } |
| // Base's word wins, and the device's cache follows it. |
| if let said = ask?.disconnected { |
| - if self.askDisconnected != said { self.askDisconnected = said } |
| + if self.disconnected(next) != said { self.setDisconnected(said, for: next) } |
| return said |
| } |
| - return self.askDisconnected |
| + return self.disconnected(next) |
| + } |
| + setup.onDisconnected = { [weak self] in |
| + guard let self, self.generation == epoch else { return } |
| + self.setDisconnected(true, for: next) |
| } |
| - setup.onDisconnected = { [weak self] in self?.askDisconnected = true } |
| ask.onNeedsSetup = { [weak self] in |
| guard let self, self.generation == epoch else { return } |
| self.startSetup() |
| @@ -212,9 +218,11 @@ import RastrilloApple |
| /// It sticks, across launches: no set-up runs by itself until they set |
| /// Ask up again or sign in afresh. |
| var askDisconnected: Bool { |
| - get { account.map { defaults.bool(forKey: Self.disconnectedKey($0)) } ?? false } |
| - set { if let account { defaults.set(newValue, forKey: Self.disconnectedKey(account)) } } |
| + get { account.map(disconnected) ?? false } |
| + set { if let account { setDisconnected(newValue, for: account) } } |
| } |
| + func disconnected(_ account: Account) -> Bool { defaults.bool(forKey: Self.disconnectedKey(account)) } |
| + func setDisconnected(_ value: Bool, for account: Account) { defaults.set(value, forKey: Self.disconnectedKey(account)) } |
| static func disconnectedKey(_ account: Account) -> String { "askDisconnected:\(account.identity)@\(account.origin.absoluteString)" } |
| /// The identity has left this device (a sign-out here, in another |
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift| index 7cc09ff..ff91393 100644 |
| --- a/native/apple/Tests/BaseKitTests/SessionTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/SessionTests.swift |
| @@ -437,3 +437,24 @@ extension SessionTests { |
| reconnect.end() |
| } |
| } |
| + |
| +extension SessionTests { |
| + /// Fable's second re-review, nit 1: a set-up of an account that has |
| + /// gone reads and writes nothing of the account now signed in. |
| + func testAnOldSetUpNeverTouchesTheNextAccountsFlag() 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()) |
| + let aliceSetup = try XCTUnwrap(session.askSetup) |
| + let aliceAsk = try XCTUnwrap(session.ask) |
| + aliceAsk.applyForPreview(try JSONDecoder().decode(AskSnapshot.self, from: Data( |
| + #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"disconnected":true}"#.utf8))) |
| + session.end() |
| + session.begin(identity: "id-bob", origin: URL(string: "https://bob.example")!, transport: GatedTransport()) |
| + XCTAssertTrue(aliceSetup.heldOff(), "a gone account's set-up is held off") |
| + aliceSetup.onDisconnected() |
| + XCTAssertFalse(session.askDisconnected, "and writes nothing for Bob") |
| + XCTAssertEqual(defaults.dictionaryRepresentation().keys.filter { $0.hasPrefix("askDisconnected:") }, []) |
| + session.end() |
| + } |
| +} |