import Foundation import Testing @testable import EquipoKit /// End to end against a real Equipo: pair, read, send, poll, sign out. /// Off unless EQUIPO_LIVE_ORIGIN is set (a local drive, `tools/drive`). /// It writes the code and approval link to EQUIPO_LIVE_HANDOFF and waits up /// to ten minutes for a person to approve the device in a browser. @MainActor @Suite(.enabled(if: ProcessInfo.processInfo.environment["EQUIPO_LIVE_ORIGIN"] != nil), .timeLimit(.minutes(15))) struct LiveTests { @Test func pairReadSendAndSignOut() async throws { let env = ProcessInfo.processInfo.environment let origin = try #require(PairingModel.origin(from: env["EQUIPO_LIVE_ORIGIN"]!)) let handoff = URL(fileURLWithPath: env["EQUIPO_LIVE_HANDOFF"] ?? NSTemporaryDirectory() + "equipo-live-handoff.txt") let store = MemorySecretStore() let accounts = DeviceAccounts(store: store) let pairing = PairingModel(accounts: accounts) pairing.start(origin: origin, label: "EquipoKit live test") var wrote = false let deadline = Date().addingTimeInterval(600) while Date() < deadline { switch pairing.step { case .waiting(let code, let url, _) where !wrote: try "\(code)\n\(url.absoluteString)\n".write(to: handoff, atomically: true, encoding: .utf8) wrote = true case .approved, .expired, .failed: break default: break } if case .approved = pairing.step { break } if case .expired = pairing.step { break } if case .failed = pairing.step { break } try await Task.sleep(for: .milliseconds(500)) } guard case .approved(let team) = pairing.step, let account = pairing.account else { Issue.record("pairing ended as \(pairing.step)"); return } print("LIVE paired with \(team) as device \(account.credential?.id ?? "?")") let session = EquipoSession(account: account, accounts: accounts) await session.refresh() #expect(session.status == .ready) #expect(session.me != nil) print("LIVE read \(session.messages.count) messages, \(session.cards.count) cards, \(session.tasks.count) tasks, needs you \(session.needsYou.map { String($0.count) } ?? "unknown")") let text = "Native slice 1 live check \(UUID().uuidString.prefix(8))" await session.send(text) #expect(session.outgoing.isEmpty, "the message was not accepted: \(session.outgoing)") #expect(session.messages.contains { $0.isOwn && $0.text == text }) // The event stream opens with this device's bearer and says something. let stream = try await session.client.events(after: 0) var first: String? for try await line in stream where line.hasPrefix("data:") { first = line; break } #expect(first != nil, "the event stream said nothing") if let task = session.tasks.first { let full = try await session.client.task(task.id) print("LIVE task \(task.id) at \(full.stage), \(full.steps?.count ?? 0) steps, thread \(full.thread?.count ?? 0)") } await session.signOut() #expect(accounts.all().isEmpty) // The credential no longer works: the server revoked it. let after = EquipoClient(transport: URLSessionTransport(origin: origin), token: account.credential?.token) do { _ = try await after.me(); Issue.record("a revoked device could still read") } catch let error as EquipoAPIError { #expect(error.endsSession, "expected 401 after revocation, got \(error)") } print("LIVE signed out; the revoked credential is refused") } }