// Page records' shots (release 37 S4), 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. // // A Wayback pages channel holds an article captured twice and a Tumblr-themed // index split into two post records. The channel page shows each record's // capture — "Original (may be gone)", "Wayback Machine copy, ", the // version list — and Capture shoots every record without a shot in a headless // Chromium (the whole page; each post clipped to its own box) and saves the // post's image through its `im_` URL; the shots show on the page. 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, PAGE_SPLIT_POSTS, PAGE_SPLIT_TS, PAGE_SPLIT_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, resetData, resolvePath, testPaths, writeSettings } from "./helpers"; import { waybackOriginFile } from "./servers"; const AUTH = { authorization: "Bearer test-worker-token" }; const SLUG = "capture-pages"; 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=60`, { 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: 90_000, intervals: [1_000] }, ) .toMatch(/^(done|failed|cancelled)$/); return last; } async function ops(request: APIRequestContext, action: string, data: Record) { const res = await request.post(`${baseUrl}/api/ops/${action}`, { headers: AUTH, data }); const body = (await res.json()) as { ok?: boolean; jobId?: string; error?: string; slug?: string }; expect(res.status(), body.error).toBe(200); return body; } test("a Wayback pages channel's records: their captures shown, then shot from the archive's replay", async ({ page, request }) => { test.setTimeout(240_000); await resetData("empty"); await writeSettings({ minFreeDiskGB: 0 }); await generateReport(page, "new"); const wb = await startFakeWayback(); const originFile = waybackOriginFile(resolvePath(testPaths().transcriptsDir)); await writeFile(originFile, wb.base); try { await ops(request, "create-channel", { slug: SLUG, fields: { name: "Capture pages", handling: "transcribe", url: "https://web.archive.org/web/*/blog.example/*" }, }); for (const url of [ `https://web.archive.org/web/${PAGE_ARTICLE_TS}/${PAGE_ARTICLE_URL}`, `https://web.archive.org/web/${PAGE_ARTICLE_LATER_TS}/${PAGE_ARTICLE_URL}`, `https://web.archive.org/web/${PAGE_SPLIT_TS}/${PAGE_SPLIT_URL}`, ]) { const job = await waitForJob(request, (await ops(request, "import-page", { slug: SLUG, url })).jobId!); expect(job.status, job.tail.join("\n")).toBe("done"); } await page.goto(`/channels/${SLUG}`); const pages = page.getByRole("region", { name: "Wayback pages" }); await expect(pages.getByLabel("pages held")).toHaveText("4 records from 3 captures"); const later = pageRecordId(PAGE_ARTICLE_LATER_TS, PAGE_ARTICLE_URL); const row = page.locator(`[data-page-record="${later}"]`); await row.locator("summary").click(); await expect(row.getByRole("link", { name: "Original (may be gone)" })).toHaveAttribute("href", PAGE_ARTICLE_URL); await expect(row.getByRole("link", { name: "Wayback Machine copy, 2015-06-11" })).toHaveAttribute( "href", `https://web.archive.org/web/${PAGE_ARTICLE_LATER_TS}/${PAGE_ARTICLE_URL}`, ); await expect(row.locator("[data-page-versions]")).toContainText("2 captures of this page; text changed on 2015-06-11."); // Capture every record without a shot: one job on archive.org's queue. await page.getByRole("button", { name: "Capture 4 without a shot" }).click(); await expect(page.getByRole("log", { name: `Capture page records of ${SLUG} output` })).toContainText("Done: 4 captured, 0 failed", { timeout: 120_000, }); const channelRoot = resolvePath(`${testPaths().transcriptsDir}/channels/${SLUG}`); const capture = async (id: string) => JSON.parse(await readFile(path.join(channelRoot, "posts-media", id, "capture.json"), "utf8")) as { state: string; shotScope?: string; mediaState: string; media: unknown[]; }; expect(await capture(later)).toMatchObject({ state: "captured", shotScope: "page" }); const crumbs = await capture(`tumblr-${PAGE_SPLIT_POSTS[1]}`); expect(crumbs).toMatchObject({ state: "captured", shotScope: "item", mediaState: "ok" }); expect(crumbs.media).toHaveLength(1); expect(wb.requests.some((r) => r.path.startsWith(`/web/${PAGE_SPLIT_TS}if_/`))).toBe(true); expect(wb.requests.some((r) => r.path.includes("im_/http://24.media.tumblr.com/"))).toBe(true); await page.reload(); const shotRow = page.locator(`[data-page-record="tumblr-${PAGE_SPLIT_POSTS[1]}"]`); await shotRow.locator("summary").click(); const img = shotRow.locator(`[data-page-shot]`); await expect(img).toBeVisible(); await expect.poll(() => img.evaluate((i: HTMLImageElement) => i.naturalWidth)).toBeGreaterThan(400); await expect(page.getByRole("button", { name: "Capture 0 without a shot" })).toBeDisabled(); } finally { await rm(originFile, { force: true }); await wb.close(); } });