import { test } from "node:test"; import assert from "node:assert/strict"; import { existsSync } from "node:fs"; import { lstat, mkdir, mkdtemp, readdir, readFile, readlink, rm, symlink, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { acquirePublishLock } from "./stageLock"; import { deleteSiteByPlan, formatSiteDeletePlan, planSiteDelete, siteDeleteBlocked, SiteDeleteRefusedError } from "./siteDelete"; // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test publish/siteDelete.test.ts // // Release 28 H5: a site's delete over a temp transcripts/ and export/ — never // the real ones. async function tree() { const dir = await mkdtemp(path.join(os.tmpdir(), "site-delete-")); const paths = { sitesDir: path.join(dir, "transcripts", "sites"), channelsDir: path.join(dir, "transcripts", "channels"), exportDir: path.join(dir, "export"), exportBuildsDir: path.join(dir, "export", ".export-builds"), exportSitesIndexDir: path.join(dir, "export", ".export-index", "sites"), }; const site = async (id: string, json: Record = {}, o: { reports?: string[]; builds?: boolean; deployed?: unknown } = {}) => { const d = path.join(paths.sitesDir, id); await mkdir(d, { recursive: true }); await writeFile(path.join(d, "site.json"), JSON.stringify({ siteId: id, siteTitle: id, channels: ["demo-channel"], audience: "private", ...json })); await writeFile(path.join(d, "tags.json"), "{}"); for (const r of o.reports ?? []) { await mkdir(path.join(d, "reports", r), { recursive: true }); await writeFile(path.join(d, "reports", r, "report.json"), "{}"); } if (o.builds !== false) { const b = path.join(paths.exportBuildsDir, id); await mkdir(path.join(b, "out"), { recursive: true }); await writeFile(path.join(b, "built.json"), "{}"); await writeFile(path.join(b, "out", "index.html"), ""); if (o.deployed) await writeFile(path.join(b, "deployed.json"), JSON.stringify(o.deployed)); await mkdir(path.join(paths.exportSitesIndexDir, id, "report-media"), { recursive: true }); await writeFile(path.join(paths.exportSitesIndexDir, id, "summary.json"), "{}"); } }; // A channel the site's pool names: never touched. await mkdir(path.join(paths.channelsDir, "demo-channel", "data", "v1"), { recursive: true }); await writeFile(path.join(paths.channelsDir, "demo-channel", "config.json"), "{}"); await writeFile(path.join(paths.channelsDir, "demo-channel", "data", "v1", "transcript.json"), "{}"); return { dir, paths, site, done: () => rm(dir, { recursive: true, force: true }) }; } test("a private site with nothing deployed: the plan, then its three directories go, and nothing else", async () => { const t = await tree(); try { await t.site("doomed"); await t.site("keeper"); const plan = await planSiteDelete("doomed", t.paths); assert.equal(plan.exists, true); assert.deepEqual(plan.refusals, []); assert.deepEqual(plan.remove.map((e) => e.label), ["sites/doomed/", "export/.export-builds/doomed/", "export/.export-index/sites/doomed/"]); assert.equal(plan.files, 5); assert.ok(plan.kept.some((k) => k.what === "every channel's data")); assert.match(formatSiteDeletePlan(plan)[0]!, /^delete site "doomed": 3 path\(s\)/); const r = await deleteSiteByPlan("doomed", t.paths); assert.equal(r.removed.length, 3); assert.deepEqual(r.failed, []); for (const e of plan.remove) assert.equal(existsSync(e.path), false, e.label); // No park left behind; the other site and the channel untouched. assert.deepEqual(await readdir(t.paths.sitesDir), ["keeper"]); assert.deepEqual((await readdir(t.paths.exportBuildsDir)).filter((n) => !n.startsWith(".publish")), ["keeper"]); assert.ok(existsSync(path.join(t.paths.exportSitesIndexDir, "keeper", "summary.json"))); assert.equal(await readFile(path.join(t.paths.channelsDir, "demo-channel", "data", "v1", "transcript.json"), "utf8"), "{}"); // Gone means gone: a second delete has no site to delete. await assert.rejects(deleteSiteByPlan("doomed", t.paths), (e: unknown) => e instanceof SiteDeleteRefusedError && /no site "doomed"/.test(e.message)); } finally { await t.done(); } }); test("a deploy target, a recorded deploy or reports of its own refuse unless force; force deletes and keeps the remote", async () => { const t = await tree(); try { await t.site("public-one", { audience: "public", cloudflareProject: "demo-pages", siteUrl: "https://demo.example" }, { reports: ["a-report"], deployed: { v: 1, target: "public-one", previews: {}, production: { builtStampId: "x", builtAt: 1, kind: "production", url: "https://demo.example", at: 2, liveCheck: null } }, }); const plan = await planSiteDelete("public-one", t.paths); assert.deepEqual( plan.refusals.map((r) => [r.reason, r.forceable]), [ ['it deploys to the Cloudflare Pages project "demo-pages"', true], ["it holds 1 report (a-report), which go with it", true], ["the publish state records a deploy: production (https://demo.example)", true], ], ); assert.equal(siteDeleteBlocked(plan), true); assert.equal(siteDeleteBlocked(plan, true), false); await assert.rejects(deleteSiteByPlan("public-one", t.paths), SiteDeleteRefusedError); assert.ok(existsSync(path.join(t.paths.sitesDir, "public-one", "site.json")), "a refusal writes nothing"); assert.ok(plan.kept.some((k) => k.what === 'the Pages project "demo-pages"')); const r = await deleteSiteByPlan("public-one", t.paths, { force: true }); assert.equal(r.removed.length, 3); assert.equal(existsSync(path.join(t.paths.sitesDir, "public-one")), false); } finally { await t.done(); } }); test("never forceable: no such site, a site dir that is a link, a publish stage running", async () => { const t = await tree(); try { const none = await planSiteDelete("ghost", t.paths); assert.equal(none.exists, false); assert.deepEqual(none.refusals, [{ reason: 'no site "ghost" (sites/ghost/site.json)', forceable: false }]); // A link: refused, never followed. await t.site("real", {}, { builds: false }); await symlink(path.join(t.paths.sitesDir, "real"), path.join(t.paths.sitesDir, "alias")); const link = await planSiteDelete("alias", t.paths); assert.equal(siteDeleteBlocked(link, true), true); assert.match(link.refusals[0]!.reason, /sites\/alias is a link/); // A publish stage holds the lock (this process, alive): refused, force or not. await t.site("busy"); const lock = await acquirePublishLock(t.paths, { kind: "build-site", target: "other" }); try { const busy = await planSiteDelete("busy", t.paths); assert.deepEqual(busy.refusals.map((r) => r.forceable), [false]); assert.match(busy.refusals[0]!.reason, /a publish stage is running: build-site other/); await assert.rejects(deleteSiteByPlan("busy", t.paths, { force: true }), SiteDeleteRefusedError); } finally { await lock.release(); } assert.ok(existsSync(path.join(t.paths.sitesDir, "busy", "site.json"))); assert.equal((await deleteSiteByPlan("busy", t.paths)).removed.length, 3); assert.ok((await lstat(path.join(t.paths.sitesDir, "real"))).isDirectory(), "the link's target is kept"); } finally { await t.done(); } }); test("export/out linking into the site's bundle is named and kept", async () => { const t = await tree(); try { await t.site("linked"); await mkdir(t.paths.exportDir, { recursive: true }); const out = path.join(t.paths.exportDir, "out"); await symlink(path.join(t.paths.exportBuildsDir, "linked", "out"), out); const plan = await planSiteDelete("linked", t.paths); assert.ok(plan.kept.some((k) => k.what === "export/out" && /next build makes it again/.test(k.why))); await deleteSiteByPlan("linked", t.paths); assert.equal(await readlink(out), path.join(t.paths.exportBuildsDir, "linked", "out")); } finally { await t.done(); } });