import { test, after } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { loadSourceManifest, sourcePublicDir } from "./source"; // Run with: // pnpm --filter homepage test // // The /source/ page's loader: the manifest, believed only when it parses AND // the mirror's info/refs and the tarball are beside it. Anything else is the // empty state — a bad file must never crash `next build` (review L10). const TMP = mkdtempSync(path.join(os.tmpdir(), "homepage-source-")); after(() => rmSync(TMP, { recursive: true, force: true })); const manifest = { version: 1, generatedAt: "2026-09-28T12:00:00.000Z", branch: "main", sourceCommit: "1".repeat(40), mirrorHead: "2".repeat(40), subject: "s", files: 10, bytes: 100, mirror: { files: 9, bytes: 80, packs: 2 }, tree: { files: 5, dirs: 2, bytes: 20 }, tarball: { href: "/downloads/archilyzer-source.tar.gz", bytes: 7, sha256: "3".repeat(64) }, cloneUrl: "https://archilyzer.pages.dev/source/archilyzer.git", treeHref: "/source/tree/", audit: { objects: 3, commits: 1, gitleaks: "clean" }, tools: { git: "2.55.0", filterRepo: "x" }, }; let n = 0; function pub(opts: { manifest?: unknown; refs?: boolean; tarball?: boolean; log?: boolean }): string { const p = path.join(TMP, `p${n++}`); mkdirSync(path.join(p, "source", "archilyzer.git", "info"), { recursive: true }); mkdirSync(path.join(p, "downloads"), { recursive: true }); if (opts.manifest !== undefined) { writeFileSync( path.join(p, "source", "manifest.json"), typeof opts.manifest === "string" ? opts.manifest : JSON.stringify(opts.manifest), ); } if (opts.refs !== false) writeFileSync(path.join(p, "source", "archilyzer.git", "info", "refs"), "x\trefs/heads/main\n"); if (opts.tarball !== false) writeFileSync(path.join(p, "downloads", "archilyzer-source.tar.gz"), "t"); if (opts.log) { mkdirSync(path.join(p, "source", "git"), { recursive: true }); writeFileSync(path.join(p, "source", "git", "log.html"), ""); } return p; } test("a good manifest with its files beside it is believed", () => { assert.equal(loadSourceManifest(pub({ manifest }))?.mirrorHead, "2".repeat(40)); }); test("malformed, wrong-version or orphaned manifests are the empty state, never a throw", () => { const cases: Array<[string, string]> = [ ["no manifest", pub({})], ["not JSON", pub({ manifest: "{ half" })], ["version 2", pub({ manifest: { ...manifest, version: 2 } })], ["tree.files missing (the page reads it)", pub({ manifest: { ...manifest, tree: { dirs: 1, bytes: 1 } } })], ["mirror.packs a string", pub({ manifest: { ...manifest, mirror: { ...manifest.mirror, packs: "2" } } })], ["no info/refs", pub({ manifest, refs: false })], ["no tarball", pub({ manifest, tarball: false })], ["no public dir at all", path.join(TMP, "missing")], ]; for (const [what, dir] of cases) { assert.doesNotThrow(() => loadSourceManifest(dir), what); assert.equal(loadSourceManifest(dir), null, what); } }); // Release 15 slice SG: the history block is believed only beside its log page. const history = { href: "/source/git/log.html", commits: 3, total: 3, head: "2".repeat(40), files: 9, bytes: 1234, sha256: "4".repeat(64), tool: "stagit (sha256 0123456789ab)", }; test("a history block beside its log page is believed; without the page the manifest comes back without it", () => { assert.deepEqual(loadSourceManifest(pub({ manifest: { ...manifest, history }, log: true }))?.history, history); const orphan = loadSourceManifest(pub({ manifest: { ...manifest, history } })); assert.equal(orphan?.mirrorHead, "2".repeat(40), "the rest of the manifest stands"); assert.ok(orphan && !("history" in orphan), "no History links for pages that are not here"); assert.equal(loadSourceManifest(pub({ manifest, log: true }))?.history, undefined, "no block, no History"); // A malformed block is a manifest nobody believes (parseSourceManifest). assert.equal(loadSourceManifest(pub({ manifest: { ...manifest, history: { ...history, commits: "3" } }, log: true })), null); }); test("the e2e's fixture publish is read only outside a production build, and only when it holds a manifest", () => { const fixture = pub({ manifest, log: true }); const empty = path.join(TMP, "empty-fixture"); mkdirSync(empty, { recursive: true }); const PUBLIC = "/checkout/homepage/public"; assert.equal(sourcePublicDir({ NODE_ENV: "development", E2E_SOURCE_PUBLIC_DIR: fixture }, PUBLIC), fixture); assert.equal(sourcePublicDir({ NODE_ENV: "production", E2E_SOURCE_PUBLIC_DIR: fixture }, PUBLIC), PUBLIC); assert.equal(sourcePublicDir({ NODE_ENV: "development", E2E_SOURCE_PUBLIC_DIR: empty }, PUBLIC), PUBLIC, "no manifest: public/"); assert.equal(sourcePublicDir({ NODE_ENV: "development" }, PUBLIC), PUBLIC); });