import test from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import path from "node:path"; import { setupOpsCorpus, callPost } from "../_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/create-site/route.test.ts" // // `create-site`: the New site form over HTTP, saved by the form's own action // (saveSiteAction -> writeSite) — and "update": the site page's Save with the // given keys changed. The action does not revalidate, so its writes are // tested here, against a temp corpus. const corpus = await setupOpsCorpus(null); for (const slug of ["pooled", "critic", "other"]) { await corpus.writeChannelConfig(slug, { url: `https://www.youtube.com/@${slug}` }); } await corpus.writeSite("neighbour", {}); const { POST } = await import("./route"); const { getSite, writeSite, withSiteLock } = await import("yt-dlp-transcript-common/lib/site"); const { getPaths } = await import("yt-dlp-transcript-common/lib/paths"); const { siteToFormData } = await import("../../../sites/lib/siteFormData"); const { saveSiteAction } = await import("../../../sites/actions"); test.after(() => corpus.cleanup()); const siteFile = async (id: string) => JSON.parse(await readFile(path.join(corpus.transcripts, "sites", id, "site.json"), "utf8")) as Record< string, unknown >; test("a create is private and unlisted by default, its header the title, through the form's writer", async () => { const r = await callPost(POST, { siteId: "efapalyzer", siteTitle: "Efapalyzer", channels: ["pooled"] }); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(r.body.created, true); const file = await siteFile("efapalyzer"); assert.equal(file.siteTitle, "Efapalyzer"); assert.equal(file.headerTitle, "Efapalyzer"); assert.equal(file.audience, "private"); assert.equal(file.listed, false); assert.deepEqual(file.channels, [{ slug: "pooled", order: 0 }]); assert.equal("publish" in file, false, "publish off"); assert.equal("search" in file, false, "search on (absent)"); assert.deepEqual((r.body.site as { siteId: string }).siteId, "efapalyzer"); // Public, listed, grouped, cited, with a policy: every key the form's. const pub = await callPost(POST, { siteId: "shown", siteTitle: "Shownalyzer", headerTitle: "Shownalyzer", wordmarkLead: "Shown", audience: "public", listed: true, accent: "#cc3366", cloudflareProject: "shown-pages", siteUrl: "https://shown.example/", publishAuto: "production", groups: [ { id: "main", name: "Main", selectedByDefault: true }, { id: "extra", name: "Extra" }, ], defaultGroupId: "main", channels: [{ slug: "pooled", groupId: "extra" }], citedChannels: ["critic", "pooled"], archiveMaxMB: 24, }); assert.equal(pub.status, 200, JSON.stringify(pub.body)); const s = await siteFile("shown"); assert.equal("audience" in s, false, "public is the absent default"); assert.equal("listed" in s, false, "listed is the absent default"); assert.equal(s.accent, "#cc3366"); assert.equal(s.siteUrl, "https://shown.example"); assert.deepEqual(s.publish, { auto: "production" }); assert.deepEqual(s.channels, [{ slug: "pooled", groupId: "extra", order: 0 }]); assert.deepEqual(s.citedChannels, [{ slug: "critic" }], "a pooled slug is dropped from the cited scope"); assert.equal(s.archiveMaxBytes, 24 * 1024 * 1024); assert.equal(s.wordmarkLead, "Shown"); }); test("an existing id is refused unless update; update keeps every key it does not name", async () => { const before = await siteFile("shown"); const again = await callPost(POST, { siteId: "shown", siteTitle: "Other" }); assert.equal(again.status, 409); assert.match(String(again.body.error), /Site "shown" already exists\. "update": true saves the keys given over it/); assert.deepEqual(await siteFile("shown"), before, "nothing written"); const up = await callPost(POST, { siteId: "shown", update: true, homeTagline: "Every stream, searchable" }); assert.equal(up.status, 200, JSON.stringify(up.body)); assert.equal(up.body.created, false); assert.deepEqual(await siteFile("shown"), { ...before, homeTagline: "Every stream, searchable" }); const none = await callPost(POST, { siteId: "nosuch", update: true, siteTitle: "X" }); assert.equal(none.status, 404); assert.match(String(none.body.error), /Site "nosuch" not found — leave out "update" to create it/); }); test("the form's refusals, and the door's, before anything is written", async () => { for (const [body, status, re] of [ [{ siteId: "Bad Id", siteTitle: "X" }, 400, /not a valid site id/], [{ siteId: "new", siteTitle: "X" }, 400, /new/], [{ siteId: "untitled" }, 400, /a new site needs a "siteTitle"/], [{ siteId: "blank", siteTitle: " " }, 400, /Site title is required/], [{ siteId: "lead", siteTitle: "Leadalyzer", wordmarkLead: "lead" }, 400, /Wordmark lead must be how the header title starts/], [{ siteId: "url", siteTitle: "U", siteUrl: "shown.example" }, 400, /Public URL must be an absolute http\(s\):\/\/ URL/], [{ siteId: "aud", siteTitle: "A", audience: "friends" }, 400, /Unknown audience "friends"/], [{ siteId: "pol", siteTitle: "P", audience: "public", publishAuto: "production" }, 400, /cloudflareProject/], [{ siteId: "acc", siteTitle: "A", accent: "chartreuse" }, 400, /Unknown brand accent "chartreuse"/], [{ siteId: "grp", siteTitle: "G", groups: [] }, 400, /At least one channel group is required/], [{ siteId: "grp2", siteTitle: "G", groups: [{ name: "No id" }] }, 400, /"groups"\[0\] is not a channel group/], [{ siteId: "ch", siteTitle: "C", channels: ["pooled", "ghost"] }, 400, /not a channel: "ghost"/], [{ siteId: "cit", siteTitle: "C", citedChannels: ["ghost"] }, 400, /not a channel: "ghost"/], [{ siteId: "typ", siteTitle: "T", listed: "yes" }, 400, /"listed" must be a boolean/], [{ siteId: "typ2", siteTitle: "T", archiveMaxMB: "12" }, 400, /"archiveMaxMB" must be a number/], [{ siteId: "unk", siteTitle: "U", pages: ["a"] }, 400, /pages/], [ { siteId: "grptypo", siteTitle: "G", channels: [{ slug: "pooled", groupId: "typo" }] }, 400, /"channels": "pooled" names group "typo", which is not one of the site's groups \("default"\)/, ], ] as const) { const r = await callPost(POST, body); assert.equal(r.status, status, JSON.stringify(body) + " " + JSON.stringify(r.body)); assert.match(String(r.body.error), re, JSON.stringify(r.body)); const id = (body as { siteId: string }).siteId; if (id !== "new" && /^[a-z0-9-]+$/.test(id)) { await assert.rejects(readFile(path.join(corpus.transcripts, "sites", id, "site.json")), `${id}: nothing written`); } } }); // The round trip "update" rests on: a stored site, made into the form it would // post untouched, saved by the form's action, is the same site.json. test("siteToFormData -> saveSiteAction gives a full site back unchanged", async () => { const paths = getPaths(); await writeSite( { siteId: "full", siteTitle: "Fullalyzer", siteDescription: "Everything set", headerTitle: "Fullalyzer", wordmarkLead: "Full", homeTagline: "All of it", socialLinks: [{ label: "Site", url: "https://full.example", svg: '' }], groups: [ { id: "main", name: "Main", selectedByDefault: true, order: 0, description: "The main ones" }, { id: "side", name: "Side", selectedByDefault: false, order: 1, inline: true }, ], defaultGroupId: "side", channels: [ { slug: "pooled", groupId: "main", order: 0 }, { slug: "other", order: 1 }, ], citedChannels: [{ slug: "critic" }], cloudflareProject: "full-pages", accent: "brass", siteUrl: "https://full.example", pages: ["intro"], relatedSites: [{ label: "Friends", siteIds: ["neighbour"] }], search: false, pwa: true, archives: false, duplicates: false, transcriptDownloads: false, archiveMaxBytes: 12_345_678, hubUrl: "https://hub.example", publish: { auto: "preview" }, }, paths, ); const before = await siteFile("full"); const r = await saveSiteAction(undefined, siteToFormData(getSite("full", paths))); assert.equal(r.ok, true, JSON.stringify(r)); assert.deepEqual(await siteFile("full"), before); // And a private, inheriting, default-accent one. await writeSite( { siteId: "plain", siteTitle: "Plain", siteDescription: "", headerTitle: "Plain", homeTagline: "", groups: [{ id: "default", name: "All", selectedByDefault: true }], defaultGroupId: "default", channels: [], audience: "private", listed: false, }, paths, ); const plain = await siteFile("plain"); assert.equal((await saveSiteAction(undefined, siteToFormData(getSite("plain", paths)))).ok, true); assert.deepEqual(await siteFile("plain"), plain); }); // An update is ONE read-modify-write under the site's lock: an edit of the // site that holds the lock when the update arrives (a Reports-tab patch, say) // is read by the update, not written over by a copy taken before it landed. test("an update waits for a held edit of the site and keeps it", async () => { const paths = getPaths(); let release!: () => void; const gate = new Promise((r) => (release = r)); const held = withSiteLock( "shown", async () => { const current = getSite("shown", paths); await gate; await writeSite({ ...current, homeTagline: "written while the update waited" }, paths); }, paths, ); const up = callPost(POST, { siteId: "shown", update: true, siteDescription: "after the held edit" }); await new Promise((r) => setTimeout(r, 20)); release(); await held; const r = await up; assert.equal(r.status, 200, JSON.stringify(r.body)); const file = await siteFile("shown"); assert.equal(file.homeTagline, "written while the update waited"); assert.equal(file.siteDescription, "after the held edit"); });