|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import plugin from "../src/index.ts"; |
| 6 | +import { decorateErrorBody, errorHint } from "../src/hooks/errors.ts"; |
| 7 | + |
| 8 | +const URL_CC = "https://api.commandcode.ai/provider/v1/chat/completions"; |
| 9 | + |
| 10 | +function envelope(message: string, code?: string) { |
| 11 | + return JSON.stringify({ error: { message, type: "authentication_error", ...(code ? { code } : {}) } }); |
| 12 | +} |
| 13 | + |
| 14 | +function makeCtx() { |
| 15 | + const hooks: Record<string, ((event: any) => Promise<void>)[]> = { |
| 16 | + "http.request": [], |
| 17 | + "http.response": [], |
| 18 | + retry: [], |
| 19 | + }; |
| 20 | + const ctx: any = { |
| 21 | + session: { |
| 22 | + hook: async (name: any, cb: any) => { |
| 23 | + hooks[name]?.push(cb); |
| 24 | + return { dispose: async () => {} }; |
| 25 | + }, |
| 26 | + synthetic: async () => ({}), |
| 27 | + }, |
| 28 | + model: { transform: async () => {} }, |
| 29 | + tool: { transform: async () => {} }, |
| 30 | + command: { transform: async () => {} }, |
| 31 | + }; |
| 32 | + return { ctx, hooks }; |
| 33 | +} |
| 34 | + |
| 35 | +async function runResponse(hooks: Record<string, ((event: any) => Promise<void>)[]>, response: Response) { |
| 36 | + const event: any = { |
| 37 | + request: new Request(URL_CC, { method: "POST", body: "{}" }), |
| 38 | + sessionID: "ses_err", |
| 39 | + response, |
| 40 | + }; |
| 41 | + for (const cb of hooks["http.response"] ?? []) await cb(event); |
| 42 | + return event.response as Response; |
| 43 | +} |
| 44 | + |
| 45 | +describe("errorHint", () => { |
| 46 | + test("maps the actionable failures", () => { |
| 47 | + expect(errorHint(402, "You have insufficient credits")).toContain("COMMANDCODE_API_KEYS"); |
| 48 | + expect(errorHint(401, "Invalid 'Authorization' header")).toContain("key was rejected"); |
| 49 | + expect(errorHint(403, "Forbidden")).toContain("not entitled"); |
| 50 | + expect(errorHint(429, "rate limited")).toContain("rate limit"); |
| 51 | + expect(errorHint(400, "MODEL_NOT_IN_PLAN: GPT-5.5 available in Pro")).toContain("not in your plan"); |
| 52 | + expect(errorHint(400, "unsupported_model")).toContain("/messages"); |
| 53 | + }); |
| 54 | + |
| 55 | + test("leaves unrelated failures alone", () => { |
| 56 | + expect(errorHint(500, "internal error")).toBe(""); |
| 57 | + expect(errorHint(400, "invalid request error")).toBe(""); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("decorateErrorBody", () => { |
| 62 | + test("keeps the envelope and appends the hint", () => { |
| 63 | + const decorated = decorateErrorBody(envelope("You have insufficient credits", "UNAUTHORIZED"), 402)!; |
| 64 | + const parsed = JSON.parse(decorated.body); |
| 65 | + expect(decorated.status).toBe(402); |
| 66 | + expect(decorated.code).toBe("UNAUTHORIZED"); |
| 67 | + expect(parsed.error.message).toContain("You have insufficient credits"); |
| 68 | + expect(parsed.error.message).toContain("add credits"); |
| 69 | + expect(parsed.error.type).toBe("authentication_error"); |
| 70 | + }); |
| 71 | + |
| 72 | + test("handles a flat message envelope", () => { |
| 73 | + const decorated = decorateErrorBody(JSON.stringify({ message: "rate limited" }), 429)!; |
| 74 | + expect(JSON.parse(decorated.body).message).toContain("rate limit or plan window"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("returns null for anything it cannot improve", () => { |
| 78 | + expect(decorateErrorBody("", 402)).toBeNull(); |
| 79 | + expect(decorateErrorBody("not json", 402)).toBeNull(); |
| 80 | + expect(decorateErrorBody(JSON.stringify({ ok: true }), 402)).toBeNull(); |
| 81 | + expect(decorateErrorBody(envelope("internal error"), 500)).toBeNull(); |
| 82 | + expect(decorateErrorBody(JSON.stringify({ error: { message: "invalid request error" } }), 400)).toBeNull(); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("plugin error decoration", () => { |
| 87 | + let stateDir: string; |
| 88 | + const previous = process.env.COMMANDCODE_STATE_DIR; |
| 89 | + |
| 90 | + beforeEach(() => { |
| 91 | + stateDir = mkdtempSync(join(tmpdir(), "cc-err-")); |
| 92 | + process.env.COMMANDCODE_STATE_DIR = stateDir; |
| 93 | + }); |
| 94 | + |
| 95 | + afterEach(() => { |
| 96 | + if (previous === undefined) delete process.env.COMMANDCODE_STATE_DIR; |
| 97 | + else process.env.COMMANDCODE_STATE_DIR = previous; |
| 98 | + try { |
| 99 | + rmSync(stateDir, { recursive: true, force: true }); |
| 100 | + } catch {} |
| 101 | + }); |
| 102 | + |
| 103 | + test("rewrites a JSON 402 with a hint and records it", async () => { |
| 104 | + const { ctx, hooks } = makeCtx(); |
| 105 | + await plugin.setup(ctx); |
| 106 | + |
| 107 | + const response = new Response(envelope("You have insufficient credits", "UNAUTHORIZED"), { |
| 108 | + status: 402, |
| 109 | + headers: { "content-type": "application/json" }, |
| 110 | + }); |
| 111 | + const outgoing = await runResponse(hooks, response); |
| 112 | + |
| 113 | + expect(outgoing.status).toBe(402); |
| 114 | + const body = JSON.parse(await outgoing.text()); |
| 115 | + expect(body.error.message).toContain("add credits"); |
| 116 | + |
| 117 | + const log = readFileSync(join(stateDir, "commandcode-errors.log"), "utf8"); |
| 118 | + expect(log).toContain("insufficient credits"); |
| 119 | + }); |
| 120 | + |
| 121 | + test("never rewrites a successful JSON body", async () => { |
| 122 | + const { ctx, hooks } = makeCtx(); |
| 123 | + await plugin.setup(ctx); |
| 124 | + const payload = JSON.stringify({ choices: [{ message: { content: "ok" } }] }); |
| 125 | + const response = new Response(payload, { status: 200, headers: { "content-type": "application/json" } }); |
| 126 | + const outgoing = await runResponse(hooks, response); |
| 127 | + expect(await outgoing.text()).toBe(payload); |
| 128 | + }); |
| 129 | + |
| 130 | + test("never rewrites an SSE stream", async () => { |
| 131 | + const { ctx, hooks } = makeCtx(); |
| 132 | + await plugin.setup(ctx); |
| 133 | + const response = new Response("data: {}\n\n", { |
| 134 | + status: 429, |
| 135 | + headers: { "content-type": "text/event-stream" }, |
| 136 | + }); |
| 137 | + const outgoing = await runResponse(hooks, response); |
| 138 | + const text = await outgoing.text(); |
| 139 | + expect(text).toBe("data: {}\n\n"); |
| 140 | + }); |
| 141 | +}); |
0 commit comments