theater-reader/tests/transcribe.test.mjs
agent 46aa1236ed Let people add plays from photos; library, editor, compose; full-page PDF
- Replace the static nginx site with a small Node server: serves public/,
  stores plays on a /data volume and exposes a JSON API. Writes need a
  shared UPLOAD_PASSWORD; unpublished plays are hidden without it.
- Page photos (shrunk to <=1.15 MP JPEG in the browser) are transcribed
  one request per page by a Claude vision model (claude-haiku-5-5 by
  default, CLAUDE_MODEL to change) into the play markup, then stitched
  together: speaker spellings unified, gaps in page numbers marked as
  missing pages, failed pages flagged. DAILY_PAGE_LIMIT caps spend.
- New pages: library (play menu), add (upload + progress), editor (text
  next to the photos, checks for [?] readings and one-off names, publish,
  delete). The reader loads plays from the API and remembers the chosen
  role per play; old /?postava= links still work.
- PDF export: text now flows across original pages and prints at the
  on-screen size, so A4 sheets are full instead of half empty; page
  numbers in the footer.
- docker-compose.yml + .env.example for one-click deploys; the 1931 play
  moves to seed/ and is copied into an empty volume on first start.
- Tests: parser leniency, transcription assembly, API end to end with a
  stubbed Claude client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
2026-11-03 09:43:32 +00:00

46 lines
2.4 KiB
JavaScript

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);
});