import { test as base, expect } from "@playwright/test"; import { CORPUS_PUBLIC_ENTRIES } from "yt-dlp-transcript-common/bin/compose-site"; // What a cited site must never ask its own origin for: the corpus-shaped // entries a full compose writes and a cited compose prunes, less the four a // cited compose writes again. A path is matched by its FIRST segment, so a // post capture under /media/posts/ is not /posts/. const REWRITTEN_FOR_CITED = new Set(["corpus.json", "llms.txt", "robots.txt", "sitemap.xml"]); export const CORPUS_ROOT_ENTRIES: readonly string[] = CORPUS_PUBLIC_ENTRIES.filter((e) => !REWRITTEN_FOR_CITED.has(e)); export function isCorpusPath(pathname: string): boolean { const first = pathname.split("/").filter(Boolean)[0] ?? ""; return CORPUS_ROOT_ENTRIES.includes(first); } type Traffic = { // Every same-origin path the page requested, in order. requested: string[]; // Same-origin responses with an error status, as " ". failed: string[]; }; // Every test records what its page asks the site for and, once it is done, // holds that to the cited promise: nothing of the corpus is requested, and // nothing the pages ask for is missing from the build. export const test = base.extend<{ traffic: Traffic }>({ traffic: [ async ({ page, baseURL }, use) => { const origin = new URL(baseURL!).origin; const traffic: Traffic = { requested: [], failed: [] }; page.on("request", (req) => { const u = new URL(req.url()); if (u.origin === origin) traffic.requested.push(u.pathname); }); page.on("response", (res) => { const u = new URL(res.url()); if (u.origin === origin && res.status() >= 400) traffic.failed.push(`${res.status()} ${u.pathname}`); }); await use(traffic); expect(traffic.requested.filter(isCorpusPath), "a cited page requested the corpus").toEqual([]); expect(traffic.failed, "a cited page requested something the build does not hold").toEqual([]); }, { auto: true }, ], }); export { expect };