import { describe, expect, it, vi } from "vitest"; import { RemoteMachineClient } from "./machineClient.js"; describe("RemoteMachineClient", () => { it("forwards raw binary request bodies with the provided content type", async () => { const fetchImpl = vi.fn(() => Promise.resolve(new Response("ok", { status: 200 }))); const client = new RemoteMachineClient({ baseUrl: "https://remote.example.test/" }, fetchImpl); const payload = Buffer.from([0x89, 0x50, 0x4e, 0x47]); await client.request("PUT", "/api/projects/p1/workspaces/w1/file?path=image.png", payload, { contentType: "image/png" }); const { input, init } = onlyFetchCall(fetchImpl); expect(fetchInputUrl(input)).toBe("https://remote.example.test/api/projects/p1/workspaces/w1/file?path=image.png"); expect(init.method).toBe("PUT"); expect(new Headers(init.headers).get("content-type")).toBe("image/png"); if (!(init.body instanceof ArrayBuffer)) throw new Error("Expected binary request body"); expect(Array.from(new Uint8Array(init.body))).toEqual([0x89, 0x50, 0x4e, 0x47]); }); it("serializes structured request bodies as JSON by default", async () => { const fetchImpl = vi.fn(() => Promise.resolve(new Response("ok", { status: 200 }))); const client = new RemoteMachineClient({ baseUrl: "https://remote.example.test/base/", token: "secret" }, fetchImpl); await client.request("POST", "/api/sessions", { cwd: "/repo" }); const { input, init } = onlyFetchCall(fetchImpl); expect(fetchInputUrl(input)).toBe("https://remote.example.test/base/api/sessions"); expect(new Headers(init.headers).get("authorization")).toBe("Bearer secret"); expect(new Headers(init.headers).get("content-type")).toBe("application/json"); expect(init.body).toBe(JSON.stringify({ cwd: "/repo" })); }); }); function fetchInputUrl(input: RequestInfo | URL): string { if (typeof input === "string") return input; if (input instanceof URL) return input.href; return input.url; } function onlyFetchCall(fetchImpl: ReturnType>): { input: RequestInfo | URL; init: RequestInit } { expect(fetchImpl).toHaveBeenCalledTimes(1); const call = fetchImpl.mock.calls[0]; if (call === undefined) throw new Error("Expected fetch call"); const [input, init] = call; if (init === undefined) throw new Error("Expected fetch init"); return { input, init }; }