// Wayback pages, live (release 37 S5), over the fake Wayback Machine // (common/testing/web/fakeWayback.mjs, its Save Page Now block) named to the // test server through E2E_WAYBACK_ORIGIN_FILE (e2e/servers.ts). Neither the // real web.archive.org nor any original site is contacted: the originals' // verdicts are written to posts-availability.json as the check would. // // A Wayback pages channel's page shows Originals (checked / gone, the gone // table) and Save Page Now: off with its sentence while no archive.org keys // are configured; with keys (a mode-600 file the settings name), "Ask the // Wayback Machine to capture this now" runs a save-page-now job whose capture // is imported into the channel. The ops door answers a dry run. import { test, expect, type APIRequestContext } from "@playwright/test"; import { chmod, mkdir, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { startFakeWayback, FAKE_SPN_ACCESS, FAKE_SPN_SECRET, PAGE_ARTICLE_TS, PAGE_ARTICLE_URL, SPN_CAPTURE_TS, } from "yt-dlp-transcript-common/testing/web/fakeWayback.mjs"; import { pageRecordId } from "yt-dlp-transcript-common/lib/waybackPages/types"; import { baseUrl } from "./baseUrl"; import { resetData, resolvePath, testPaths, writeSettings } from "./helpers"; import { waybackOriginFile } from "./servers"; const AUTH = { authorization: "Bearer test-worker-token" }; const SLUG = "old-blog-live"; const LIVE = "https://blog.example/still-up.html"; 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: 90_000, intervals: [1_000] }, ) .toMatch(/^(done|failed|cancelled)$/); return last; } test("Originals and Save Page Now on a Wayback pages channel: off without keys, then a capture imported", async ({ page, request }) => { test.setTimeout(180_000); await resetData("empty"); const root = resolvePath(testPaths().transcriptsDir); const keyFile = path.join(root, "spn-keys.env"); await rm(keyFile, { force: true }); await writeSettings({ minFreeDiskGB: 0, archiveOrg: { s3KeyFile: keyFile } }); const channelRoot = path.join(root, "channels", SLUG); await mkdir(channelRoot, { recursive: true }); await writeFile( path.join(channelRoot, "config.json"), JSON.stringify({ name: "Old blog (live)", handling: "transcribe", sourceKind: "social", platform: "webpage", socialHandle: "blog.example", url: "https://web.archive.org/web/*/blog.example/*", wayback: { targets: [{ url: "blog.example/", match: "prefix" }] }, }), ); await fetch(`${baseUrl}/api/test/invalidate-cache`).catch(() => {}); const wb = await startFakeWayback(); const originFile = waybackOriginFile(root); await writeFile(originFile, wb.base); try { // One record, imported from the fake. const imp = await request.post(`${baseUrl}/api/ops/import-page`, { headers: AUTH, data: { slug: SLUG, url: `https://web.archive.org/web/${PAGE_ARTICLE_TS}/${PAGE_ARTICLE_URL}` }, }); const impBody = (await imp.json()) as { jobId?: string; error?: string }; expect(imp.status(), impBody.error).toBe(200); expect((await waitForJob(request, impBody.jobId!)).status).toBe("done"); const articleId = pageRecordId(PAGE_ARTICLE_TS, PAGE_ARTICLE_URL); await page.goto(`/channels/${SLUG}`); const originals = page.getByRole("region", { name: "originals" }); await expect(originals.getByLabel("originals checked")).toHaveText("0 of 1 original URL checked · 0 gone"); await expect(originals.getByRole("button", { name: "Check originals" })).toBeEnabled(); // No keys (lane spn-batch): Save Page Now asks anonymously, and says so; // a dry run names the anonymous request and asks nothing of SPN. const spn = page.getByRole("region", { name: "save page now" }); await expect(spn.getByLabel("save page now anonymous")).toContainText("Save Page Now asks anonymously"); const anon = await request.post(`${baseUrl}/api/ops/save-page-now`, { headers: AUTH, data: { url: LIVE, slug: SLUG, dryRun: true } }); expect(anon.status()).toBe(200); const anonBody = (await anon.json()) as { mode: string; wouldAsk: string }; expect(anonBody.mode).toBe("anonymous"); expect(anonBody.wouldAsk).toContain("anonymous"); expect(wb.requests.filter((r) => r.path.startsWith("/save"))).toEqual([]); // The original's verdict, as the check writes it: gone — the Wayback copy is the only copy. await writeFile( path.join(channelRoot, "posts-availability.json"), JSON.stringify({ [articleId]: { availability: "deleted", checkedAt: "2026-10-20T10:00:00.000Z", status: 404, reason: "HTTP 404" } }), ); // The keys, in a mode-600 file the settings name. await writeFile(keyFile, `ARCHIVE_ORG_S3_ACCESS=${FAKE_SPN_ACCESS}\nARCHIVE_ORG_S3_SECRET=${FAKE_SPN_SECRET}\n`); await chmod(keyFile, 0o600); await page.reload(); await expect(originals.getByLabel("originals checked")).toHaveText("1 of 1 original URL checked · 1 gone"); const goneRow = page.locator(`[data-original-gone="${articleId}"]`); await expect(goneRow.getByRole("link", { name: "the only copy" })).toHaveAttribute( "href", `https://web.archive.org/web/${PAGE_ARTICLE_TS}/${PAGE_ARTICLE_URL}`, ); await expect(goneRow).toContainText("HTTP 404"); await expect(spn.getByLabel("save page now off")).toHaveCount(0); // A dry run over ops asks nothing. const dry = await request.post(`${baseUrl}/api/ops/save-page-now`, { headers: AUTH, data: { url: LIVE, slug: SLUG, dryRun: true } }); const dryBody = (await dry.json()) as { ok: boolean; wouldAsk?: string }; expect(dryBody.ok).toBe(true); expect(dryBody.wouldAsk).toContain(`then import the capture into ${SLUG}`); expect(wb.requests.filter((r) => r.path.startsWith("/save"))).toEqual([]); // Ask the Wayback Machine to capture the live page now: the capture, then its import. await spn.getByLabel("page to capture").fill(LIVE); await spn.getByRole("button", { name: "Ask the Wayback Machine to capture this now" }).click(); const log = page.getByRole("log", { name: `Save Page Now into ${SLUG} output` }); await expect(log).toContainText('"ok":true', { timeout: 90_000 }); const text = (await log.textContent()) ?? ""; expect(text).toContain(`"captureTs":"${SPN_CAPTURE_TS}"`); expect(text).not.toContain(FAKE_SPN_SECRET); const importJobId = /"importJobId":"([^"]+)"/.exec(text)?.[1]; expect(importJobId, text).toBeTruthy(); expect((await waitForJob(request, importJobId!)).status).toBe("done"); expect(wb.spnForms).toEqual([{ url: LIVE, capture_outlinks: "0" }]); await page.reload(); await expect(page.getByRole("region", { name: "Wayback pages" }).getByLabel("pages held")).toHaveText("2 records from 2 captures"); await expect(page.locator(`[data-page-record="${pageRecordId(SPN_CAPTURE_TS, LIVE)}"]`)).toBeVisible(); await expect(originals.getByLabel("originals checked")).toHaveText("1 of 2 original URLs checked · 1 gone"); } finally { await rm(originFile, { force: true }); await rm(keyFile, { force: true }); await wb.close(); } });