import { afterEach, describe, expect, it, vi } from "vitest"; import { ApiClient, ApiError } from "./client"; afterEach(() => { vi.unstubAllGlobals(); }); describe("ApiClient", () => { it("preserves HTTP status on failed requests", async () => { vi.stubGlobal( "fetch", vi.fn().mockResolvedValue( new Response(JSON.stringify({ error: "workspace slug already exists" }), { status: 409, statusText: "Conflict", headers: { "Content-Type": "application/json" }, }), ), ); const client = new ApiClient("https://api.example.test"); try { await client.createWorkspace({ name: "Test", slug: "test" }); throw new Error("expected createWorkspace to fail"); } catch (error) { expect(error).toBeInstanceOf(ApiError); expect(error).toMatchObject({ message: "workspace slug already exists", status: 409, statusText: "Conflict", }); } }); });