376 lines
14 KiB
JavaScript
376 lines
14 KiB
JavaScript
import { describe, it, expect, vi } from "vitest";
|
|
import { handleDebugIssue } from "../../src/tools/debug-issue.js";
|
|
|
|
const mockConfig = {
|
|
openaiApiKey: "sk-test-key",
|
|
openaiModel: "gpt-5.1",
|
|
temperature: 0.2,
|
|
maxOutputTokens: 4000,
|
|
logLevel: "info",
|
|
enableFileContext: false,
|
|
contextDir: "./context",
|
|
maxInputChars: 30000,
|
|
maxFileChars: 12000,
|
|
maxFiles: 5,
|
|
maxLogChars: 10000,
|
|
redactSecrets: true,
|
|
};
|
|
|
|
function makeValidInput(description, context) {
|
|
return { question: description || "App crashes on login", context: context || "x" };
|
|
}
|
|
|
|
// --- Success path ---
|
|
|
|
describe("success path", () => {
|
|
it("returns ok:true with answer on full happy flow", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("App crashes on login", "x"), {
|
|
loadConfig, createProvider,
|
|
});
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(result.answer).toBe("OK");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("propagates budget warnings through to success result", async () => {
|
|
const trimmedBudget = { ...mockConfig, maxInputChars: 50 };
|
|
const loadConfig = vi.fn(() => trimmedBudget);
|
|
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("App crashes on login", "x"), {
|
|
loadConfig, createProvider,
|
|
});
|
|
|
|
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 description is missing", async () => {
|
|
const loadConfig = vi.fn();
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue({}, { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("returns structured error when question is missing", async () => {
|
|
const loadConfig = vi.fn();
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue({}, { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("short-circuits — no other deps called", async () => {
|
|
const loadConfig = vi.fn();
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
await handleDebugIssue({ foo: "bar" }, { loadConfig, createProvider });
|
|
expect(loadConfig).not.toHaveBeenCalled();
|
|
expect(createProvider).not.toHaveBeenCalled();
|
|
expect(sendMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns structured error when description is empty string", async () => {
|
|
const loadConfig = vi.fn();
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue({ question: "", context: "x" }, { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("returns structured error when description is wrong type", async () => {
|
|
const loadConfig = vi.fn();
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue({ question: 123, context: "x" }, { loadConfig, createProvider });
|
|
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 () => {
|
|
const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); });
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toContain("OPENAI_API_KEY is missing");
|
|
});
|
|
|
|
it("short-circuits — no provider or send calls after config failure", async () => {
|
|
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(createProvider).not.toHaveBeenCalled();
|
|
expect(sendMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes original error message", async () => {
|
|
const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); });
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
|
});
|
|
});
|
|
|
|
// --- Budget failure (short-circuit before prompt/provider) ---
|
|
|
|
describe("budget failure", () => {
|
|
it("returns structured error with budget warnings when input exceeds budget", async () => {
|
|
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => tinyBudget);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
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 provider created, no send called", async () => {
|
|
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => tinyBudget);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(createProvider).not.toHaveBeenCalled();
|
|
expect(sendMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes budget warnings through to the error result", async () => {
|
|
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => tinyBudget);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(
|
|
{ question: "x", context: "a".repeat(20) },
|
|
{ loadConfig, createProvider },
|
|
);
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// --- Provider creation failure ---
|
|
|
|
describe("provider creation failure", () => {
|
|
it("returns structured error when createProvider throws", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("short-circuits — no send called after provider creation failure", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(sendMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes original error message unchanged", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.error).toBe("Error: Invalid config.");
|
|
});
|
|
});
|
|
|
|
// --- Provider send failure (pass-through) ---
|
|
|
|
describe("provider send failure", () => {
|
|
it("passes through err.message unchanged", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toBe("Error: API key invalid.");
|
|
expect(result.warnings).toEqual([]);
|
|
});
|
|
|
|
it("short-circuits — no extra processing after provider send failure", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn().mockRejectedValue(new Error("rate limit"));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(result.error).toBe("Error: rate limit");
|
|
});
|
|
|
|
it("does not wrap or reformat the error", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.error).toBe("Error: 429 Too Many Requests");
|
|
});
|
|
});
|
|
|
|
// --- Dependency call order ---
|
|
|
|
describe("dependency call order", () => {
|
|
it("calls deps in correct order: config -> provider -> send", async () => {
|
|
const callLog = [];
|
|
const loadConfig = vi.fn(() => { callLog.push("config"); return mockConfig; });
|
|
const createProvider = vi.fn(() => { callLog.push("provider"); return { send: vi.fn().mockImplementation(async () => { callLog.push("send"); return { content: "OK" }; }) }; });
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(callLog).toEqual(["config", "provider", "send"]);
|
|
});
|
|
});
|
|
|
|
// --- No throws escaping ---
|
|
|
|
describe("no throws escaping", () => {
|
|
it("returns structured result when send throws null", async () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn().mockRejectedValue(null);
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(result.ok).toBe(false);
|
|
expect(typeof result.error).toBe("string");
|
|
});
|
|
|
|
it("returns structured result when loadConfig throws non-Error", async () => {
|
|
const loadConfig = vi.fn(() => { throw "string error"; });
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
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 () => {
|
|
const trimmedBudget = { ...mockConfig, maxInputChars: 50 };
|
|
const loadConfig = vi.fn(() => trimmedBudget);
|
|
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(
|
|
{ question: "hi", context: "x".repeat(49) },
|
|
{ loadConfig, createProvider },
|
|
);
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(Array.isArray(result.warnings)).toBe(true);
|
|
});
|
|
|
|
it("includes budget warnings in failure result when budget fails", async () => {
|
|
const emptyBudget = { ...mockConfig, maxInputChars: 0 };
|
|
const loadConfig = vi.fn(() => emptyBudget);
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
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 () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
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 () => {
|
|
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
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 () => {
|
|
const loadConfig = vi.fn(() => mockConfig);
|
|
const sendMock = vi.fn().mockRejectedValue(new Error("boom"));
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(loadConfig).toHaveBeenCalledTimes(1);
|
|
expect(createProvider).toHaveBeenCalledTimes(1);
|
|
expect(sendMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("stops at config failure without calling downstream deps", async () => {
|
|
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
|
const sendMock = vi.fn();
|
|
const createProvider = vi.fn(() => ({ send: sendMock }));
|
|
|
|
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
|
expect(loadConfig).toHaveBeenCalledTimes(1);
|
|
expect(createProvider).not.toHaveBeenCalled();
|
|
expect(sendMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|