454 lines
17 KiB
JavaScript
454 lines
17 KiB
JavaScript
import { describe, it, expect, vi } from "vitest";
|
|
|
|
// Module-level mock capture for buildDebugIssuePrompt integration testing.
|
|
const buildDebugIssuePromptCalls = [];
|
|
|
|
vi.mock("../../src/prompts/debug-issue.js", async () => ({
|
|
buildDebugIssuePrompt: vi.fn((input) => {
|
|
buildDebugIssuePromptCalls.push(input);
|
|
return "mocked debug issue prompt";
|
|
}),
|
|
}));
|
|
|
|
// Import after the mock is registered (hoisted by vitest).
|
|
const { handleDebugIssue } = await import("../../src/tools/debug-issue.js");
|
|
|
|
const mockConfig = {
|
|
openaiApiKey: "sk-test-key",
|
|
openaiModel: "gpt-5.1",
|
|
temperature: 0.2,
|
|
maxOutputTokens: 2000,
|
|
logLevel: "info",
|
|
enableFileContext: false,
|
|
contextDir: "./context",
|
|
maxInputChars: 30000,
|
|
maxFileChars: 12000,
|
|
maxFiles: 5,
|
|
maxLogChars: 10000,
|
|
redactSecrets: true,
|
|
};
|
|
|
|
function makeValidInput(question) {
|
|
return { question };
|
|
}
|
|
|
|
// --- Success path ---
|
|
|
|
describe("success path", () => {
|
|
it("returns ok:true with answer on full happy flow", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("Debug my issue"), {
|
|
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
|
});
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(result.answer).toBe("OK");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("propagates budget warnings through to success result", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const trimmedBudget = { ...mockConfig, maxInputChars: 50 };
|
|
const loadConfig = vi.fn(() => trimmedBudget);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), {
|
|
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
|
});
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- Validation failure (short-circuit before config) ---
|
|
|
|
describe("validation failure", () => {
|
|
it("returns structured error when question is missing", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn();
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue({}, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("short-circuits — no other deps called", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn();
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
await handleDebugIssue({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(loadConfig).not.toHaveBeenCalled();
|
|
expect(createOpenAIClient).not.toHaveBeenCalled();
|
|
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns structured error when question is empty string", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn();
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("returns structured error when question is wrong type", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn();
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
});
|
|
|
|
// --- Config failure ---
|
|
|
|
describe("config failure", () => {
|
|
it("returns structured error when loadConfig throws", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); });
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toContain("OPENAI_API_KEY is missing");
|
|
});
|
|
|
|
it("short-circuits — no client or response calls after config failure", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(createOpenAIClient).not.toHaveBeenCalled();
|
|
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes original error message", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); });
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
|
});
|
|
});
|
|
|
|
// --- Budget failure (short-circuit before prompt/client) ---
|
|
|
|
describe("budget failure", () => {
|
|
it("returns structured error with budget warnings when input exceeds budget", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => tinyBudget);
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
|
|
it("short-circuits — no prompt built, no client created, no response sent", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => tinyBudget);
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(createOpenAIClient).not.toHaveBeenCalled();
|
|
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes budget warnings through to the error result", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => tinyBudget);
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(
|
|
{ question: "x", context: "a".repeat(20) },
|
|
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
|
);
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- Client creation failure ---
|
|
|
|
describe("client creation failure", () => {
|
|
it("returns structured error when createOpenAIClient throws", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("short-circuits — no response sent after client failure", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes original error message unchanged", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.error).toBe("Error: Invalid config.");
|
|
});
|
|
});
|
|
|
|
// --- OpenAI failure (pass-through) ---
|
|
|
|
describe("OpenAI failure", () => {
|
|
it("passes through err.message unchanged", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toBe("Error: API key invalid.");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("short-circuits — no extra processing after API failure", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit"));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toBe("Error: rate limit");
|
|
});
|
|
|
|
it("does not wrap or reformat the error", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.error).toBe("Error: 429 Too Many Requests");
|
|
});
|
|
});
|
|
|
|
// --- Dependency call order ---
|
|
|
|
describe("dependency call order", () => {
|
|
it("calls deps in correct order: config -> client -> response", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const callLog = [];
|
|
const loadConfig = vi.fn(() => { callLog.push("config"); return mockConfig; });
|
|
const createOpenAIClient = vi.fn(() => { callLog.push("client"); return { responses: { create: vi.fn() } }; });
|
|
const sendOpenAIResponse = vi.fn(async () => { callLog.push("response"); return { content: "OK" }; });
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(callLog).toEqual(["config", "client", "response"]);
|
|
});
|
|
});
|
|
|
|
// --- buildDebugIssuePrompt integration ---
|
|
|
|
describe("buildDebugIssuePrompt integration", () => {
|
|
it("calls buildDebugIssuePrompt and receives budget.input as argument", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
|
|
|
await handleDebugIssue(makeValidInput("test question"), {
|
|
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
|
});
|
|
|
|
// Verify buildDebugIssuePrompt was called (once) with the trimmed input from checkContextBudget.
|
|
expect(buildDebugIssuePromptCalls.length).toBe(1);
|
|
const captured = buildDebugIssuePromptCalls[0];
|
|
expect(typeof captured).toBe("object");
|
|
expect(captured.question).toBe("test question");
|
|
});
|
|
});
|
|
|
|
// --- No throws escaping ---
|
|
|
|
describe("no throws escaping", () => {
|
|
it("returns structured result when sendOpenAIResponse throws null", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn().mockRejectedValue(null);
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
});
|
|
|
|
it("returns structured result when loadConfig throws non-Error", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => { throw "string error"; });
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
});
|
|
});
|
|
|
|
// --- Warning propagation ---
|
|
|
|
describe("warning propagation", () => {
|
|
it("includes budget warnings in success result when budget passes with warnings", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const trimmedBudget = { ...mockConfig, maxInputChars: 50 };
|
|
const loadConfig = vi.fn(() => trimmedBudget);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
|
|
|
const result = await handleDebugIssue(
|
|
{ question: "hi", context: "x".repeat(49) },
|
|
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
|
);
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
|
|
it("includes budget warnings in failure result when budget fails", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const emptyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => emptyBudget);
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(result.ok).toBe(false);
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- Result shape ---
|
|
|
|
describe("result shape", () => {
|
|
it("returns exactly { ok, answer, warnings } on success", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(Object.keys(result).sort()).toEqual(["answer", "ok", "warnings"]);
|
|
expect(result.ok).toBe(true);
|
|
expect(typeof result.answer).toBe("string");
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
|
|
it("returns exactly { ok, error, warnings } on failure", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(Object.keys(result).sort()).toEqual(["error", "ok", "warnings"]);
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- Short-circuit behavior ---
|
|
|
|
describe("short-circuit behavior", () => {
|
|
it("stops at first failure without calling downstream deps", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
|
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("boom"));
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(loadConfig).toHaveBeenCalledTimes(1);
|
|
expect(createOpenAIClient).toHaveBeenCalledTimes(1);
|
|
expect(sendOpenAIResponse).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("stops at config failure without calling downstream deps", async () => {
|
|
buildDebugIssuePromptCalls.length = 0;
|
|
|
|
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
|
const createOpenAIClient = vi.fn();
|
|
const sendOpenAIResponse = vi.fn();
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
|
expect(loadConfig).toHaveBeenCalledTimes(1);
|
|
expect(createOpenAIClient).not.toHaveBeenCalled();
|
|
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
|
});
|
|
});
|