Files
hose-use/scripts/release-validation.test.ts
T

121 lines
6.6 KiB
TypeScript

import { afterEach, describe, expect, test } from "bun:test";
import { execFileSync, spawnSync } from "node:child_process";
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { parse } from "yaml";
import { bumpVersion } from "./bump-version.js";
import { verifyReleaseTag } from "./verify-release.js";
const roots: string[] = [];
afterEach(() => { for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); });
function repository() {
const root = mkdtempSync(join(tmpdir(), "hose-release-test-"));
roots.push(root);
const git = (...args: string[]) => execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
git("init", "--initial-branch=main");
git("config", "user.name", "Release fixture");
git("config", "user.email", "fixture@example.test");
git("config", "core.hooksPath", "/dev/null");
git("config", "commit.gpgsign", "false");
git("config", "tag.gpgsign", "false");
for (const dir of ["skills/hose-use", "specs/openapi", ".zcode-plugin"]) mkdirSync(join(root, dir), { recursive: true });
writeFileSync(join(root, "package.json"), '{"version":"1.0.0"}\n');
writeFileSync(join(root, "README.md"), "# hose-use (v1.0.0)\n");
writeFileSync(join(root, "skills/hose-use/SKILL.md"), "version: 1.0.0\n# hose-use (v1.0.0)\n");
writeFileSync(join(root, "specs/openapi/openapi.yaml"), "info:\n version: 1.0.0\n");
writeFileSync(join(root, ".zcode-plugin/plugin.json"), '{"version":"1.0.0"}\n');
git("add", ".");
git("commit", "-m", "fixture");
git("update-ref", "refs/remotes/origin/main", "HEAD");
return { root, git, version: () => JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version };
}
describe("release preparation and publication guards", () => {
test("bump rejects wrong branches, dirty trees and invalid/non-increasing versions before writes", () => {
const f = repository();
expect(() => bumpVersion(f.root, "patch", () => {})).toThrow("release-v1.0.1");
f.git("switch", "-c", "codex/release-v1.0.1");
writeFileSync(join(f.root, "untracked.txt"), "work in progress");
expect(() => bumpVersion(f.root, "patch", () => {})).toThrow("clean working tree");
rmSync(join(f.root, "untracked.txt"));
for (const version of ["1.0.0", "0.9.9", "1.0.01", "1.0.1-rc.01", "invalid"]) expect(() => bumpVersion(f.root, version, () => {})).toThrow();
expect(f.version()).toBe("1.0.0");
expect(f.git("status", "--porcelain")).toBe("");
});
test("bump updates sources on its release branch and explicitly invokes regeneration", () => {
const f = repository();
f.git("switch", "-c", "release/v1.0.1");
let rebuilt = false;
expect(bumpVersion(f.root, "patch", () => { rebuilt = true; })).toBe("1.0.1");
expect(rebuilt).toBe(true);
expect(f.version()).toBe("1.0.1");
expect(readFileSync(join(f.root, "skills/hose-use/SKILL.md"), "utf8")).toContain("version: 1.0.1");
expect(parse(readFileSync(join(f.root, "specs/openapi/openapi.yaml"), "utf8")).info.version).toBe("1.0.1");
});
test("an existing release tag cannot be reused during bump", () => {
const f = repository();
f.git("switch", "-c", "release/v1.0.1");
f.git("tag", "v1.0.1");
expect(() => bumpVersion(f.root, "patch", () => {})).toThrow("already exists");
expect(f.version()).toBe("1.0.0");
});
test("publication validates the actual HEAD, main ancestry and package version", () => {
const f = repository();
f.git("tag", "v1.0.0");
expect(() => verifyReleaseTag(f.root, "v1.0.0")).not.toThrow();
expect(() => verifyReleaseTag(f.root, "main")).toThrow("Invalid release tag");
f.git("tag", "v9.9.9");
expect(() => verifyReleaseTag(f.root, "v9.9.9")).toThrow("does not match");
f.git("switch", "-c", "unmerged");
writeFileSync(join(f.root, "change"), "unreviewed");
f.git("add", "."); f.git("commit", "-m", "unmerged fixture");
expect(() => verifyReleaseTag(f.root, "v1.0.0")).toThrow("exact tagged commit");
f.git("tag", "v1.0.1");
expect(() => verifyReleaseTag(f.root, "v1.0.1")).toThrow("ancestor");
});
test("workflows validate committed versions before generation and ship the checksum manifest", () => {
const root = join(import.meta.dir, "..");
const ci = parse(readFileSync(join(root, ".github/workflows/ci.yml"), "utf8"));
const release = parse(readFileSync(join(root, ".github/workflows/release.yml"), "utf8"));
expect(ci.on.pull_request.branches).toContain("main");
expect(ci.permissions.contents).toBe("read");
const steps = release.jobs["build-and-publish"].steps;
const runs = steps.map((s: { run?: string }) => s.run || "");
expect(runs.findIndex((s: string) => s.includes("check:version"))).toBeLessThan(runs.findIndex((s: string) => s.includes("openapi:all")));
expect(steps[0].with.ref).toContain("inputs.tag");
expect(runs.some((s: string) => s.includes("verify-release.ts"))).toBe(true);
expect(runs.some((s: string) => s.includes("sha256sum hose-use-* > checksums.txt"))).toBe(true);
for (const step of [...ci.jobs.validate.steps, ...steps]) if (step.uses) expect(step.uses).toMatch(/@[a-f0-9]{40}$/);
expect(JSON.parse(readFileSync(join(root, ".mcp.json"), "utf8")).mcpServers["hose-use"].command).toBe("bun");
});
test("a failed background Gitea upload makes the Release step fail", () => {
const f = repository();
const workflow = parse(readFileSync(join(import.meta.dir, "../.github/workflows/release.yml"), "utf8"));
const step = workflow.jobs["build-and-publish"].steps.find((s: { name?: string }) => s.name === "Publish Gitea Release");
mkdirSync(join(f.root, "tools")); mkdirSync(join(f.root, "dist/bin"), { recursive: true });
writeFileSync(join(f.root, "dist/bin/hose-use-linux-x64"), "fixture");
writeFileSync(join(f.root, "dist/bin/checksums.txt"), "fixture checksums");
writeFileSync(join(f.root, "tools/curl"), `#!/bin/sh
for arg in "$@"; do
case "$arg" in
*/assets?*) exit 22 ;;
*/releases) printf '{"id":42}'; exit 0 ;;
*/releases/42) printf '{"assets":[]}'; exit 0 ;;
esac
done
exit 22
`, { mode: 0o755 });
const result = spawnSync("bash", ["-eo", "pipefail", "-c", step.run], { cwd: f.root, env: { ...process.env, PATH: `${join(f.root, "tools")}:${process.env.PATH}`, GITEA_TOKEN: "fixture", GITEA_HOST: "https://example.test", GITEA_REPO: "test/repo", RELEASE_TAG: "v1.0.0" }, encoding: "utf8", timeout: 5000 });
expect(result.status).toBe(1);
expect(result.stdout).toContain("assets failed to upload");
expect(result.stdout).not.toContain("Successfully published");
});
});