// End-to-end API test against a real server on a temp data dir, with Claude stubbed out. import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { config, createServer } from "../server/main.mjs"; import { createStore } from "../server/store.mjs"; const PASS = "tajne-heslo"; // 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: 4 }); const store = createStore(dir); await store.init(); await store.seed(config.seedDir); server = createServer(store, provider).listen(0); base = `http://127.0.0.1:${server.address().port}`; }); test.after(async () => { server.close(); await rm(dir, { recursive: true, force: true }); }); const call = (p, opts = {}) => fetch(base + p, opts); const authed = (opts = {}) => ({ ...opts, headers: { "x-upload-password": PASS, ...(opts.headers || {}) } }); test("seeded play is listed and readable", async () => { const plays = await (await call("/api/plays")).json(); assert.deepEqual(plays.map((p) => p.id), ["hra-1931"]); const text = await (await call("/api/plays/hra-1931/text")).text(); 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); assert.match(res.headers.get("content-type"), /text\/html/); assert.match(res.headers.get("content-security-policy"), /default-src 'self'/); assert.equal((await call("/../server/main.mjs")).status, 404); assert.equal((await call("/%2e%2e/package.json")).status, 404); assert.equal((await call("/api/plays/..%2fx")).status, 404); }); test("writing needs the password", async () => { const res = await call("/api/plays", { method: "POST", body: JSON.stringify({ title: "X" }) }); assert.equal(res.status, 401); assert.equal((await call("/api/plays", { method: "POST", headers: { "x-upload-password": "nope" }, body: "{}" })).status, 401); }); test("upload → transcribe → draft → publish", async () => { let res = await call("/api/plays", authed({ method: "POST", body: JSON.stringify({ title: "Nová hra", note: "zkouška" }) })); assert.equal(res.status, 201); const { id } = await res.json(); assert.match(id, /^nova-hra-[0-9a-f]{6}$/); // drafts are hidden from the public assert.equal((await (await call("/api/plays")).json()).length, 1); assert.equal((await call(`/api/plays/${id}`)).status, 404); 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(n) })); assert.equal(res.status, 200); } res = await call(`/api/plays/${id}/transcribe`, authed({ method: "POST" })); assert.equal(res.status, 202); // wait for the background job let meta; for (let i = 0; i < 50; i++) { meta = await (await call(`/api/plays/${id}`, authed())).json(); if (meta.status === "draft") break; await new Promise((r) => setTimeout(r, 20)); } assert.equal(meta.status, "draft"); 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 (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" })); assert.equal(res.status, 429); // editing the text, then deleting res = await call(`/api/plays/${id}/text`, authed({ method: "PUT", body: "=1\n@Lída\nUpraveno.\n" })); assert.equal((await res.json()).edited, true); assert.equal((await call(`/api/plays/${id}`, authed({ method: "DELETE" }))).status, 200); assert.equal((await call(`/api/plays/${id}`, authed())).status, 404); });