// Tests for the local git-ref fixtures of scripts/install-ref-fixtures.mjs, // which later phases reuse to test ref resolution, redirect classification and // rollback without network access. Run with: node --test scripts/test-install-refs.mjs // // skill-script-helpers.mjs is imported first on purpose: it scrubs the whole // test process's git config, so the fixtures, the modules they use and any // spawned git agree (the AGENTS.md failure-log rule). The fixtures themselves // are real bare repositories in the OS temp dir; `git ls-remote` against a // local path never touches the network. import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { execFileSync, spawnSync } from "node:child_process"; import "./skill-script-helpers.mjs"; import { createInstallRefFixture, DEFAULT_RELEASES } from "./install-ref-fixtures.mjs"; import { latestTag } from "../src/web-console/update-check.mjs"; /** `git ` inside a fixture's bare repo, for type/sha checks. */ const gitIn = (dir, args) => execFileSync("git", ["--git-dir", dir, ...args], { encoding: "utf8" }).trim(); const SHA = /^[0-9a-f]{40}$/; test("fixture: a bare repository in a temp dir, no working tree", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.ok(fx.root.startsWith(fs.realpathSync.native(os.tmpdir())), "the fixture lives in the OS temp dir"); assert.ok(fs.existsSync(path.join(fx.dir, "HEAD")), "a git dir"); assert.equal(gitIn(fx.dir, ["rev-parse", "--is-bare-repository"]), "true"); assert.equal(fs.existsSync(path.join(fx.root, "install.js")), false, "nothing is checked out beside it"); }); test("fixture: main and feature branches exist and point at real commits", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.match(fx.refs.heads.main, SHA); assert.match(fx.refs.heads["feature/demo"], SHA); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/heads/main"]), "commit"); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/heads/feature/demo"]), "commit"); // A custom branch set is honored. const custom = createInstallRefFixture({ featureBranches: ["feature/one", "bugfix/two"] }); t.after(custom.cleanup); assert.deepEqual(Object.keys(custom.refs.heads).sort(), ["bugfix/two", "feature/one", "main", "release"].sort()); }); test("fixture: lightweight v* tags point straight at commits", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/tags/v1.0.0"]), "commit"); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/tags/v2.0.0"]), "commit", "the default v2.0.0 is lightweight"); assert.equal(fx.refs.peeled["v1.0.0"], undefined, "no peeled line for a lightweight tag"); }); test("fixture: annotated v* tags are tag objects and produce peeled ^{} lines", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/tags/v3.1.2"]), "tag", "the ref names a tag object"); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/tags/v3.1.2^{commit}"]), "commit"); // The tag object sha and the peeled commit sha differ, and both are recorded. assert.notEqual(fx.refs.tags["v3.1.2"], fx.refs.peeled["v3.1.2"]); assert.match(fx.refs.tags["v3.1.2"], SHA); assert.match(fx.refs.peeled["v3.1.2"], SHA); // Peeled lines are in the real ls-remote output and in the derived snapshot. const peeledLine = `${fx.refs.peeled["v3.1.2"]}\trefs/tags/v3.1.2^{}`; assert.ok(fx.lsRemote().includes(peeledLine), "real output"); assert.ok(fx.snapshotText().includes(peeledLine), "derived output"); // Exactly one peeled line: lightweight tags never grow one. assert.equal(fx.snapshotText().split("^{}").length - 1, 1); }); test("fixture: synthetic install-v* tags exist for the configured versions only", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.match(fx.refs.tags["install-v3.1.2"], SHA); assert.match(fx.refs.tags["install-v2.0.0"], SHA); assert.equal(fx.refs.tags["install-v1.0.0"], undefined, "v1.0.0 has no synthetic ref: the rollback fallback case"); assert.equal(gitIn(fx.dir, ["cat-file", "-t", "refs/tags/install-v3.1.2"]), "commit"); // The slim payload carries its binding manifest and none of the heavy docs. const listing = gitIn(fx.dir, ["ls-tree", "-r", "--name-only", "refs/tags/install-v3.1.2"]); assert.ok(listing.split("\n").includes(".plan2code-install-ref.json")); assert.ok(!listing.split("\n").some((n) => n.startsWith("docs/")), "the synthetic tree is slim"); assert.ok(gitIn(fx.dir, ["ls-tree", "-r", "--name-only", "refs/tags/v3.1.2^{commit}"]).split("\n").some((n) => n.startsWith("docs/")), "the full tree is not"); }); test("fixture: rollback versions exist, oldest to newest, and numeric ordering beats lexical", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); const versions = fx.versions.map((v) => v.version); assert.deepEqual(versions, ["1.0.0", "2.0.0", "3.1.2"]); assert.equal(fx.newest, "3.1.2"); // Every rollback target resolves to its own commit on main. for (const v of fx.versions) assert.equal(gitIn(fx.dir, ["rev-parse", `refs/tags/v${v.version}^{commit}`]), v.commit); // A 3.10.0 release outranks 3.9.0 numerically (lexical order would pick 3.9.0). const numeric = createInstallRefFixture({ releases: [{ version: "3.10.0" }, { version: "3.9.0" }] }); t.after(numeric.cleanup); assert.equal(numeric.newest, "3.10.0"); assert.deepEqual(numeric.versions.map((v) => v.version), ["3.9.0", "3.10.0"]); }); test("fixture: the release branch is present by default at the newest synthetic tip, or absent", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.match(fx.refs.heads.release, SHA); assert.equal(fx.refs.heads.release, fx.refs.tags["install-v3.1.2"], "the published shape: release is the slim tip"); const absent = createInstallRefFixture({ releaseBranch: false }); t.after(absent.cleanup); assert.equal(absent.refs.heads.release, undefined); assert.equal(absent.snapshotText().includes("refs/heads/release"), false); assert.equal(absent.lsRemote().includes("refs/heads/release"), false); const verify = spawnSync("git", ["--git-dir", absent.dir, "rev-parse", "--verify", "-q", "refs/heads/release"], { encoding: "utf8" }); assert.notEqual(verify.status, 0, "git itself agrees the branch is absent"); }); test("fixture: the release branch can pin a stale or legacy tip", (t) => { // A stale moving release: still on the older version's synthetic commit. const stale = createInstallRefFixture({ releaseVersion: "2.0.0" }); t.after(stale.cleanup); assert.equal(stale.refs.heads.release, stale.refs.tags["install-v2.0.0"]); assert.notEqual(stale.refs.heads.release, stale.refs.tags["install-v3.1.2"]); // No synthetic refs at all: a legacy full-source release branch on main's tip. const legacy = createInstallRefFixture({ releases: [{ version: "1.0.0" }, { version: "2.0.0" }] }); t.after(legacy.cleanup); assert.equal(legacy.refs.heads.release, legacy.refs.heads.main); assert.throws(() => createInstallRefFixture({ releaseVersion: "9.9.9" }), /not one of the releases/); }); test("fixture: the derived snapshot matches real git ls-remote output byte for byte", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); assert.equal(fx.snapshotText(), fx.lsRemote(), "--tags --heads"); assert.equal(fx.snapshotText({ heads: false }), fx.lsRemote({ heads: false }), "tags only"); assert.equal(fx.snapshotText({ tags: false }), fx.lsRemote({ tags: false }), "heads only"); // Every line is a sha, a tab and a ref: the shape the installer parses. for (const line of fx.snapshotText().trimEnd().split("\n")) { assert.match(line, /^[0-9a-f]{40}\trefs\/(heads|tags)\/[^\s]+(\^\{\})?$/, line); } }); test("fixture: the shipped parser reads the snapshot as a consumer would", (t) => { const fx = createInstallRefFixture(); t.after(fx.cleanup); // update-check.mjs's latestTag is the plain-release compatibility API the // console notice uses; the snapshot must give it the newest plain release. assert.equal(latestTag(fx.snapshotText()), "3.1.2"); assert.equal(latestTag(fx.snapshotText({ heads: false })), "3.1.2"); // install-v* and nightly tags never count as plain releases. assert.ok(!fx.snapshotText().split("\n").some((l) => /refs\/tags\/v\d+\.\d+\.\d+$/.test(l) && l.includes("install-"))); }); test("fixture: two fixtures are independent and a non-release tag is there to be ignored", (t) => { const a = createInstallRefFixture(); const b = createInstallRefFixture(); t.after(a.cleanup); t.after(b.cleanup); assert.notEqual(a.dir, b.dir); assert.ok(fs.existsSync(a.dir) && fs.existsSync(b.dir)); // The default extra tag rides on main's tip and is not a release shape. assert.match(a.refs.tags.nightly, SHA); assert.equal(gitIn(a.dir, ["rev-parse", "refs/tags/nightly"]), a.refs.heads.main); assert.equal(latestTag(a.snapshotText().replace(/^.*refs\/tags\/v3\.1\.2.*$\n?/gm, "")), "2.0.0", "nightly never interferes"); }); test("fixture: cleanup removes everything and is reliable when repeated", (t) => { const fx = createInstallRefFixture(); assert.ok(fs.existsSync(fx.dir)); fx.cleanup(); assert.equal(fs.existsSync(fx.root), false, "the temp root and every object in it are gone"); fx.cleanup(); fx.cleanup(); assert.equal(fs.existsSync(fx.root), false, "repeated cleanup neither throws nor resurrects"); // The temp dir is not littered when several fixtures cycle. Each fixture's own root is // checked, never a count of the whole temp dir: other test files build fixtures alongside. const ones = [createInstallRefFixture(), createInstallRefFixture(), createInstallRefFixture()]; for (const one of ones) one.cleanup(); for (const one of ones) assert.equal(fs.existsSync(one.root), false, one.root); t.after(() => {}); // nothing to sweep: cleanup already ran, and idempotently so }); test("fixture: default releases cover the rollback matrix the spec asks for", () => { // Documentation-by-test for later phases: what createInstallRefFixture() // builds with no options. const matrix = DEFAULT_RELEASES.map((r) => ({ version: r.version, annotated: !!r.annotated, installTag: !!r.installTag })); assert.deepEqual(matrix, [ { version: "1.0.0", annotated: false, installTag: false }, { version: "2.0.0", annotated: false, installTag: true }, { version: "3.1.2", annotated: true, installTag: true }, ]); });