import { describe, expect, it } from "vite-plus/test";
import {
compareSemverVersions,
normalizeSemverVersion,
parseSemver,
satisfiesSemverRange,
} from "./semver.ts";
describe("semver helpers", () => {
it("matches supported range groups", () => {
const range = "^22.16 || ^23.11 || >=24.10";
expect(satisfiesSemverRange("22.16.0", range)).toBe(true);
expect(satisfiesSemverRange("23.11.1", range)).toBe(true);
expect(satisfiesSemverRange("24.10.0", range)).toBe(true);
expect(satisfiesSemverRange("22.15.9", range)).toBe(false);
expect(satisfiesSemverRange("23.10.9", range)).toBe(false);
expect(satisfiesSemverRange("24.9.9", range)).toBe(false);
});
it("normalizes and parses shorthand major-only versions", () => {
expect(normalizeSemverVersion("20")).toBe("20.0.0");
expect(normalizeSemverVersion("v18")).toBe("v18.0.0");
expect(normalizeSemverVersion("20-rc.1")).toBe("20.0.0-rc.1");
expect(parseSemver("20")).toEqual({ major: 20, minor: 0, patch: 0, prerelease: [] });
});
it("compares shorthand versions numerically instead of lexically", () => {
expect(compareSemverVersions("20", "9")).toBeGreaterThan(0);
expect(compareSemverVersions("18", "18.0.0")).toBe(0);
});
it("compares prerelease versions before stable versions", () => {
expect(compareSemverVersions("2.1.111-beta.1", "2.1.111")).toBeLessThan(0);
});
it("preserves hyphens within prerelease identifiers", () => {
expect(normalizeSemverVersion("20-alpha-1")).toBe("20.0.0-alpha-1");
expect(parseSemver("1.2.3-alpha-1.2")).toEqual({
major: 1,
minor: 2,
patch: 3,
prerelease: ["alpha-1", "2"],
});
expect(parseSemver("1.2.3-x-y-z.--")?.prerelease).toEqual(["x-y-z", "--"]);
});
it("distinguishes releases whose identifiers differ after a hyphen", () => {
expect(compareSemverVersions("1.2.3-alpha-1", "1.2.3-alpha-2")).toBeLessThan(0);
expect(compareSemverVersions("1.2.3-alpha-2", "1.2.3-alpha-1")).toBeGreaterThan(0);
expect(compareSemverVersions("1.2.3-alpha-1.2", "1.2.3-alpha-1.10")).toBeLessThan(0);
expect(compareSemverVersions("1.2.3-alpha.1+build-2", "1.2.3-alpha.1+build-3")).toBe(0);
});
it("supports comparison comparators", () => {
expect(satisfiesSemverRange("24.9.0", ">=24.0 <24.10")).toBe(true);
expect(satisfiesSemverRange("24.10.0", ">=24.0 <24.10")).toBe(false);
for (const gap of [" ", "\t", "\n"]) {
const range = `>=${gap}24.0 <${gap}24.10`;
expect(satisfiesSemverRange("24.9.0", range)).toBe(true);
expect(satisfiesSemverRange("24.10.0", range)).toBe(false);
expect(satisfiesSemverRange("22.16.0", `^${gap}22.16 || >=${gap}24.10`)).toBe(true);
}
});
it("honors caret range upper bounds for zero-major versions", () => {
expect(satisfiesSemverRange("0.5.0", "^0")).toBe(true);
expect(satisfiesSemverRange("0.0.4", "^0.0")).toBe(true);
expect(satisfiesSemverRange("1.0.0", "^0")).toBe(false);
expect(satisfiesSemverRange("0.1.0", "^0.0")).toBe(false);
expect(satisfiesSemverRange("0.2.3", "^0.2.3")).toBe(true);
expect(satisfiesSemverRange("0.2.9", "^0.2.3")).toBe(true);
expect(satisfiesSemverRange("0.3.0", "^0.2.3")).toBe(false);
expect(satisfiesSemverRange("0.5.0", "^0.2.3")).toBe(false);
expect(satisfiesSemverRange("0.0.3", "^0.0.3")).toBe(true);
expect(satisfiesSemverRange("0.0.4", "^0.0.3")).toBe(false);
expect(satisfiesSemverRange("0.0.0", "^0.0.0")).toBe(true);
expect(satisfiesSemverRange("0.0.1", "^0.0.0")).toBe(false);
});
it("keeps the range checker stringifiable and executable as plain JavaScript", () => {
const source = satisfiesSemverRange.toString();
const recreated = Function(`return (${source});`)() as typeof satisfiesSemverRange;
expect(source).toContain("function satisfiesSemverRange");
expect(source).not.toContain(": string");
expect(source).not.toContain(": boolean");
expect(recreated("0.5.0", "^0")).toBe(true);
expect(recreated("0.0.4", "^0.0")).toBe(true);
expect(recreated("24.10.0", ">=24.10")).toBe(true);
expect(recreated("24.9.9", ">=24.10")).toBe(false);
});
});