import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // The source picker (demo epic D1): connected archives are offered through // the one resolver; with no local build, the first archive is the default. const root = await mkdtemp(path.join(os.tmpdir(), "umtool-sources-")); const local = path.join(root, "public"); const settingsFile = path.join(root, "settings.json"); await mkdir(path.join(root, "sites", "mysite"), { recursive: true }); await writeFile(path.join(root, "sites", "mysite", "site.json"), JSON.stringify({ siteUrl: "https://stub.test/", siteTitle: "From site.json" })); await writeFile(settingsFile, JSON.stringify({ remoteArchives: [{ id: "stub", origin: "https://stub.test", title: "Stubalyzer", addedAt: "2026-11-11T00:00:00Z" }] })); process.env.SITES_DIR = path.join(root, "sites"); process.env.SETTINGS_FILE = settingsFile; process.env.TRANSCRIPT_LOCAL_DIR = local; process.env.TRANSCRIPT_HUB_URL = ""; process.env.TRANSCRIPT_SITE_URL = "https://env.test"; const { listSources, buildIdentity } = await import("./sources"); test.after(() => rm(root, { recursive: true, force: true })); test("no local build: the connected archives come first, site.json's duplicate is dropped, the env's is offered", async () => { const s = await listSources(); assert.deepEqual( s.map((x) => [x.handle, x.label]), [ ["remote:https://stub.test", "Stubalyzer"], ["remote:https://env.test", "env.test"], [`local:${local}`, "Local corpus"], ], ); assert.equal(s[2].detail, "this machine ยท no build"); }); test("with a local build the local corpus is the default, the archives after it", async () => { await mkdir(local, { recursive: true }); await writeFile(path.join(local, "corpus.json"), JSON.stringify({ site: { title: "Mine" } })); const s = await listSources(); assert.equal(s[0].handle, `local:${local}`); assert.deepEqual(s.slice(1).map((x) => x.handle), ["remote:https://stub.test", "remote:https://env.test"]); }); // demo epic D1 re-review, item 4: a source's build is read from its own origin, bounded. test("a remote source's build identity: corpus.json on its own origin; a redirect elsewhere or an oversize answer is no identity", async () => { const option = { handle: "remote:https://stub.test", kind: "remote", label: "Stub" } as never; const asked: { url: string; redirect?: string }[] = []; const fake = (answers: Record Response>) => (async (url: string, init?: { redirect?: string }) => { asked.push({ url, redirect: init?.redirect }); return (answers[url] ?? (() => new Response("nope", { status: 404 })))(); }) as never; assert.equal(await buildIdentity(option, fake({ "https://stub.test/corpus.json": () => new Response(JSON.stringify({ generatedAt: "2026-11-01T00:00:00Z" })) })), "2026-11-01T00:00:00Z"); assert.deepEqual(asked, [{ url: "https://stub.test/corpus.json", redirect: "manual" }]); // A same-origin redirect is followed. assert.equal( await buildIdentity( option, fake({ "https://stub.test/corpus.json": () => new Response(null, { status: 301, headers: { location: "/v2/corpus.json" } }), "https://stub.test/v2/corpus.json": () => new Response(JSON.stringify({ generatedAt: "g2" })), }), ), "g2", ); // One that leaves the origin is not: nothing is asked of the other host. asked.length = 0; assert.equal( await buildIdentity( option, fake({ "https://stub.test/corpus.json": () => new Response(null, { status: 302, headers: { location: "http://127.0.0.1:3001/api/ops/settings" } }), "http://127.0.0.1:3001/api/ops/settings": () => new Response(JSON.stringify({ generatedAt: "leaked" })), }), ), null, ); assert.deepEqual(asked.map((a) => a.url), ["https://stub.test/corpus.json"]); // An answer declaring more than the document bound is not read. assert.equal( await buildIdentity(option, fake({ "https://stub.test/corpus.json": () => new Response(JSON.stringify({ generatedAt: "big" }), { headers: { "content-length": String(64 * 1024 * 1024) } }) })), null, ); });