import { test } from "node:test"; import assert from "node:assert/strict"; import { archiveHandle, archiveOriginOf, connectedArchives, findConnectedArchive, freeArchiveId, originOfHandle, sanitizeRemoteArchives, } from "./remoteArchives"; import { ArchiveChannelError, ArchiveProbeError, archiveChannelFacts, probeArchive, type FetchLike } from "./remoteArchives-server"; import { defaults } from "./settingsSchema"; test("an archive origin is normalized: https assumed, trailing slash and /corpus.json dropped", () => { assert.equal(archiveOriginOf("jeralyzer.pages.dev"), "https://jeralyzer.pages.dev"); assert.equal(archiveOriginOf(" https://Jeralyzer.pages.dev/ "), "https://jeralyzer.pages.dev"); assert.equal(archiveOriginOf("https://x.test/corpus.json"), "https://x.test"); assert.equal(archiveOriginOf("http://127.0.0.1:4100/arch/"), "http://127.0.0.1:4100/arch"); for (const bad of ["", "ftp://x.test", "https://u:p@x.test", "https://x.test/?q=1", "https://x.test/#a", 7, null]) { assert.equal(archiveOriginOf(bad), null, String(bad)); } }); test("a handle names its archive and back", () => { assert.equal(archiveHandle("https://a.test"), "remote:https://a.test"); assert.equal(originOfHandle("remote:https://a.test/"), "https://a.test"); assert.equal(originOfHandle("local:/x"), null); assert.equal(originOfHandle(undefined), null); }); test("ids: the archive's own site id when free, else from the host, made unique", () => { assert.equal(freeArchiveId("https://jeralyzer.pages.dev", [], "jeralyzer"), "jeralyzer"); assert.equal(freeArchiveId("https://jeralyzer.pages.dev", ["jeralyzer"], "jeralyzer"), "jeralyzer-2"); assert.equal(freeArchiveId("https://jeralyzer.pages.dev", [], null), "jeralyzer-pages-dev"); assert.equal(freeArchiveId("https://jeralyzer.pages.dev", [], "Not A Slug"), "jeralyzer-pages-dev"); }); test("the stored list is sanitized: bad entries and later duplicates dropped", () => { const out = sanitizeRemoteArchives([ { id: "a", origin: "https://a.test/", title: "A", addedAt: "2026-11-11T00:00:00Z" }, { id: "b", origin: "https://a.test" }, // same origin { id: "a", origin: "https://other.test" }, // same id { id: "BAD", origin: "https://c.test" }, { id: "d", origin: "not a url at all ::" }, "junk", { id: "e", origin: "https://e.test" }, ]); assert.deepEqual(out, [ { id: "a", origin: "https://a.test", title: "A", addedAt: "2026-11-11T00:00:00.000Z" }, { id: "e", origin: "https://e.test", title: "e.test", addedAt: "1970-01-01T00:00:00.000Z" }, ]); assert.deepEqual(sanitizeRemoteArchives({ not: "an array" }), []); assert.deepEqual(defaults().remoteArchives, []); }); test("TRANSCRIPT_SITE_URL is the default archive, listed but never stored, and not twice", () => { const none = connectedArchives([], {}); assert.deepEqual(none, []); const env = connectedArchives([], { TRANSCRIPT_SITE_URL: "https://jeralyzer.pages.dev/" }); assert.equal(env.length, 1); assert.equal(env[0].origin, "https://jeralyzer.pages.dev"); assert.equal(env[0].fromEnv, true); const stored = connectedArchives([{ id: "jeralyzer", origin: "https://jeralyzer.pages.dev", title: "Jeralyzer", addedAt: "2026-11-11T00:00:00Z" }], { TRANSCRIPT_SITE_URL: "https://jeralyzer.pages.dev", }); assert.equal(stored.length, 1); assert.equal(stored[0].fromEnv, undefined); assert.equal(findConnectedArchive(stored, "jeralyzer.pages.dev/")?.id, "jeralyzer"); assert.equal(findConnectedArchive(stored, "https://elsewhere.test"), null); }); // ─── probeArchive, against a fake fetch only ─── const respond = (body: unknown, status = 200): Response => new Response(typeof body === "string" ? body : JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); function fakeFetch(map: Record Response>): FetchLike & { asked: string[] } { const asked: string[] = []; const f = (async (url: string) => { asked.push(url); const r = map[url]; if (!r) return respond({ error: "nope" }, 404); return r(); }) as FetchLike & { asked: string[] }; f.asked = asked; return f; } const CORPUS = { spec: 6, kind: "site", generatedAt: "2026-11-08T08:26:37.854Z", site: { id: "stubalyzer", title: "Stubalyzer", description: "A stub", url: "https://stub.test" }, totals: { channels: 2, videos: 30 }, channels: [ { slug: "alpha", name: "Alpha", videoCount: 20, platform: "youtube", url: "https://www.youtube.com/@alpha", manifests: {} }, { slug: "beta", name: "Beta", videoCount: 10, manifests: {} }, ], }; test("a probe reads corpus.json once and says what the archive holds", async () => { const f = fakeFetch({ "https://stub.test/corpus.json": () => respond(CORPUS) }); const p = await probeArchive("stub.test/", { fetch: f }); assert.deepEqual(f.asked, ["https://stub.test/corpus.json"]); assert.equal(p.origin, "https://stub.test"); assert.equal(p.siteId, "stubalyzer"); assert.equal(p.title, "Stubalyzer"); assert.equal(p.spec, 6); assert.deepEqual(p.totals, { channels: 2, videos: 30 }); assert.deepEqual(p.channels, [ { slug: "alpha", name: "Alpha", videoCount: 20, platform: "youtube", url: "https://www.youtube.com/@alpha" }, { slug: "beta", name: "Beta", videoCount: 10 }, ]); }); test("a probe refuses what is not one searchable archive, with a sentence", async () => { const cases: [string, () => Response, RegExp][] = [ ["https://a.test/corpus.json", () => respond("", 200), /is not JSON/], ["https://b.test/corpus.json", () => respond({}, 500), /answered HTTP 500/], ["https://c.test/corpus.json", () => respond({ kind: "hub", sites: [{ url: "https://m1.test" }] }), /federation hub of 1 site.*https:\/\/m1\.test/], ["https://d.test/corpus.json", () => respond({ ...CORPUS, site: { scope: "cited" }, channels: [] }), /publishes only its reports/], ["https://e.test/corpus.json", () => respond({ ...CORPUS, channels: [] }), /lists no channels/], ]; for (const [url, r, re] of cases) { await assert.rejects(probeArchive(url.replace("/corpus.json", ""), { fetch: fakeFetch({ [url]: r }) }), (e: unknown) => e instanceof ArchiveProbeError && re.test((e as Error).message)); } await assert.rejects(probeArchive("ftp://x", { fetch: fakeFetch({}) }), /not an archive's address/); const down = (async () => { throw new Error("ECONNREFUSED"); }) as FetchLike; await assert.rejects(probeArchive("https://f.test", { fetch: down }), /did not answer: ECONNREFUSED/); }); // ─── archiveChannelFacts (item 4): where a channel is, from corpus.json or its first record ─── test("a spec-6 archive names a channel's platform and page in corpus.json: one read", async () => { const f = fakeFetch({ "https://stub.test/corpus.json": () => respond(CORPUS) }); const facts = await archiveChannelFacts("https://stub.test", "alpha", { fetch: f }); assert.deepEqual(facts, { slug: "alpha", name: "Alpha", platform: "youtube", url: "https://www.youtube.com/@alpha", from: "corpus" }); assert.deepEqual(f.asked, ["https://stub.test/corpus.json"]); }); test("an older archive: the platform from the first shard's record, read in order, the lowest page", async () => { const f = fakeFetch({ "https://stub.test/corpus.json": () => respond({ ...CORPUS, spec: 5 }), "https://stub.test/transcripts/beta/manifest.json": () => respond({ pageCount: 3, slugToPage: { v9: 2, v1: 1, v2: 1 } }), "https://stub.test/transcripts/beta/page-0001.json": () => respond([{ id: "v1", title: "no url" }, { id: "v2", webpageUrl: "https://rumble.com/v2-x.html" }]), }); const facts = await archiveChannelFacts("https://stub.test", "beta", { fetch: f }); assert.deepEqual(facts, { slug: "beta", name: "Beta", platform: "rumble", url: null, from: "record", sampleVideoUrl: "https://rumble.com/v2-x.html" }); assert.deepEqual(f.asked, [ "https://stub.test/corpus.json", "https://stub.test/transcripts/beta/manifest.json", "https://stub.test/transcripts/beta/page-0001.json", ]); }); test("a channel the archive does not list is refused; one with no readable record is unknown", async () => { const f = fakeFetch({ "https://stub.test/corpus.json": () => respond({ ...CORPUS, spec: 5 }) }); await assert.rejects(archiveChannelFacts("https://stub.test", "nope", { fetch: f }), (e: unknown) => e instanceof ArchiveChannelError && /lists no channel "nope"/.test((e as Error).message)); const facts = await archiveChannelFacts("https://stub.test", "beta", { fetch: f }); assert.deepEqual(facts, { slug: "beta", name: "Beta", platform: null, url: null, from: "none" }); });