import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, readdir, writeFile } from "node:fs/promises"; import path from "node:path"; import { callPost, setupOpsCorpus } from "./_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_localMediaDoors.test.ts" // // lane/local-media review fix 5: every door that would fetch, transcribe, // persist or write media refuses a local media library channel and a local // recording with lib/localMedia.ts's ONE sentence ("... with no online source, // so no clip can be fetched for it."), or its sibling for the metadata doors // ("... so its metadata cannot be read from one."), never a generic empty-URL // failure; and attach-media / import-media refuse a record linked to a library // original (external-media.json). Through each ops route; no job is queued and // no record changes. const corpus = await setupOpsCorpus(null); test.after(() => corpus.cleanup()); const route = async (name: string) => (await import(`./${name}/route`)).POST; const LOCAL = "local-" + "e".repeat(32); const lib = { sourceId: "library-1", directory: path.join(corpus.root, "library") }; await mkdir(lib.directory, { recursive: true }); await corpus.writeChannelConfig("library", { name: "Library", handling: "youtube", url: undefined, sourceKind: "video", localMedia: lib }); await corpus.writeChannelConfig("hybrid", { name: "Hybrid", handling: "youtube", url: "https://www.youtube.com/@hybrid", platform: "youtube", localMedia: lib }); const record = async (slug: string, id: string, meta: unknown, extra: Record = {}) => { const dir = path.join(corpus.transcripts, "channels", slug, "data", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify(meta)); await writeFile(path.join(dir, "transcript.en.vtt"), "WEBVTT\n\n00:00:01.000 --> 00:00:02.000\nA cue\n"); for (const [name, body] of Object.entries(extra)) await writeFile(path.join(dir, name), body); }; await record("library", "aaaaaaaaaaa", { id: "aaaaaaaaaaa", title: "Provider", upload_date: "20200101", webpage_url: "https://www.youtube.com/watch?v=aaaaaaaaaaa" }); await record("hybrid", LOCAL, { id: LOCAL, title: "Talk", local_recording: { version: 1, titleSource: "filename", inspection: "manual", durationSource: "unknown" } }); await record("hybrid", "linkedrec01", { id: "linkedrec01", title: "Linked", upload_date: "20200101", webpage_url: "https://www.youtube.com/watch?v=linkedrec01" }, { "external-media.json": JSON.stringify({ version: 1, kind: "external", sourceId: "library-1", relativePath: "linked.mp4" }) }); const FILE = path.join(corpus.root, "a-file.mp4"); await writeFile(FILE, "not really a video"); const FETCH = /with no online source, so no clip can be fetched for it\.$/; const METADATA = /with no online source, so its metadata cannot be read from one\.$/; const LINKED = /^"hybrid\/linkedrec01" is linked to a local media library original \(external-media\.json\), so no media is written into it\.$/; const CHANNEL = /^"library" is a local media library channel /; const RECORD = new RegExp(`^"${LOCAL}" is a local recording `); const DOORS: Array<[string, Record, RegExp, RegExp]> = [ ["transcribe-one", { slug: "library", id: "aaaaaaaaaaa" }, CHANNEL, FETCH], ["transcribe-one", { slug: "hybrid", id: LOCAL }, RECORD, FETCH], ["prepare-playable", { slug: "library" }, CHANNEL, FETCH], ["prepare-playable", { slug: "hybrid", ids: [LOCAL] }, RECORD, FETCH], ["persist-videos", { items: [{ slug: "library", id: "aaaaaaaaaaa" }] }, CHANNEL, FETCH], ["persist-videos", { items: [{ slug: "hybrid", id: LOCAL }] }, RECORD, FETCH], ["refresh-metadata", { slug: "library", id: "aaaaaaaaaaa" }, CHANNEL, METADATA], ["refresh-metadata", { slug: "hybrid", id: LOCAL }, RECORD, METADATA], ["metadata-scan", { slug: "library" }, CHANNEL, METADATA], ["browser-lookup", { slug: "library", listing: true }, CHANNEL, METADATA], ["browser-lookup", { slug: "hybrid", ids: [LOCAL] }, RECORD, METADATA], ["fetch-chat", { slug: "library", id: "aaaaaaaaaaa" }, CHANNEL, FETCH], ["fetch-chat", { slug: "hybrid", id: LOCAL }, RECORD, FETCH], ["live-clip", { slug: "library", id: "aaaaaaaaaaa", start: 1, end: 5 }, CHANNEL, FETCH], ["live-clip", { slug: "hybrid", id: LOCAL, start: 1, end: 5 }, RECORD, FETCH], ["import-media", { slug: "library", path: FILE, id: "aaaaaaaaaaa" }, CHANNEL, FETCH], ["import-media", { slug: "hybrid", path: FILE, id: LOCAL }, RECORD, FETCH], ["import-media", { slug: "hybrid", path: FILE, id: "linkedrec01" }, LINKED, LINKED], ["attach-media", { slug: "library", source: corpus.root }, CHANNEL, FETCH], ["attach-media", { slug: "hybrid", source: corpus.root, items: [{ id: LOCAL, path: "a-file.mp4" }] }, RECORD, FETCH], ["attach-media", { slug: "hybrid", source: corpus.root, items: [{ id: "linkedrec01", path: "a-file.mp4" }] }, LINKED, LINKED], ]; test("every door refuses a local library channel, a local recording and (writers) a linked record, with the one sentence", async () => { const before = await corpus.listJobIds(); const dirs = async () => (await readdir(path.join(corpus.transcripts, "channels", "hybrid", "data", "linkedrec01"))).sort(); const linkedBefore = await dirs(); for (const [name, body, who, sentence] of DOORS) { const r = await callPost(await route(name), body); const label = `${name} ${JSON.stringify(body)} -> ${r.status} ${JSON.stringify(r.body)}`; assert.equal(r.body.ok, false, label); assert.match(String(r.body.error), who, label); assert.match(String(r.body.error), sentence, label); assert.doesNotMatch(String(r.body.error), /Could not determine the video URL|no `url` configured/, label); } assert.deepEqual(await corpus.listJobIds(), before, "no job was queued"); assert.deepEqual(await dirs(), linkedBefore, "the linked record is unchanged"); });