diff --git a/src/updater.semver.test.ts b/src/updater.semver.test.ts new file mode 100644 index 00000000..e0fd8fb4 --- /dev/null +++ b/src/updater.semver.test.ts @@ -0,0 +1,73 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { compareSemver } from "./updater.js"; +import { VERSION } from "./version.js"; + +describe("compareSemver", () => { + it("orders numeric releases", () => { + expect(compareSemver("0.12.315", "0.12.314")).toBe(1); + expect(compareSemver("0.12.314", "0.12.315")).toBe(-1); + expect(compareSemver("1.0.0", "0.99.99")).toBe(1); + expect(compareSemver("0.12.315", "0.12.315")).toBe(0); + }); + + it("compares a prerelease by its numeric core", () => { + expect(compareSemver("0.12.316-rc.1", "0.12.315")).toBe(1); + expect(compareSemver("1.2.0-beta.3", "1.1.9")).toBe(1); + expect(compareSemver("0.12.316+build.7", "0.12.315")).toBe(1); + expect(compareSemver("0.12.315-rc.1", "0.12.315")).toBe(0); + }); + + it("treats missing segments as zero", () => { + expect(compareSemver("1.2", "1.2.0")).toBe(0); + }); +}); + +/** + * The startup update hint compares the npm `latest` version with the installed + * one. A version carrying a prerelease suffix (for example 0.12.316-rc.1) used + * to be parsed as patch 0, so a newer prerelease looked older than the installed + * stable release and no hint was printed. + */ +describe("checkForUpdates version comparison", () => { + afterEach(() => { + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + }); + + async function runWithLatest(latest: string): Promise { + const lines: string[] = []; + vi.spyOn(console, "log").mockImplementation((...args: unknown[]) => { + lines.push(args.map(String).join(" ")); + }); + vi.stubGlobal( + "fetch", + vi.fn().mockResolvedValue({ + ok: true, + json: async () => ({ version: latest }), + }), + ); + const { checkForUpdates } = await import("./updater.js"); + await checkForUpdates(); + return lines; + } + + it("prints a hint for a newer stable release", async () => { + const [major, minor, patch] = (VERSION.split(/[+-]/, 1)[0] ?? "").split(".").map(Number); + const newer = `${major}.${minor}.${patch + 1}`; + const lines = await runWithLatest(newer); + expect(lines.some((line) => line.includes(`${newer} available`))).toBe(true); + }); + + it("prints a hint for a newer prerelease instead of treating its patch as zero", async () => { + const [major, minor, patch] = (VERSION.split(/[+-]/, 1)[0] ?? "").split(".").map(Number); + const newerPrerelease = `${major}.${minor}.${patch + 1}-rc.1`; + const lines = await runWithLatest(newerPrerelease); + expect(lines.some((line) => line.includes(`${newerPrerelease} available`))).toBe(true); + }); + + it("prints no hint for the installed version", async () => { + const lines = await runWithLatest(VERSION); + expect(lines.some((line) => line.includes("available"))).toBe(false); + }); +}); diff --git a/src/updater.ts b/src/updater.ts index f056e516..09df8e7c 100644 --- a/src/updater.ts +++ b/src/updater.ts @@ -8,18 +8,32 @@ import { VERSION } from "./version.js"; const NPM_REGISTRY = "https://registry.npmjs.org/@blockrun/clawrouter/latest"; const CHECK_TIMEOUT_MS = 5_000; // Don't block startup for more than 5s +/** Parse the numeric release core of a version, ignoring prerelease/build suffixes. */ +function versionCore(version: string): number[] { + const core = version.split(/[+-]/, 1)[0] ?? ""; + return core.split(".").map((part) => { + const value = Number(part); + return Number.isFinite(value) ? value : 0; + }); +} + /** * Compare semver versions. Returns: * 1 if a > b * 0 if a === b * -1 if a < b + * + * Only the numeric core is compared. Passing a prerelease such as + * `0.12.316-rc.1` through `Number()` yields NaN, and the old `|| 0` fallback + * turned its patch into 0, so a newer prerelease looked older than the + * installed stable release and no update hint was printed. */ -function compareSemver(a: string, b: string): number { - const pa = a.split(".").map(Number); - const pb = b.split(".").map(Number); +export function compareSemver(a: string, b: string): number { + const pa = versionCore(a); + const pb = versionCore(b); for (let i = 0; i < 3; i++) { - if ((pa[i] || 0) > (pb[i] || 0)) return 1; - if ((pa[i] || 0) < (pb[i] || 0)) return -1; + if ((pa[i] ?? 0) > (pb[i] ?? 0)) return 1; + if ((pa[i] ?? 0) < (pb[i] ?? 0)) return -1; } return 0; }