Files
fleet/scripts/ci/git-package-currency.test.mjs
Cato 00215923f3 fix(ci): reduce shared GitHub API pressure (#282)
* fix(ci): isolate auto-merge API quota

* fix(ci): preserve drift audit read diagnostics

* fix(ci): cut package census API fanout

* fix(ci): retain organization audit read errors
2026-10-27 11:16:21 +00:00

176 lines
4.7 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
registeredGitDependency,
planGitUpdate,
planRegistryMigration,
latestVerifiedGitRelease,
} from "./git-package-currency.mjs";
const sha = "a".repeat(40),
old = "b".repeat(40);
const registry = {
packages: [
{
name: "@bitbaum/accountkit",
repo: "https://github.com/bitbaum/accountkit",
},
],
};
test("immutable registered Git dependencies are recognized across supported spellings", () => {
for (const spec of [
`github:bitbaum/accountkit#v0.2.1`,
`git+https://github.com/bitbaum/accountkit.git#${old}`,
])
assert.equal(
registeredGitDependency(spec, registry).repo,
"bitbaum/accountkit",
);
for (const spec of [
"github:someone/accountkit#v1.0.0",
"github:bitbaum/accountkit#main",
"workspace:*",
"^0.5.0",
])
assert.equal(registeredGitDependency(spec, registry), null);
});
test("Git updates preserve the dependency alias and pin immutable commits without downgrades", () => {
const target = {
name: "@bitbaum/accountkit",
sha,
version: "0.6.0",
currentVersion: "0.2.1",
};
const update = planGitUpdate(
"app",
"frontend/package.json",
"account-alias",
"github:bitbaum/accountkit#v0.2.1",
target,
);
assert.equal(update.name, "account-alias");
assert.equal(update.to, `github:bitbaum/accountkit#${sha}`);
for (const currentVersion of ["0.6.0", "0.7.0", undefined])
assert.equal(
planGitUpdate(
"app",
"package.json",
"x",
`github:bitbaum/accountkit#${old}`,
{ ...target, currentVersion },
),
null,
);
});
test("only matching stable versions with successful exact-commit CI are eligible", () => {
const responses =
(conclusion, declaredVersion = "0.6.0") =>
(path) => {
if (path.includes("matching-refs"))
return [
{ ref: "refs/tags/v0.6.0" },
{ ref: "refs/tags/v0.7.0-beta.1" },
];
if (path.includes("/commits/")) return { sha };
if (path.includes("contents"))
return {
content: Buffer.from(
JSON.stringify({ version: declaredVersion }),
).toString("base64"),
};
return {
workflow_runs: [
{ path: ".github/workflows/ci.yml", event: "push", conclusion },
],
};
};
assert.equal(
latestVerifiedGitRelease("bitbaum/accountkit", responses("success")).sha,
sha,
);
for (const state of [null, "failure", "cancelled"])
assert.equal(
latestVerifiedGitRelease("bitbaum/accountkit", responses(state)),
null,
);
assert.equal(
latestVerifiedGitRelease(
"bitbaum/accountkit",
responses("success", "0.5.2"),
),
null,
);
});
test("production Git release discovery uses remote tags and raw source, not three REST reads", () => {
const object = "c".repeat(40), commit = "d".repeat(40), latest = "e".repeat(40);
const gitCalls = [];
const run = (_command, args) => {
gitCalls.push(args);
return `${object}\trefs/tags/v0.6.0\n${commit}\trefs/tags/v0.6.0^{}\n${latest}\trefs/tags/v0.7.0\n`;
};
const sourceReads = [];
const result = latestVerifiedGitRelease(
"bitbaum/accountkit",
undefined,
run,
(_repo, ref) => { sourceReads.push(ref); return "0.7.0"; },
(path) => {
assert.equal(path, `repos/bitbaum/accountkit/actions/runs?head_sha=${latest}&per_page=100`);
return { workflow_runs: [{ path: ".github/workflows/ci.yml", event: "push", conclusion: "success" }] };
},
);
assert.deepEqual(gitCalls[0].slice(0, 2), ["ls-remote", "--tags"]);
assert.deepEqual(sourceReads, [latest]);
assert.deepEqual(result, { tag: "v0.7.0", version: "0.7.0", sha: latest });
});
test("published packages migrate from Git without changing aliases or downgrading", () => {
const identity = { name: "@bitbaum/chatkit", npm: true };
assert.equal(
planRegistryMigration(
"app",
"package.json",
"chat",
"github:bitbaum/chatkit#v0.1.0",
identity,
"0.1.0",
"0.15.2",
).to,
"npm:@bitbaum/chatkit@^0.15.2",
);
assert.equal(
planRegistryMigration(
"app",
"package.json",
identity.name,
"git",
identity,
"0.15.2",
"0.15.2",
).to,
"^0.15.2",
);
assert.equal(
planRegistryMigration(
"app",
"package.json",
identity.name,
"git",
identity,
"0.16.0",
"0.15.2",
),
null,
);
assert.equal(
planRegistryMigration(
"app",
"package.json",
"listkit",
"git",
{ name: "listkit", npm: false },
"0.1.0",
"1.0.0",
),
null,
);
});