// Release 37 S1: a draft as a page record, the split-record merge through // upsertPosts, versions of one page, and parsePost keeping (or dropping) a // record's `page`. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/toPost.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { assignPageVersions, draftToPost, isWholePageRecord, mergePageRecord } from "./toPost"; import { pageRecordId, pageThreadId, type PageRecordDraft } from "./types"; import { parsePost, postPlatformLabel, isPostPlatform, type Post } from "../posts"; import { readAllPosts, replaceChannelPosts, upsertPosts, countPosts } from "../posts-server"; const URL_A = "https://blog.example/2014/03/a-post.html"; const ctx = (captureTs: string, originalUrl = URL_A, extra: Partial[1]> = {}) => ({ channelSlug: "old-blog", capture: { originalUrl, captureTs, mimetype: "text/html" }, extractor: { name: "generic", version: 1 }, ...extra, }); const draft = (over: Partial = {}): PageRecordDraft => ({ kind: "generic", text: "Words.", links: [], ...over }); test("a whole-page draft: id, dates, author, url, thread and provenance", () => { const p = draftToPost(draft({ title: "A Post", publishedAt: "2014-03-04T18:30:00Z" }), ctx("20140305120000")); assert.equal(p.id, pageRecordId("20140305120000", URL_A)); assert.equal(p.slug, `old-blog/${p.id}`); assert.equal(p.platform, "webpage"); assert.equal(p.createdAt, "2014-03-04T18:30:00.000Z"); assert.equal(p.uploadDate, "20140304"); assert.equal(p.author, "blog.example"); assert.equal(p.url, URL_A); assert.equal(p.threadId, pageThreadId(URL_A)); assert.deepEqual(p.page, { originalUrl: URL_A, captureTs: "20140305120000", waybackUrl: `https://web.archive.org/web/20140305120000/${URL_A}`, rawUrl: `https://web.archive.org/web/20140305120000id_/${URL_A}`, kind: "generic", extractor: "generic", extractorVersion: 1, title: "A Post", mimetype: "text/html", publishedAt: "2014-03-04T18:30:00.000Z", }); assert.equal(isWholePageRecord(p), true); // No date of its own: the capture time is when it was said, as best known. const undated = draftToPost(draft(), ctx("20140305120000")); assert.equal(undated.createdAt, "2014-03-05T12:00:00.000Z"); // A fallback is on the record. const fb = draftToPost(draft(), ctx("2014", URL_A, { fallback: { from: "tumblr", fromVersion: 2, error: "x" } })); assert.deepEqual(fb.page?.extractorFallback, { from: "tumblr", fromVersion: 2, error: "x" }); }); test("an item draft keeps its own id and url; a tweet keeps X's thread ids", () => { const item = draftToPost(draft({ kind: "tumblr-post", itemId: "tumblr-77", url: "https://b.example/post/77", author: "b" }), ctx("20140101000000")); assert.equal(item.id, "tumblr-77"); assert.equal(item.url, "https://b.example/post/77"); assert.equal(item.author, "b"); assert.equal(item.page?.firstSeenTs, "20140101000000"); assert.equal(isWholePageRecord(item), false); const tweet = draftToPost(draft({ kind: "tweet", itemId: "tweet-1001", url: "https://twitter.com/s/status/1001" }), ctx("2014", "https://twitter.com/s")); assert.equal(tweet.threadId, "1001"); const reply = draftToPost( draft({ kind: "tweet", itemId: "tweet-1002", replyTo: { platform: "twitter", id: "1001" } }), ctx("2014", "https://twitter.com/s"), ); assert.equal(reply.threadId, "1001"); assert.equal(reply.isReply, true); }); test("parsePost keeps a page record's page, drops a malformed page (never the post); the platform reads as a Web page", () => { const p = draftToPost(draft({ title: "T" }), ctx("20140305120000")); const back = parsePost(JSON.parse(JSON.stringify(p))); assert.deepEqual(back?.page, p.page); assert.equal(back?.url, URL_A); const broken = parsePost({ ...JSON.parse(JSON.stringify(p)), page: { originalUrl: URL_A, kind: "generic" } }); assert.ok(broken); assert.equal(broken!.page, undefined); // A record with no url of its own takes its page's original. const { url: _u, ...noUrl } = JSON.parse(JSON.stringify(p)); assert.equal(parsePost(noUrl)?.url, URL_A); assert.equal(isPostPlatform("webpage"), true); assert.equal(postPlatformLabel("webpage"), "Web page"); }); test("the split-record merge: the later capture's facts, the first capture kept", () => { const first = draftToPost(draft({ kind: "tumblr-post", itemId: "tumblr-77", text: "v1" }), ctx("20140101000000")); const later = draftToPost(draft({ kind: "tumblr-post", itemId: "tumblr-77", text: "v2" }), ctx("20150101000000")); const m = mergePageRecord(first, later)!; assert.equal(m.text, "v2"); assert.equal(m.page?.captureTs, "20150101000000"); assert.equal(m.page?.firstSeenTs, "20140101000000"); // An earlier capture arriving after a later one replaces nothing but the // first sighting. const m2 = mergePageRecord(later, first)!; assert.equal(m2.text, "v2"); assert.equal(m2.page?.captureTs, "20150101000000"); assert.equal(m2.page?.firstSeenTs, "20140101000000"); // The same capture again: unchanged. assert.equal(mergePageRecord(first, draftToPost(draft({ kind: "tumblr-post", itemId: "tumblr-77", text: "v1" }), ctx("20140101000000"))), null); }); test("versions of one page: every later whole-page capture names the first", () => { const a = draftToPost(draft(), ctx("20140305120000")); const b = draftToPost(draft(), ctx("20150611090000")); const c = draftToPost(draft(), ctx("20130101000000")); const other = draftToPost(draft(), ctx("20150611090000", "https://blog.example/other")); const changed = new Map(assignPageVersions([a, b, other]).map((p) => [p.id, p.page?.versionOf])); assert.deepEqual([...changed], [[b.id, a.id]]); const again = new Map(assignPageVersions([{ ...a }, { ...b, page: { ...b.page!, versionOf: a.id } }, c]).map((p) => [p.id, p.page?.versionOf])); assert.deepEqual(again.get(a.id), c.id); assert.deepEqual(again.get(b.id), c.id); assert.equal(again.has(c.id), false); }); test("upsertPosts: an item seen in a later capture is ONE record, updated in place; the same capture again changes nothing", async () => { // The directory IS the channel (readPostShard re-derives the slug from it). const tmp = await mkdtemp(path.join(tmpdir(), "page-upsert-")); const root = path.join(tmp, "old-blog"); try { const first = draftToPost(draft({ kind: "tumblr-post", itemId: "tumblr-77", text: "v1", publishedAt: "2013-06-01T00:00:00Z" }), ctx("20140101000000")); const whole = draftToPost(draft({ text: "index page" }), ctx("20140101000000")); const r1 = await upsertPosts(root, [first, whole]); assert.equal(r1.written, 2); const again = await upsertPosts(root, [first, whole]); assert.equal(again.written, 0); assert.equal(again.updated, 0); assert.equal(again.unchanged, 2); const later = draftToPost(draft({ kind: "tumblr-post", itemId: "tumblr-77", text: "v2", publishedAt: "2013-06-01T00:00:00Z" }), ctx("20150101000000")); const r2 = await upsertPosts(root, [later]); assert.equal(r2.updated, 1); const all = await readAllPosts(root); assert.equal(all.length, 2); const item = all.find((p) => p.id === "tumblr-77")!; assert.equal(item.text, "v2"); assert.equal(item.page?.firstSeenTs, "20140101000000"); assert.equal(item.createdAt, "2013-06-01T00:00:00.000Z"); assert.equal(await countPosts(root), 2); } finally { await rm(tmp, { recursive: true, force: true }); } }); test("replaceChannelPosts: the store rewritten whole — a record may move month, an id may go, the archive matches", async () => { const root = await mkdtemp(path.join(tmpdir(), "page-replace-")); try { const a = draftToPost(draft(), ctx("20140305120000")); const b = draftToPost(draft(), ctx("20150611090000", "https://blog.example/other")); await upsertPosts(root, [a, b]); const moved: Post = { ...a, createdAt: "2013-01-02T00:00:00.000Z", uploadDate: "20130102" }; const res = await replaceChannelPosts(root, [moved]); assert.deepEqual(res.shards, ["2013-01"]); const all = await readAllPosts(root); assert.deepEqual(all.map((p) => [p.id, p.createdAt]), [[a.id, "2013-01-02T00:00:00.000Z"]]); assert.equal(await countPosts(root), 1); } finally { await rm(root, { recursive: true, force: true }); } });