oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import Testing
3@testable import EquipoKit
4
5/// End to end against a real Equipo: pair, read, send, poll, sign out.
6/// Off unless EQUIPO_LIVE_ORIGIN is set (a local drive, `tools/drive`).
7/// It writes the code and approval link to EQUIPO_LIVE_HANDOFF and waits up
8/// to ten minutes for a person to approve the device in a browser.
9@MainActor @Suite(.enabled(if: ProcessInfo.processInfo.environment["EQUIPO_LIVE_ORIGIN"] != nil), .timeLimit(.minutes(15)))
10struct LiveTests {
11 @Test func pairReadSendAndSignOut() async throws {
12 let env = ProcessInfo.processInfo.environment
13 let origin = try #require(PairingModel.origin(from: env["EQUIPO_LIVE_ORIGIN"]!))
14 let handoff = URL(fileURLWithPath: env["EQUIPO_LIVE_HANDOFF"] ?? NSTemporaryDirectory() + "equipo-live-handoff.txt")
15 let store = MemorySecretStore()
16 let accounts = DeviceAccounts(store: store)
17 let pairing = PairingModel(accounts: accounts)
18 pairing.start(origin: origin, label: "EquipoKit live test")
19
20 var wrote = false
21 let deadline = Date().addingTimeInterval(600)
22 while Date() < deadline {
23 switch pairing.step {
24 case .waiting(let code, let url, _) where !wrote:
25 try "\(code)\n\(url.absoluteString)\n".write(to: handoff, atomically: true, encoding: .utf8)
26 wrote = true
27 case .approved, .expired, .failed: break
28 default: break
29 }
30 if case .approved = pairing.step { break }
31 if case .expired = pairing.step { break }
32 if case .failed = pairing.step { break }
33 try await Task.sleep(for: .milliseconds(500))
34 }
35 guard case .approved(let team) = pairing.step, let account = pairing.account else {
36 Issue.record("pairing ended as \(pairing.step)"); return
37 }
38 print("LIVE paired with \(team) as device \(account.credential?.id ?? "?")")
39
40 let session = EquipoSession(account: account, accounts: accounts)
41 await session.refresh()
42 #expect(session.status == .ready)
43 #expect(session.me != nil)
44 print("LIVE read \(session.messages.count) messages, \(session.cards.count) cards, \(session.tasks.count) tasks, needs you \(session.needsYou.map { String($0.count) } ?? "unknown")")
45
46 let text = "Native slice 1 live check \(UUID().uuidString.prefix(8))"
47 await session.send(text)
48 #expect(session.outgoing.isEmpty, "the message was not accepted: \(session.outgoing)")
49 #expect(session.messages.contains { $0.isOwn && $0.text == text })
50
51 // The event stream opens with this device's bearer and says something.
52 let stream = try await session.client.events(after: 0)
53 var first: String?
54 for try await line in stream where line.hasPrefix("data:") { first = line; break }
55 #expect(first != nil, "the event stream said nothing")
56
57 if let task = session.tasks.first {
58 let full = try await session.client.task(task.id)
59 print("LIVE task \(task.id) at \(full.stage), \(full.steps?.count ?? 0) steps, thread \(full.thread?.count ?? 0)")
60 }
61
62 await session.signOut()
63 #expect(accounts.all().isEmpty)
64 // The credential no longer works: the server revoked it.
65 let after = EquipoClient(transport: URLSessionTransport(origin: origin), token: account.credential?.token)
66 do { _ = try await after.me(); Issue.record("a revoked device could still read") }
67 catch let error as EquipoAPIError { #expect(error.endsSession, "expected 401 after revocation, got \(error)") }
68 print("LIVE signed out; the revoked credential is refused")
69 }
70}
71