diff --git a/src/lib/resume-parser.test.ts b/src/lib/resume-parser.test.ts new file mode 100644 index 00000000..1ba2f780 --- /dev/null +++ b/src/lib/resume-parser.test.ts @@ -0,0 +1,16 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +afterEach(() => { + vi.unstubAllEnvs(); + vi.resetModules(); +}); + +describe("resume-parser module", () => { + it("can be imported without an OpenAI API key", async () => { + vi.stubEnv("OPENAI_API_KEY", ""); + + await expect(import("./resume-parser")).resolves.toHaveProperty( + "parseResumeFile" + ); + }); +}); diff --git a/src/lib/resume-parser.ts b/src/lib/resume-parser.ts index 62ac408f..516c33c8 100644 --- a/src/lib/resume-parser.ts +++ b/src/lib/resume-parser.ts @@ -34,9 +34,13 @@ export interface ParsedResumeProfile { }; } -const openai = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY, -}); +function getOpenAIClient() { + const apiKey = process.env.OPENAI_API_KEY; + if (!apiKey) { + throw new Error("OPENAI_API_KEY is not configured"); + } + return new OpenAI({ apiKey }); +} // Use OpenAI to parse resume text into structured data async function parseWithOpenAI(text: string): Promise { @@ -82,6 +86,7 @@ Resume text: ${text}`; try { + const openai = getOpenAIClient(); const response = await openai.chat.completions.create({ model: "gpt-4o-mini", messages: [