| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @Suite struct ClientTests { |
| 6 | @Test func everyFixtureDecodes() throws { |
| 7 | _ = try Fixture.decode("me", as: Me.self) |
| 8 | _ = try Fixture.decode("ack", as: Ack.self) |
| 9 | _ = try Fixture.decode("device-challenge", as: DeviceChallenge.self) |
| 10 | _ = try Fixture.decode("device-credential", as: DeviceCredential.self) |
| 11 | _ = try Fixture.decode("devices", as: Page<Device>.self) |
| 12 | // The thin fixtures are the server's own answers (internal/thintest). |
| 13 | let snapshot = try Fixture.decode("thin/snapshot", as: Snapshot.self) |
| 14 | _ = try Fixture.decode("thin/tasks", as: ThinList<ThinTask>.self) |
| 15 | _ = try Fixture.decode("thin/needs-you", as: ThinList<ThinTask>.self) |
| 16 | _ = try Fixture.decode("thin/projects", as: ThinList<ThinProject>.self) |
| 17 | let task = try Fixture.decode("thin/task", as: ThinTask.self) |
| 18 | _ = try Fixture.decode("thin/messages", as: MessagePage.self) |
| 19 | _ = try Fixture.decode("thin/sent", as: Sent.self) |
| 20 | _ = try Fixture.decode("thin/answered", as: Answered.self) |
| 21 | _ = try Fixture.decode("thin/card-conflict", as: CardConflict.self) |
| 22 | _ = try Fixture.decode("thin/thread-entry", as: ThreadEntry.self) |
| 23 | // Go writes nanoseconds; they parse. |
| 24 | #expect(snapshot.messages?.count == 6) |
| 25 | #expect(snapshot.me.teamName.isEmpty == false) |
| 26 | #expect(task.steps?.count == 3) |
| 27 | } |
| 28 | |
| 29 | @Test func readsCarryTheBearerAndNoCookies() async throws { |
| 30 | let t = ScriptedTransport(); t.on("GET /snapshot", fixture: "thin/snapshot") |
| 31 | _ = try await EquipoClient(transport: t, token: "secret-token").snapshot() |
| 32 | let r = try #require(t.requests.first) |
| 33 | let url = try r.urlRequest(origin: t.origin) |
| 34 | #expect(url.url?.absoluteString == "https://oficina.example.test/api/v1/snapshot") |
| 35 | #expect(url.value(forHTTPHeaderField: "Authorization") == "Bearer secret-token") |
| 36 | #expect(url.httpShouldHandleCookies == false) |
| 37 | #expect(url.value(forHTTPHeaderField: "Idempotency-Key") == nil) |
| 38 | } |
| 39 | |
| 40 | @Test func anAnswerCarriesKeyAndTheRevisionSeen() async throws { |
| 41 | let snapshot: Snapshot = try Fixture.decode("thin/snapshot") |
| 42 | let card = try #require(snapshot.cards?.first { $0.kind == "merge" && $0.isOpen }) |
| 43 | let t = ScriptedTransport(); t.on("POST /cards/\(card.id)/answer", fixture: "thin/answered") |
| 44 | let write = try EquipoClient.answer("wait_mac", to: card) |
| 45 | _ = try await EquipoClient(transport: t, token: "tok").answer(card: card.id, write) |
| 46 | let url = try t.requests[0].urlRequest(origin: t.origin) |
| 47 | #expect(url.value(forHTTPHeaderField: "Idempotency-Key") == write.key) |
| 48 | #expect(url.value(forHTTPHeaderField: "If-Match") == "\"\(card.revision)\"") |
| 49 | let body = try JSONSerialization.jsonObject(with: url.httpBody!) as! [String: Any] |
| 50 | #expect(body["choice"] as? String == "wait_mac") |
| 51 | #expect((body["revision"] as? NSNumber)?.int64Value == card.revision) |
| 52 | #expect(body["note"] == nil) |
| 53 | } |
| 54 | |
| 55 | @Test func aMessageCarriesItsKeyInBodyAndHeader() throws { |
| 56 | let write = try EquipoClient.message("Fix the README") |
| 57 | let body = try JSONSerialization.jsonObject(with: write.body) as! [String: String] |
| 58 | #expect(body == ["text": "Fix the README", "idempotency_key": write.key]) |
| 59 | } |
| 60 | |
| 61 | @Test func anAnswerWithoutARevisionIsNeverSent() async throws { |
| 62 | let t = ScriptedTransport() |
| 63 | let unbound = PendingWrite(body: Data("{}".utf8)) |
| 64 | await #expect(throws: EquipoAPIError.status(428)) { _ = try await EquipoClient(transport: t, token: "t").answer(card: "c", unbound) } |
| 65 | #expect(t.requests.isEmpty) |
| 66 | } |
| 67 | |
| 68 | @Test func aCardConflictKeepsTheCardAsItIsNow() async throws { |
| 69 | let t = ScriptedTransport(); t.on("POST /cards/c1/answer", status: 409, fixture: "thin/card-conflict") |
| 70 | do { _ = try await EquipoClient(transport: t, token: "t").answer(card: "c1", PendingWrite(body: Data("{}".utf8), ifMatch: 1)); Issue.record("no error") } |
| 71 | catch let e as EquipoAPIError { |
| 72 | #expect(e.isStale); #expect(e.status == 409); #expect(e.code == "card_answered") |
| 73 | guard case .card(_, _, let card) = e else { Issue.record("not a card conflict"); return } |
| 74 | #expect(card.state == "answered") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Test func errorsKeepCodeAndCurrentRevision() async throws { |
| 79 | let t = ScriptedTransport(); t.on("GET /me", status: 409, fixture: "error-revision-conflict") |
| 80 | do { _ = try await EquipoClient(transport: t, token: "t").me(); Issue.record("no error") } |
| 81 | catch let e as EquipoAPIError { |
| 82 | #expect(e.isStale); #expect(e.currentRevision == 9); #expect(e.status == 409) |
| 83 | } |
| 84 | t.on("GET /tasks", .success(APIResponse(status: 401, data: Data("not json".utf8)))) |
| 85 | do { _ = try await EquipoClient(transport: t, token: "t").tasks(); Issue.record("no error") } |
| 86 | catch let e as EquipoAPIError { #expect(e.endsSession) } |
| 87 | } |
| 88 | |
| 89 | @Test func olderMessagesStayInRange() async throws { |
| 90 | let t = ScriptedTransport(); t.on("GET /messages", fixture: "thin/messages") |
| 91 | _ = try await EquipoClient(transport: t, token: "t").olderMessages(before: "m1", limit: 500) |
| 92 | #expect(t.requests[0].query.first { $0.name == "limit" }?.value == "200") |
| 93 | } |
| 94 | |
| 95 | @Test func eventsResumeAfterTheCursor() async throws { |
| 96 | let t = ScriptedTransport(); t.onStream(.success([])) |
| 97 | _ = try await EquipoClient(transport: t, token: "t").events(after: 64) |
| 98 | #expect(t.requests[0].path == "/events") |
| 99 | #expect(t.requests[0].query.first { $0.name == "after" }?.value == "64") |
| 100 | #expect(t.requests[0].bearer == "t") |
| 101 | } |
| 102 | |
| 103 | @Test func idsAreEscapedInPaths() async throws { |
| 104 | let t = ScriptedTransport() |
| 105 | _ = try? await EquipoClient(transport: t, token: "t").task("a/b?c") |
| 106 | #expect(t.requests[0].path == "/tasks/a%2Fb%3Fc") |
| 107 | } |
| 108 | } |
| 109 | |