// How a page record is shown (release 37 S4): the kind, the links, the // version list, no content and the fallback note. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/view.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import type { Post } from "../posts"; import { pageRecordId } from "./types"; import { PAGE_ORIGINAL_GONE_LABEL } from "./liveness"; import { isWholePageId, pageCaptureLine, pageCaptureView, pageKindLabel, pageVersionsLine } from "./view"; const URL_A = "https://blog.example/a.html"; function rec(ts: string, text: string, over: Partial = {}): Post { const id = pageRecordId(ts, URL_A); return { id, slug: `pages/${id}`, channelSlug: "pages", author: "blog.example", createdAt: "2014-03-05T12:00:00.000Z", uploadDate: "20140305", text, url: URL_A, platform: "webpage", isReply: false, isRepost: false, links: [], page: { originalUrl: URL_A, captureTs: ts, waybackUrl: `https://web.archive.org/web/${ts}/${URL_A}`, rawUrl: `https://web.archive.org/web/${ts}id_/${URL_A}`, kind: "generic", extractor: "generic", extractorVersion: 1, ...over, }, }; } test("a whole-page record: links, kind, and the versions of the same page", () => { const a = rec("20140305120000", "First."); const b = rec("20150611090000", "First."); const c = rec("20160102000000", "Second."); const other = { ...rec("20150101000000", "x"), page: { ...rec("20150101000000", "x").page!, originalUrl: "https://blog.example/b.html" } }; assert.ok(isWholePageId(a.id)); assert.ok(!isWholePageId("tumblr-123")); const v = pageCaptureView(b, [a, c, other])!; assert.equal(v.kindLabel, "Web page"); assert.equal(v.captureDate, "2015-06-11"); assert.deepEqual(v.links.original, { label: "Original (may be gone)", url: URL_A }); assert.deepEqual(v.links.copy, { label: "Wayback Machine copy, 2015-06-11", url: b.page!.waybackUrl }); assert.equal(v.versions?.count, 3); assert.deepEqual(v.versions?.changedOn, ["2016-01-02"]); assert.deepEqual(v.versions?.captures.map((x) => x.current), [false, true, false]); assert.equal(pageVersionsLine(v.versions!), "3 captures of this page; text changed on 2016-01-02."); assert.equal(pageVersionsLine({ count: 2, changedOn: [] }), "2 captures of this page; the text is the same in both."); assert.equal(pageVersionsLine({ count: 4, changedOn: ["a", "b", "c"] }), "4 captures of this page; text changed on a, b and c."); assert.equal(pageCaptureView(a)?.versions, undefined, "one capture: no list"); assert.equal(pageCaptureView({ id: "p", text: "" }), undefined); }); test("no content, the fallback note, an item seen first in an earlier capture", () => { const shell = rec("20220101000000", "", { noContent: true, kind: "x-profile", extractorFallback: { from: "x", fromVersion: 1, error: "boom" } }); const v = pageCaptureView(shell)!; assert.equal(v.noContent, true); assert.equal(v.kindLabel, "X/Twitter profile"); assert.equal(v.fallbackNote, "The x reader could not read this capture (boom); the text is the generic reader's."); const item = { ...rec("20140305120000", "t", { kind: "tumblr-post", firstSeenTs: "20120601000000" }), id: "tumblr-1" }; const iv = pageCaptureView(item)!; assert.equal(iv.firstSeenDate, "2012-06-01"); assert.equal(pageCaptureLine(iv), "Tumblr post ยท captured 2014-03-05 (first seen 2012-06-01)"); assert.equal(pageKindLabel("newkind"), "Web page (newkind)"); }); test("an original known gone is named as such; the Wayback copy is the only copy", () => { const r = { ...rec("20140305120000", "x"), isDeleted: true }; const v = pageCaptureView(r)!; assert.equal(v.originalGone, true); assert.equal(v.links.original.label, PAGE_ORIGINAL_GONE_LABEL); assert.equal(pageCaptureView(rec("20140305120000", "x"), [], { originalGone: true })!.links.original.label, PAGE_ORIGINAL_GONE_LABEL); assert.equal(pageCaptureView(rec("20140305120000", "x"))!.links.original.label, "Original (may be gone)"); });