import test from "node:test"; import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { mkdir, 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/_articleDelete.test.ts" // // delete-article and delete-iteration (release 27 follow-up): adapters over // lib/report/delete.ts — behind the token, a dry run that writes nothing, a // refusal that is a 409 carrying the plan, and the projects root read from // settings.generation. const corpus = await setupOpsCorpus(null); const projectsRoot = path.join(corpus.root, "projects"); await corpus.writeSettings({ minFreeDiskGB: 0, generation: { projectsRoot } }); test.after(() => corpus.cleanup()); type Post = Parameters[0]; const route = async (name: string) => (await import(`./${name}/route`)).POST as Post; const articles = path.join(corpus.transcripts, "reports"); const doc = (id: string) => ({ format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, sections: [{ id: "s1", title: "One", body: "Plain words." }], }); test("both routes are behind the token and refuse unknown keys", async () => { for (const name of ["delete-article", "delete-iteration"]) { const res = await callPost(await route(name), { id: "a" }, {}); assert.equal(res.status, 401, name); } const stray = await callPost(await route("delete-article"), { id: "a", confirm: "a" }); assert.equal(stray.status, 400); const badN = await callPost(await route("delete-iteration"), { id: "a", n: "1" }); assert.equal(badN.status, 400); assert.match(String(badN.body.error), /iteration number/); }); test("delete-article: a dry run plans, a published one is a 409 with its plan, force deletes", async () => { const { writeArticle, configureArticle } = await import("yt-dlp-transcript-common/lib/report/registry"); const { createIteration, updateIteration } = await import("yt-dlp-transcript-common/lib/report/iterations"); await writeArticle(doc("gone")); await configureArticle("gone", { published: true }); await createIteration("gone", { kind: "sweep", model: "qwen3:8b", question: "Q?" }); await updateIteration("gone", 1, { status: "done" }); await mkdir(path.join(projectsRoot, "gone", "i1"), { recursive: true }); await writeFile(path.join(projectsRoot, "gone", "i1", "video.manifest.json"), "{}"); const post = await route("delete-article"); const missing = await callPost(post, { id: "nope", dryRun: true }); assert.equal(missing.status, 400); assert.match(String(missing.body.error), /no article "nope"/); const refused = await callPost(post, { id: "gone", dryRun: true }); assert.equal(refused.status, 409); assert.match(String(refused.body.error), /is published/); assert.ok((refused.body.plan as { remove: unknown[] }).remove.length >= 2); const dry = await callPost(post, { id: "gone", dryRun: true, force: true }); assert.equal(dry.status, 200); assert.equal(dry.body.applied, false); const labels = (dry.body.plan as { remove: { label: string }[] }).remove.map((e) => e.label); assert.deepEqual(labels, ["reports/gone/", "projects/gone/i1/"]); assert.ok(existsSync(path.join(articles, "gone", "report.json")), "a dry run writes nothing"); const done = await callPost(post, { id: "gone", force: true }); assert.equal(done.status, 200); assert.equal(done.body.applied, true); assert.deepEqual(done.body.failed, []); assert.equal(existsSync(path.join(articles, "gone")), false); assert.equal(existsSync(path.join(projectsRoot, "gone")), false); }); test("delete-iteration: a parent is refused with its children named; a leaf goes", async () => { const { createIteration, updateIteration } = await import("yt-dlp-transcript-common/lib/report/iterations"); await createIteration("its", { kind: "sweep", model: "qwen3:8b", question: "Q?" }); await updateIteration("its", 1, { status: "done" }); await createIteration("its", { kind: "sweep", model: "claude-sonnet-5-5", copyOf: 1 }); await updateIteration("its", 2, { status: "done" }); const post = await route("delete-iteration"); const parent = await callPost(post, { id: "its", n: 1 }); assert.equal(parent.status, 409); assert.match(String(parent.body.error), /i2 is a copy of it/); const leaf = await callPost(post, { id: "its", n: 2 }); assert.equal(leaf.status, 200); assert.equal(existsSync(path.join(articles, "its", "iterations", "2")), false); const none = await callPost(post, { id: "its", n: 2, dryRun: true }); assert.equal(none.status, 400); assert.match(String(none.body.error), /has no iteration i2/); });