1 +import assert from "node:assert/strict";
2 +import { spawnSync } from "node:child_process";
3 +import { tmpdir } from "node:os";
4 +import { fileURLToPath } from "node:url";
5 +import { test } from "node:test";
6 +import { compare, inspect, parseVersion, readBaseline } from "./inspect.mjs";
8 +const sha = n => n.toString(16).padStart(40, "0");
9 +const commit = n => ({ sha: sha(n), commit: { message: `Change ${n}\nDetails` } });
10 +const page = (commits, total = commits.length, status = "ahead") => ({ status, total_commits: total, commits });
11 +const release = { tag_name: "v26.04", html_url: "https://github.com/niri-wm/niri/releases/tag/v26.04" };
13 +function responses(t, values) {
15 + t.mock.method(globalThis, "fetch", async (url, options) => {
16 + assert.equal(new URL(url).origin, "https://api.github.com");
17 + assert.ok(options.signal instanceof AbortSignal);
19 + assert.ok(values.length, `Unexpected request: ${url}`);
20 + return new Response(JSON.stringify(values.shift()));
25 +test("versions retain release labels and exact build hashes", () => {
26 + assert.deepEqual(parseVersion("26.04 (8ed0da4)"), { version: "26.04", hash: "8ed0da4" });
27 + assert.equal(parseVersion("26.04.1 (8ed0da4)").version, "26.04.1");
28 + for (const value of ["", "26.04", "nightly", "26.04 (unknown)", "26.04 (8ed0da4-dirty)"])
29 + assert.throws(() => parseVersion(value), /Unrecognized/);
32 +test("baseline is an explicit single comment, never inferred from installed Niri", () => {
33 + assert.equal(readBaseline('include "cfg/input.kdl"'), null);
34 + assert.equal(readBaseline("// reviewed-with: unknown\n"), null);
35 + assert.equal(readBaseline("// reviewed-with: 26.04 (8ed0da4)\r\n").version, "26.04");
36 + for (const source of [
37 + "// reviewed-with: latest",
38 + "// reviewed-with: unknown\n// reviewed-with: unknown",
39 + "// reviewed-with: 26.04 (8ed0da4) // inferred",
40 + ]) assert.throws(() => readBaseline(source));
43 +test("comparison collects every commit across pages", async t => {
44 + const commits = Array.from({ length: 251 }, (_, i) => commit(i + 1));
45 + const calls = responses(t, [page(commits.slice(0, 100), 251), page(commits.slice(100, 200), 251), page(commits.slice(200), 251)]);
46 + const result = await compare(sha(0), sha(251));
47 + assert.equal(result.commits.length, 251);
48 + assert.deepEqual(result.commits[0], { sha: sha(1), title: "Change 1", url: `https://github.com/niri-wm/niri/commit/${sha(1)}` });
49 + assert.match(calls[2], /page=3$/);
52 +test("identical commits have a complete empty range", async t => {
53 + responses(t, [page([], 0, "identical")]);
54 + assert.equal((await compare(sha(1), sha(1))).total_commits, 0);
57 +for (const [name, pages] of [
58 + ["older installed revision", [page([], 0, "behind")]],
59 + ["divergent history", [page([], 0, "diverged")]],
60 + ["incomplete pages", [page([commit(1)], 2), page([], 2)]],
61 + ["duplicate commits", [page([commit(1)], 2), page([commit(1)], 2)]],
62 + ["changing total", [page([commit(1)], 2), page([commit(2)], 3)]],
63 + ["excess commits", [page([commit(1), commit(2)], 1)]],
64 + ["missing total", [{ status: "ahead", commits: [] }]],
65 + ["missing commits", [{ status: "ahead", total_commits: 1 }]],
66 + ["invalid hash", [page([{ ...commit(1), sha: "bad" }])]],
67 + ["missing message", [page([{ sha: sha(1) }])]],
69 + test(`rejects ${name} rather than claiming a complete review`, async t => {
70 + responses(t, pages);
71 + await assert.rejects(compare(sha(0), sha(2)));
75 +test("reports reviewed-to-installed separately from development changes", async t => {
76 + const calls = responses(t, [
77 + release, { sha: sha(3) }, { sha: sha(2) }, { sha: sha(1) },
78 + page([commit(2)]), page([commit(3)]),
80 + const result = await inspect("// reviewed-with: 25.11 (abcdef0)", "niri 26.04 (8ed0da4)");
81 + assert.equal(result.baseline.version, "25.11");
82 + assert.equal(result.installed.version, "26.04");
83 + assert.equal(result.latest_release.tag, "v26.04");
84 + assert.equal(result.upstream.ref, "main");
85 + assert.match(result.installed.docs, new RegExp(`/${sha(2)}/docs/wiki/$`));
86 + assert.match(result.installed.default_config, new RegExp(`/${sha(2)}/resources/default-config.kdl$`));
87 + assert.equal(result.review.commits[0].sha, sha(2));
88 + assert.equal(result.requires_upgrade.commits[0].sha, sha(3));
89 + assert.match(calls[4], new RegExp(`compare/${sha(1)}\\.\\.\\.${sha(2)}`));
92 +test("unknown baseline requires full review", async t => {
93 + responses(t, [release, { sha: sha(2) }, { sha: sha(2) }, page([], 0, "identical")]);
94 + const result = await inspect("// reviewed-with: unknown", "niri 26.04 (8ed0da4)");
95 + assert.equal(result.baseline, null);
96 + assert.equal(result.review, null);
99 +test("malformed local metadata fails before networking", async t => {
100 + const calls = responses(t, []);
101 + await assert.rejects(inspect("// reviewed-with: latest", "niri 26.04 (8ed0da4)"));
102 + await assert.rejects(inspect("", "other-program 26.04 (8ed0da4)"));
103 + assert.equal(calls.length, 0);
106 +test("bad release metadata and resolved hashes fail closed", async t => {
107 + responses(t, [{}, release, { sha: "bad" }]);
108 + await assert.rejects(inspect("", "niri 26.04 (8ed0da4)"), /release/);
109 + await assert.rejects(inspect("", "niri 26.04 (8ed0da4)"), /SHA/);
112 +test("CLI locates config outside cwd and fails nonzero when Niri is missing", () => {
113 + const result = spawnSync(process.execPath, [fileURLToPath(new URL("inspect.mjs", import.meta.url))], {
114 + cwd: tmpdir(), env: { ...process.env, PATH: "" }, encoding: "utf8",
116 + assert.equal(result.status, 1);
117 + assert.equal(result.stdout, "");
118 + assert.match(result.stderr, /spawnSync niri ENOENT/);
121 +test("HTTP, JSON, and network errors propagate", async t => {
122 + t.mock.method(globalThis, "fetch", async () => new Response("rate limited", { status: 403 }));
123 + await assert.rejects(compare(sha(0), sha(1)), /HTTP 403/);
124 + t.mock.method(globalThis, "fetch", async () => new Response("invalid"));
125 + await assert.rejects(compare(sha(0), sha(1)), SyntaxError);
126 + t.mock.method(globalThis, "fetch", async () => { throw new Error("offline"); });
127 + await assert.rejects(compare(sha(0), sha(1)), /offline/);