import test from "node:test"; import assert from "node:assert/strict"; import type { SpaceScanResult, SpaceSuggestion } from "yt-dlp-transcript-common/lib/spaceSuggestions"; import { runSpaceSuggestion, type SpaceRunner } from "./runSpaceAction"; // Run with: // pnpm -C editor exec tsx --test "app/cleanup/space/lib/runSpaceAction.test.ts" // // The page's button names a suggestion by its key; what runs is the body the // SCAN wrote for it, through the existing writer — never a body the browser // sends. function row(extra: Partial): SpaceSuggestion { return { key: "k", kind: "evict-clips", title: "", frees: { disk: "corpus", bytes: 1 }, basis: "measured", basisText: "", loss: "", ...extra }; } function result(rows: SpaceSuggestion[]): SpaceScanResult { return { suggestions: rows } as unknown as SpaceScanResult; } function recorder() { const calls: { op: string; body: Record; dryRun: boolean }[] = []; const make = (op: string): SpaceRunner => async (body, o) => { calls.push({ op, body, dryRun: o.dryRun }); return { ok: true, lines: [`${op} ran`], jobIds: o.dryRun ? [] : [`job-${op}`], dryRun: o.dryRun }; }; const runners: Record = Object.fromEntries( ["compress-text", "evict-clips", "drop-saved-alternate", "relocate", "delete-file"].map((op) => [op, make(op)]), ); return { calls, runners }; } test("the scan's own body runs, each action in turn; a dry run only where the writer has one", async () => { const { calls, runners } = recorder(); const r = result([ row({ key: "a", actions: [{ op: "evict-clips", body: { slug: "s", id: "v1", olderThanDays: 30 } }, { op: "evict-clips", body: { slug: "s", id: "v2", olderThanDays: 30 } }] }), row({ key: "b", kind: "compress-captions", actions: [{ op: "compress-text", body: { slugs: ["s"], kinds: ["captions"] } }] }), ]); const out = await runSpaceSuggestion({ result: r, key: "a", dryRun: false, runners, recheck: async () => null }); assert.equal(out.ok, true); assert.deepEqual(out.jobIds, ["job-evict-clips", "job-evict-clips"]); assert.deepEqual(calls.map((c) => c.body.id), ["v1", "v2"]); // evict-clips' dry run is a job of its own: not offered as a preview. const pre = await runSpaceSuggestion({ result: r, key: "a", dryRun: true, runners, recheck: async () => null }); assert.equal(pre.ok, false); assert.match(pre.lines[0], /no dry run/); const dry = await runSpaceSuggestion({ result: r, key: "b", dryRun: true, runners, recheck: async () => null }); assert.equal(dry.ok, true); assert.equal(calls.at(-1)!.dryRun, true); assert.deepEqual(calls.at(-1)!.body, { slugs: ["s"], kinds: ["captions"] }); }); test("refusals: an unknown key, a row with only a link, a disabled row, an op with no runner, a body of the wrong shape", async () => { const { calls, runners } = recorder(); const r = result([ row({ key: "link", link: { href: "/x", label: "x" } }), row({ key: "off", actions: [{ op: "relocate", body: { slugs: ["s"], locationId: "p" } }], disabled: "does not fit" }), row({ key: "odd", actions: [{ op: "rm-rf", body: {} }] }), row({ key: "bad", actions: [{ op: "delete-file", body: { slug: "../etc", id: "v", file: "a.part" } }] }), row({ key: "badfile", actions: [{ op: "delete-file", body: { slug: "s", id: "v", file: "x/../../y" } }] }), ]); for (const [key, re] of [ ["nope", /no suggestion "nope"/], ["link", /nothing to run here/], ["off", /does not fit/], ["odd", /not a writer this page runs: rm-rf/], ["bad", /not a channel slug/], ["badfile", /not a file name/], ] as const) { const out = await runSpaceSuggestion({ result: r, key, dryRun: false, runners, recheck: async () => null }); assert.equal(out.ok, false, key); assert.match(out.lines.join(" "), re, key); } assert.equal(calls.length, 0); }); test("the click re-checks the row first: a refusal runs nothing and says why; the former-original flag is never sent", async () => { const { calls, runners } = recorder(); const r = result([row({ key: "p", actions: [{ op: "delete-file", body: { slug: "s", id: "v", file: "a.part" } }] })]); const out = await runSpaceSuggestion({ result: r, key: "p", dryRun: false, runners, recheck: async () => "the last scan is 30 h old: rescan first" }); assert.equal(out.ok, false); assert.match(out.lines[0], /rescan first/); assert.equal(calls.length, 0); const src = await import("node:fs/promises").then((fs) => fs.readFile(new URL("./runSpaceAction.ts", import.meta.url), "utf8")); assert.ok(!src.includes("confirm" + "FormerOriginal")); }); test("a failing action stops the rest and says so", async () => { const { runners } = recorder(); runners["delete-file"] = async () => ({ ok: false, lines: ["refused: drive not answering"], jobIds: [], dryRun: false }); const r = result([ row({ key: "p", actions: [{ op: "delete-file", body: { slug: "s", id: "v", file: "a.part" } }, { op: "delete-file", body: { slug: "s", id: "v", file: "a.ytdl" } }] }), ]); const out = await runSpaceSuggestion({ result: r, key: "p", dryRun: false, runners, recheck: async () => null }); assert.equal(out.ok, false); assert.deepEqual(out.lines, ["refused: drive not answering", "1 more not run"]); }); test("review 2: a scan file of another version is refused with rescan first, not run", async () => { const { calls, runners } = recorder(); const out = await runSpaceSuggestion({ result: null, fileVersion: 1, key: "p", dryRun: false, runners, recheck: async () => null }); assert.equal(out.ok, false); assert.match(out.lines[0], /older format \(version 1,.*rescan first/); assert.equal(calls.length, 0); });