Repository Public
1import { readFileSync } from "node:fs";2import { afterAll, describe, expect, it } from "vitest";3import { Client, sql } from "./helpers";45/**6 * Against the real Anthropic API (REAL_AI=1 scripts/test.sh). Costs a few cents. A fresh recovery link is issued7 * through the database to sign in, because the earlier test run consumed the setup token.8 */9const admin = new Client();10afterAll(async () => { await sql.end(); });1112describe("real Claude generation", () => {13 it("signs in", async () => {14 const { execFileSync } = await import("node:child_process");15 const out = execFileSync("node_modules/.bin/tsx", ["scripts/setup-token.ts", "--force-print"], { encoding: "utf8", env: process.env });16 const token = out.split("\n")[0].split("#")[1];17 expect((await admin.enrol(token)).status).toBe(200);18 void readFileSync;19 });20 it("produces a valid, editable draft from a prompt, then applies a targeted edit", async () => {21 const r = await admin.json<{ id: string }>("/api/admin/generations", { json: { kind: "generate", brief: { prompt: "Fiche courte : la moyenne mobile exponentielle (EMA). Une équation, un diagramme du calcul récursif, un quiz.", locale: "fr", length: "short" } } });22 expect(r.status).toBe(200);23 const j = await admin.job(r.body.id, 600_000);24 expect(j.status, JSON.stringify(j.error)).toBe("succeeded");25 const sheetId = (j.output as { sheetId: string }).sheetId;26 const d = (await admin.json<{ version: number; doc: { blocks: { id: string; type: string }[]; sources: { status: string }[]; metadata: { author: string } }; quality: { errors: number } }>(`/api/admin/sheets/${sheetId}`)).body;27 expect(d.doc.blocks.length).toBeGreaterThanOrEqual(6); expect(d.doc.metadata.author).toBe("Simon-Pierre Boucher");28 expect(d.doc.blocks.some((b) => b.type === "equation")).toBe(true); expect(d.doc.blocks.some((b) => b.type === "diagram")).toBe(true);29 expect(d.doc.sources.every((s) => s.status === "unverified")).toBe(true);30 console.log(`generated ${d.doc.blocks.length} blocks, ${d.quality.errors} quality errors, cost ${j.usage.costUsd.toFixed(4)} USD`);31 const e = await admin.json<{ id: string }>(`/api/admin/sheets/${sheetId}/ai-edit`, { json: { instruction: "Ajoute, juste après le titre principal, un encadré « définition » d’une phrase. Ne touche à rien d’autre.", baseVersion: d.version } });32 const ej = await admin.job(e.body.id, 600_000);33 expect(ej.status, JSON.stringify(ej.error)).toBe("succeeded");34 const out = ej.output as { changed: boolean; diff: { added: string[]; changed: string[]; removed: string[] } };35 expect(out.changed).toBe(true); expect(out.diff.added.length).toBe(1); expect(out.diff.removed).toEqual([]);36 console.log(`edit: ${JSON.stringify(out.diff)}, cost ${ej.usage.costUsd.toFixed(4)} USD`);37 }, 900_000);38});39