import { http, HttpResponse } from "msw"; import { server } from "../test/msw"; import { asPiManagementApiError, getPiManagementLogs, getPiManagementOptions, getPiManagementStatus, runPiManagementTest, savePiManagementConfig, } from "./pi-management"; test("Pi management client calls only the sanctioned sanitized endpoints", async () => { const calls: Array<{ method: string; path: string; body?: unknown }> = []; server.use( http.get("/api/pi-management/status", ({ request }) => { calls.push({ method: request.method, path: new URL(request.url).pathname }); return HttpResponse.json({ ready: true, version: "0.80.3", credentials: "present", config: {}, checkedAt: "2026-08-05T10:00:00.000Z" }); }), http.get("/api/pi-management/options", ({ request }) => { calls.push({ method: request.method, path: new URL(request.url).pathname }); return HttpResponse.json({ providers: ["zai"], models: [{ provider: "zai", id: "glm-5.2" }], reasoning: ["low", "medium", "high"], checkedAt: "2026-08-05T10:00:00.000Z" }); }), http.put("/api/pi-management/config", async ({ request }) => { calls.push({ method: request.method, path: new URL(request.url).pathname, body: await request.json() }); return HttpResponse.json({ provider: "zai", model: "glm-5.2", reasoning: "high", updatedAt: "2026-08-05T10:00:00.000Z" }); }), http.post("/api/pi-management/test", ({ request }) => { calls.push({ method: request.method, path: new URL(request.url).pathname }); return HttpResponse.json({ ready: true, checkedAt: "2026-08-05T10:00:00.000Z" }); }), http.get("/api/pi-management/logs", ({ request }) => { calls.push({ method: request.method, path: new URL(request.url).pathname }); return HttpResponse.json({ lines: ["Pi smoke check succeeded"], checkedAt: "2026-08-05T10:00:00.000Z" }); }), ); await expect(getPiManagementStatus()).resolves.toMatchObject({ version: "0.80.3", ready: true, credentials: "present" }); await expect(getPiManagementOptions()).resolves.toMatchObject({ providers: ["zai"] }); await expect(savePiManagementConfig({ provider: "zai", model: "glm-5.2", reasoning: "high" })).resolves.toMatchObject({ reasoning: "high" }); await expect(runPiManagementTest()).resolves.toMatchObject({ ready: true }); await expect(getPiManagementLogs()).resolves.toMatchObject({ lines: ["Pi smoke check succeeded"] }); expect(calls).toEqual([ { method: "GET", path: "/api/pi-management/status" }, { method: "GET", path: "/api/pi-management/options" }, { method: "PUT", path: "/api/pi-management/config", body: { provider: "zai", model: "glm-5.2", reasoning: "high" } }, { method: "POST", path: "/api/pi-management/test" }, { method: "GET", path: "/api/pi-management/logs" }, ]); expect(calls.some((call) => /update|terminal|shell/i.test(call.path))).toBe(false); }); test("Pi management client exposes the stable forbidden message without raw response text", async () => { server.use(http.get("/api/pi-management/status", () => HttpResponse.json({ code: "pi_management_forbidden", error: "Pi management is not permitted", raw: "token=do-not-show" }, { status: 403 }), )); await expect(getPiManagementStatus()).rejects.toSatisfy((error: unknown) => { expect(asPiManagementApiError(error)).toEqual({ status: 403, code: "pi_management_forbidden", message: "Pi management is not permitted", }); expect(asPiManagementApiError(error)?.message).not.toContain("token"); return true; }); });