import Foundation import Testing @testable import EquipoKit @Suite struct ClientTests { @Test func everyFixtureDecodes() throws { _ = try Fixture.decode("me", as: Me.self) _ = try Fixture.decode("ack", as: Ack.self) _ = try Fixture.decode("device-challenge", as: DeviceChallenge.self) _ = try Fixture.decode("device-credential", as: DeviceCredential.self) _ = try Fixture.decode("devices", as: Page.self) // The thin fixtures are the server's own answers (internal/thintest). let snapshot = try Fixture.decode("thin/snapshot", as: Snapshot.self) _ = try Fixture.decode("thin/tasks", as: ThinList.self) _ = try Fixture.decode("thin/needs-you", as: ThinList.self) _ = try Fixture.decode("thin/projects", as: ThinList.self) let task = try Fixture.decode("thin/task", as: ThinTask.self) _ = try Fixture.decode("thin/messages", as: MessagePage.self) _ = try Fixture.decode("thin/sent", as: Sent.self) _ = try Fixture.decode("thin/answered", as: Answered.self) _ = try Fixture.decode("thin/card-conflict", as: CardConflict.self) _ = try Fixture.decode("thin/thread-entry", as: ThreadEntry.self) // Go writes nanoseconds; they parse. #expect(snapshot.messages?.count == 6) #expect(snapshot.me.teamName.isEmpty == false) #expect(task.steps?.count == 3) } @Test func readsCarryTheBearerAndNoCookies() async throws { let t = ScriptedTransport(); t.on("GET /snapshot", fixture: "thin/snapshot") _ = try await EquipoClient(transport: t, token: "secret-token").snapshot() let r = try #require(t.requests.first) let url = try r.urlRequest(origin: t.origin) #expect(url.url?.absoluteString == "https://oficina.example.test/api/v1/snapshot") #expect(url.value(forHTTPHeaderField: "Authorization") == "Bearer secret-token") #expect(url.httpShouldHandleCookies == false) #expect(url.value(forHTTPHeaderField: "Idempotency-Key") == nil) } @Test func anAnswerCarriesKeyAndTheRevisionSeen() async throws { let snapshot: Snapshot = try Fixture.decode("thin/snapshot") let card = try #require(snapshot.cards?.first { $0.kind == "merge" && $0.isOpen }) let t = ScriptedTransport(); t.on("POST /cards/\(card.id)/answer", fixture: "thin/answered") let write = try EquipoClient.answer("wait_mac", to: card) _ = try await EquipoClient(transport: t, token: "tok").answer(card: card.id, write) let url = try t.requests[0].urlRequest(origin: t.origin) #expect(url.value(forHTTPHeaderField: "Idempotency-Key") == write.key) #expect(url.value(forHTTPHeaderField: "If-Match") == "\"\(card.revision)\"") let body = try JSONSerialization.jsonObject(with: url.httpBody!) as! [String: Any] #expect(body["choice"] as? String == "wait_mac") #expect((body["revision"] as? NSNumber)?.int64Value == card.revision) #expect(body["note"] == nil) } @Test func aMessageCarriesItsKeyInBodyAndHeader() throws { let write = try EquipoClient.message("Fix the README") let body = try JSONSerialization.jsonObject(with: write.body) as! [String: String] #expect(body == ["text": "Fix the README", "idempotency_key": write.key]) } @Test func anAnswerWithoutARevisionIsNeverSent() async throws { let t = ScriptedTransport() let unbound = PendingWrite(body: Data("{}".utf8)) await #expect(throws: EquipoAPIError.status(428)) { _ = try await EquipoClient(transport: t, token: "t").answer(card: "c", unbound) } #expect(t.requests.isEmpty) } @Test func aCardConflictKeepsTheCardAsItIsNow() async throws { let t = ScriptedTransport(); t.on("POST /cards/c1/answer", status: 409, fixture: "thin/card-conflict") do { _ = try await EquipoClient(transport: t, token: "t").answer(card: "c1", PendingWrite(body: Data("{}".utf8), ifMatch: 1)); Issue.record("no error") } catch let e as EquipoAPIError { #expect(e.isStale); #expect(e.status == 409); #expect(e.code == "card_answered") guard case .card(_, _, let card) = e else { Issue.record("not a card conflict"); return } #expect(card.state == "answered") } } @Test func errorsKeepCodeAndCurrentRevision() async throws { let t = ScriptedTransport(); t.on("GET /me", status: 409, fixture: "error-revision-conflict") do { _ = try await EquipoClient(transport: t, token: "t").me(); Issue.record("no error") } catch let e as EquipoAPIError { #expect(e.isStale); #expect(e.currentRevision == 9); #expect(e.status == 409) } t.on("GET /tasks", .success(APIResponse(status: 401, data: Data("not json".utf8)))) do { _ = try await EquipoClient(transport: t, token: "t").tasks(); Issue.record("no error") } catch let e as EquipoAPIError { #expect(e.endsSession) } } @Test func olderMessagesStayInRange() async throws { let t = ScriptedTransport(); t.on("GET /messages", fixture: "thin/messages") _ = try await EquipoClient(transport: t, token: "t").olderMessages(before: "m1", limit: 500) #expect(t.requests[0].query.first { $0.name == "limit" }?.value == "200") } @Test func eventsResumeAfterTheCursor() async throws { let t = ScriptedTransport(); t.onStream(.success([])) _ = try await EquipoClient(transport: t, token: "t").events(after: 64) #expect(t.requests[0].path == "/events") #expect(t.requests[0].query.first { $0.name == "after" }?.value == "64") #expect(t.requests[0].bearer == "t") } @Test func idsAreEscapedInPaths() async throws { let t = ScriptedTransport() _ = try? await EquipoClient(transport: t, token: "t").task("a/b?c") #expect(t.requests[0].path == "/tasks/a%2Fb%3Fc") } }