// Integration (release 37 S1): page records imported from the fake Wayback // flow through the REAL index build and the REAL site compose with no reader // of their own — the shared posts tree carries them with their `page`, a // site's posts manifest lists the channel as platform "webpage", and the // served page holds the record. A Wayback pages channel scoped on X is an X // channel for `social.x.visibility`: a public site leaves it out while X is // private, a private site carries it. // // Run with: node_modules/.bin/tsx --test common/bin/compose-site.waybackPages.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "wayback-pages-compose-")); const PINNED: Record = { TRANSCRIPTS_DIR: path.join(ROOT, "transcripts"), SAVED_VIDEOS_DIR: path.join(ROOT, "saved-videos"), SITES_DIR: path.join(ROOT, "transcripts", "sites"), SETTINGS_FILE: path.join(ROOT, "settings.json"), EXPORT_PUBLIC_DIR: path.join(ROOT, "public"), EXPORT_INDEX_DIR: path.join(ROOT, ".export-index"), EXPORT_BUILDS_DIR: path.join(ROOT, ".export-builds"), EDITOR_CHANGELOG_FILE: path.join(ROOT, "editor-CHANGELOG.md"), EXPORT_CHANGELOG_FILE: path.join(ROOT, "export-CHANGELOG.md"), CHARTS_CONFIG_FILE: path.join(ROOT, "chart-templates.json"), SEARCH_ALIASES_FILE: path.join(ROOT, "transcripts", "search-aliases.json"), CURATED_TAGS_FILE: path.join(ROOT, "transcripts", "tags.json"), ARCHILYZER_CONFIG_DIR: path.join(ROOT, "config"), ARCHILYZER_SOURCE_SCRATCH: path.join(ROOT, "source-scratch"), }; Object.assign(process.env, PINNED); after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { buildIndex } = await import("../controller/buildIndex"); const { importPage } = await import("../controller/importPage"); const { WaybackClient } = await import("../lib/waybackClient"); const { pageRecordId } = await import("../lib/waybackPages/types"); const { draftToPost } = await import("../lib/waybackPages/toPost"); const { upsertPosts } = await import("../lib/posts-server"); const { main: composeSite } = await import("./compose-site"); const fake = await import("../testing/web/fakeWayback.mjs"); const paths = getPaths(); const BLOG = "old-blog-pages"; const XP = "old-x-pages"; const writeJson = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value, null, 2)); }; const readJson = (file: string): T => JSON.parse(readFileSync(file, "utf8")) as T; const quiet = () => {}; const pagesConfig = (name: string, target: string) => ({ handling: "transcribe", name, sourceKind: "social", platform: "webpage", socialHandle: target.split("/")[0], url: `https://web.archive.org/web/*/${target}*`, wayback: { targets: [{ url: target, match: "prefix" }] }, }); async function compose(siteId: string) { const log = console.log; console.log = quiet; try { await composeSite({ siteId, paths }); } finally { console.log = log; } const pub = paths.exportPublicDir; return { postsManifest: readJson<{ channels: { slug: string; platform: string; postCount: number }[] }>(path.join(pub, "posts", "manifest.json")), page: (slug: string) => readJson[]>(path.join(pub, "posts", slug, "page-0000.json")), has: (slug: string) => existsSync(path.join(pub, "posts", slug, "manifest.json")) && readJson<{ pageCount: number }>(path.join(pub, "posts", slug, "manifest.json")).pageCount > 0, }; } test("page records: through the index and a site compose as posts; an X-scoped pages channel private by the X rule", async () => { writeJson(path.join(paths.channelsDir, BLOG, "config.json"), pagesConfig("Old blog", "blog.example/")); writeJson(path.join(paths.channelsDir, XP, "config.json"), pagesConfig("Old X", "twitter.com/someone")); const site = (siteId: string, extra: Record = {}) => writeJson(path.join(paths.sitesDir, siteId, "site.json"), { siteId, siteTitle: siteId, siteDescription: "fixture", headerTitle: siteId, homeTagline: "", socialLinks: [], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", channels: [BLOG, XP].map((slug) => ({ slug, groupId: "default" })), siteUrl: `https://${siteId}.example.test`, archives: false, ...extra, }); site("pub"); site("priv", { audience: "private" }); writeJson(paths.settingsFile, { social: { x: { visibility: "private" } } }); const wb = await fake.startFakeWayback(); try { const client = new WaybackClient({ fetch: wb.fetch }, { minGapMs: 0 }); const r = await importPage({ paths, slug: BLOG, url: `https://web.archive.org/web/${fake.PAGE_ARTICLE_TS}/${fake.PAGE_ARTICLE_URL}`, client, settings: { social: { x: { visibility: "private" } } }, }); assert.equal(r.ok, true, r.error); } finally { await wb.close(); } // The X-scoped channel's record, as an X extractor (S3) would make it. await upsertPosts(path.join(paths.channelsDir, XP), [ draftToPost( { kind: "tweet", itemId: "tweet-1001", url: "https://twitter.com/someone/status/1001", author: "someone", text: "a captured tweet", links: [] }, { channelSlug: XP, capture: { originalUrl: "https://twitter.com/someone", captureTs: "20140101000000" }, extractor: { name: "x", version: 1 } }, ), ]); await buildIndex({ paths, onLog: quiet }); const id = pageRecordId(fake.PAGE_ARTICLE_TS, fake.PAGE_ARTICLE_URL); const shared = readJson[]>(path.join(paths.exportSharedPostsDir, BLOG, "page-0000.json")); assert.equal(shared.length, 1); assert.equal(shared[0].id, id); assert.equal(shared[0].platform, "webpage"); assert.equal((shared[0].page as { title?: string }).title, "A Post About Bread"); // The shared tree is corpus-wide: the X-scoped channel is in it. assert.ok(existsSync(path.join(paths.exportSharedPostsDir, XP, "manifest.json"))); const sitePosts = (siteId: string) => readJson<{ channels: { slug: string; platform: string }[] }>(path.join(paths.exportSitesIndexDir, siteId, "posts", "manifest.json")); assert.deepEqual(sitePosts("pub").channels.map((c) => [c.slug, c.platform]), [[BLOG, "webpage"]]); assert.deepEqual(sitePosts("priv").channels.map((c) => c.slug).sort(), [BLOG, XP].sort()); const pub = await compose("pub"); assert.deepEqual(pub.postsManifest.channels.map((c) => c.slug), [BLOG]); assert.equal(pub.has(BLOG), true); assert.equal(pub.has(XP), false); const served = pub.page(BLOG)[0]; assert.equal(served.id, id); assert.ok(String(served.text).includes("The first loaf came out flat")); assert.equal((served.page as { waybackUrl?: string }).waybackUrl, `https://web.archive.org/web/${fake.PAGE_ARTICLE_TS}/${fake.PAGE_ARTICLE_URL}`); const priv = await compose("priv"); assert.equal(priv.has(XP), true); assert.equal(priv.page(XP)[0].id, "tweet-1001"); });