import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, stat } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { writeSite, type Site } from "../lib/site"; import { dropChannelFromSites } from "./siteMemberships"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/siteMemberships.test.ts // // A deleted channel leaves no site naming it (9 Nov: `mooler` stayed in // efapalyzer's pool): the pool and the cited scope drop it, through writeSite; // a site that does not name it is not written. const site = (siteId: string, extra: Partial = {}): Site => ({ siteId, siteTitle: siteId, siteDescription: "", headerTitle: siteId, homeTagline: "", groups: [{ id: "default", name: "All", selectedByDefault: true }], defaultGroupId: "default", channels: [], ...extra, }); test("drops the slug from every pool and cited scope; leaves other sites unwritten", async () => { const dir = await mkdtemp(path.join(tmpdir(), "ttb-site-drop-")); const paths = { sitesDir: path.join(dir, "sites"), channelsDir: path.join(dir, "channels") } as Paths; const file = (id: string) => path.join(paths.sitesDir, id, "site.json"); const read = async (id: string) => JSON.parse(await readFile(file(id), "utf8")) as Site; try { await writeSite(site("pool", { channels: [{ slug: "gone", order: 0 }, { slug: "kept", groupId: "default", order: 1 }] }), paths); await writeSite(site("cites", { citedChannels: [{ slug: "gone" }, { slug: "other" }] }), paths); await writeSite(site("only-cites", { citedChannels: [{ slug: "gone" }] }), paths); await writeSite(site("untouched", { channels: [{ slug: "kept", order: 0 }] }), paths); const untouchedAt = (await stat(file("untouched"))).mtimeMs; const r = await dropChannelFromSites(paths, "gone"); assert.deepEqual(r, { updated: ["cites", "only-cites", "pool"], failed: [] }); assert.deepEqual((await read("pool")).channels, [{ slug: "kept", groupId: "default", order: 1 }]); assert.deepEqual((await read("cites")).citedChannels, [{ slug: "other" }]); assert.equal("citedChannels" in (await read("only-cites")), false, "an empty scope is not written"); assert.equal((await stat(file("untouched"))).mtimeMs, untouchedAt, "a site not naming it is not written"); assert.deepEqual(await dropChannelFromSites(paths, "gone"), { updated: [], failed: [] }, "a second pass finds none"); assert.deepEqual(await dropChannelFromSites({ ...paths, sitesDir: path.join(dir, "none") }, "gone"), { updated: [], failed: [], }); } finally { await rm(dir, { recursive: true, force: true }); } });