import { test } from "node:test"; import assert from "node:assert/strict"; import { parseSourceManifest, TARBALL_HREF } from "./sourceManifest"; // Run with: // pnpm --filter yt-dlp-transcript-common test // // The homepage believes a manifest only through parseSourceManifest, and the // /source/ page reads its numbers during `next build`: a malformed or // foreign one must be the empty state (null), never a crash (review L10). const good = () => ({ 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: TARBALL_HREF, bytes: 7, sha256: "3".repeat(64) }, cloneUrl: "x", treeHref: "/source/tree/", audit: { objects: 3, commits: 1, gitleaks: "clean" }, tools: { git: "2", filterRepo: "x" }, }); test("a well-formed version-1 manifest parses", () => { assert.deepEqual(parseSourceManifest(good()), good()); }); test("a wrong version, a bad id or sha, or any number the page reads that is not one is null", () => { const broken: Array<[string, (m: ReturnType) => unknown]> = [ ["version 2", (m) => ({ ...m, version: 2 })], ["short mirror head", (m) => ({ ...m, mirrorHead: "abc" })], ["upper-case sha", (m) => ({ ...m, tarball: { ...m.tarball, sha256: "A".repeat(64) } })], ["no tree", (m) => ({ ...m, tree: undefined })], ["tree.files a string", (m) => ({ ...m, tree: { ...m.tree, files: "5" } })], ["mirror.packs missing", (m) => ({ ...m, mirror: { files: 1, bytes: 1 } })], ["mirror.bytes NaN", (m) => ({ ...m, mirror: { ...m.mirror, bytes: Number.NaN } })], ["negative files", (m) => ({ ...m, files: -1 })], ["tarball.bytes missing", (m) => ({ ...m, tarball: { href: TARBALL_HREF, sha256: "3".repeat(64) } })], ["unparsable date", (m) => ({ ...m, generatedAt: "yesterday" })], ["no audit", (m) => ({ ...m, audit: null })], ]; for (const [what, make] of broken) assert.equal(parseSourceManifest(make(good())), null, what); for (const junk of [null, 1, "x", [], {}]) assert.equal(parseSourceManifest(junk), null, JSON.stringify(junk)); }); // Release 15 slice SG: the history block is optional — absent is a manifest // without History links — and all-or-nothing when present. 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 manifest with a history block parses; without one it is the same manifest", () => { const withHistory = { ...good(), history: history() }; assert.deepEqual(parseSourceManifest(withHistory), withHistory); const capped = { ...good(), history: { ...history(), total: 12_345 } }; assert.deepEqual(parseSourceManifest(capped), capped, "the latest 3 of 12,345"); assert.equal(parseSourceManifest(good())?.history, undefined); }); test("a history block that is present but malformed makes the manifest untrusted", () => { const broken: Array<[string, (h: ReturnType) => unknown]> = [ ["null", () => null], ["commits a string", (h) => ({ ...h, commits: "3" })], ["no total", (h) => ({ ...h, total: undefined })], ["fewer in all than with a page", (h) => ({ ...h, total: 2 })], ["files negative", (h) => ({ ...h, files: -1 })], ["bytes missing", (h) => ({ ...h, bytes: undefined })], ["short head", (h) => ({ ...h, head: "abc" })], ["sha256 not hex", (h) => ({ ...h, sha256: "z".repeat(64) })], ["no href", (h) => ({ ...h, href: undefined })], ["tool a number", (h) => ({ ...h, tool: 1 })], ]; for (const [what, make] of broken) { assert.equal(parseSourceManifest({ ...good(), history: make(history()) }), null, what); } });