// Wayback pages (release 37 S1), end to end over the fake Wayback Machine // (common/testing/web/fakeWayback.mjs), named to the test server through // E2E_WAYBACK_ORIGIN_FILE (e2e/servers.ts). The real web.archive.org is never // contacted. // // The New channel form makes a Wayback pages channel of a Wayback listing URL // (platform "webpage", one target derived), and the channel page's Import a // capture runs an import-page job: the raw body stored under pages/, the page // record in the channel's posts and on the page. The ops door answers a dry run // and imports the capture nearest a date. import { test, expect, type APIRequestContext } from "@playwright/test"; import { readFile, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { startFakeWayback, PAGE_ARTICLE_LATER_TS, PAGE_ARTICLE_TS, PAGE_ARTICLE_URL, } from "yt-dlp-transcript-common/testing/web/fakeWayback.mjs"; import { pageRecordId } from "yt-dlp-transcript-common/lib/waybackPages/types"; import { baseUrl } from "./baseUrl"; import { generateReport, pathExists, readJson, resetData, resolvePath, testPaths, writeSettings } from "./helpers"; import { waybackOriginFile } from "./servers"; const AUTH = { authorization: "Bearer test-worker-token" }; const SLUG = "old-blog"; async function waitForJob(request: APIRequestContext, jobId: string): Promise<{ status: string; tail: string[] }> { let last = { status: "", tail: [] as string[] }; await expect .poll( async () => { const res = await request.get(`${baseUrl}/api/ops/job/${jobId}?tail=40`, { headers: AUTH }); const body = (await res.json()) as { job: { status: string }; tail?: string[] }; last = { status: body.job.status, tail: body.tail ?? [] }; return last.status; }, { timeout: 60_000, intervals: [1_000] }, ) .toMatch(/^(done|failed|cancelled)$/); return last; } test("a Wayback pages channel from the New channel form, then Import a capture on its page", async ({ page, request }) => { test.setTimeout(150_000); await resetData("empty"); await writeSettings({ minFreeDiskGB: 0 }); await generateReport(page, "new"); await page.goto("/channels/new"); await page.locator('input[name="url"]').fill("https://web.archive.org/web/*/blog.example/2014/*"); // Offline: a Wayback URL of a page that is not a video is a pages channel. await expect(page.locator('select[name="platform"]')).toHaveValue("webpage"); await expect(page.getByLabel("target 1 URL")).toHaveValue("blog.example/2014/"); await expect(page.getByLabel("target 1 match")).toHaveValue("prefix"); // Nothing fetched on create, and no video controls. await expect(page.locator('input[name="fetchPostsNow"]')).toHaveCount(0); await expect(page.locator('select[name="audioFormat"]')).toHaveCount(0); // The scope widened by hand: the whole blog. await page.getByLabel("target 1 URL").fill("blog.example/"); await page.locator('input[name="name"]').fill("Old blog"); await page.locator('input[name="slug"]').fill(SLUG); await page.getByRole("button", { name: /create|add channel|save/i }).first().click(); const configFile = `${testPaths().transcriptsDir}/channels/${SLUG}/config.json`; await expect .poll(async () => ((await pathExists(configFile)) ? await readJson(configFile) : null), { timeout: 15_000 }) .toMatchObject({ sourceKind: "social", platform: "webpage", socialHandle: "blog.example", wayback: { targets: [{ url: "blog.example/", match: "prefix" }] }, }); const wb = await startFakeWayback(); const originFile = waybackOriginFile(resolvePath(testPaths().transcriptsDir)); await writeFile(originFile, wb.base); try { await page.goto(`/channels/${SLUG}`); const pages = page.getByRole("region", { name: "Wayback pages" }); await expect(pages.getByLabel("pages held")).toHaveText("0 records from 0 captures"); // A video's capture is refused before any job. await page.getByLabel("capture URL").fill("https://web.archive.org/web/20140101000000/https://www.youtube.com/watch?v=abcdefghijk"); await page.getByRole("button", { name: "Import capture" }).click(); await expect(page.getByRole("alert", { name: `Import a capture into ${SLUG} error` })).toContainText("use import-video"); expect(wb.requests).toEqual([]); await page.getByLabel("capture URL").fill(`https://web.archive.org/web/${PAGE_ARTICLE_TS}/${PAGE_ARTICLE_URL}`); await page.getByRole("button", { name: "Import capture" }).click(); await expect(page.getByRole("log", { name: `Import a capture into ${SLUG} output` })).toContainText('"ok":true', { timeout: 60_000, }); await expect(page.getByRole("link", { name: "import job" })).toHaveAttribute("href", /^\/jobs\//); const id = pageRecordId(PAGE_ARTICLE_TS, PAGE_ARTICLE_URL); const channelRoot = resolvePath(`${testPaths().transcriptsDir}/channels/${SLUG}`); const capture = JSON.parse(await readFile(path.join(channelRoot, "pages", id, "capture.json"), "utf8")) as Record; expect(capture).toMatchObject({ originalUrl: PAGE_ARTICLE_URL, captureTs: PAGE_ARTICLE_TS, mimetype: "text/html", extractor: "generic" }); const shard = await readFile(path.join(channelRoot, "posts", "2014-03.jsonl"), "utf8"); expect(shard).toContain(`"id":"${id}"`); expect(shard).toContain('"platform":"webpage"'); await page.reload(); await expect(pages.getByLabel("pages held")).toHaveText("1 record from 1 capture"); const row = page.locator(`[data-page-record="${id}"]`); await expect(row.getByRole("link", { name: "A Post About Bread" })).toHaveAttribute("href", PAGE_ARTICLE_URL); await expect(row.getByRole("link", { name: "2014-03-05" })).toHaveAttribute( "href", `https://web.archive.org/web/${PAGE_ARTICLE_TS}/${PAGE_ARTICLE_URL}`, ); // The ops door: a dry run fetches nothing; `at` imports the nearest capture. const before = wb.requests.length; const dry = await request.post(`${baseUrl}/api/ops/import-page`, { headers: AUTH, data: { slug: SLUG, url: PAGE_ARTICLE_URL, at: "20150601", dryRun: true }, }); const dryBody = (await dry.json()) as { ok: boolean; dryRun?: boolean; wouldFetch?: string }; expect(dryBody).toMatchObject({ ok: true, dryRun: true }); expect(dryBody.wouldFetch).toContain("nearest 20150601"); expect(wb.requests.length).toBe(before); const res = await request.post(`${baseUrl}/api/ops/import-page`, { headers: AUTH, data: { slug: SLUG, url: PAGE_ARTICLE_URL, at: "20150601" } }); const body = (await res.json()) as { ok?: boolean; jobId?: string; error?: string }; expect(res.status(), body.error).toBe(200); const job = await waitForJob(request, body.jobId!); expect(job.status, job.tail.join("\n")).toBe("done"); expect(job.tail.join("\n")).toContain(`"captureTs":"${PAGE_ARTICLE_LATER_TS}"`); await page.reload(); await expect(pages.getByLabel("pages held")).toHaveText("2 records from 2 captures"); // A refusal over ops is the controller's sentence. const refused = await request.post(`${baseUrl}/api/ops/import-page`, { headers: AUTH, data: { slug: SLUG, url: "https://web.archive.org/web/*/blog.example/*" }, }); expect(refused.status()).toBe(400); expect(((await refused.json()) as { error: string }).error).toContain("not one capture"); } finally { await rm(originFile, { force: true }); await wb.close(); } });