import { test, expect } from "@playwright/test"; import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { WARM_TIMEOUT, warm } from "./warm"; // --------------------------------------------------------------------------- // Import this capture (release 37 S4): a store article's `page` citation whose // archived copy is a Wayback Machine capture. Its evidence offers Import this // capture; into a new Wayback pages channel (the stub editor's create-channel // writes its config, its import-page writes ONE synthetic page record — // e2e/fixtures/editor-stub-import-page.mjs); the record is cited, and the // article's citation is a post citation of it — written as a hand iteration by // the REAL replacePageCitationWithRecord, its quote checked against the // record — with the page kept as its fallback. Then its evidence shows the // page record: "Original (may be gone)", the Wayback copy, the kind. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const ARTICLES = path.join(FIXTURE, "articles"); const CHANNELS = path.join(FIXTURE, "channels"); const ID = "capture-import"; const DIR = path.join(ARTICLES, ID); const SLUG = "blog-example-pages"; const ORIGINAL = "https://blog.example/2014/03/a-post.html"; const ARCHIVE = `https://web.archive.org/web/20140305120000/${ORIGINAL}`; const report = { format: "archilyzer-report", version: 1, id: ID, kind: "sweep", title: "The Bread Blog", citations: { p1: { kind: "page", url: ORIGINAL, title: "A Post About Bread", archiveUrl: ARCHIVE, quote: "The first loaf came out flat" }, }, sections: [{ id: "s1", title: "The blog", claims: [{ id: "c1", text: "The blog said the first loaf was flat.", citations: ["p1"] }] }], }; function clean() { rmSync(DIR, { recursive: true, force: true }); rmSync(path.join(CHANNELS, SLUG), { recursive: true, force: true }); } test.beforeAll(async ({ playwright }) => { test.setTimeout(WARM_TIMEOUT); clean(); mkdirSync(DIR, { recursive: true }); writeFileSync(path.join(DIR, "report.json"), JSON.stringify(report, null, 2)); await warm(playwright, [`/articles/${ID}`, `/api/sites/evidence?site=articles&report=${ID}&cite=p1`, "/api/articles/import-capture"]); }); test.afterAll(clean); test("a page citation's capture imported as a page record, then cited as one, the page kept as its fallback", async ({ page }) => { test.setTimeout(90_000); await page.goto(`/articles/${ID}?tab=article`); await page.locator("button[data-cite='p1']").first().click(); const ev = page.locator("[data-evidence='p1']"); await expect(ev.getByRole("link", { name: /Wayback Machine copy, 2014-03-05/ })).toHaveAttribute("href", ARCHIVE); await ev.getByRole("button", { name: "Import this capture" }).click(); const box = ev.locator("[data-import-capture='p1']"); await box.getByLabel("Wayback pages channel").selectOption({ label: "a new Wayback pages channel…" }); await box.getByLabel("new channel name").fill("Blog example pages"); await box.getByRole("button", { name: "Import" }).click(); await expect(box.getByRole("status")).toContainText("1 record in blog-example-pages", { timeout: 30_000 }); expect(existsSync(path.join(CHANNELS, SLUG, "config.json"))).toBe(true); const rec = box.locator("[data-imported-record]"); await expect(rec).toHaveCount(1); await expect(rec).toContainText("A Post About Bread"); await rec.getByRole("button", { name: "Cite this" }).click(); await expect(box.getByRole("status")).toContainText("p1 now cites the page record; written to i1"); // The hand iteration: a post citation of the record, the page its fallback. const its = readdirSync(path.join(DIR, "iterations")).filter((n) => /^\d+$/.test(n)); expect(its).toEqual(["1"]); const written = JSON.parse(readFileSync(path.join(DIR, "iterations", "1", "report.json"), "utf8")); expect(written.citations.p1).toEqual({ kind: "post", channel: SLUG, id: "20140305120000-0123456789ab", quote: "The first loaf came out flat", fallback: { kind: "page", url: ORIGINAL, title: "A Post About Bread", archiveUrl: ARCHIVE }, }); // What crossed the wire: the channel scoped on the page's host, the capture. const asked = JSON.parse(readFileSync(path.join(FIXTURE, "editor-stub.requests.json"), "utf8")) as { path: string; body?: Record }[]; // The LAST ask of each: the stub's log is the whole run's, and an earlier spec (case-stages) asks both too. const create = asked.findLast((r) => r.path === "/api/ops/create-channel"); expect(create?.body).toMatchObject({ fields: { name: "Blog example pages", url: "https://web.archive.org/web/*/blog.example/*" } }); expect(asked.findLast((r) => r.path === "/api/ops/import-page")?.body).toEqual({ slug: SLUG, url: ARCHIVE }); });