import { test, type TestContext } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, readdir, rm, writeFile, lstat } from "node:fs/promises"; import path from "node:path"; import { tmpdir } from "node:os"; import { commitLocalMediaRecord } from "./localMediaCommit-server"; async function fixture(t: TestContext) { const root = await mkdtemp(path.join(tmpdir(), "local-media-commit-")); t.after(() => rm(root, { recursive: true, force: true })); const dataDirectory = path.join(root, "data"); await mkdir(dataDirectory); const input = { dataDirectory, directoryId: "test-id", metadata: JSON.stringify({ id: "test-id", title: "Synthetic", duration: 3, upload_date: "20261030", webpage_url: "https://www.youtube.com/watch?v=test-id" }), transcript: "WEBVTT\r\n\r\n00:01.000 --> 00:02.000\r\nSynthetic cue\r\n", reference: { version: 1, kind: "external", sourceId: "library-1", relativePath: "Original ⧸ $title.mp4" } }; return { root, input, target: path.join(dataDirectory, "test-id") }; } test("publishes only canonical text/reference files, metadata last, preserving supplied bytes", async (t) => { const f = await fixture(t); const result = await commitLocalMediaRecord(f.input, { afterReserve: async () => { assert.deepEqual(await readdir(f.target), []); assert.deepEqual(await readdir(f.input.dataDirectory), ["test-id"]); } }); assert.deepEqual(result, { status: "imported" }); assert.deepEqual((await readdir(f.target)).sort(), ["external-media.json", "metadata.info.json", "transcript.en.vtt"]); assert.equal(await readFile(path.join(f.target, "metadata.info.json"), "utf8"), f.input.metadata); assert.equal(await readFile(path.join(f.target, "transcript.en.vtt"), "utf8"), f.input.transcript); assert.deepEqual(JSON.parse(await readFile(path.join(f.target, "external-media.json"), "utf8")), f.input.reference); assert.deepEqual(await readdir(f.root), ["data"]); }); test("retry never overwrites an imported record", async (t) => { const f = await fixture(t); assert.deepEqual(await commitLocalMediaRecord(f.input), { status: "imported" }); await writeFile(path.join(f.target, "transcript.en.vtt"), "operator edit"); assert.deepEqual(await commitLocalMediaRecord(f.input), { status: "occupied" }); assert.equal(await readFile(path.join(f.target, "transcript.en.vtt"), "utf8"), "operator edit"); }); test("existing empty directory is occupied, not replaced by rename", async (t) => { const f = await fixture(t); await mkdir(f.target); assert.deepEqual(await commitLocalMediaRecord(f.input), { status: "occupied" }); assert.deepEqual(await readdir(f.target), []); }); test("concurrent commits have exactly one winner", async (t) => { const f = await fixture(t); const results = await Promise.all([commitLocalMediaRecord(f.input), commitLocalMediaRecord(f.input)]); assert.deepEqual(results.map(r => r.status).sort(), ["imported", "occupied"]); assert.equal(await readFile(path.join(f.target, "metadata.info.json"), "utf8"), f.input.metadata); }); test("competing file publication is preserved and metadata is not published", async (t) => { const f = await fixture(t); const result = await commitLocalMediaRecord(f.input, { afterReserve: () => writeFile(path.join(f.target, "transcript.en.vtt"), "other producer") }); assert.equal(result.status, "failed"); assert.equal(result.incomplete, true); assert.equal(await readFile(path.join(f.target, "transcript.en.vtt"), "utf8"), "other producer"); await assert.rejects(lstat(path.join(f.target, "metadata.info.json")), { code: "ENOENT" }); assert.deepEqual(await commitLocalMediaRecord(f.input), { status: "occupied" }); }); test("cancellation before starting has no writes; cancellation after reservation leaves reviewable partial directory", async (t) => { const f = await fixture(t); const early = new AbortController(); early.abort(); await assert.rejects(commitLocalMediaRecord({ ...f.input, signal: early.signal })); assert.deepEqual(await readdir(f.input.dataDirectory), []); const late = new AbortController(); const result = await commitLocalMediaRecord({ ...f.input, signal: late.signal }, { afterReserve: async () => { late.abort(); } }); assert.deepEqual(result, { status: "failed", incomplete: true, reason: "cancelled" }); assert.deepEqual(await readdir(f.target), []); assert.deepEqual(await readdir(f.root), ["data"]); }); test("invalid input is rejected before creating a record or staging directory", async (t) => { const f = await fixture(t); for (const change of [{ directoryId: "../escape" }, { metadata: "{}" }, { transcript: "WEBVTT\n" }, { reference: {} }]) { await assert.rejects(commitLocalMediaRecord({ ...f.input, ...change })); } assert.deepEqual(await readdir(f.root), ["data"]); assert.deepEqual(await readdir(f.input.dataDirectory), []); }); test("injected disk failure leaves no indexable record and never reports success", async (t) => { const f = await fixture(t); const result = await commitLocalMediaRecord(f.input, { afterReserve: async () => { throw Object.assign(new Error("disk full"), { code: "ENOSPC" }); } }); assert.deepEqual(result, { status: "failed", incomplete: true, reason: "ENOSPC" }); await assert.rejects(lstat(path.join(f.target, "metadata.info.json")), { code: "ENOENT" }); }); test("failure after publishing transcript still leaves metadata absent and preserves competing reference", async (t) => { const f = await fixture(t); const result = await commitLocalMediaRecord(f.input, { afterReserve: () => writeFile(path.join(f.target, "external-media.json"), "other reference") }); assert.equal(result.status, "failed"); assert.equal(result.incomplete, true); assert.equal(await readFile(path.join(f.target, "transcript.en.vtt"), "utf8"), f.input.transcript); assert.equal(await readFile(path.join(f.target, "external-media.json"), "utf8"), "other reference"); await assert.rejects(lstat(path.join(f.target, "metadata.info.json")), { code: "ENOENT" }); });