// Linking a video project to an article, in the plan's order: the article's // iterations, then the manifest's `article`, then the slug -- and only the // slug is ever ambiguous. import assert from "node:assert/strict"; import { mkdir, mkdtemp, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { clearLinksCache, iterationProjects, linkedProjects, linksCacheEpoch } from "./links.mjs"; async function put(file, value) { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, typeof value === "string" ? value : JSON.stringify(value)); } const manifest = (slug, extra = {}) => ({ slug, title: slug, timeline: [], ...extra }); const iteration = (article, n, projectDir) => ({ format: "archilyzer-iteration", version: 1, article, n, ...(projectDir ? { projectDir } : {}) }); async function fixture() { const base = await mkdtemp(path.join(tmpdir(), "umtool-links-")); const reports = path.join(base, "reports"); const articles = path.join(base, "articles"); // (1) zeta's iterations made two projects; a third is on another machine. await put(path.join(reports, "articles/zeta/i1/video.manifest.json"), manifest("zeta")); await put(path.join(reports, "articles/zeta/i3/video.manifest.json"), manifest("zeta")); await put(path.join(articles, "zeta/iterations/1/iteration.json"), iteration("zeta", 1, path.join(reports, "articles/zeta/i1"))); await put(path.join(articles, "zeta/iterations/2/iteration.json"), iteration("zeta", 2, null)); await put(path.join(articles, "zeta/iterations/3/iteration.json"), iteration("zeta", 3, path.join(reports, "articles/zeta/i3"))); await put(path.join(articles, "zeta/iterations/4/iteration.json"), iteration("zeta", 4, "/elsewhere/reports/articles/zeta/i4")); await put(path.join(articles, "zeta/iterations/current.json"), { format: "archilyzer-iterations-current", version: 1, report: 1, reportFrom: 1, video: 3 }); // (2) a hand-made project whose manifest names zeta. await put(path.join(reports, "hand/zeta-cut/video.manifest.json"), manifest("zeta", { article: "articles/zeta" })); // (3) slugs: one in a workspace, one outside it; two that only share a slug. await put(path.join(reports, "ws/polemic-alpha/video.manifest.json"), manifest("polemic-alpha")); await put(path.join(reports, "elsewhere/alpha/video.manifest.json"), manifest("alpha")); await put(path.join(reports, "one/beta/video.manifest.json"), manifest("beta")); await put(path.join(reports, "two/beta/video.manifest.json"), manifest("beta")); clearLinksCache(); return { base, reports, articles, o: { reportsRoot: reports, articlesDir: articles } }; } test("(1) an article's iterations link their projects; current.video is the cut; a far one is unreachable", async () => { const { o } = await fixture(); const r = await linkedProjects("articles", "zeta", o); assert.deepEqual( r.linked.map((p) => [p.id, p.via]), [ ["articles/zeta/i3", "iteration"], ["articles/zeta/i1", "iteration"], ["hand/zeta-cut", "manifest"], ], ); assert.equal(r.cut, "articles/zeta/i3"); assert.deepEqual(r.possible, []); assert.deepEqual(r.unreachable, [{ n: 4, dir: "/elsewhere/reports/articles/zeta/i4" }]); assert.match(r.how, /2 made by the article's iterations; manifest names the article/); }); test("(3) a claimed project is never another article's slug match", async () => { const { o } = await fixture(); // A site's own `zeta` shares the slug with zeta's i1 and i3 and with the // hand-made cut. Before (1) claimed them, each was "possible" here -- and the // store's zeta listed its own i1 as possible too. const r = await linkedProjects("priv", "zeta", o); assert.deepEqual(r.linked, []); assert.deepEqual(r.possible, []); assert.equal(r.cut, null); assert.deepEqual(r.unreachable, []); }); test("(3) a unique slug in the article's workspace links; without one it is only possible", async () => { const { reports, o } = await fixture(); const inWs = await linkedProjects("priv", "polemic-alpha", { ...o, workspace: path.join(reports, "ws") }); assert.deepEqual(inWs.linked.map((p) => [p.id, p.via]), [["ws/polemic-alpha", "slug"]]); assert.equal(inWs.cut, "ws/polemic-alpha"); const noWs = await linkedProjects("priv", "polemic-alpha", o); assert.deepEqual(noWs.linked, []); assert.deepEqual(noWs.possible.map((p) => p.id).sort(), ["elsewhere/alpha", "ws/polemic-alpha"]); }); test("(3) two projects sharing a slug are possible, never picked", async () => { const { o } = await fixture(); const r = await linkedProjects("priv", "beta", o); assert.deepEqual(r.linked, []); assert.deepEqual(r.possible.map((p) => p.id).sort(), ["one/beta", "two/beta"]); }); test("iteration dirs: a root reached through a link still matches; far dirs are kept, not dropped", async () => { const { base, reports, articles } = await fixture(); const alias = path.join(base, "alias"); await symlink(reports, alias); await put(path.join(articles, "kappa/iterations/1/iteration.json"), iteration("kappa", 1, path.join(alias, "articles/zeta/i1"))); clearLinksCache(); const its = await iterationProjects({ articlesDir: articles, reportsRoot: reports }); assert.deepEqual(its.get("kappa"), [{ n: 1, dir: path.join(alias, "articles/zeta/i1"), current: false, reachable: true, projectId: "articles/zeta/i1" }]); assert.deepEqual( its.get("zeta").map((x) => [x.n, x.reachable, x.projectId, x.current]), [ [4, false, null, false], [3, true, "articles/zeta/i3", true], [1, true, "articles/zeta/i1", false], ], ); }); test("clearing the cache bumps the epoch a memo compares", () => { const before = linksCacheEpoch(); clearLinksCache(); assert.equal(linksCacheEpoch(), before + 1); });