40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const MockClient = vi.fn(function (opts) {
|
|
this.apiKey = opts?.apiKey;
|
|
});
|
|
|
|
// Mock the OpenAI SDK — Vitest hoists vi.mock automatically.
|
|
vi.mock("openai", () => ({ default: MockClient }));
|
|
|
|
const { createOpenAIClient } = await import("../../src/openai/client.js");
|
|
|
|
describe("createOpenAIClient", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("creates OpenAI client with supplied apiKey", () => {
|
|
const config = { openaiApiKey: "sk-test-key" };
|
|
const client = createOpenAIClient(config);
|
|
|
|
expect(MockClient).toHaveBeenCalledTimes(1);
|
|
expect(MockClient).toHaveBeenCalledWith({ apiKey: "sk-test-key" });
|
|
expect(client.apiKey).toBe("sk-test-key");
|
|
});
|
|
|
|
it("throws when openaiApiKey is missing", () => {
|
|
const config = {};
|
|
|
|
expect(() => createOpenAIClient(config)).toThrow("Missing config.openaiApiKey.");
|
|
expect(MockClient).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("throws when openaiApiKey is empty string", () => {
|
|
const config = { openaiApiKey: "" };
|
|
|
|
expect(() => createOpenAIClient(config)).toThrow("Missing config.openaiApiKey.");
|
|
expect(MockClient).not.toHaveBeenCalled();
|
|
});
|
|
});
|