|
| 1 | +import { describe, it, expect, mock, beforeEach, afterEach } from "bun:test"; |
| 2 | +import { ApiRequestError, apiFetch, apiPost } from "../lib/api"; |
| 3 | + |
| 4 | +describe("ApiRequestError", () => { |
| 5 | + it("parses server error JSON into status and message", () => { |
| 6 | + const err = new ApiRequestError(422, "Validation failed"); |
| 7 | + expect(err.status).toBe(422); |
| 8 | + expect(err.serverMessage).toBe("Validation failed"); |
| 9 | + expect(err.name).toBe("ApiRequestError"); |
| 10 | + expect(err.message).toBe("Validation failed"); |
| 11 | + }); |
| 12 | +}); |
| 13 | + |
| 14 | +describe("apiFetch", () => { |
| 15 | + const originalFetch = globalThis.fetch; |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + globalThis.fetch = originalFetch; |
| 19 | + }); |
| 20 | + |
| 21 | + it("throws ApiRequestError with server error message on non-ok response", async () => { |
| 22 | + globalThis.fetch = mock(() => |
| 23 | + Promise.resolve({ |
| 24 | + ok: false, |
| 25 | + status: 400, |
| 26 | + statusText: "Bad Request", |
| 27 | + json: () => Promise.resolve({ error: "Invalid task ID" }), |
| 28 | + }), |
| 29 | + ) as unknown as typeof fetch; |
| 30 | + |
| 31 | + try { |
| 32 | + await apiFetch("/tasks"); |
| 33 | + throw new Error("should have thrown"); |
| 34 | + } catch (err) { |
| 35 | + expect(err).toBeInstanceOf(ApiRequestError); |
| 36 | + expect((err as ApiRequestError).status).toBe(400); |
| 37 | + expect((err as ApiRequestError).serverMessage).toBe("Invalid task ID"); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + it("falls back to statusText when response body is not JSON", async () => { |
| 42 | + globalThis.fetch = mock(() => |
| 43 | + Promise.resolve({ |
| 44 | + ok: false, |
| 45 | + status: 500, |
| 46 | + statusText: "Internal Server Error", |
| 47 | + json: () => Promise.reject(new SyntaxError("not json")), |
| 48 | + }), |
| 49 | + ) as unknown as typeof fetch; |
| 50 | + |
| 51 | + try { |
| 52 | + await apiFetch("/tasks"); |
| 53 | + throw new Error("should have thrown"); |
| 54 | + } catch (err) { |
| 55 | + expect((err as ApiRequestError).serverMessage).toBe( |
| 56 | + "Internal Server Error", |
| 57 | + ); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it("returns parsed JSON on success", async () => { |
| 62 | + globalThis.fetch = mock(() => |
| 63 | + Promise.resolve({ |
| 64 | + ok: true, |
| 65 | + json: () => Promise.resolve({ count: 3, tasks: [] }), |
| 66 | + }), |
| 67 | + ) as unknown as typeof fetch; |
| 68 | + |
| 69 | + const result = await apiFetch<{ count: number }>("/tasks"); |
| 70 | + expect(result.count).toBe(3); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe("apiPost", () => { |
| 75 | + const originalFetch = globalThis.fetch; |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + globalThis.fetch = originalFetch; |
| 79 | + }); |
| 80 | + |
| 81 | + it("sends POST with JSON-stringified body", async () => { |
| 82 | + const mockFn = mock(() => |
| 83 | + Promise.resolve({ |
| 84 | + ok: true, |
| 85 | + json: () => Promise.resolve({ id: "task-1" }), |
| 86 | + }), |
| 87 | + ); |
| 88 | + globalThis.fetch = mockFn as unknown as typeof fetch; |
| 89 | + |
| 90 | + await apiPost("/tasks/task-1/start", { force: true }); |
| 91 | + |
| 92 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 93 | + const call = mockFn.mock.calls[0] as unknown as [string, RequestInit]; |
| 94 | + const [url, opts] = call; |
| 95 | + expect(url).toBe("/api/v1/tasks/task-1/start"); |
| 96 | + expect(opts.method).toBe("POST"); |
| 97 | + expect(opts.body).toBe(JSON.stringify({ force: true })); |
| 98 | + }); |
| 99 | +}); |
0 commit comments