oficina / base Public

List the kit's cached teams in Switch team

Astra's Task 16 finding 2: refreshTeams(maxAge:) answers nil when its
cache is fresh (right after sign-in, or reopening the picker within a
minute), when a refresh is already running, or when it fails; Base read
nil as no teams and showed an empty picker. Switch team now reads the
current identity's cached teams whenever the kit didn't refresh, and
shows none if the account changed while it asked. The regression signs
in through the kit's in-memory Oficina ID, opens the list twice and
switches to the other team.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev ca7910917ea26a61bbf00be3b8174411912ee8a2 parent 196d937
3 files changed, +55 −1
  • native/apple/Sources/BaseKit/Session/BaseSession.swift +9 −1
  • native/apple/Tests/BaseKitTests/SessionTests.swift +19 −0
  • native/apple/Tests/BaseKitTests/Support.swift +27 −0
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift
index 7a51513..fcc6133 100644
--- a/native/apple/Sources/BaseKit/Session/BaseSession.swift
+++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift
@@ -175,8 +175,16 @@ import RastrilloApple
}
/// The teams this Oficina ID is in, for Switch team.
+ ///
+ /// The kit refreshes its cache when it's older than a minute; nil means
+ /// it didn't (fresh already, a refresh already running, or a failure),
+ /// never "no teams", so the cache for this identity is the answer then.
+ /// Signed out or someone else meanwhile: none.
public func teams() async -> [OficinaTeam] {
- await oficina.refreshTeams(maxAge: 60) ?? []
+ guard let asked = account else { return [] }
+ let fresh = await oficina.refreshTeams(maxAge: 60)
+ guard account == asked else { return [] }
+ return fresh ?? oficina.store.teams(asked.identity)?.teams ?? []
}
/// Switch team: the kit opens the other team's Base.
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift
index e97c3b4..18675d3 100644
--- a/native/apple/Tests/BaseKitTests/SessionTests.swift
+++ b/native/apple/Tests/BaseKitTests/SessionTests.swift
@@ -104,3 +104,22 @@ extension SessionTests {
session.end()
}
}
+
+extension SessionTests {
+ /// Astra's Task 16 finding 2: right after sign-in, and on reopening the
+ /// picker within a minute, the kit's cache is the list; choosing the
+ /// other team signs Base in there.
+ func testSwitchTeamListsTheCachedTeamsAndSwitches() async throws {
+ let (oficina, teams) = try await signedInOficina()
+ let session = BaseSession(oficina: oficina)
+ try await waitUntil { session.signedIn != nil }
+ let first = await session.teams()
+ XCTAssertEqual(first.map(\.sqid), teams.map(\.sqid), "the cached teams, though the cache is fresh")
+ let again = await session.teams()
+ XCTAssertEqual(again.map(\.sqid), teams.map(\.sqid), "reopened within a minute")
+ let other = try XCTUnwrap(first.first { $0.sqid != session.signedIn?.team.sqid })
+ await session.switchTo(other)
+ try await waitUntil { session.signedIn?.team.sqid == other.sqid }
+ XCTAssertEqual(session.signedIn?.origin.host, "\(other.label).base.invalid")
+ }
+}
diff --git a/native/apple/Tests/BaseKitTests/Support.swift b/native/apple/Tests/BaseKitTests/Support.swift
index e48a8e7..5ebae48 100644
--- a/native/apple/Tests/BaseKitTests/Support.swift
+++ b/native/apple/Tests/BaseKitTests/Support.swift
@@ -134,3 +134,30 @@ final class GatedTransport: NativeTransport, @unchecked Sendable {
return OficinaIDModel(configuration: BaseSession.configuration, store: store, service: FakeOficinaIDService(),
passkeys: FakePasskeys(), appDefaults: defaults)
}
+
+/// A kit model signed in for real through the kit's in-memory Oficina ID,
+/// to a person in two teams, each with a Base (`.invalid` origins: nothing
+/// leaves the machine).
+@MainActor func signedInOficina() async throws -> (OficinaIDModel, [OficinaTeam]) {
+ let service = FakeOficinaIDService()
+ service.addPerson(email: "paul@fennel.example", identityID: "id-paul")
+ let teams = ["fennel", "rye"].map { label in
+ OficinaTeam(sqid: "t-\(label)", label: label, name: label.capitalized, role: "member",
+ apps: [OficinaTeamApp(app: "base", origin: "https://\(label).base.invalid", consumerID: "t-\(label)-base")])
+ }
+ service.setTeams(teams, for: "id-paul")
+ for team in teams { service.admit("id-paul", at: team.origin(for: "base")!, as: .linked) }
+ let defaults = UserDefaults(suiteName: "BaseKitTests.\(UUID().uuidString)")!
+ let store = OficinaIDStore(shared: OficinaMemoryStore(), appPrivate: OficinaMemoryStore(), group: defaults)
+ let model = OficinaIDModel(configuration: BaseSession.configuration, store: store, service: service,
+ passkeys: FakePasskeys(), makeKey: { OficinaSoftwareKey() }, appDefaults: defaults)
+ await model.start()
+ model.continueWithEmail()
+ model.email = "paul@fennel.example"
+ await model.submitEmail()
+ model.code = FakeOficinaIDService.goodCode
+ await model.submitCode()
+ if case .teams(let list) = model.step, let first = list.first { await model.choose(first) }
+ guard case .signedIn = model.step else { throw XCTSkip("the fake sign-in did not complete: \(model.step)") }
+ return (model, teams)
+}