import { test } from "node:test"; import assert from "node:assert/strict"; import { connectRemoteArchive, removeRemoteArchive, RemoteArchiveError, type RemoteArchiveDeps } from "./remoteArchives"; import type { RemoteArchive } from "../lib/remoteArchives"; import type { FetchLike } from "../lib/remoteArchives-server"; const CORPUS = { spec: 6, kind: "site", generatedAt: "2026-11-08T00:00:00.000Z", site: { id: "stubalyzer", title: "Stubalyzer" }, totals: { channels: 1, videos: 3 }, channels: [{ slug: "alpha", name: "Alpha", videoCount: 3 }], }; function world(initial: RemoteArchive[] = [], env: Record = {}) { let stored: unknown = initial; const writes: RemoteArchive[][] = []; const asked: string[] = []; const fetch: FetchLike = async (url) => { asked.push(url); return new Response(JSON.stringify(CORPUS), { status: 200 }); }; const deps: RemoteArchiveDeps = { stored: () => stored, save: async (next) => { writes.push(next); stored = next; }, fetch, now: () => new Date("2026-11-11T12:00:00Z"), env, // No real DNS in a test: every name is a public address unless a test says. lookup: async () => [{ address: "93.184.216.34" }], }; return { deps, writes, asked, stored: () => stored }; } test("connect: probed once, saved with the archive's own id and title", async () => { const w = world(); const r = await connectRemoteArchive({ origin: "https://stub.test/" }, w.deps); assert.deepEqual(w.asked, ["https://stub.test/corpus.json"]); assert.equal(r.already, false); assert.deepEqual(w.writes, [[{ id: "stubalyzer", origin: "https://stub.test", title: "Stubalyzer", addedAt: "2026-11-11T12:00:00.000Z" }]]); // Again: nothing written, it says so. const again = await connectRemoteArchive({ origin: "stub.test" }, w.deps); assert.equal(again.already, true); assert.equal(w.writes.length, 1); }); test("a dry run reads and writes nothing", async () => { const w = world(); const r = await connectRemoteArchive({ origin: "https://stub.test", dryRun: true }, w.deps); assert.equal(r.already, null); assert.equal(r.probe.totals.videos, 3); assert.equal(w.writes.length, 0); }); test("the environment's archive can be connected (stored) but not removed", async () => { const w = world([], { TRANSCRIPT_SITE_URL: "https://stub.test" }); await assert.rejects(removeRemoteArchive("https://stub.test", w.deps), (e: unknown) => e instanceof RemoteArchiveError && /TRANSCRIPT_SITE_URL/.test((e as Error).message)); const r = await connectRemoteArchive({ origin: "https://stub.test" }, w.deps); assert.equal(r.already, false); assert.equal(w.writes.length, 1); }); test("remove by id or by origin; an unknown one is refused, naming what is connected", async () => { const a: RemoteArchive = { id: "a", origin: "https://a.test", title: "A", addedAt: "2026-11-11T00:00:00.000Z" }; const b: RemoteArchive = { id: "b", origin: "https://b.test", title: "B", addedAt: "2026-11-11T00:00:00.000Z" }; const w = world([a, b]); assert.deepEqual(await removeRemoteArchive("a", w.deps), a); assert.deepEqual(await removeRemoteArchive("https://b.test/", w.deps), b); assert.deepEqual(w.stored(), []); await assert.rejects(removeRemoteArchive("zzz", w.deps), /no connected archive "zzz" — connected: none/); }); // demo epic D1 review, blocker 2c: this machine or its network is not connected unless decided here. test("a local or private archive host is refused before anything is asked of it; the operator's own word allows it", async () => { for (const origin of ["http://127.0.0.1:4100", "http://[::1]:8080", "http://localhost:3000", "http://nas.local", "http://10.0.0.7", "http://192.168.1.20/archive"]) { const w = world(); await assert.rejects(connectRemoteArchive({ origin }, w.deps), /an archive on this machine or its network is connected only when/, origin); assert.deepEqual(w.asked, [], `${origin}: nothing asked`); } // A public name that resolves to a private address. const sneaky = world(); sneaky.deps.lookup = async () => [{ address: "93.184.216.34" }, { address: "192.168.1.9" }]; await assert.rejects(connectRemoteArchive({ origin: "https://rebind.example" }, sneaky.deps), /a name for a local or private address \(192\.168\.1\.9\)/); assert.deepEqual(sneaky.asked, []); // Decided on this machine: the editor's environment, TRANSCRIPT_SITE_URL, or settings.json. const allowed = world([], { ARCHILYZER_ALLOW_PRIVATE_ARCHIVES: "1" }); assert.equal((await connectRemoteArchive({ origin: "http://127.0.0.1:4100" }, allowed.deps)).already, false); const fromEnv = world([], { TRANSCRIPT_SITE_URL: "http://127.0.0.1:4100" }); assert.equal((await connectRemoteArchive({ origin: "http://127.0.0.1:4100" }, fromEnv.deps)).already, false); const stored = world([{ id: "lan", origin: "http://10.0.0.7", title: "LAN", addedAt: "2026-11-11T00:00:00.000Z" }]); assert.equal((await connectRemoteArchive({ origin: "http://10.0.0.7" }, stored.deps)).already, true); }); // demo epic D1 re-review, must-fix 2: the address forms a URL may spell, and a redirect at connect. test("a local address in any spelling is refused at connect, nothing asked; allowed only by the operator's own word", async () => { for (const origin of ["http://[::ffff:7f00:1]", "http://[::ffff:127.0.0.1]:3001", "http://[::1]", "http://0.0.0.0:3001", "http://2130706433", "http://0x7f000001", "http://0177.0.0.1", "http://127.1"]) { const w = world(); await assert.rejects(connectRemoteArchive({ origin }, w.deps), /an archive on this machine or its network is connected only when/, origin); assert.deepEqual(w.asked, [], `${origin}: nothing asked`); assert.equal(w.writes.length, 0, `${origin}: nothing written`); const allowed = world([], { ARCHILYZER_ALLOW_PRIVATE_ARCHIVES: "1" }); assert.equal((await connectRemoteArchive({ origin }, allowed.deps)).already, false, `${origin}: allowed when decided here`); } }); test("a public archive that redirects its corpus.json to a private address is not followed and not connected", async () => { for (const location of ["http://127.0.0.1:3001/corpus.json", "http://[::ffff:7f00:1]/corpus.json", "http://169.254.169.254/latest/meta-data/", "http://2130706433/corpus.json", "//localhost:3001/corpus.json"]) { const w = world(); const asked: string[] = []; w.deps.fetch = async (url) => { asked.push(url); return new Response(null, { status: 302, headers: { location } }); }; await assert.rejects(connectRemoteArchive({ origin: "https://stub.test" }, w.deps), /an archive's documents are read from its own origin only/, location); assert.deepEqual(asked, ["https://stub.test/corpus.json"], `${location}: the redirect is not followed`); assert.equal(w.writes.length, 0, `${location}: nothing written`); } });