oficina / base Public

Hold every automatic set-up to "auto": true

Base's contract is strict: an automatic set-up posts exactly
"auto": true, and a missing auto counts as the person's own, which
would undo their Disconnect. AskSetup already sent it on every run the
person didn't ask for; this pins it. A session test drives each
automatic path, after sign-in, a renewal, coming forward to connect and
a write's 409 reconnect, and checks each body says auto true, then that
the person's own Set it up sends none; a client test checks auto is a
JSON boolean and absent from explicit set-ups.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev fd637d749f1d63fe9116d8157e79641394f60938 parent 37059d1
3 files changed, +91 −0
  • native/apple/Tests/BaseKitTests/ClientTests.swift +15 −0
  • native/apple/Tests/BaseKitTests/SessionTests.swift +72 −0
  • native/apple/Tests/BaseKitTests/Support.swift +4 −0
diff --git a/native/apple/Tests/BaseKitTests/ClientTests.swift b/native/apple/Tests/BaseKitTests/ClientTests.swift
index 55c497a..45145d9 100644
--- a/native/apple/Tests/BaseKitTests/ClientTests.swift
+++ b/native/apple/Tests/BaseKitTests/ClientTests.swift
@@ -97,6 +97,21 @@ final class ClientTests: XCTestCase {
XCTAssertEqual(body, ["assertion": "a", "verifier": "v"], "explicit: no auto")
}
+ /// auto is strict: exactly true when the app sets Ask up by itself,
+ /// absent for the person's own.
+ func testConnectAskSaysAutoExactly() async throws {
+ let t = ScriptedTransport([json(200, #"{"state":"ready"}"#), json(200, #"{"state":"ready"}"#)])
+ let c = client(t)
+ _ = try await c.connectAsk(assertion: "a", verifier: "v", auto: true)
+ _ = try await c.connectAsk(assertion: "a", verifier: "v")
+ let auto = try XCTUnwrap(JSONSerialization.jsonObject(with: t.calls[0].body!) as? [String: Any])
+ XCTAssertEqual(auto["auto"] as? Bool, true)
+ XCTAssertTrue(auto["auto"] is Bool, "a JSON boolean, not a string or number")
+ XCTAssertEqual(String(decoding: t.calls[0].body!, as: UTF8.self).contains(#""auto":true"#), true)
+ let own = try XCTUnwrap(JSONSerialization.jsonObject(with: t.calls[1].body!) as? [String: Any])
+ XCTAssertEqual(Set(own.keys), ["assertion", "verifier"])
+ }
+
func testReadsDecodeTolerantly() async throws {
let c = client(ScriptedTransport([.respond(status: 200, body: Fixture.data("me.json")), .respond(status: 200, body: Fixture.data("home.json"))]))
let me = try await c.me()
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift
index 4c9ca9b..7cc09ff 100644
--- a/native/apple/Tests/BaseKitTests/SessionTests.swift
+++ b/native/apple/Tests/BaseKitTests/SessionTests.swift
@@ -365,3 +365,75 @@ extension SessionTests {
session.end()
}
}
+
+extension SessionTests {
+ func autoValues(_ t: GatedTransport) -> [Bool?] {
+ t.bodies("/api/v1/ask/oficina").map { body in
+ (body.flatMap { try? JSONSerialization.jsonObject(with: $0) as? [String: Any] })?["auto"] as? Bool
+ }
+ }
+
+ func readySession(state: String) -> (BaseSession, GatedTransport) {
+ let session = BaseSession(oficina: memoryOficina(), defaults: freshDefaults())
+ session.assertion = { _ in ("a", "v") }
+ let t = gated("alice", "Alice's", "Hello")
+ t.answer("/ask/state", state)
+ t.answer("/api/v1/ask/oficina", #"{"state":"ready"}"#)
+ for path in ["/api/v1/me", "/api/v1/home", "/ask/state", "/api/v1/ask/oficina"] { t.release(path) }
+ return (session, t)
+ }
+
+ /// The contract is strict (coordinator, 2026-11-23): every set-up the
+ /// app runs by itself posts exactly "auto": true, and only the person's
+ /// own Set up leaves it out. A missing auto would undo a Disconnect.
+ func testEveryAutomaticSetUpSaysAutoAndOnlyThePersonsOwnDoesnt() async throws {
+ let origin = URL(string: "https://alice.example")!
+ let connect = #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"equipo_origin":"https://equipo.alice.example","disconnected":false}"#
+ let ready = #"{"state":"ready","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":3,"equipo_origin":"https://equipo.alice.example","disconnected":false}"#
+
+ // After sign-in.
+ let (signIn, t1) = readySession(state: connect)
+ signIn.begin(identity: "id-alice", origin: origin, transport: t1)
+ await signIn.refreshing?.value
+ try await waitUntil { !t1.bodies("/api/v1/ask/oficina").isEmpty }
+ XCTAssertEqual(autoValues(t1).first, true, "after sign-in")
+ signIn.end()
+
+ // Renewal: renew_after has passed, on coming forward.
+ let (renewal, t2) = readySession(state: ready.replacingOccurrences(of: #""cursor":3"#, with: #""cursor":3,"renew_after":"2020-01-01T00:00:00Z""#))
+ renewal.begin(identity: "id-alice", origin: origin, transport: t2)
+ await renewal.refreshing?.value
+ try await waitUntil { !t2.bodies("/api/v1/ask/oficina").isEmpty }
+ XCTAssertEqual(autoValues(t2), [true], "a renewal")
+ renewal.end()
+
+ // Coming forward to connect (another client's set-up went away).
+ let (forward, t3) = readySession(state: ready)
+ forward.begin(identity: "id-alice", origin: origin, transport: t3)
+ await forward.refreshing?.value
+ forward.background()
+ t3.answer("/ask/state", connect)
+ await forward.foreground()
+ try await waitUntil { !t3.bodies("/api/v1/ask/oficina").isEmpty }
+ XCTAssertEqual(autoValues(t3), [true], "on coming forward")
+ forward.end()
+
+ // Reconnect: a write answered 409 reconnect.
+ let (reconnect, t4) = readySession(state: ready)
+ t4.answer("/ask/messages", status: 409, #"{"state":"reconnect"}"#)
+ t4.release("/ask/messages")
+ reconnect.begin(identity: "id-alice", origin: origin, transport: t4)
+ await reconnect.refreshing?.value
+ reconnect.ask?.text = "Hello"
+ await reconnect.ask?.send()
+ try await waitUntil { !t4.bodies("/api/v1/ask/oficina").isEmpty }
+ XCTAssertEqual(autoValues(t4).first, true, "after a 409 reconnect")
+
+ // The person's own Set it up: no auto at all.
+ await reconnect.setUpAsk()
+ let own = try XCTUnwrap(t4.bodies("/api/v1/ask/oficina").last.flatMap { $0 })
+ let fields = try XCTUnwrap(JSONSerialization.jsonObject(with: own) as? [String: Any])
+ XCTAssertNil(fields["auto"], "the person's own set-up is explicit")
+ reconnect.end()
+ }
+}
diff --git a/native/apple/Tests/BaseKitTests/Support.swift b/native/apple/Tests/BaseKitTests/Support.swift
index 569318b..a596482 100644
--- a/native/apple/Tests/BaseKitTests/Support.swift
+++ b/native/apple/Tests/BaseKitTests/Support.swift
@@ -94,8 +94,11 @@ final class GatedTransport: NativeTransport, @unchecked Sendable {
private var waiting: [String: [CheckedContinuation<Void, Never>]] = [:]
private var released: Set<String> = []
private var seen: [String] = []
+ private var sent: [(String, Data?)] = []
init(origin: URL = URL(string: "https://gated.invalid")!) { self.origin = origin }
var calls: [String] { lock.withLock { seen } }
+ /// The bodies posted to `path`, in order.
+ func bodies(_ path: String) -> [Data?] { lock.withLock { sent.filter { $0.0 == path }.map(\.1) } }
/// What `path` answers once released.
func answer(_ path: String, status: Int = 200, headers: [String: String] = [:], _ body: String) {
@@ -113,6 +116,7 @@ final class GatedTransport: NativeTransport, @unchecked Sendable {
await withCheckedContinuation { (c: CheckedContinuation<Void, Never>) in
let go = lock.withLock { () -> Bool in
seen.append(path)
+ sent.append((path, body))
if released.contains(path) { return true }
waiting[path, default: []].append(c)
return false