import { afterEach, describe, expect, it, vi } from "vitest"; import { extract, modelLabel, modelProvider } from "../server/model"; const sources = [ { id: "synthetic", title: "Test note", kind: "Synthetic", date: "2026-10-23", text: "Patient: Amina Stone\nShe takes Metformin 500 mg twice daily.", }, ]; const reply = () => new Response( JSON.stringify({ choices: [ { finish_reason: "stop", message: { content: JSON.stringify({ facts: [[2, "Medication: Metformin; strength: 500 mg; frequency: 2/day"]], }), }, }, ], usage: { prompt_tokens: 100, completion_tokens: 30 }, }), ); const useMeta = () => { vi.stubEnv("CONTEXT_MODEL_ENDPOINT", "https://api.meta.ai/v1/chat/completions"); vi.stubEnv("CONTEXT_MODEL_API_KEY", "synthetic-test-key"); vi.stubEnv("CONTEXT_MODEL_ID", "muse-spark-1.3"); vi.stubEnv("CONTEXT_MODEL_AUTH", "bearer"); }; afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); }); describe("Meta Model API reader", () => { it("names Meta Muse Spark as the reader", () => { useMeta(); expect(modelProvider()).toBe("meta"); expect(modelLabel()).toBe("Meta Muse Spark (muse-spark-1.3)"); }); it("sends Meta's chat-completions shape with a bearer key", async () => { useMeta(); const fetcher = vi.fn(async () => reply()); vi.stubGlobal("fetch", fetcher); const draft = await extract(sources, true, true); expect(fetcher).toHaveBeenCalledTimes(1); const [url, init] = fetcher.mock.calls[0] as unknown as [string, RequestInit]; expect(url).toBe("https://api.meta.ai/v1/chat/completions"); expect((init.headers as Record).Authorization).toBe("Bearer synthetic-test-key"); const body = JSON.parse(String(init.body)); expect(body).toMatchObject({ model: "muse-spark-1.3", max_completion_tokens: 12_000, reasoning_effort: "low" }); expect(body).not.toHaveProperty("max_tokens"); expect(draft.mode).toBe("model"); }); it("keeps the Azure request shape for Azure endpoints", async () => { vi.stubEnv("CONTEXT_MODEL_ENDPOINT", "https://synthetic.services.ai.azure.com/openai/v1/chat/completions"); vi.stubEnv("CONTEXT_MODEL_API_KEY", "synthetic-test-key"); vi.stubEnv("CONTEXT_MODEL_ID", "test-model"); const fetcher = vi.fn(async () => reply()); vi.stubGlobal("fetch", fetcher); await extract(sources, true, true); const body = JSON.parse(String((fetcher.mock.calls[0] as unknown as [string, RequestInit])[1].body)); expect(body).toHaveProperty("max_tokens"); expect(body).not.toHaveProperty("reasoning_effort"); }); });