// Release 37 lane F: what a memento URL says, with no request. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/archives/mementos.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { archiveTodayHostOf, archiveTodayTs, httpDateOfTimestamp, mementoSourceOf, pageArchiveLink, parseArchiveTodayUrl, timestampOfDate, unwrapArchiveTodayOutlink, } from "./mementos"; import { parseArchiveCaptureAsk } from "./ask"; test("archive.today copies in every form, on the canonical host; listings and links out are not copies", () => { assert.deepEqual(parseArchiveTodayUrl("https://archive.ph/Ab3dE"), { source: "archive.today", mementoUrl: "https://archive.ph/Ab3dE", id: "Ab3dE" }); // Any of its names, read as the canonical host; a copy's sub-page is the copy. assert.equal(parseArchiveTodayUrl("http://archive.is/Ab3dE/image")?.mementoUrl, "https://archive.ph/Ab3dE"); assert.equal(parseArchiveTodayUrl("https://archive.today/Ab3dE", "archive.li")?.mementoUrl, "https://archive.li/Ab3dE"); assert.deepEqual(parseArchiveTodayUrl("https://archive.ph/2019.05.12-083000/https://journal.example/a.html"), { source: "archive.today", mementoUrl: "https://archive.ph/20190512083000/https://journal.example/a.html", originalUrl: "https://journal.example/a.html", captureTs: "20190512083000", }); assert.equal(parseArchiveTodayUrl("https://archive.ph/20190512/journal.example/a.html?x=1")?.originalUrl, "http://journal.example/a.html?x=1"); assert.deepEqual(parseArchiveTodayUrl("https://archive.vn/newest/https://journal.example/a.html"), { source: "archive.today", mementoUrl: "https://archive.ph/newest/https://journal.example/a.html", originalUrl: "https://journal.example/a.html", pick: "newest", }); for (const notACopy of [ "https://archive.ph/", "https://archive.ph/journal.example", "https://archive.ph/https://journal.example/*", "https://archive.ph/o/Ab3dE/https://journal.example/b.html", "https://archive.ph/timemap/https://journal.example/", "https://archive.ph/search/?q=journal", "https://archive.ph/submit/", "https://web.archive.org/web/2019/https://journal.example/", "https://journal.example/Ab3dE", ]) { assert.equal(parseArchiveTodayUrl(notACopy), null, notACopy); } }); test("timestamps: dotted and digit forms, HTTP dates both ways; the canonical host is one of its names", () => { assert.equal(archiveTodayTs("2019.05.12-083000"), "20190512083000"); assert.equal(timestampOfDate("Sun, 12 May 2019 08:30:00 GMT"), "20190512083000"); assert.equal(timestampOfDate("2019-05-12T08:30:00Z"), "20190512083000"); assert.equal(timestampOfDate("not a date"), null); assert.equal(httpDateOfTimestamp("20190512083000"), "Sun, 12 May 2019 08:30:00 GMT"); assert.equal(archiveTodayHostOf("ARCHIVE.IS"), "archive.is"); assert.equal(archiveTodayHostOf("evil.example"), "archive.ph"); assert.equal(archiveTodayHostOf(undefined), "archive.ph"); }); test("a link out of a copy is the URL it points at", () => { assert.equal(unwrapArchiveTodayOutlink("https://archive.ph/o/Ab3dE/https://other.example/account"), "https://other.example/account"); assert.equal(unwrapArchiveTodayOutlink("/o/Ab3dE/https://other.example/x?y=1"), "https://other.example/x?y=1"); assert.equal(unwrapArchiveTodayOutlink("https://other.example/x"), "https://other.example/x"); }); test("which source a URL is a copy on", () => { assert.equal(mementoSourceOf("https://web.archive.org/web/20140305120000/https://blog.example/"), "wayback"); assert.equal(mementoSourceOf("https://archive.ph/Ab3dE"), "archive.today"); assert.equal(mementoSourceOf("https://blog.example/"), null); assert.equal(mementoSourceOf("https://web.archive.org/web/*/blog.example/*"), null); }); test("a record's copy is cited by its source's URL and name; a record with no source is the Wayback Machine's", () => { assert.deepEqual( pageArchiveLink({ captureTs: "20140305120000", originalUrl: "https://blog.example/", waybackUrl: "https://web.archive.org/web/20140305120000/https://blog.example/" }), { source: "wayback", label: "Wayback Machine", url: "https://web.archive.org/web/20140305120000/https://blog.example/" }, ); assert.deepEqual( pageArchiveLink({ source: "archive.today", archiveUrl: "https://archive.ph/Ab3dE", waybackUrl: "https://archive.ph/Ab3dE", captureTs: "20190512083000", originalUrl: "https://journal.example/a.html" }), { source: "archive.today", label: "archive.today", url: "https://archive.ph/Ab3dE" }, ); }); test("an import's ask across sources: a copy's URL names its source; a page's URL takes `at` and a source", () => { const ok = (url: string, at?: string, source?: string) => { const r = parseArchiveCaptureAsk(url, at, source); assert.ok(r.ok, r.ok ? "" : r.error); return r.ask; }; const err = (url: string, at?: string, source?: string) => { const r = parseArchiveCaptureAsk(url, at, source); assert.equal(r.ok, false); return r.ok ? "" : r.error; }; assert.deepEqual(ok("https://web.archive.org/web/20140305120000/https://blog.example/"), { kind: "capture", captureTs: "20140305120000", originalUrl: "https://blog.example/", source: "wayback", }); assert.deepEqual(ok("https://blog.example/", "2014"), { kind: "nearest", at: "2014", originalUrl: "https://blog.example/", source: "wayback" }); assert.deepEqual(ok("https://archive.is/Ab3dE"), { kind: "memento", source: "archive.today", ref: { source: "archive.today", mementoUrl: "https://archive.ph/Ab3dE", id: "Ab3dE" }, }); assert.equal(ok("https://archive.ph/newest/https://journal.example/a.html").originalUrl, "https://journal.example/a.html"); assert.deepEqual(ok("https://journal.example/a.html", "20190601", "archive.today"), { kind: "nearest", source: "archive.today", at: "20190601", originalUrl: "https://journal.example/a.html", }); assert.match(err("https://archive.ph/https://journal.example/*"), /archive\.today listing or link, not one copy/); assert.match(err("https://archive.ph/Ab3dE", "2019"), /"at" goes with a page's own URL/); assert.match(err("https://archive.ph/Ab3dE", undefined, "wayback"), /is an archive\.today copy/); assert.match(err("https://journal.example/a.html", undefined, "archive.today"), /give "at"/); assert.match(err("https://web.archive.org/web/2014/https://blog.example/", "2014", "archive.today"), /is a Wayback Machine capture/); assert.match(err("https://journal.example/a.html", "2019", "ghostarchive"), /"source" must be/); // The Wayback Machine's own sentences, unchanged. assert.match(err("https://blog.example/"), /not a Wayback Machine capture/); }); test("the view's copy link names archive.today for its records (lane D's pageCopyLinks)", async () => { const { pageCopyLinks, pageCaptureView } = await import("../waybackPages/view"); const page = { originalUrl: "https://journal.example/a.html", captureTs: "20190512083000", waybackUrl: "https://archive.ph/Ab3dE", rawUrl: "https://archive.ph/Ab3dE", source: "archive.today", archiveUrl: "https://archive.ph/Ab3dE", }; assert.deepEqual(pageCopyLinks(page).copy, { label: "archive.today copy, 2019-05-12", url: "https://archive.ph/Ab3dE" }); const { source: _s, archiveUrl: _a, ...wb } = page; assert.match(pageCopyLinks({ ...wb, waybackUrl: "https://web.archive.org/web/20190512083000/https://journal.example/a.html" }).copy.label, /^Wayback Machine copy, /); const view = pageCaptureView({ id: "20190512083000-aaaaaaaaaaaa", text: "x", page: { ...page, kind: "generic", extractor: "generic", extractorVersion: 1 } }); assert.equal(view?.source, "archive.today"); assert.equal(view?.waybackUrl, "https://archive.ph/Ab3dE"); });