Sort pages by their number, drop repeated photos, mark uncertain words
The model now wraps words it isn't sure of in {word} or {word|other}; the
reader shows the first reading. Pages are ordered by the number on each
photo and a page photographed twice is kept once (fewer uncertain words,
then more text). meta.photos records which photo goes with which page.
New POST /images/:n/transcribe re-reads a single photo for the editor.
Seeded plays get an img/ folder on their first photo.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVRDXpoukzgMFumoJt
This commit is contained in:
parent
3f2cf95f1d
commit
ab842a1d73
8 changed files with 187 additions and 43 deletions
|
|
@ -8,14 +8,16 @@ import { config, createServer } from "../server/main.mjs";
|
|||
import { createStore } from "../server/store.mjs";
|
||||
|
||||
const PASS = "tajne-heslo";
|
||||
const JPEG = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 1, 2, 3]);
|
||||
const replies = ["=-1-\n~JEDNÁNÍ PRVNÍ\n@LÍDA\nDobrý den.", "=2\nještě já.\n@Mise\nAno."];
|
||||
const provider = { name: "stub", model: "stub-model", transcribe: async () => replies.shift() };
|
||||
// photo n is a tiny "JPEG" ending in byte n; the stub answers by that byte
|
||||
const jpeg = (n) => Buffer.from([0xff, 0xd8, 0xff, 0xe0, n]);
|
||||
const JPEG = jpeg(1);
|
||||
const replies = { 1: "=-1-\n~JEDNÁNÍ PRVNÍ\n@LÍDA\nDobrý den.", 2: "=2\nještě já.\n@Mise\nAno." };
|
||||
const provider = { name: "stub", model: "stub-model", transcribe: async (img) => replies[img.at(-1)] };
|
||||
|
||||
let dir, server, base;
|
||||
test.before(async () => {
|
||||
dir = await mkdtemp(path.join(tmpdir(), "tr-"));
|
||||
Object.assign(config, { dataDir: dir, password: PASS, dailyPageLimit: 3 });
|
||||
Object.assign(config, { dataDir: dir, password: PASS, dailyPageLimit: 4 });
|
||||
const store = createStore(dir);
|
||||
await store.init();
|
||||
await store.seed(config.seedDir);
|
||||
|
|
@ -34,6 +36,12 @@ test("seeded play is listed and readable", async () => {
|
|||
assert.match(text, /^!1-4\n=5\n@Mise/);
|
||||
});
|
||||
|
||||
test("a photo can be added to a seeded play (which has no img/ folder)", async () => {
|
||||
const res = await call("/api/plays/hra-1931/images/1", authed({ method: "PUT", headers: { "content-type": "image/jpeg" }, body: JPEG }));
|
||||
assert.equal(res.status, 200);
|
||||
assert.equal((await res.json()).images, 1);
|
||||
});
|
||||
|
||||
test("static files are served with security headers, traversal is refused", async () => {
|
||||
const res = await call("/");
|
||||
assert.equal(res.status, 200);
|
||||
|
|
@ -62,7 +70,7 @@ test("upload → transcribe → draft → publish", async () => {
|
|||
|
||||
assert.equal((await call(`/api/plays/${id}/images/1`, authed({ method: "PUT", headers: { "content-type": "image/png" }, body: JPEG }))).status, 415);
|
||||
for (const n of [1, 2]) {
|
||||
res = await call(`/api/plays/${id}/images/${n}`, authed({ method: "PUT", headers: { "content-type": "image/jpeg" }, body: JPEG }));
|
||||
res = await call(`/api/plays/${id}/images/${n}`, authed({ method: "PUT", headers: { "content-type": "image/jpeg" }, body: jpeg(n) }));
|
||||
assert.equal(res.status, 200);
|
||||
}
|
||||
res = await call(`/api/plays/${id}/transcribe`, authed({ method: "POST" }));
|
||||
|
|
@ -78,13 +86,21 @@ test("upload → transcribe → draft → publish", async () => {
|
|||
assert.equal(meta.failed, 0);
|
||||
const text = await (await call(`/api/plays/${id}/text`, authed())).text();
|
||||
assert.equal(text, "=1\n~JEDNÁNÍ PRVNÍ\n@Lída\nDobrý den.\n=2\nještě já.\n@Mise\nAno.\n");
|
||||
assert.deepEqual(meta.photos, [1, 2]);
|
||||
|
||||
// the editor can re-read one photo; speaker spellings follow the play
|
||||
replies[2] = "=2\n@LÍDA\nZnovu {přečteno}.";
|
||||
res = await call(`/api/plays/${id}/images/2/transcribe`, authed({ method: "POST" }));
|
||||
assert.deepEqual(await res.json(), { num: "2", body: "@Lída\nZnovu {přečteno}." });
|
||||
res = await call(`/api/plays/${id}`, authed({ method: "PATCH", body: JSON.stringify({ photos: [2, 9] }) }));
|
||||
assert.equal(res.status, 400); // there is no photo 9
|
||||
|
||||
res = await call(`/api/plays/${id}`, authed({ method: "PATCH", body: JSON.stringify({ published: true }) }));
|
||||
assert.equal((await res.json()).published, true);
|
||||
assert.equal((await (await call("/api/plays")).json()).length, 2);
|
||||
assert.equal((await call(`/api/plays/${id}/images/2`)).status, 200);
|
||||
|
||||
// the daily cap (3 pages) is now nearly used up
|
||||
// the daily cap (4 pages) is now nearly used up
|
||||
const id2 = (await (await call("/api/plays", authed({ method: "POST", body: JSON.stringify({ title: "Další" }) }))).json()).id;
|
||||
for (const n of [1, 2]) await call(`/api/plays/${id2}/images/${n}`, authed({ method: "PUT", headers: { "content-type": "image/jpeg" }, body: JPEG }));
|
||||
res = await call(`/api/plays/${id2}/transcribe`, authed({ method: "POST" }));
|
||||
|
|
|
|||
|
|
@ -17,19 +17,48 @@ test("speaker spellings are unified across pages", () => {
|
|||
});
|
||||
|
||||
test("assemble numbers pages, marks gaps and failed pages", () => {
|
||||
const text = assemble([
|
||||
const { text, photos } = 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");
|
||||
assert.deepEqual(photos, [1, 2, null, 3, 4]);
|
||||
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), []);
|
||||
});
|
||||
|
||||
test("assemble sorts by page number and drops repeated photos of a page", () => {
|
||||
const { text, photos, duplicates } = assemble([
|
||||
{ num: "1", body: "{jedna}" },
|
||||
{ num: "3", body: "tři" },
|
||||
{ num: "?", body: "po trojce" }, // no number: stays after the photo uploaded before it
|
||||
{ num: "2", body: "dva" },
|
||||
{ num: "1", body: "jedna" }, // retake of page 1 with no uncertain words: wins
|
||||
{ num: "3", body: "tři" }, // the same photo again
|
||||
]);
|
||||
assert.equal(text, "=1\njedna\n=2\ndva\n=3\ntři\n=4\npo trojce\n");
|
||||
assert.deepEqual(photos, [5, 4, 2, 3]);
|
||||
assert.deepEqual(duplicates.map((d) => [d.image, d.page, d.kept]), [[1, "1", 5], [6, "3", 2]]);
|
||||
});
|
||||
|
||||
test("an unnumbered page whose guess is taken keeps no number", () => {
|
||||
const { text } = assemble([{ num: "1", body: "a" }, { num: "?", body: "b" }, { num: "2", body: "c" }]);
|
||||
assert.equal(text, "=1\na\n=?\nb\n=2\nc\n");
|
||||
});
|
||||
|
||||
test("uncertain words: old [?] marks are converted, the reader drops the braces", () => {
|
||||
assert.equal(splitPage("=1\nTo je divné[?] slovo, ne[?]?").body, "To je {divné} slovo, {ne}?");
|
||||
const [page] = parsePlay("=1\n@{Lída}\nTo je {divné|dívčí} slovo.", { plain: true });
|
||||
assert.equal(page.blocks[0].speaker, "Lída");
|
||||
assert.deepEqual(page.blocks[0].parts[0].lines, ["To je divné slovo."]);
|
||||
assert.equal(normalizeSpeakers(["@{LÍDA}\nx"])[0], "@Lída\nx");
|
||||
assert.deepEqual(lint(parsePlay("=1\n@A\nnějaké {slovo")).map((x) => x.message), ["Neuzavřená složená závorka: „nějaké {slovo“"]);
|
||||
});
|
||||
|
||||
const stub = (response) => ({ messages: { create: async (params) => { stub.last = params; return response; } } });
|
||||
const JPEG = Buffer.from([0xff, 0xd8, 1]);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue