theater-reader/tests/parse.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

50 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Run: node --test tests/ (or see AGENTS.md for the container one-liner)
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { parsePlay, speakers, joinLines, tidySpacing } from "../public/parse.js";
const play = parsePlay(readFileSync(new URL("../seed/hra-1931/play.txt", import.meta.url), "utf8"));
test("pages are in order and missing ones are marked", () => {
const nums = play.map((p) => p.num);
assert.deepEqual(nums.slice(0, 4), ["1-4", "5", "6", "7"]);
assert.deepEqual(play.filter((p) => p.missing).map((p) => p.num), ["1-4", "7"]);
assert.equal(nums.at(-1), "27");
});
test("text at the top of a page continues the previous speaker", () => {
const p15 = play.find((p) => p.num === "15");
assert.equal(p15.blocks[0].speaker, "Lysák");
assert.equal(p15.blocks[0].cont, true);
});
test("cast is found and counted", () => {
const names = speakers(play).map((s) => s.name);
for (const n of ["Mise", "Lída", "Lysák", "Továrník", "Švec", "Paní bába", "Lysáková"]) assert.ok(names.includes(n), n);
assert.ok(!names.includes(""));
});
test("verse stays separate from the stage direction before it", () => {
const p19 = play.find((p) => p.num === "19");
const julius = p19.blocks.find((b) => b.speaker === "Julius" && b.parts.some((x) => x.kind === "verse"));
assert.deepEqual(julius.parts.map((x) => x.kind), ["stage", "verse"]);
});
test("hyphenated line breaks are rejoined, dashes are kept", () => {
assert.equal(joinLines(["Mám vždycky velmi nepří-", "jemné poslání."]), "Mám vždycky velmi nepříjemné poslání.");
assert.equal(joinLines(["takovém houfci ne -", "spokojenců"]), "takovém houfci nespokojenců");
assert.equal(joinLines(["Jsem jen tak –", "nalehko."]), "Jsem jen tak – nalehko.");
});
test("spacing is tidied without touching numbers", () => {
assert.equal(tidySpacing("Ano, roky.Nyní je rok 1931.Jaké ?"), "Ano, roky. Nyní je rok 1931. Jaké?");
assert.equal(tidySpacing("nadějí .Jsem směšný"), "nadějí. Jsem směšný");
});
test("machine output without a speaker or page marker still parses", () => {
const pages = parsePlay("úvodní řádek\n=2\n@Lída\nAno.\n");
assert.equal(pages[0].num, "?");
assert.equal(pages[0].blocks[0].speaker, "");
assert.deepEqual(speakers(pages).map((s) => s.name), ["Lída"]);
});