import { test } from "node:test"; import assert from "node:assert/strict"; import { connectWith, fakeEditor, isError, textOf } from "./editorOps.testkit"; import { editorGet, editorPost, editorTranscript, renderEditorTranscript, describeEditorFailure } from "./editorOps"; // ─── The editor's ops routes from the MCP (release 19 A7) ─── // // No network: a fake editor answers by URL, and records what was sent. test("a GET and a POST carry the token to the editor's URL; refusals and silence are said", async () => { const { deps, sent } = fakeEditor((s) => s.url.endsWith("/bad") ? { status: 400, body: { ok: false, error: "no such thing" } } : { status: 200, body: { ok: true, x: 1 } }, ); const ok = await editorGet(deps, "/api/ops/job/abc"); assert.equal(ok.kind, "ok"); assert.equal(sent[0].url, "http://editor.test/api/ops/job/abc"); assert.equal(sent[0].headers?.authorization, "Bearer tok"); const posted = await editorPost(deps, "/api/ops/sync", { slug: "x" }); assert.equal(posted.kind, "ok"); assert.deepEqual(sent[1].body, { slug: "x" }); assert.equal(sent[1].headers?.["content-type"], "application/json"); const bad = await editorGet(deps, "/bad"); assert.equal(bad.kind, "refused"); assert.match(describeEditorFailure("t", bad as never), /^t: the editor refused \(HTTP 400\): no such thing$/); const none = await editorGet({ env: {}, fetch: deps.fetch }, "/x"); assert.equal(none.kind, "no_editor"); assert.match(describeEditorFailure("t", none as never), /ARCHILYZER_EDITOR_URL.*WORKER_TOKEN/); const gone = await editorGet(fakeEditor(() => new TypeError("fetch failed")).deps, "/x"); assert.equal(gone.kind, "unreachable"); assert.match(describeEditorFailure("t", gone as never), /did not answer: fetch failed/); }); const CUES = [ { start: 1, end: 3, text: "first line" }, { start: 65, end: 67, text: "second line" }, ]; test("editorTranscript asks GET /api/ops/transcript; a 404 is not an error, other refusals are", async () => { const { deps, sent } = fakeEditor((s) => s.url.includes("id=held") ? { status: 200, body: { ok: true, slug: "demo", id: "held", source: "vtt", cuesJson: "missing", cues: CUES } } : { status: 404, body: { ok: false, error: "no channel holds it" } }, ); const held = await editorTranscript(deps, "held", "demo"); assert.equal(held.ok, true); assert.equal(new URL(sent[0].url).searchParams.get("slug"), "demo"); const missing = await editorTranscript(deps, "nope"); assert.deepEqual(missing, { ok: false, error: null }); assert.equal(new URL(sent[1].url).searchParams.has("slug"), false); assert.deepEqual(await editorTranscript(deps, "../x"), { ok: false, error: null }); assert.equal(sent.length, 2); }); test("the rendered fallback says where it came from and carries no moment links", () => { const md = renderEditorTranscript( { slug: "demo", id: "held", source: "built", cuesJson: "missing", title: "A title", file: "transcript.en.vtt", cues: CUES }, { timestamps: true }, ); assert.match(md, /^> NOT IN THE ARCHIVE — read from the local editor's disk \(channel demo\)/); assert.match(md, /normalized in memory/); assert.match(md, /# A title/); assert.match(md, /\[1:05\] second line/); assert.doesNotMatch(md, /\]\(http/); }); test("get_transcript: a video not in the archive is read off the editor's disk", async () => { const { deps } = fakeEditor(() => ({ status: 200, body: { ok: true, slug: "demo", id: "fresh01", source: "cues.json", cuesJson: "fresh", title: "Fresh", cues: CUES }, })); const client = await connectWith(deps); const res = await client.callTool({ name: "get_transcript", arguments: { video_id: "fresh01", channel: "demo" } }); assert.equal(isError(res), false, textOf(res)); assert.match(textOf(res), /NOT IN THE ARCHIVE/); assert.match(textOf(res), /first line/); }); test("get_transcript: with no editor, or one that holds nothing, it is still 'video not found'", async () => { const none = await connectWith(fakeEditor(() => ({ status: 500, body: {} }), {}).deps); const a = await none.callTool({ name: "get_transcript", arguments: { video_id: "x1" } }); assert.equal(isError(a), true); assert.match(textOf(a), /^video not found: x1/); const empty = await connectWith(fakeEditor(() => ({ status: 404, body: { ok: false, error: "no" } })).deps); const b = await empty.callTool({ name: "get_transcript", arguments: { video_id: "x1" } }); assert.match(textOf(b), /^video not found: x1/); // A track is the archive's: the editor is not asked for one. const { deps, sent } = fakeEditor(() => ({ status: 200, body: { ok: true, cues: [] } })); const c = await (await connectWith(deps)).callTool({ name: "get_transcript", arguments: { video_id: "x1", track: "en" } }); assert.match(textOf(c), /^video not found: x1/); assert.equal(sent.length, 0); });