61 lines
3.7 KiB
TypeScript
61 lines
3.7 KiB
TypeScript
import { createServer, type Server } from "node:http";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("server-only", () => ({}));
|
|
vi.mock("@/lib/vault", () => ({ unseal: async () => "sk-test" }));
|
|
|
|
// lib/ai.ts gegen einen kleinen Server in OpenAI-Form.
|
|
describe("ai", () => {
|
|
let server: Server;
|
|
let endpoint = "";
|
|
const seen: { auth?: string; body?: Record<string, unknown> }[] = [];
|
|
let ai: typeof import("@/lib/ai");
|
|
|
|
beforeAll(async () => {
|
|
ai = await import("@/lib/ai");
|
|
server = createServer((req, res) => {
|
|
let raw = "";
|
|
req.on("data", (c) => (raw += c));
|
|
req.on("end", () => {
|
|
seen.push({ auth: req.headers.authorization, body: raw ? JSON.parse(raw) : undefined });
|
|
if (req.url === "/login/v1/models") return res.writeHead(302, { location: "/anmeldung" }).end();
|
|
if (req.url === "/html/v1/models") return res.writeHead(200, { "content-type": "text/html" }).end("<html>Anmelden</html>");
|
|
if (req.headers.authorization !== "Bearer sk-test") return res.writeHead(401).end();
|
|
if (req.url === "/v1/models") return res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ data: [{ id: "qwen-vl" }] }));
|
|
const content = '<think>hm</think>```json\n{"title":"Linsensuppe","portions":4,"minutes":30,"ingredients":["250 g rote Linsen"],"steps":["Kochen."]}\n```';
|
|
res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ choices: [{ message: { content } }] }));
|
|
});
|
|
});
|
|
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
|
|
endpoint = `http://127.0.0.1:${(server.address() as { port: number }).port}/v1/`;
|
|
});
|
|
afterAll(() => new Promise<void>((done) => server.close(() => done())));
|
|
|
|
const settings = () => ({ endpoint, textModel: "qwen-text", visionModel: "qwen-vl", apiKeyEnc: Buffer.from([1]) });
|
|
|
|
it("checks the connection and which models exist", async () => {
|
|
expect(await ai.checkConnection(settings())).toEqual({ ok: true, models: ["qwen-vl"], textFound: false, visionFound: true });
|
|
expect(await ai.checkConnection({ ...settings(), apiKeyEnc: null })).toEqual({ ok: false, reason: "key" });
|
|
expect(await ai.checkConnection({ ...settings(), endpoint: "http://127.0.0.1:9/v1" })).toEqual({ ok: false, reason: "unreachable" });
|
|
// Eine Anmeldeseite statt des Dienstes (Olares-Zugang von außen) wird erkannt.
|
|
const root = endpoint.replace(/\/v1\/$/, "");
|
|
expect(await ai.checkConnection({ ...settings(), endpoint: `${root}/login/v1` })).toEqual({ ok: false, reason: "login" });
|
|
expect(await ai.checkConnection({ ...settings(), endpoint: `${root}/html/v1` })).toEqual({ ok: false, reason: "login" });
|
|
// default-chat löst der Olares-Router selbst auf.
|
|
expect(await ai.checkConnection({ ...settings(), textModel: "default-chat" })).toMatchObject({ ok: true, textFound: true });
|
|
});
|
|
|
|
it("asks the vision model and reads a recipe from the reply", async () => {
|
|
const reply = await ai.askJson(settings(), "vision", ai.RECIPE_PROMPT, [{ type: "text", text: "Rezept" }]);
|
|
expect(seen.at(-1)!.body!.model).toBe("qwen-vl");
|
|
expect(ai.recipeFromReply(reply)).toMatchObject({ title: "Linsensuppe", yield: 4, totalMinutes: 30, ingredients: ["250 g rote Linsen"] });
|
|
});
|
|
|
|
it("refuses without a model, and treats empty or broken replies as no recipe", async () => {
|
|
await expect(ai.askJson({ ...settings(), textModel: null }, "text", "x", "y")).rejects.toBeInstanceOf(ai.AiNotConfigured);
|
|
expect(ai.parseJsonReply("kein JSON")).toBeNull();
|
|
expect(ai.recipeFromReply({ title: "", ingredients: [] })).toBeNull();
|
|
expect(ai.recipeFromReply({ title: "X", ingredients: ["1 Ei"], portions: 9000 })).toMatchObject({ yield: null });
|
|
});
|
|
});
|