import test from "node:test"; import assert from "node:assert/strict"; import { splitPage, normalizeSpeakers, assemble, transcribePage, PageError } from "../server/transcribe.mjs"; import { parsePlay, lint } from "../public/parse.js"; test("splitPage takes the page number from the first line", () => { assert.deepEqual(splitPage("=-12-\n@Lída\nAno."), { num: "12", body: "@Lída\nAno." }); assert.deepEqual(splitPage("=?\ntext"), { num: "?", body: "text" }); assert.deepEqual(splitPage("```\n=3\n@A\nx\n=4\n```"), { num: "3", body: "@A\nx" }); assert.equal(splitPage("@A\nx").num, "?"); }); test("speaker spellings are unified across pages", () => { const out = normalizeSpeakers(["@LÍDA\nx\n@Lida\ny", "@Lida:\nz\n@PANÍ BÁBA\nw\n@Paní bába\nv"]); assert.deepEqual(out, ["@Lída\nx\n@Lída\ny", "@Lída\nz\n@Paní bába\nw\n@Paní bába\nv"]); }); test("assemble numbers pages, marks gaps and failed pages", () => { const text = assemble([ { num: "5", body: "@Mise\nAno." }, { num: "?", body: "dál" }, { num: "9", body: "@Lída\nNe." }, { error: "model declined" }, ]); assert.equal(text, "=5\n@Mise\nAno.\n=6\ndál\n!7-8\n=9\n@Lída\nNe.\n=10\n>(Stranu se nepodařilo přepsat: model declined)\n"); const pages = parsePlay(text); assert.deepEqual(pages.map((p) => p.num), ["5", "6", "7-8", "9", "10"]); assert.equal(pages[1].blocks[0].speaker, "Mise"); // continues across the page break assert.deepEqual(lint(pages), []); }); const stub = (response) => ({ messages: { create: async (params) => { stub.last = params; return response; } } }); test("transcribePage sends the image and reads text blocks", async () => { const client = stub({ stop_reason: "end_turn", content: [{ type: "thinking", thinking: "" }, { type: "text", text: "=1\n@A\nx" }], usage: {} }); const { text } = await transcribePage(client, "claude-haiku-5-5", Buffer.from([0xff, 0xd8, 1])); assert.equal(text, "=1\n@A\nx"); const img = stub.last.messages[0].content[0]; assert.equal(img.source.media_type, "image/jpeg"); assert.equal(stub.last.model, "claude-haiku-5-5"); }); test("transcribePage reports refusals and truncation", async () => { await assert.rejects(transcribePage(stub({ stop_reason: "refusal", content: [] }), "m", Buffer.alloc(2)), PageError); await assert.rejects(transcribePage(stub({ stop_reason: "max_tokens", content: [{ type: "text", text: "x" }] }), "m", Buffer.alloc(2)), PageError); });