Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(scripts): add coverage for module pin status and repin flows

Steven Enamakela45e209087e5
diff --git a/scripts/__tests__/module-pin-status.test.mjs b/scripts/__tests__/module-pin-status.test.mjs
new file mode 100644
index 0000000000..70d36df513
--- /dev/null
+++ b/scripts/__tests__/module-pin-status.test.mjs
@@ -0,0 +1,151 @@
+// Tests for scripts/modules/pin-status.mjs (tinyhumansai/openhuman#7615):
+// in sync, ahead, behind, diverged and exemption cases, then the CLI against a
+// fixture tree built from throwaway git repositories.
+import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { test } from "node:test";
+
+import {
+ aheadIds,
+ AHEAD_MESSAGE,
+ classifyStatus,
+ parseDescribe,
+} from "../lib/module-pin-status.mjs";
+import { main } from "../modules/pin-status.mjs";
+
+const base = { id: "tinyx", version: "0.2.0", submodulePath: "vendor/tinyx" };
+const kind = (over) => classifyStatus({ ...base, ...over }).kind;
+
+test("parseDescribe reads exact tags and N-past forms", () => {
+ assert.deepEqual(parseDescribe("v1.2.3"), {
+ tag: "v1.2.3",
+ parts: [1, 2, 3],
+ commitsPast: 0,
+ });
+ assert.equal(parseDescribe("v1.2.3-7-gabcdef01").commitsPast, 7);
+ assert.equal(parseDescribe("nonsense"), null);
+});
+
+test("a gitlink on the registry tag is in sync", () => {
+ assert.equal(kind({ actual: "v0.2.0", wantedIsAncestor: true }), "in-sync");
+});
+
+test("a gitlink commits past the pinned tag is ahead", () => {
+ const s = classifyStatus({ ...base, actual: "v0.2.0-3-gabcdef01", wantedIsAncestor: true });
+ assert.equal(s.kind, "ahead");
+ assert.match(s.message, /you're running a locally compiled module/);
+ assert.ok(s.message.startsWith(AHEAD_MESSAGE));
+});
+
+test("a gitlink on a newer release than the registry is ahead", () => {
+ assert.equal(kind({ actual: "v0.2.2", wantedIsAncestor: true }), "ahead");
+});
+
+test("a gitlink on an older release than the registry is behind", () => {
+ const s = classifyStatus({ ...base, actual: "v0.1.9", wantedIsAncestor: true });
+ assert.equal(s.kind, "behind");
+ assert.match(s.message, /behind\/diverged/);
+});
+
+test("a registry tag that is not an ancestor is diverged", () => {
+ assert.equal(kind({ actual: "v0.2.2", wantedIsAncestor: false }), "diverged");
+ assert.equal(kind({ actual: "garbage" }), "diverged");
+});
+
+test("a declared exemption reports exempt only while the drift is unchanged", () => {
+ const exemption = { id: "tinyx", expect: "v0.1.17-159-gf95c461e", reason: "known" };
+ assert.equal(kind({ actual: "v0.1.17-159-gf95c461e", exemption }), "exempt");
+ // Drift moved: falls back to a real classification, and says why.
+ const moved = classifyStatus({ ...base, actual: "v0.1.17-200-g00000000", exemption, wantedIsAncestor: true });
+ assert.equal(moved.kind, "behind");
+ assert.match(moved.message, /moved past its module-pin-exemptions\.json entry/);
+ // Pins agree but the exemption lingers: in sync, flagged.
+ const stale = classifyStatus({ ...base, actual: "v0.2.0", exemption });
+ assert.equal(stale.kind, "in-sync");
+ assert.match(stale.message, /stale/);
+});
+
+test("aheadIds lists only ahead modules", () => {
+ assert.deepEqual(
+ aheadIds([{ id: "a", kind: "ahead" }, { id: "b", kind: "in-sync" }, { id: "c", kind: "behind" }]),
+ ["a"],
+ );
+});
+
+// ── CLI against a fixture tree ───────────────────────────────────────────────
+
+const git = (cwd, ...args) =>
+ execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false", "-c", "tag.gpgsign=false", ...args], {
+ cwd,
+ encoding: "utf8",
+ stdio: ["ignore", "pipe", "pipe"],
+ }).trim();
+
+function record(name, id, version, sub) {
+ return `
+pub(crate) const ${name}: ModuleRecord = ModuleRecord {
+ id: "${id}",
+ version: "${version}",
+ release_url: "https://github.com/tinyhumansai/${sub}/releases/tag/v${version}",
+ assets: &[],
+ source: SourceSpec {
+ submodule: "${sub}",
+ manifest: "Cargo.toml",
+ package: "${sub}-module",
+ lib_name: "${sub}_module",
+ features: &[],
+ },
+};
+`;
+}
+
+function fixture() {
+ const root = mkdtempSync(join(tmpdir(), "pin-status-"));
+ const reg = join(root, "crates/openhuman-core/src/modules");
+ mkdirSync(join(reg, "registry"), { recursive: true });
+ writeFileSync(join(reg, "registry.rs"), "");
+ writeFileSync(
+ join(reg, "registry/records_a.rs"),
+ record("SYNCED", "synced", "1.0.0", "synced") +
+ record("AHEAD", "ahead", "1.0.0", "ahead") +
+ record("MISSING", "missing", "1.0.0", "missing"),
+ );
+ mkdirSync(join(root, "scripts/ci"), { recursive: true });
+ writeFileSync(join(root, "scripts/ci/module-pin-exemptions.json"), '{"exemptions":[]}');
+ for (const sub of ["synced", "ahead"]) {
+ const dir = join(root, "vendor", sub);
+ mkdirSync(dir, { recursive: true });
+ git(dir, "init", "-q");
+ writeFileSync(join(dir, "f"), "1");
+ git(dir, "add", ".");
+ git(dir, "commit", "-qm", "one");
+ git(dir, "tag", "v1.0.0");
+ if (sub === "ahead") {
+ writeFileSync(join(dir, "f"), "2");
+ git(dir, "commit", "-qam", "two");
+ }
+ }
+ return root;
+}
+
+test("the CLI reports each fixture module and exports the ahead ids", () => {
+ const root = fixture();
+ try {
+ const { lines, code } = main(["--print-env"], root);
+ const text = lines.join("\n");
+ assert.match(text, /synced\s+in-sync/);
+ assert.match(text, /ahead\s+ahead\s+gitlink ahead of released pin/);
+ assert.match(text, /missing\s+unavailable\s+vendor\/missing is not checked out/);
+ assert.match(text, /OPENHUMAN_MODULE_PIN_AHEAD=ahead$/);
+ assert.equal(code, 0, "advisory unless --strict");
+ assert.equal(main(["--strict"], root).code, 1);
+ const hint = main(["--hint"], root).lines.join("\n");
+ assert.doesNotMatch(hint, /synced/);
+ assert.match(hint, /release-and-repin\.mjs/);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
diff --git a/scripts/__tests__/module-release-and-repin.test.mjs b/scripts/__tests__/module-release-and-repin.test.mjs
new file mode 100644
index 0000000000..959440278f
--- /dev/null
+++ b/scripts/__tests__/module-release-and-repin.test.mjs
@@ -0,0 +1,242 @@
+// Tests for scripts/modules/release-and-repin.mjs (tinyhumansai/openhuman#7615).
+// Fixtures only: gh and git are faked, no release is dispatched.
+import assert from "node:assert/strict";
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import { test } from "node:test";
+import { fileURLToPath } from "node:url";
+
+import {
+ bumpVersion,
+ dropExemption,
+ parseChecksumToml,
+ rewriteRecord,
+ unusedChecksums,
+ updateProviderPins,
+} from "../lib/module-repin.mjs";
+import { parseArgs, repin } from "../modules/release-and-repin.mjs";
+
+const HERE = dirname(fileURLToPath(import.meta.url));
+const OLD = "a".repeat(64);
+const SHA_X = "1".repeat(64);
+const SHA_A = "2".repeat(64);
+const CHECKSUM = `[sha256]
+"tinyx-module-0.3.0-ubuntu-24.04-x86_64.tar.gz" = "${SHA_X}"
+"tinyx-module-0.3.0-macos-15-arm64.tar.gz" = "${SHA_A}"
+"tinyx-module-0.3.0-windows-2025-x86_64.zip" = "${"3".repeat(64)}"
+`;
+
+const RECORDS = `
+pub(crate) const OTHER: ModuleRecord = ModuleRecord {
+ id: "other",
+ version: "0.2.0",
+ release_url: "https://github.com/tinyhumansai/other/releases/tag/v0.2.0",
+ assets: &[
+ PlatformAsset {
+ host_key: "ubuntu-24.04-x86_64",
+ archive: "other-module-0.2.0-ubuntu-24.04-x86_64.tar.gz",
+ sha256: "${OLD}",
+ },
+ ],
+ source: SourceSpec {
+ submodule: "other",
+ manifest: "Cargo.toml",
+ package: "other-module",
+ lib_name: "other_module",
+ features: &[],
+ },
+};
+
+pub(crate) const TINYX: ModuleRecord = ModuleRecord {
+ id: "tinyx",
+ version: "0.2.0",
+ release_url: "https://github.com/tinyhumansai/tinyx/releases/tag/v0.2.0",
+ assets: &[
+ PlatformAsset {
+ host_key: "ubuntu-24.04-x86_64",
+ archive: "tinyx-module-0.2.0-ubuntu-24.04-x86_64.tar.gz",
+ sha256: "${OLD}",
+ },
+ PlatformAsset {
+ host_key: "macos-15-arm64",
+ archive: "tinyx-module-0.2.0-macos-15-arm64.tar.gz",
+ sha256: "${OLD}",
+ },
+ PlatformAsset {
+ host_key: "windows-2025-x86_64",
+ archive: "tinyx-module-0.2.0-windows-2025-x86_64.zip",
+ sha256: "${OLD}",
+ },
+ ],
+ source: SourceSpec {
+ submodule: "tinyx",
+ manifest: "Cargo.toml",
+ package: "tinyx-module",
+ lib_name: "tinyx_module",
+ features: &[],
+ },
+};
+`;
+
+test("parseChecksumToml reads the [sha256] table and rejects bad digests", () => {
+ assert.equal(parseChecksumToml(CHECKSUM).size, 3);
+ assert.throws(() => parseChecksumToml('[sha256]\n"a.tar.gz" = "xyz"\n'), /64-hex/);
+ assert.throws(() => parseChecksumToml("[other]\n"), /no \[sha256\]/);
+});
+
+test("rewriteRecord takes version, url, archives and digests verbatim from checksum.toml", () => {
+ const sums = parseChecksumToml(CHECKSUM);
+ const { src, changed } = rewriteRecord(RECORDS, "tinyx", "0.3.0", sums);
+ assert.ok(changed);
+ assert.match(src, /id: "tinyx",\s+version: "0\.3\.0"/);
+ assert.match(src, /tinyx\/releases\/tag\/v0\.3\.0"/);
+ assert.match(src, new RegExp(`tinyx-module-0\\.3\\.0-ubuntu-24\\.04-x86_64\\.tar\\.gz",\\s+sha256: "${SHA_X}"`));
+ assert.match(src, new RegExp(`macos-15-arm64\\.tar\\.gz",\\s+sha256: "${SHA_A}"`));
+ assert.match(src, /windows-2025-x86_64\.zip",\s+sha256: "3{64}"/);
+ // The neighbouring record is untouched.
+ assert.match(src, /other-module-0\.2\.0-ubuntu-24\.04-x86_64\.tar\.gz",\s+sha256: "a{64}"/);
+ assert.equal(src.match(/version: "0\.2\.0"/g).length, 1);
+ assert.deepEqual(unusedChecksums(src, "tinyx", sums), []);
+});
+
+test("rewriteRecord refuses a release that lacks an asset, and an unknown id is unchanged", () => {
+ const partial = parseChecksumToml(
+ `[sha256]\n"tinyx-module-0.3.0-ubuntu-24.04-x86_64.tar.gz" = "${SHA_X}"\n`,
+ );
+ assert.throws(() => rewriteRecord(RECORDS, "tinyx", "0.3.0", partial), /no entry for/);
+ const none = rewriteRecord(RECORDS, "nope", "0.3.0", parseChecksumToml(CHECKSUM));
+ assert.equal(none.changed, false);
+ assert.equal(none.src, RECORDS);
+});
+
+test("the helper never computes a digest", () => {
+ for (const file of ["../lib/module-repin.mjs", "../modules/release-and-repin.mjs"]) {
+ const text = readFileSync(resolve(HERE, file), "utf8");
+ assert.doesNotMatch(text, /node:crypto|createHash|sha256sum|shasum|openssl/, file);
+ }
+});
+
+test("bumpVersion and parseArgs", () => {
+ assert.equal(bumpVersion("0.2.9", "patch"), "0.2.10");
+ assert.equal(bumpVersion("0.2.9", "minor"), "0.3.0");
+ assert.equal(bumpVersion("0.2.9", "major"), "1.0.0");
+ assert.throws(() => bumpVersion("0.2.9", "huge"), /--bump/);
+ assert.deepEqual(parseArgs(["tinyx", "--bump", "minor", "--yes"]), { bump: "minor", yes: true, dryRun: false, id: "tinyx" });
+ assert.throws(() => parseArgs([]), /usage/);
+ assert.throws(() => parseArgs(["x", "--bump", "no"]), /--bump/);
+});
+
+test("provider pins and exemptions are updated only for the named id", () => {
+ const pins = { providers: { tinyx: { repo: "o/r", version: "0.2.0", built_against: "b" }, y: { version: "1" } } };
+ const out = updateProviderPins(pins, "tinyx", "0.3.0", "c".repeat(40));
+ assert.equal(out.json.providers.tinyx.version, "0.3.0");
+ assert.equal(out.json.providers.tinyx.built_against, "c".repeat(40));
+ assert.equal(out.json.providers.y.version, "1");
+ assert.equal(updateProviderPins(pins, "zzz", "1.0.0", "c").changed, false);
+ const ex = dropExemption({ exemptions: [{ id: "tinyx" }, { id: "y" }] }, "tinyx");
+ assert.deepEqual(ex.json.exemptions, [{ id: "y" }]);
+});
+
+// ── the whole flow, with gh and git faked ────────────────────────────────────
+
+function tree() {
+ const root = mkdtempSync(join(tmpdir(), "repin-fixture-"));
+ const dir = join(root, "crates/openhuman-core/src/modules/registry");
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, "records_a.rs"), RECORDS);
+ mkdirSync(join(root, "scripts/ci"), { recursive: true });
+ writeFileSync(join(root, "scripts/ci/module-pin-exemptions.json"), JSON.stringify({ exemptions: [{ id: "tinyx", expect: "v0", reason: "r" }] }));
+ writeFileSync(join(root, "scripts/ci/module-provider-pins.json"), JSON.stringify({ providers: {} }));
+ return root;
+}
+
+function fakeRun(calls, { checksum = CHECKSUM } = {}) {
+ return (cmd, args, opts = {}) => {
+ calls.push([cmd, ...args]);
+ if (cmd === "gh" && args[0] === "release" && args[1] === "download") {
+ writeFileSync(join(args[args.indexOf("-D") + 1], "checksum.toml"), checksum);
+ }
+ if (cmd === "gh" && args[0] === "run" && args[1] === "list") return { code: 0, stdout: "42\n", stderr: "" };
+ if (cmd === "gh" && args[0] === "release" && args[1] === "view") return { code: 0, stdout: "v0.3.0\n", stderr: "" };
+ if (cmd === "git" && args[0] === "rev-parse") return { code: 0, stdout: "d".repeat(40) + "\n", stderr: "" };
+ void opts;
+ return { code: 0, stdout: "", stderr: "" };
+ };
+}
+
+test("an existing release is re-pinned without dispatching anything", async () => {
+ const root = tree();
+ const calls = [];
+ try {
+ const res = await repin({ id: "tinyx", version: "0.3.0", bump: "patch", root }, { run: fakeRun(calls), confirm: async () => assert.fail("must not ask"), log: () => {} });
+ assert.equal(res.version, "0.3.0");
+ assert.deepEqual(res.failed, []);
+ assert.ok(!calls.some((c) => c.includes("workflow")), "no dispatch");
+ assert.ok(calls.some((c) => c[0] === "git" && c[1] === "checkout" && c[2] === "v0.3.0"));
+ assert.ok(calls.some((c) => c[0] === "git" && c[1] === "add" && c[2] === "vendor/tinyx"));
+ assert.ok(calls.some((c) => c[0] === "node" && c[1].endsWith("check-module-pins.mjs")));
+ assert.ok(calls.some((c) => c[0] === "node" && c[1].endsWith("check-submodule-monotonic.mjs")));
+ const src = readFileSync(join(root, "crates/openhuman-core/src/modules/registry/records_a.rs"), "utf8");
+ assert.match(src, new RegExp(SHA_X));
+ assert.deepEqual(JSON.parse(readFileSync(join(root, "scripts/ci/module-pin-exemptions.json"), "utf8")).exemptions, []);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test("dispatching a release asks first and a refusal dispatches nothing", async () => {
+ const root = tree();
+ const calls = [];
+ try {
+ await assert.rejects(
+ repin({ id: "tinyx", bump: "minor", root }, { run: fakeRun(calls), confirm: async () => false, log: () => {} }),
+ /not confirmed/,
+ );
+ assert.deepEqual(calls, []);
+
+ const asked = [];
+ const res = await repin({ id: "tinyx", bump: "minor", root }, {
+ run: fakeRun(calls),
+ confirm: async (q) => (asked.push(q), true),
+ log: () => {},
+ });
+ assert.equal(asked.length, 1);
+ assert.match(asked[0], /hard to undo/);
+ assert.equal(res.version, "0.3.0");
+ const dispatch = calls.find((c) => c[1] === "workflow");
+ assert.deepEqual(dispatch, ["gh", "workflow", "run", "release.yml", "-R", "tinyhumansai/tinyx", "-f", "bump=minor"]);
+ assert.ok(calls.findIndex((c) => c[1] === "workflow") < calls.findIndex((c) => c[1] === "release" && c[2] === "download"));
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test("--dry-run changes and dispatches nothing", async () => {
+ const root = tree();
+ const calls = [];
+ try {
+ const before = readFileSync(join(root, "crates/openhuman-core/src/modules/registry/records_a.rs"), "utf8");
+ const res = await repin({ id: "tinyx", bump: "patch", dryRun: true, root }, { run: fakeRun(calls), log: () => {} });
+ assert.ok(res.dryRun);
+ assert.deepEqual(calls, []);
+ assert.equal(readFileSync(join(root, "crates/openhuman-core/src/modules/registry/records_a.rs"), "utf8"), before);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});
+
+test("a failed pin gate is reported as failed", async () => {
+ const root = tree();
+ const calls = [];
+ const run = fakeRun(calls);
+ try {
+ const res = await repin({ id: "tinyx", version: "0.3.0", bump: "patch", root }, {
+ run: (cmd, args, o) => (cmd === "node" && args[0].endsWith("check-module-pins.mjs") ? { code: 1, stdout: "", stderr: "drift" } : run(cmd, args, o)),
+ log: () => {},
+ });
+ assert.deepEqual(res.failed, ["check-module-pins.mjs"]);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+});