|
| 1 | +import { test, expect } from "bun:test" |
| 2 | +import { createIntakeClient } from "./intake-client" |
| 3 | + |
| 4 | +test("POSTs to intakeUrl/reports with multipart body and Idempotency-Key header", async () => { |
| 5 | + let capturedUrl = "" |
| 6 | + let capturedHeaders: Record<string, string> = {} |
| 7 | + const mockFetch: typeof fetch = async (input, init) => { |
| 8 | + capturedUrl = typeof input === "string" ? input : (input as Request).url |
| 9 | + capturedHeaders = Object.fromEntries(new Headers(init?.headers).entries()) |
| 10 | + return new Response(JSON.stringify({ id: "server-id" }), { |
| 11 | + status: 201, |
| 12 | + headers: { "content-type": "application/json" }, |
| 13 | + }) |
| 14 | + } |
| 15 | + const client = createIntakeClient({ |
| 16 | + intakeUrl: "https://ex.com/api/intake", |
| 17 | + fetchImpl: mockFetch, |
| 18 | + }) |
| 19 | + const res = await client.submit({ |
| 20 | + idempotencyKey: "idem-1", |
| 21 | + input: { |
| 22 | + projectKey: "rp_pk_" + "a".repeat(24), |
| 23 | + title: "t", |
| 24 | + context: { |
| 25 | + source: "expo", |
| 26 | + pageUrl: "myapp://x", |
| 27 | + userAgent: "u", |
| 28 | + viewport: { w: 1, h: 1 }, |
| 29 | + timestamp: new Date().toISOString(), |
| 30 | + }, |
| 31 | + } as never, |
| 32 | + attachments: [], |
| 33 | + }) |
| 34 | + expect(capturedUrl).toBe("https://ex.com/api/intake/reports") |
| 35 | + expect(capturedHeaders["idempotency-key"]).toBe("idem-1") |
| 36 | + expect(res.id).toBe("server-id") |
| 37 | +}) |
| 38 | + |
| 39 | +const mockServerErrorFetch: typeof fetch = async () => new Response("boom", { status: 503 }) |
| 40 | + |
| 41 | +test("surfaces 5xx errors to the caller", async () => { |
| 42 | + const client = createIntakeClient({ |
| 43 | + intakeUrl: "https://ex.com/api/intake", |
| 44 | + fetchImpl: mockServerErrorFetch, |
| 45 | + }) |
| 46 | + await expect( |
| 47 | + client.submit({ |
| 48 | + idempotencyKey: "k", |
| 49 | + input: { projectKey: "rp_pk_" + "a".repeat(24), title: "t", context: {} as never } as never, |
| 50 | + attachments: [], |
| 51 | + }), |
| 52 | + ).rejects.toMatchObject({ status: 503 }) |
| 53 | +}) |
0 commit comments