oficina / base Public

End the session only on Base's own refusal

Review finding 2: any 401, and a 303, called the kit's sessionRejected().
Only exactly 401 {"code":"unauthenticated"} does now; another 401
code, a bare 401 or a redirect is thrown as it is. A refused event stream
carries no body through the kit, so it is never a rejection by itself:
Ask reads /ask/state afresh and that answer decides.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 857f29920ae1bcc993ba0111153a53b0671a8878 parent 995f981
3 files changed, +48 −14
  • native/apple/Sources/BaseKit/API/Client.swift +12 −11
  • native/apple/Tests/BaseKitTests/ClientTests.swift +23 −3
  • native/apple/Tests/BaseKitTests/EventsTests.swift +13 −0
diff --git a/native/apple/Sources/BaseKit/API/Client.swift b/native/apple/Sources/BaseKit/API/Client.swift
index 987502e..353d5d7 100644
--- a/native/apple/Sources/BaseKit/API/Client.swift
+++ b/native/apple/Sources/BaseKit/API/Client.swift
@@ -4,8 +4,7 @@ import RastrilloApple
/// Why a call to Base failed, typed so the app can tell an ended session
/// from Ask's own states and from the network.
public enum BaseAPIError: Error, Sendable, Equatable {
- /// Base doesn't know the session (`401 {"code":"unauthenticated"}`, or an
- /// old Base's redirect to /signin):
+ /// Base doesn't know the session (`401 {"code":"unauthenticated"}`):
/// the client has already told the kit (`sessionRejected()`).
case unauthenticated
/// The request never reached Base, or its answer never came back. A
@@ -24,9 +23,9 @@ public enum BaseAPIError: Error, Sendable, Equatable {
/// and `Accept: application/json`, and refuses redirects. Ask's routes are
/// the web's own (P4); the rest are `/api/v1` reads.
///
-/// A 401 is only ever Base refusing the session (`{"code":
-/// "unauthenticated"}`): it calls `rejected` (the kit's `sessionRejected()`)
-/// before it throws. Equipo refusing Base's key for this person is Ask's
+/// Exactly `401 {"code":"unauthenticated"}` is Base refusing the session:
+/// it calls `rejected` (the kit's `sessionRejected()`) before it throws.
+/// Any other 401 or a 303 is only thrown. Equipo refusing Base's key for this person is Ask's
/// own state, `409 {"state":"reconnect"}`, and is only thrown.
public struct BaseClient: Sendable {
public let transport: NativeTransport
@@ -83,7 +82,7 @@ public struct BaseClient: Sendable {
/// A form the web answers with `303 /settings` once done. NativeHTTP
/// follows no redirect, so the 303 is the answer: to /settings it is
- /// success; to anywhere else (an old Base's /signin), the session ended.
+ /// success; anything else is thrown.
func postSettling(_ path: String, form fields: [(String, String)]) async throws(BaseAPIError) {
let response: NativeResponse
do {
@@ -153,9 +152,9 @@ public struct BaseClient: Sendable {
func refused(status: Int, body: Data) async -> BaseAPIError {
let fields = (try? JSONSerialization.jsonObject(with: body)) as? [String: Any]
let code = fields?["code"] as? String, state = fields?["state"] as? String
- // 303: NativeHTTP refuses redirects, so an old Base's redirect to
- // /signin arrives as a status.
- if status == 401 || status == 303 {
+ // Only exactly 401 {"code":"unauthenticated"} is Base ending the
+ // session (the contract); any other 401, or a redirect, is not.
+ if status == 401, code == "unauthenticated" {
await rejected()
return .unauthenticated
}
@@ -167,8 +166,10 @@ public struct BaseClient: Sendable {
switch error as? NativeClientError {
case .offline?: return .offline
case .unauthorized?:
- await rejected()
- return .unauthenticated
+ // The kit's stream refusal carries no body: it can't say whether
+ // this is Base ending the session. The caller reads /ask/state
+ // afresh, whose answer says.
+ return .server(status: 401, code: nil, state: nil)
case .conflict(let body)?: return await refused(status: 409, body: body)
case .rejected(let status, let body)?: return await refused(status: status, body: body)
case .server(let status)?: return .server(status: status, code: nil, state: nil)
diff --git a/native/apple/Tests/BaseKitTests/ClientTests.swift b/native/apple/Tests/BaseKitTests/ClientTests.swift
index 3497c57..3796c00 100644
--- a/native/apple/Tests/BaseKitTests/ClientTests.swift
+++ b/native/apple/Tests/BaseKitTests/ClientTests.swift
@@ -29,10 +29,30 @@ final class ClientTests: XCTestCase {
func testBaseRefusingTheSessionTellsTheKit() async {
let rejections = Rejections()
- let c = client(ScriptedTransport([json(401, #"{"code":"unauthenticated"}"#), .respond(status: 303)]), rejections)
+ let c = client(ScriptedTransport([json(401, #"{"code":"unauthenticated"}"#)]), rejections)
await XCTAssertThrows(.unauthenticated) { _ = try await c.me() }
- await XCTAssertThrows(.unauthenticated) { _ = try await c.askState() }
- XCTAssertEqual(rejections.count, 2)
+ XCTAssertEqual(rejections.count, 1)
+ }
+
+ /// Review finding 2: only exactly 401 {"code":"unauthenticated"} ends
+ /// the session; another 401's code, a bare 401 or a 303 doesn't.
+ func testOnlyTheContractsRefusalEndsTheSession() async {
+ let rejections = Rejections()
+ let c = client(ScriptedTransport([json(401, #"{"code":"upstream_error"}"#), .respond(status: 401),
+ .respond(status: 303, headers: ["location": "/signin"])]), rejections)
+ await XCTAssertThrows(.server(status: 401, code: "upstream_error", state: nil)) { _ = try await c.me() }
+ await XCTAssertThrows(.server(status: 401, code: nil, state: nil)) { _ = try await c.askState() }
+ await XCTAssertThrows(.server(status: 303, code: nil, state: nil)) { try await c.disconnectAsk() }
+ XCTAssertEqual(rejections.count, 0)
+ }
+
+ /// A refused stream can't say why (the kit keeps no body): it is not a
+ /// rejection by itself.
+ func testARefusedStreamIsNotARejection() async {
+ let rejections = Rejections()
+ let c = client(ScriptedTransport([json(401, #"{"code":"unauthenticated"}"#)]), rejections)
+ await XCTAssertThrows(.server(status: 401, code: nil, state: nil)) { try await c.events(after: 1) { _, _ in } }
+ XCTAssertEqual(rejections.count, 0)
}
/// `409 {"state":"reconnect"}` is Equipo refusing Ask's key: Ask's to
diff --git a/native/apple/Tests/BaseKitTests/EventsTests.swift b/native/apple/Tests/BaseKitTests/EventsTests.swift
index d6f005d..0288f91 100644
--- a/native/apple/Tests/BaseKitTests/EventsTests.swift
+++ b/native/apple/Tests/BaseKitTests/EventsTests.swift
@@ -79,6 +79,19 @@ final class EventsTests: XCTestCase {
model.pause()
}
+ /// Review finding 2: a stream refused with a 401 reads /ask/state
+ /// afresh, and that answer decides: here, Base ending the session.
+ @MainActor func testARefusedStreamReadsTheStateWhichDecides() async throws {
+ let rejections = Rejections()
+ let transport = ScriptedTransport([.respond(status: 200, body: Fixture.data("state-ready.json")), .respond(status: 401),
+ json(401, #"{"code":"unauthenticated"}"#)])
+ let model = AskModel(client: BaseClient(transport: transport) { rejections.hit() })
+ await model.load()
+ try await waitUntil { transport.calls.count == 3 }
+ try await waitUntil { rejections.count == 1 }
+ XCTAssertEqual(transport.calls.map(\.path), ["/ask/state", "/ask/events?after=42", "/ask/state"])
+ }
+
@MainActor func testAStateEventStopsListening() async throws {
let body = "event: state\ndata: {\"state\":\"reconnect\"}\n\n"
let transport = ScriptedTransport([.respond(status: 200, body: Fixture.data("state-ready.json")), .respond(status: 200, body: Data(body.utf8))])