feat: add provider abstraction for review backends
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { createChatProvider, openaiProvider } from "../../src/providers/factory.js";
|
||||
|
||||
function mockConfig(provider) {
|
||||
const cfg = { chatgptMcpProvider: provider };
|
||||
return cfg;
|
||||
}
|
||||
|
||||
// --- Default provider ---
|
||||
|
||||
describe("default provider", () => {
|
||||
it("returns openai provider when no provider specified", () => {
|
||||
const provider = createChatProvider({});
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
|
||||
it("returns openai provider when null config", () => {
|
||||
const provider = createChatProvider(null);
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
|
||||
it("returns openai provider when undefined config", () => {
|
||||
const provider = createChatProvider(undefined);
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
|
||||
it("returns openai provider when chatgptMcpProvider is empty string", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "" });
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
|
||||
it("returns openai provider explicitly set", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Supported providers ---
|
||||
|
||||
describe("supported providers", () => {
|
||||
it("supports openai", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
|
||||
it("returns the same instance for repeated calls with same provider", () => {
|
||||
const p1 = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
const p2 = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
expect(p1).toBe(p2);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Unsupported providers ---
|
||||
|
||||
describe("unsupported providers", () => {
|
||||
it("throws on ollama provider", () => {
|
||||
expect(() => createChatProvider({ chatgptMcpProvider: "ollama" })).toThrow(
|
||||
/Unsupported chat provider "ollama"/,
|
||||
);
|
||||
});
|
||||
|
||||
it("throws on unknown provider name", () => {
|
||||
expect(() => createChatProvider({ chatgptMcpProvider: "unknown" })).toThrow(
|
||||
/Unsupported chat provider "unknown"/,
|
||||
);
|
||||
});
|
||||
|
||||
it("defaults to openai for null provider name", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: null });
|
||||
expect(provider.send).toBeDefined();
|
||||
});
|
||||
|
||||
it("throws on numeric provider name (truthy but not supported)", () => {
|
||||
expect(() => createChatProvider({ chatgptMcpProvider: 123 })).toThrow('Unsupported chat provider "123"');
|
||||
});
|
||||
|
||||
it("contains provider name in error message for unsupported providers", () => {
|
||||
try {
|
||||
createChatProvider({ chatgptMcpProvider: "ollama" });
|
||||
} catch (err) {
|
||||
expect(err.message).toContain("ollama");
|
||||
}
|
||||
});
|
||||
|
||||
it("mentions provider name in error message", () => {
|
||||
try {
|
||||
createChatProvider({ chatgptMcpProvider: "bedrock" });
|
||||
} catch (err) {
|
||||
expect(err.message).toContain("bedrock");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// --- Provider interface ---
|
||||
|
||||
describe("provider interface", () => {
|
||||
it("returned provider has send method", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
expect(typeof provider.send).toBe("function");
|
||||
});
|
||||
|
||||
it("send is a function, not undefined", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
expect(provider.send).not.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// --- Exported openaiProvider ---
|
||||
|
||||
describe("exported openaiProvider", () => {
|
||||
it("openaiProvider is exported from factory", () => {
|
||||
expect(openaiProvider).toBeDefined();
|
||||
});
|
||||
|
||||
it("openaiProvider has send method", () => {
|
||||
expect(typeof openaiProvider.send).toBe("function");
|
||||
});
|
||||
});
|
||||
|
||||
// --- Edge cases ---
|
||||
|
||||
describe("edge cases", () => {
|
||||
it("handles case-sensitive provider name (Ollama != ollama)", () => {
|
||||
// This should also fail since only lowercase "openai" is supported
|
||||
expect(() => createChatProvider({ chatgptMcpProvider: "Ollama" })).toThrow();
|
||||
});
|
||||
|
||||
it("handles whitespace provider name", () => {
|
||||
expect(() => createChatProvider({ chatgptMcpProvider: " openai " })).toThrow();
|
||||
});
|
||||
|
||||
it("handles JSON string provider name", () => {
|
||||
expect(() => createChatProvider({ chatgptMcpProvider: '"openai"' })).toThrow();
|
||||
});
|
||||
|
||||
it("creates provider even with minimal config object", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
expect(typeof provider.send).toBe("function");
|
||||
});
|
||||
|
||||
it("works when config has extra unrelated fields", () => {
|
||||
const provider = createChatProvider({
|
||||
chatgptMcpProvider: "openai",
|
||||
openaiApiKey: "sk-test",
|
||||
someOtherField: "ignored",
|
||||
});
|
||||
expect(provider).toBe(openaiProvider);
|
||||
});
|
||||
|
||||
it("does not mutate the config object", () => {
|
||||
const cfg = { chatgptMcpProvider: "openai" };
|
||||
createChatProvider(cfg);
|
||||
expect(cfg.chatgptMcpProvider).toBe("openai");
|
||||
});
|
||||
|
||||
it("does not throw for all falsy values except explicit openai", () => {
|
||||
const falsyValues = [null, undefined, "", NaN];
|
||||
// Only empty string and no-provider should default to openai
|
||||
// null/undefined → config check passes (defaults to "openai")
|
||||
// "" → defaults to "openai"
|
||||
});
|
||||
|
||||
it("defaults to openai for NaN provider name", () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: NaN });
|
||||
expect(provider.send).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
// --- Integration-like test ---
|
||||
|
||||
describe("integration: send delegation", () => {
|
||||
it("provider.send delegates to the underlying provider implementation", async () => {
|
||||
const provider = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
// The real openaiProvider.send calls OpenAI API — we just verify it exists and is callable
|
||||
expect(typeof provider.send).toBe("function");
|
||||
// We don't call it here to avoid actual API calls in tests
|
||||
});
|
||||
});
|
||||
|
||||
// --- Repeatability ---
|
||||
|
||||
describe("repeatability", () => {
|
||||
it("creates identical providers for same config each time", () => {
|
||||
const results = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
results.push(createChatProvider({ chatgptMcpProvider: "openai" }));
|
||||
}
|
||||
expect(results.every((p) => p === openaiProvider)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not share mutable state between calls", () => {
|
||||
const p1 = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
const p2 = createChatProvider({ chatgptMcpProvider: "openai" });
|
||||
// Both should be the same singleton instance (by design)
|
||||
expect(p1).toBe(p2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,274 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
// Use module-level mock objects that we can mutate between tests.
|
||||
// vi.mock() hoists to top-of-file, so these functions are set once and
|
||||
// their closure captures persist across the whole test file execution.
|
||||
// We reset call history in beforeEach but leave impl as-is (or update per-test).
|
||||
|
||||
const _impl = { client: null, send: null };
|
||||
|
||||
function createOpenAIClientMock(config) {
|
||||
if (_impl.client) return _impl.client(config);
|
||||
return { apiKey: config?.openaiApiKey || "default-key" };
|
||||
}
|
||||
|
||||
async function sendOpenAIResponseMock(client, params) {
|
||||
if (_impl.send) return _impl.send(client, params);
|
||||
return { content: "mocked response from openaiProvider.send" };
|
||||
}
|
||||
|
||||
vi.mock("../../src/openai/client.js", () => ({
|
||||
createOpenAIClient: createOpenAIClientMock,
|
||||
}));
|
||||
|
||||
vi.mock("../../src/openai/responses.js", () => ({
|
||||
sendOpenAIResponse: sendOpenAIResponseMock,
|
||||
}));
|
||||
|
||||
const { openaiProvider } = await import("../../src/providers/openai.js");
|
||||
|
||||
function resetImpl() {
|
||||
_impl.client = null;
|
||||
_impl.send = null;
|
||||
}
|
||||
|
||||
describe("openaiProvider.send", () => {
|
||||
beforeEach(resetImpl);
|
||||
|
||||
it("calls createOpenAIClient with the full config", async () => {
|
||||
const config = { openaiApiKey: "sk-test", openaiModel: "gpt-5.1" };
|
||||
let capturedConfig = null;
|
||||
_impl.client = (cfg) => { capturedConfig = cfg; return { apiKey: cfg?.openaiApiKey }; };
|
||||
|
||||
await openaiProvider.send("test input", config);
|
||||
|
||||
expect(capturedConfig).toBe(config);
|
||||
});
|
||||
|
||||
it("passes input as system message in the requests array", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input).toEqual([{ role: "system", content: "hello world" }]);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("hello world", { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("passes model from config", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.model).toBe("gpt-5.1-preview");
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("test", { openaiApiKey: "sk-test", openaiModel: "gpt-5.1-preview" });
|
||||
});
|
||||
|
||||
it("passes temperature from config", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.temperature).toBe(0.7);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("test", { openaiApiKey: "sk-test", temperature: 0.7 });
|
||||
});
|
||||
|
||||
it("passes maxOutputTokens from config", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.maxOutputTokens).toBe(4096);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("test", { openaiApiKey: "sk-test", maxOutputTokens: 4096 });
|
||||
});
|
||||
|
||||
it("returns { content } from sendOpenAIResponse result", async () => {
|
||||
_impl.send = async () => ({ content: "hello AI" });
|
||||
const config = { openaiApiKey: "sk-test" };
|
||||
const result = await openaiProvider.send("test input", config);
|
||||
expect(result).toEqual({ content: "hello AI" });
|
||||
});
|
||||
|
||||
it("calls createOpenAIClient before sendOpenAIResponse (order)", async () => {
|
||||
const callOrder = [];
|
||||
_impl.client = (cfg) => { callOrder.push("client"); return { apiKey: cfg?.openaiApiKey }; };
|
||||
_impl.send = async () => { callOrder.push("send"); return { content: "ok" }; };
|
||||
|
||||
await openaiProvider.send("test", { openaiApiKey: "sk-test" });
|
||||
expect(callOrder).toEqual(["client", "send"]);
|
||||
});
|
||||
|
||||
it("passes client as first arg to sendOpenAIResponse", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(client.apiKey).toBe("sk-test");
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("test", { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("creates a new client on each send call (no reuse)", async () => {
|
||||
let count = 0;
|
||||
_impl.client = () => { count++; return { apiKey: "sk-test" }; };
|
||||
const config = { openaiApiKey: "sk-test" };
|
||||
await openaiProvider.send("test 1", config);
|
||||
await openaiProvider.send("test 2", config);
|
||||
expect(count).toBe(2);
|
||||
});
|
||||
|
||||
it("returns the raw content string wrapped in { content } object", async () => {
|
||||
_impl.send = async () => ({ content: "raw AI output" });
|
||||
const config = { openaiApiKey: "sk-test" };
|
||||
const result = await openaiProvider.send("test", config);
|
||||
expect(result.content).toBe("raw AI output");
|
||||
});
|
||||
|
||||
it("works with empty input string", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input).toEqual([{ role: "system", content: "" }]);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("", { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("works with multi-line input", async () => {
|
||||
const multiline = "line 1\nline 2\nline 3";
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input).toEqual([{ role: "system", content: multiline }]);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send(multiline, { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("uses undefined values when config fields are missing", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.model).toBeUndefined();
|
||||
expect(params.temperature).toBeUndefined();
|
||||
expect(params.maxOutputTokens).toBeUndefined();
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("test", { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("throws when createOpenAIClient throws", async () => {
|
||||
_impl.client = () => { throw new Error("No API key"); };
|
||||
const config = {};
|
||||
await expect(openaiProvider.send("test", config)).rejects.toThrow("No API key");
|
||||
});
|
||||
|
||||
it("passes through sendOpenAIResponse errors", async () => {
|
||||
_impl.send = async () => { throw new Error("API error"); };
|
||||
const config = { openaiApiKey: "sk-test" };
|
||||
await expect(openaiProvider.send("test", config)).rejects.toThrow("API error");
|
||||
});
|
||||
|
||||
it("is idempotent — same input produces same call pattern", async () => {
|
||||
let capturedModel, capturedContent;
|
||||
_impl.send = async (client, params) => {
|
||||
capturedModel = params.model;
|
||||
capturedContent = params.input[0].content;
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send("identical input", { openaiApiKey: "sk-test", openaiModel: "gpt-5.1" });
|
||||
expect(capturedModel).toBe("gpt-5.1");
|
||||
expect(capturedContent).toBe("identical input");
|
||||
});
|
||||
|
||||
it("handles special characters in input", async () => {
|
||||
const specialInput = '<script>alert("xss")</script>';
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input[0].content).toBe(specialInput);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send(specialInput, { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("handles unicode in input", async () => {
|
||||
const unicodeInput = "こんにちは世界 🌍";
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input[0].content).toBe(unicodeInput);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send(unicodeInput, { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("handles very long input", async () => {
|
||||
const longInput = "a".repeat(50000);
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input[0].content).toBe(longInput);
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send(longInput, { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("does not modify the input parameter", async () => {
|
||||
const input = "original";
|
||||
_impl.send = async () => ({ content: "ok" });
|
||||
await openaiProvider.send(input, { openaiApiKey: "sk-test" });
|
||||
expect(input).toBe("original");
|
||||
});
|
||||
|
||||
it("does not modify the config object", async () => {
|
||||
const config = { openaiApiKey: "sk-test", temperature: 0.2 };
|
||||
_impl.send = async () => ({ content: "ok" });
|
||||
await openaiProvider.send("test", config);
|
||||
expect(config.temperature).toBe(0.2);
|
||||
});
|
||||
|
||||
it("returns mocked response when no custom impl set", async () => {
|
||||
const result = await openaiProvider.send("any input", { openaiApiKey: "sk-test" });
|
||||
expect(result.content).toBe("mocked response from openaiProvider.send");
|
||||
});
|
||||
|
||||
it("works with null input", async () => {
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input[0].content).toBeNull();
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send(null, { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("works with object input", async () => {
|
||||
const objInput = { foo: "bar" };
|
||||
_impl.send = async (client, params) => {
|
||||
expect(params.input[0].role).toBe("system");
|
||||
return { content: "ok" };
|
||||
};
|
||||
|
||||
await openaiProvider.send(objInput, { openaiApiKey: "sk-test" });
|
||||
});
|
||||
|
||||
it("passes the correct config through createOpenAIClient", async () => {
|
||||
let captured = null;
|
||||
_impl.client = (cfg) => { captured = cfg; return { apiKey: cfg.openaiApiKey }; };
|
||||
_impl.send = async () => ({ content: "ok" });
|
||||
|
||||
const config = { openaiApiKey: "sk-specific", temperature: 0.9, maxOutputTokens: 8192 };
|
||||
await openaiProvider.send("test", config);
|
||||
expect(captured.openaiApiKey).toBe("sk-specific");
|
||||
expect(captured.temperature).toBe(0.9);
|
||||
expect(captured.maxOutputTokens).toBe(8192);
|
||||
});
|
||||
|
||||
it("does not share state between send calls", async () => {
|
||||
let clientCalls = [];
|
||||
_impl.client = (cfg) => { clientCalls.push(cfg.openaiApiKey); return { apiKey: cfg.openaiApiKey }; };
|
||||
_impl.send = async () => ({ content: "ok" });
|
||||
|
||||
await openaiProvider.send("test 1", { openaiApiKey: "key-1" });
|
||||
await openaiProvider.send("test 2", { openaiApiKey: "key-2" });
|
||||
expect(clientCalls).toEqual(["key-1", "key-2"]);
|
||||
});
|
||||
|
||||
it("handles undefined config gracefully (no crashes)", async () => {
|
||||
_impl.send = async () => ({ content: "ok" });
|
||||
await openaiProvider.send("test", {});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user