/** * @file Pins the codexConfig API routes and payloads. Tests that the client * sends the right method, URL, and body shape to the Codex config backend. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, vi, afterEach } from "vitest"; import { api } from "../api"; afterEach(() => { vi.unstubAllGlobals(); }); function stubFetch(response: unknown) { const spy = vi.fn().mockResolvedValue({ ok: true, status: 200, headers: { get: () => "application/json" }, json: async () => response, text: async () => JSON.stringify(response), }); vi.stubGlobal("fetch", spy); return spy; } describe("api.codexConfig", () => { it("sends a write as a single PUT with the artifact envelope", async () => { const spy = stubFetch({ ok: true, file: "/x", created: true }); await api.codexConfig.write({ scope: "user", type: "rules", name: "default", content: "allow", }); expect(spy).toHaveBeenCalledTimes(1); const [, options] = spy.mock.calls[0] as [string, RequestInit | undefined]; expect(options?.method).toBe("PUT"); expect(JSON.parse(options?.body as string)).toEqual({ scope: "user", type: "rules", name: "default", content: "allow", }); }); });