|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { compareReleaseTags } from "../scripts/release-notes"; |
| 5 | + |
| 6 | +const repoRoot = fileURLToPath(new URL("../", import.meta.url)); |
| 7 | + |
| 8 | +/** |
| 9 | + * The in-tree version must never sit BEHIND a version this repository has already |
| 10 | + * released. |
| 11 | + * |
| 12 | + * This is not hypothetical. dev accumulated 254 commits without ever touching |
| 13 | + * package.json, so its version string (2.32.1-preview.20260825) fell behind the |
| 14 | + * published latest dist-tag (2.33.0) once v2.33.0 was cut from main. One stale line, |
| 15 | + * two distinct failure modes: |
| 16 | + * |
| 17 | + * - assertChannelVersionMovesForward in scripts/release.ts refuses the string as a |
| 18 | + * channel regression, so a release cut from that tree cannot publish at all. |
| 19 | + * - merging dev into main resolves package.json to main's side, producing a tree that |
| 20 | + * claims to be the ALREADY-PUBLISHED 2.33.0 - a silent duplicate instead of a loud |
| 21 | + * failure. |
| 22 | + * |
| 23 | + * Commit 32529c2b2 repaired precisely this by hand once, and nothing has enforced it |
| 24 | + * since. The assertion reads the local tag set rather than the npm registry, so it needs |
| 25 | + * no network and no edit at each release. |
| 26 | + * |
| 27 | + * compareReleaseTags comes from scripts/release-notes and not from scripts/release: the |
| 28 | + * latter parses process.argv and calls process.exit at module scope, so importing it from |
| 29 | + * a test kills the runner. release-notes guards its CLI behind import.meta.main. |
| 30 | + */ |
| 31 | + |
| 32 | +/** Release tags shaped vX.Y.Z[-pre]. Non-release tags are ignored, not an error. */ |
| 33 | +const RELEASE_TAG = /^v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/; |
| 34 | + |
| 35 | +function localReleaseTags(): string[] { |
| 36 | + const result = Bun.spawnSync(["git", "tag", "--list", "v*"], { cwd: repoRoot }); |
| 37 | + // A tarball install, a shallow CI checkout, or a missing git binary all land here. |
| 38 | + // Absent tags cannot prove a regression, so the check reports nothing rather than |
| 39 | + // inventing a failure. |
| 40 | + if (result.exitCode !== 0) return []; |
| 41 | + return new TextDecoder() |
| 42 | + .decode(result.stdout) |
| 43 | + .split("\n") |
| 44 | + .map((line) => line.trim()) |
| 45 | + .filter((line) => RELEASE_TAG.test(line)); |
| 46 | +} |
| 47 | + |
| 48 | +function inTreeVersion(): string { |
| 49 | + const raw = readFileSync(new URL("../package.json", import.meta.url), "utf8"); |
| 50 | + const version = (JSON.parse(raw) as { version?: unknown }).version; |
| 51 | + if (typeof version !== "string") throw new Error("package.json has no string version"); |
| 52 | + return version; |
| 53 | +} |
| 54 | + |
| 55 | +/** Newest tag by SemVer precedence, or null when the set is empty. */ |
| 56 | +function highestReleaseTag(tags: string[]): string | null { |
| 57 | + if (tags.length === 0) return null; |
| 58 | + const sorted = [...tags].sort(compareReleaseTags); |
| 59 | + return sorted[sorted.length - 1] ?? null; |
| 60 | +} |
| 61 | + |
| 62 | +describe("release version line", () => { |
| 63 | + test("package.json carries a parseable SemVer version", () => { |
| 64 | + expect(inTreeVersion()).toMatch(/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/); |
| 65 | + }); |
| 66 | + |
| 67 | + test("the in-tree version is strictly ahead of every tagged release", () => { |
| 68 | + const tags = localReleaseTags(); |
| 69 | + if (tags.length === 0) { |
| 70 | + // Shallow checkout: actions/checkout fetches no tags by default. Reporting nothing |
| 71 | + // keeps this honest instead of asserting against an empty set that always passes. |
| 72 | + expect(tags).toEqual([]); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const version = inTreeVersion(); |
| 77 | + const highest = highestReleaseTag(tags); |
| 78 | + expect(highest).not.toBeNull(); |
| 79 | + |
| 80 | + const ordering = compareReleaseTags("v" + version, highest!); |
| 81 | + expect( |
| 82 | + ordering, |
| 83 | + "package.json version " + version + " is not ahead of the highest release tag " + |
| 84 | + highest + |
| 85 | + ". A release cut from this tree is either rejected as a channel regression or " + |
| 86 | + "silently republishes an already-published version. Bump package.json.", |
| 87 | + ).toBeGreaterThan(0); |
| 88 | + }); |
| 89 | + |
| 90 | + test("a version behind the tag set is detected rather than tolerated", () => { |
| 91 | + // Proves the comparison above is not vacuous: the same helper, given the exact string |
| 92 | + // dev actually carried, must order it BEHIND the release that stranded it. |
| 93 | + expect(compareReleaseTags("v2.32.1-preview.20260825", "v2.33.0")).toBeLessThan(0); |
| 94 | + expect(compareReleaseTags("v2.33.0", "v2.33.0")).toBe(0); |
| 95 | + expect(compareReleaseTags("v2.34.0", "v2.33.0")).toBeGreaterThan(0); |
| 96 | + }); |
| 97 | +}); |