feat: add provider abstraction for review backends
This commit is contained in:
@@ -1,23 +1,11 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
// Module-level mock capture for buildArchitectureReviewPrompt integration testing.
|
||||
const buildArchitectureReviewPromptCalls = [];
|
||||
|
||||
vi.mock("../../src/prompts/architecture-review.js", async () => ({
|
||||
buildArchitectureReviewPrompt: vi.fn((input) => {
|
||||
buildArchitectureReviewPromptCalls.push(input);
|
||||
return "mocked architecture review prompt";
|
||||
}),
|
||||
}));
|
||||
|
||||
// Import after the mock is registered (hoisted by vitest).
|
||||
const { handleArchitectureReview } = await import("../../src/tools/architecture-review.js");
|
||||
import { handleArchitectureReview } from "../../src/tools/architecture-review.js";
|
||||
|
||||
const mockConfig = {
|
||||
openaiApiKey: "sk-test-key",
|
||||
openaiModel: "gpt-5.1",
|
||||
temperature: 0.2,
|
||||
maxOutputTokens: 2000,
|
||||
maxOutputTokens: 4000,
|
||||
logLevel: "info",
|
||||
enableFileContext: false,
|
||||
contextDir: "./context",
|
||||
@@ -28,22 +16,20 @@ const mockConfig = {
|
||||
redactSecrets: true,
|
||||
};
|
||||
|
||||
function makeValidInput(question) {
|
||||
return { question };
|
||||
function makeValidInput(question, context) {
|
||||
return { question: question || "Review architecture", context: context || "x" };
|
||||
}
|
||||
|
||||
// --- Success path ---
|
||||
|
||||
describe("success path", () => {
|
||||
it("returns ok:true with answer on full happy flow", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("What architecture should we use?"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleArchitectureReview(makeValidInput("Review architecture", "x"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -52,15 +38,13 @@ describe("success path", () => {
|
||||
});
|
||||
|
||||
it("propagates budget warnings through to success result", async () => {
|
||||
buildArchitectureReviewPromptCalls.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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleArchitectureReview(makeValidInput("Review architecture", "x"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -72,52 +56,44 @@ describe("success path", () => {
|
||||
|
||||
describe("validation failure", () => {
|
||||
it("returns structured error when question is missing", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview({}, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview({}, { 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 () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleArchitectureReview({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleArchitectureReview({ foo: "bar" }, { loadConfig, createProvider });
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns structured error when question is empty string", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview({ 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 question is wrong type", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview({ question: 123, context: "x" }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -128,82 +104,70 @@ describe("validation failure", () => {
|
||||
|
||||
describe("config failure", () => {
|
||||
it("returns structured error when loadConfig throws", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no provider or send calls after config failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes original error message", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => { throw new Error("OPENAI_API_KEY is missing."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Budget failure (short-circuit before prompt/client) ---
|
||||
// --- Budget failure (short-circuit before prompt/provider) ---
|
||||
|
||||
describe("budget failure", () => {
|
||||
it("returns structured error with budget warnings when input exceeds budget", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(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 client created, no response sent", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no prompt built, no provider created, no send called", async () => {
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes budget warnings through to the error result", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(
|
||||
{ question: "x", context: "a".repeat(20) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
@@ -211,81 +175,69 @@ describe("budget failure", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// --- Client creation failure ---
|
||||
|
||||
describe("client creation failure", () => {
|
||||
it("returns structured error when createOpenAIClient throws", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
// --- Provider creation failure ---
|
||||
|
||||
describe("provider creation failure", () => {
|
||||
it("returns structured error when createProvider throws", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no send called after provider creation failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes original error message unchanged", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: Invalid config.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- OpenAI failure (pass-through) ---
|
||||
// --- Provider send failure (pass-through) ---
|
||||
|
||||
describe("OpenAI failure", () => {
|
||||
describe("provider send failure", () => {
|
||||
it("passes through err.message unchanged", async () => {
|
||||
buildArchitectureReviewPromptCalls.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 sendMock = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(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 API failure", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no extra processing after provider send failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(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 () => {
|
||||
buildArchitectureReviewPromptCalls.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 sendMock = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: 429 Too Many Requests");
|
||||
});
|
||||
});
|
||||
@@ -293,64 +245,35 @@ describe("OpenAI failure", () => {
|
||||
// --- Dependency call order ---
|
||||
|
||||
describe("dependency call order", () => {
|
||||
it("calls deps in correct order: config -> client -> response", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
it("calls deps in correct order: config -> provider -> send", async () => {
|
||||
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" }; });
|
||||
const createProvider = vi.fn(() => { callLog.push("provider"); return { send: vi.fn().mockImplementation(async () => { callLog.push("send"); return { content: "OK" }; }) }; });
|
||||
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(callLog).toEqual(["config", "client", "response"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- buildArchitectureReviewPrompt integration ---
|
||||
|
||||
describe("buildArchitectureReviewPrompt integration", () => {
|
||||
it("calls buildArchitectureReviewPrompt and receives budget.input as argument", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
|
||||
await handleArchitectureReview(makeValidInput("test question"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
});
|
||||
|
||||
// Verify buildArchitectureReviewPrompt was called (once) with the trimmed input from checkContextBudget.
|
||||
expect(buildArchitectureReviewPromptCalls.length).toBe(1);
|
||||
const captured = buildArchitectureReviewPromptCalls[0];
|
||||
expect(typeof captured).toBe("object");
|
||||
expect(captured.question).toBe("test question");
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(callLog).toEqual(["config", "provider", "send"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- No throws escaping ---
|
||||
|
||||
describe("no throws escaping", () => {
|
||||
it("returns structured result when sendOpenAIResponse throws null", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
it("returns structured result when send throws null", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(null);
|
||||
const sendMock = vi.fn().mockRejectedValue(null);
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(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 () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => { throw "string error"; });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
});
|
||||
@@ -360,16 +283,14 @@ describe("no throws escaping", () => {
|
||||
|
||||
describe("warning propagation", () => {
|
||||
it("includes budget warnings in success result when budget passes with warnings", async () => {
|
||||
buildArchitectureReviewPromptCalls.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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(
|
||||
{ question: "hi", context: "x".repeat(49) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -377,14 +298,12 @@ describe("warning propagation", () => {
|
||||
});
|
||||
|
||||
it("includes budget warnings in failure result when budget fails", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const emptyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => emptyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(Array.isArray(result.warnings)).toBe(true);
|
||||
});
|
||||
@@ -394,13 +313,11 @@ describe("warning propagation", () => {
|
||||
|
||||
describe("result shape", () => {
|
||||
it("returns exactly { ok, answer, warnings } on success", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["answer", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(true);
|
||||
expect(typeof result.answer).toBe("string");
|
||||
@@ -408,13 +325,11 @@ describe("result shape", () => {
|
||||
});
|
||||
|
||||
it("returns exactly { ok, error, warnings } on failure", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["error", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
@@ -426,28 +341,24 @@ describe("result shape", () => {
|
||||
|
||||
describe("short-circuit behavior", () => {
|
||||
it("stops at first failure without calling downstream deps", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).toHaveBeenCalledTimes(1);
|
||||
expect(sendOpenAIResponse).toHaveBeenCalledTimes(1);
|
||||
expect(createProvider).toHaveBeenCalledTimes(1);
|
||||
expect(sendMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("stops at config failure without calling downstream deps", async () => {
|
||||
buildArchitectureReviewPromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleArchitectureReview(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
+104
-108
@@ -25,11 +25,11 @@ function makeValidInput(question) {
|
||||
describe("success path", () => {
|
||||
it("returns ok:true with answer on full happy flow", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("What is 2+2?"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -40,15 +40,14 @@ describe("success path", () => {
|
||||
it("propagates budget warnings through to success result", async () => {
|
||||
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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
// Budget warnings may or may not be present depending on exact input size vs budget.
|
||||
expect(Array.isArray(result.warnings)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -58,10 +57,10 @@ describe("success path", () => {
|
||||
describe("validation failure", () => {
|
||||
it("returns structured error when question is missing", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt({}, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt({}, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -69,21 +68,21 @@ describe("validation failure", () => {
|
||||
|
||||
it("short-circuits — no other deps called", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleAskChatGpt({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleAskChatGpt({ foo: "bar" }, { loadConfig, createProvider });
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns structured error when question is empty string", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt({ question: "" }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -91,10 +90,10 @@ describe("validation failure", () => {
|
||||
|
||||
it("returns structured error when question is wrong type", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt({ question: 123 }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -106,70 +105,69 @@ describe("validation 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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("short-circuits — no provider or send calls after config failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleAskChatGpt(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Budget failure (short-circuit before prompt/client) ---
|
||||
// --- 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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(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 client created, no response sent", async () => {
|
||||
it("short-circuits — no prompt built, no provider created, no send called", async () => {
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleAskChatGpt(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
// maxInputChars=0 forces rejection even with minimal input.
|
||||
const result = await handleAskChatGpt(
|
||||
{ question: "x", context: "a".repeat(20) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
@@ -177,69 +175,69 @@ describe("budget failure", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// --- Client creation failure ---
|
||||
// --- Provider creation failure ---
|
||||
|
||||
describe("client creation failure", () => {
|
||||
it("returns structured error when createOpenAIClient throws", async () => {
|
||||
describe("provider creation failure", () => {
|
||||
it("returns structured error when createProvider throws", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("short-circuits — no send called after provider creation failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes original error message unchanged", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: Invalid config.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- OpenAI failure (pass-through) ---
|
||||
// --- Provider send failure (pass-through) ---
|
||||
|
||||
describe("OpenAI failure", () => {
|
||||
describe("provider send failure", () => {
|
||||
it("passes through err.message unchanged", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(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 API failure", async () => {
|
||||
it("short-circuits — no extra processing after provider send failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(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 createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: 429 Too Many Requests");
|
||||
});
|
||||
});
|
||||
@@ -247,36 +245,35 @@ describe("OpenAI failure", () => {
|
||||
// --- Dependency call order ---
|
||||
|
||||
describe("dependency call order", () => {
|
||||
it("calls deps in correct order: config -> client -> response", async () => {
|
||||
it("calls deps in correct order: config -> provider -> send", async () => {
|
||||
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" }; });
|
||||
const createProvider = vi.fn(() => { callLog.push("provider"); return { send: vi.fn().mockImplementation(async () => { callLog.push("send"); return { content: "OK" }; }) }; });
|
||||
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(callLog).toEqual(["config", "client", "response"]);
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(callLog).toEqual(["config", "provider", "send"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- No throws escaping ---
|
||||
|
||||
describe("no throws escaping", () => {
|
||||
it("returns structured result when sendOpenAIResponse throws null", async () => {
|
||||
it("returns structured result when send throws null", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(null);
|
||||
const sendMock = vi.fn().mockRejectedValue(null);
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
});
|
||||
@@ -288,13 +285,12 @@ 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 createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
// question("hi") + context ~49 chars should hit the budget trim.
|
||||
const result = await handleAskChatGpt(
|
||||
{ question: "hi", context: "x".repeat(49) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -304,10 +300,10 @@ describe("warning propagation", () => {
|
||||
it("includes budget warnings in failure result when budget fails", async () => {
|
||||
const emptyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => emptyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(Array.isArray(result.warnings)).toBe(true);
|
||||
});
|
||||
@@ -318,10 +314,10 @@ describe("warning propagation", () => {
|
||||
describe("result shape", () => {
|
||||
it("returns exactly { ok, answer, warnings } on success", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["answer", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(true);
|
||||
expect(typeof result.answer).toBe("string");
|
||||
@@ -330,10 +326,10 @@ describe("result shape", () => {
|
||||
|
||||
it("returns exactly { ok, error, warnings } on failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["error", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
@@ -346,23 +342,23 @@ describe("result shape", () => {
|
||||
describe("short-circuit behavior", () => {
|
||||
it("stops at first failure without calling downstream deps", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).toHaveBeenCalledTimes(1);
|
||||
expect(sendOpenAIResponse).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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleAskChatGpt(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
+124
-202
@@ -1,23 +1,11 @@
|
||||
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");
|
||||
import { handleDebugIssue } from "../../src/tools/debug-issue.js";
|
||||
|
||||
const mockConfig = {
|
||||
openaiApiKey: "sk-test-key",
|
||||
openaiModel: "gpt-5.1",
|
||||
temperature: 0.2,
|
||||
maxOutputTokens: 2000,
|
||||
maxOutputTokens: 4000,
|
||||
logLevel: "info",
|
||||
enableFileContext: false,
|
||||
contextDir: "./context",
|
||||
@@ -28,22 +16,20 @@ const mockConfig = {
|
||||
redactSecrets: true,
|
||||
};
|
||||
|
||||
function makeValidInput(question) {
|
||||
return { question };
|
||||
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 () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("Debug my issue"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleDebugIssue(makeValidInput("App crashes on login", "x"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -52,15 +38,13 @@ describe("success path", () => {
|
||||
});
|
||||
|
||||
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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleDebugIssue(makeValidInput("App crashes on login", "x"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -71,53 +55,56 @@ describe("success path", () => {
|
||||
// --- Validation failure (short-circuit before config) ---
|
||||
|
||||
describe("validation failure", () => {
|
||||
it("returns structured error when question is missing", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("returns structured error when description is missing", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue({}, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleDebugIssue({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleDebugIssue({ foo: "bar" }, { loadConfig, createProvider });
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns structured error when question is empty string", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("returns structured error when description is empty string", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 question is wrong type", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("returns structured error when description is wrong type", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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([]);
|
||||
@@ -128,82 +115,70 @@ describe("validation 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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 client or response calls after config failure", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no provider or send calls after config failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Budget failure (short-circuit before prompt/client) ---
|
||||
// --- Budget failure (short-circuit before prompt/provider) ---
|
||||
|
||||
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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 client created, no response sent", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no prompt built, no provider created, no send called", async () => {
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(
|
||||
{ question: "x", context: "a".repeat(20) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
@@ -211,81 +186,69 @@ describe("budget failure", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// --- Client creation failure ---
|
||||
|
||||
describe("client creation failure", () => {
|
||||
it("returns structured error when createOpenAIClient throws", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
// --- Provider creation failure ---
|
||||
|
||||
describe("provider creation failure", () => {
|
||||
it("returns structured error when createProvider throws", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 response sent after client failure", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no send called after provider creation failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(sendMock).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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: Invalid config.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- OpenAI failure (pass-through) ---
|
||||
// --- Provider send failure (pass-through) ---
|
||||
|
||||
describe("OpenAI failure", () => {
|
||||
describe("provider send 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 sendMock = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 API failure", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("short-circuits — no extra processing after provider send failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 () => {
|
||||
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 sendMock = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: 429 Too Many Requests");
|
||||
});
|
||||
});
|
||||
@@ -293,64 +256,35 @@ describe("OpenAI failure", () => {
|
||||
// --- Dependency call order ---
|
||||
|
||||
describe("dependency call order", () => {
|
||||
it("calls deps in correct order: config -> client -> response", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("calls deps in correct order: config -> provider -> send", async () => {
|
||||
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" }; });
|
||||
const createProvider = vi.fn(() => { callLog.push("provider"); return { send: vi.fn().mockImplementation(async () => { callLog.push("send"); 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");
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(callLog).toEqual(["config", "provider", "send"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- No throws escaping ---
|
||||
|
||||
describe("no throws escaping", () => {
|
||||
it("returns structured result when sendOpenAIResponse throws null", async () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
it("returns structured result when send throws null", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(null);
|
||||
const sendMock = vi.fn().mockRejectedValue(null);
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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 () => {
|
||||
buildDebugIssuePromptCalls.length = 0;
|
||||
|
||||
const loadConfig = vi.fn(() => { throw "string error"; });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
});
|
||||
@@ -360,16 +294,14 @@ describe("no throws escaping", () => {
|
||||
|
||||
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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(
|
||||
{ question: "hi", context: "x".repeat(49) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -377,14 +309,12 @@ describe("warning propagation", () => {
|
||||
});
|
||||
|
||||
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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(Array.isArray(result.warnings)).toBe(true);
|
||||
});
|
||||
@@ -394,13 +324,11 @@ describe("warning propagation", () => {
|
||||
|
||||
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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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");
|
||||
@@ -408,13 +336,11 @@ describe("result shape", () => {
|
||||
});
|
||||
|
||||
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 sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
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");
|
||||
@@ -426,28 +352,24 @@ describe("result shape", () => {
|
||||
|
||||
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"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).toHaveBeenCalledTimes(1);
|
||||
expect(sendOpenAIResponse).toHaveBeenCalledTimes(1);
|
||||
expect(createProvider).toHaveBeenCalledTimes(1);
|
||||
expect(sendMock).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();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleDebugIssue(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
+109
-134
@@ -5,7 +5,7 @@ const mockConfig = {
|
||||
openaiApiKey: "sk-test-key",
|
||||
openaiModel: "gpt-5.1",
|
||||
temperature: 0.2,
|
||||
maxOutputTokens: 2000,
|
||||
maxOutputTokens: 4000,
|
||||
logLevel: "info",
|
||||
enableFileContext: false,
|
||||
contextDir: "./context",
|
||||
@@ -16,8 +16,8 @@ const mockConfig = {
|
||||
redactSecrets: true,
|
||||
};
|
||||
|
||||
function makeValidInput(question) {
|
||||
return { question };
|
||||
function makeValidInput(question, context) {
|
||||
return { question: question || "Review this code", context: context || "x" };
|
||||
}
|
||||
|
||||
// --- Success path ---
|
||||
@@ -25,11 +25,11 @@ function makeValidInput(question) {
|
||||
describe("success path", () => {
|
||||
it("returns ok:true with answer on full happy flow", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("Review my code"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleReviewCode(makeValidInput("Review this code", "x"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -40,11 +40,11 @@ describe("success path", () => {
|
||||
it("propagates budget warnings through to success result", async () => {
|
||||
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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleReviewCode(makeValidInput("Review this code", "x"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -57,10 +57,10 @@ describe("success path", () => {
|
||||
describe("validation failure", () => {
|
||||
it("returns structured error when question is missing", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode({}, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode({}, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -68,21 +68,21 @@ describe("validation failure", () => {
|
||||
|
||||
it("short-circuits — no other deps called", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewCode({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleReviewCode({ foo: "bar" }, { loadConfig, createProvider });
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns structured error when question is empty string", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode({ question: "", context: "x" }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -90,10 +90,10 @@ describe("validation failure", () => {
|
||||
|
||||
it("returns structured error when question is wrong type", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode({ question: 123, context: "x" }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -105,69 +105,69 @@ describe("validation 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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("short-circuits — no provider or send calls after config failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleReviewCode(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Budget failure (short-circuit before prompt/client) ---
|
||||
// --- 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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(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 client created, no response sent", async () => {
|
||||
it("short-circuits — no prompt built, no provider created, no send called", async () => {
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleReviewCode(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(
|
||||
{ question: "x", context: "a".repeat(20) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
@@ -175,69 +175,69 @@ describe("budget failure", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// --- Client creation failure ---
|
||||
// --- Provider creation failure ---
|
||||
|
||||
describe("client creation failure", () => {
|
||||
it("returns structured error when createOpenAIClient throws", async () => {
|
||||
describe("provider creation failure", () => {
|
||||
it("returns structured error when createProvider throws", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("short-circuits — no send called after provider creation failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes original error message unchanged", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: Invalid config.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- OpenAI failure (pass-through) ---
|
||||
// --- Provider send failure (pass-through) ---
|
||||
|
||||
describe("OpenAI failure", () => {
|
||||
describe("provider send failure", () => {
|
||||
it("passes through err.message unchanged", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(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 API failure", async () => {
|
||||
it("short-circuits — no extra processing after provider send failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(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 createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: 429 Too Many Requests");
|
||||
});
|
||||
});
|
||||
@@ -245,60 +245,35 @@ describe("OpenAI failure", () => {
|
||||
// --- Dependency call order ---
|
||||
|
||||
describe("dependency call order", () => {
|
||||
it("calls deps in correct order: config -> client -> response", async () => {
|
||||
it("calls deps in correct order: config -> provider -> send", async () => {
|
||||
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" }; });
|
||||
const createProvider = vi.fn(() => { callLog.push("provider"); return { send: vi.fn().mockImplementation(async () => { callLog.push("send"); return { content: "OK" }; }) }; });
|
||||
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(callLog).toEqual(["config", "client", "response"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- buildReviewCodePrompt called with correct input ---
|
||||
|
||||
describe("buildReviewCodePrompt integration", () => {
|
||||
it("calls buildReviewCodePrompt with budget.input (trimmed data)", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
|
||||
// Spy on buildReviewCodePrompt import by capturing the prompt arg.
|
||||
const promptCapture = [];
|
||||
const patchedSend = vi.fn(async (client, params) => {
|
||||
promptCapture.push(params.input[0].content);
|
||||
return { content: "OK" };
|
||||
});
|
||||
|
||||
await handleReviewCode(makeValidInput("test question"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse: patchedSend,
|
||||
});
|
||||
|
||||
expect(promptCapture.length).toBe(1);
|
||||
expect(typeof promptCapture[0]).toBe("string");
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(callLog).toEqual(["config", "provider", "send"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- No throws escaping ---
|
||||
|
||||
describe("no throws escaping", () => {
|
||||
it("returns structured result when sendOpenAIResponse throws null", async () => {
|
||||
it("returns structured result when send throws null", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(null);
|
||||
const sendMock = vi.fn().mockRejectedValue(null);
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
});
|
||||
@@ -310,12 +285,12 @@ 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 createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(
|
||||
{ question: "hi", context: "x".repeat(49) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -325,10 +300,10 @@ describe("warning propagation", () => {
|
||||
it("includes budget warnings in failure result when budget fails", async () => {
|
||||
const emptyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => emptyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(Array.isArray(result.warnings)).toBe(true);
|
||||
});
|
||||
@@ -339,10 +314,10 @@ describe("warning propagation", () => {
|
||||
describe("result shape", () => {
|
||||
it("returns exactly { ok, answer, warnings } on success", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["answer", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(true);
|
||||
expect(typeof result.answer).toBe("string");
|
||||
@@ -351,10 +326,10 @@ describe("result shape", () => {
|
||||
|
||||
it("returns exactly { ok, error, warnings } on failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["error", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
@@ -367,23 +342,23 @@ describe("result shape", () => {
|
||||
describe("short-circuit behavior", () => {
|
||||
it("stops at first failure without calling downstream deps", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).toHaveBeenCalledTimes(1);
|
||||
expect(sendOpenAIResponse).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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleReviewCode(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
+123
-138
@@ -5,7 +5,7 @@ const mockConfig = {
|
||||
openaiApiKey: "sk-test-key",
|
||||
openaiModel: "gpt-5.1",
|
||||
temperature: 0.2,
|
||||
maxOutputTokens: 2000,
|
||||
maxOutputTokens: 4000,
|
||||
logLevel: "info",
|
||||
enableFileContext: false,
|
||||
contextDir: "./context",
|
||||
@@ -16,8 +16,8 @@ const mockConfig = {
|
||||
redactSecrets: true,
|
||||
};
|
||||
|
||||
function makeValidInput(question) {
|
||||
return { question };
|
||||
function makeValidInput(plan, workspaceId) {
|
||||
return { question: plan || "Execute this task", context: workspaceId || "ws-001" };
|
||||
}
|
||||
|
||||
// --- Success path ---
|
||||
@@ -25,11 +25,11 @@ function makeValidInput(question) {
|
||||
describe("success path", () => {
|
||||
it("returns ok:true with answer on full happy flow", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("Review my plan"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleReviewPlan(makeValidInput("Execute this task", "ws-001"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -40,11 +40,11 @@ describe("success path", () => {
|
||||
it("propagates budget warnings through to success result", async () => {
|
||||
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 sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse,
|
||||
const result = await handleReviewPlan(makeValidInput("Execute this task"), {
|
||||
loadConfig, createProvider,
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -55,12 +55,23 @@ describe("success path", () => {
|
||||
// --- Validation failure (short-circuit before config) ---
|
||||
|
||||
describe("validation failure", () => {
|
||||
it("returns structured error when question is missing", async () => {
|
||||
it("returns structured error when plan is missing", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan({}, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan({}, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns structured error when question is empty", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan({ question: "" }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -68,32 +79,32 @@ describe("validation failure", () => {
|
||||
|
||||
it("short-circuits — no other deps called", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewPlan({ foo: "bar" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleReviewPlan({ foo: "bar" }, { loadConfig, createProvider });
|
||||
expect(loadConfig).not.toHaveBeenCalled();
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns structured error when question is empty string", async () => {
|
||||
it("returns structured error when plan is empty string", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan({ question: "" }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan({ question: "", context: "ws-001" }, { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("returns structured error when plan is wrong type", async () => {
|
||||
const loadConfig = vi.fn();
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan({ question: 123 }, { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan({ question: 123, context: "ws-001" }, { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
expect(result.warnings).toEqual([]);
|
||||
@@ -105,69 +116,69 @@ describe("validation 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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("short-circuits — no provider or send calls after config failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("No key."); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleReviewPlan(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: OPENAI_API_KEY is missing.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Budget failure (short-circuit before prompt/client) ---
|
||||
// --- 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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(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 client created, no response sent", async () => {
|
||||
it("short-circuits — no prompt built, no provider created, no send called", async () => {
|
||||
const tinyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => tinyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleReviewPlan(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(
|
||||
{ question: "x", context: "a".repeat(20) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
@@ -175,69 +186,69 @@ describe("budget failure", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// --- Client creation failure ---
|
||||
// --- Provider creation failure ---
|
||||
|
||||
describe("client creation failure", () => {
|
||||
it("returns structured error when createOpenAIClient throws", async () => {
|
||||
describe("provider creation failure", () => {
|
||||
it("returns structured error when createProvider throws", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
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 () => {
|
||||
it("short-circuits — no send called after provider creation failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("passes original error message unchanged", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => { throw new Error("Invalid config."); });
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: Invalid config.");
|
||||
});
|
||||
});
|
||||
|
||||
// --- OpenAI failure (pass-through) ---
|
||||
// --- Provider send failure (pass-through) ---
|
||||
|
||||
describe("OpenAI failure", () => {
|
||||
describe("provider send failure", () => {
|
||||
it("passes through err.message unchanged", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("API key invalid."));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(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 API failure", async () => {
|
||||
it("short-circuits — no extra processing after provider send failure", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("rate limit"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(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 createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("429 Too Many Requests"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.error).toBe("Error: 429 Too Many Requests");
|
||||
});
|
||||
});
|
||||
@@ -245,61 +256,35 @@ describe("OpenAI failure", () => {
|
||||
// --- Dependency call order ---
|
||||
|
||||
describe("dependency call order", () => {
|
||||
it("calls deps in correct order: config -> client -> response", async () => {
|
||||
it("calls deps in correct order: config -> provider -> send", async () => {
|
||||
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" }; });
|
||||
const createProvider = vi.fn(() => { callLog.push("provider"); return { send: vi.fn().mockImplementation(async () => { callLog.push("send"); return { content: "OK" }; }) }; });
|
||||
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
expect(callLog).toEqual(["config", "client", "response"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- buildReviewPlanPrompt called with correct input ---
|
||||
|
||||
describe("buildReviewPlanPrompt integration", () => {
|
||||
it("calls buildReviewPlanPrompt with budget.input (trimmed data)", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
|
||||
// Spy on buildReviewPlanPrompt import by capturing the prompt arg.
|
||||
const promptCapture = [];
|
||||
const patchedSend = vi.fn(async (client, params) => {
|
||||
promptCapture.push(params.input[0].content);
|
||||
return { content: "OK" };
|
||||
});
|
||||
|
||||
await handleReviewPlan(makeValidInput("test question"), {
|
||||
loadConfig, createOpenAIClient, sendOpenAIResponse: patchedSend,
|
||||
});
|
||||
|
||||
expect(promptCapture.length).toBe(1);
|
||||
expect(typeof promptCapture[0]).toBe("string");
|
||||
expect(promptCapture[0].includes("plan reviewer")).toBe(true);
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(callLog).toEqual(["config", "provider", "send"]);
|
||||
});
|
||||
});
|
||||
|
||||
// --- No throws escaping ---
|
||||
|
||||
describe("no throws escaping", () => {
|
||||
it("returns structured result when sendOpenAIResponse throws null", async () => {
|
||||
it("returns structured result when send throws null", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(null);
|
||||
const sendMock = vi.fn().mockRejectedValue(null);
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
});
|
||||
@@ -311,12 +296,12 @@ 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 createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(
|
||||
{ question: "hi", context: "x".repeat(49) },
|
||||
{ loadConfig, createOpenAIClient, sendOpenAIResponse },
|
||||
{ loadConfig, createProvider },
|
||||
);
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
@@ -326,10 +311,10 @@ describe("warning propagation", () => {
|
||||
it("includes budget warnings in failure result when budget fails", async () => {
|
||||
const emptyBudget = { ...mockConfig, maxInputChars: 0 };
|
||||
const loadConfig = vi.fn(() => emptyBudget);
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(result.ok).toBe(false);
|
||||
expect(Array.isArray(result.warnings)).toBe(true);
|
||||
});
|
||||
@@ -340,10 +325,10 @@ describe("warning propagation", () => {
|
||||
describe("result shape", () => {
|
||||
it("returns exactly { ok, answer, warnings } on success", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn(async () => ({ content: "OK" }));
|
||||
const sendMock = vi.fn(async () => ({ content: "OK" }));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["answer", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(true);
|
||||
expect(typeof result.answer).toBe("string");
|
||||
@@ -352,10 +337,10 @@ describe("result shape", () => {
|
||||
|
||||
it("returns exactly { ok, error, warnings } on failure", async () => {
|
||||
const loadConfig = vi.fn(() => { throw new Error("fail"); });
|
||||
const createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
const result = await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(Object.keys(result).sort()).toEqual(["error", "ok", "warnings"]);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(typeof result.error).toBe("string");
|
||||
@@ -368,23 +353,23 @@ describe("result shape", () => {
|
||||
describe("short-circuit behavior", () => {
|
||||
it("stops at first failure without calling downstream deps", async () => {
|
||||
const loadConfig = vi.fn(() => mockConfig);
|
||||
const createOpenAIClient = vi.fn(() => ({ responses: { create: vi.fn() } }));
|
||||
const sendOpenAIResponse = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const sendMock = vi.fn().mockRejectedValue(new Error("boom"));
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).toHaveBeenCalledTimes(1);
|
||||
expect(sendOpenAIResponse).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 createOpenAIClient = vi.fn();
|
||||
const sendOpenAIResponse = vi.fn();
|
||||
const sendMock = vi.fn();
|
||||
const createProvider = vi.fn(() => ({ send: sendMock }));
|
||||
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createOpenAIClient, sendOpenAIResponse });
|
||||
await handleReviewPlan(makeValidInput("hi"), { loadConfig, createProvider });
|
||||
expect(loadConfig).toHaveBeenCalledTimes(1);
|
||||
expect(createOpenAIClient).not.toHaveBeenCalled();
|
||||
expect(sendOpenAIResponse).not.toHaveBeenCalled();
|
||||
expect(createProvider).not.toHaveBeenCalled();
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user