The best way to control Claude Code, Codex and more from any computer or phone

main
ts 99 lines 4.36 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import {
4 compareSemverVersions,
5 normalizeSemverVersion,
6 parseSemver,
7 satisfiesSemverRange,
8 } from "./semver.ts";
9
10 describe("semver helpers", () => {
11 it("matches supported range groups", () => {
12 const range = "^22.16 || ^23.11 || >=24.10";
13
14 expect(satisfiesSemverRange("22.16.0", range)).toBe(true);
15 expect(satisfiesSemverRange("23.11.1", range)).toBe(true);
16 expect(satisfiesSemverRange("24.10.0", range)).toBe(true);
17 expect(satisfiesSemverRange("22.15.9", range)).toBe(false);
18 expect(satisfiesSemverRange("23.10.9", range)).toBe(false);
19 expect(satisfiesSemverRange("24.9.9", range)).toBe(false);
20 });
21
22 it("normalizes and parses shorthand major-only versions", () => {
23 expect(normalizeSemverVersion("20")).toBe("20.0.0");
24 expect(normalizeSemverVersion("v18")).toBe("v18.0.0");
25 expect(normalizeSemverVersion("20-rc.1")).toBe("20.0.0-rc.1");
26 expect(parseSemver("20")).toEqual({ major: 20, minor: 0, patch: 0, prerelease: [] });
27 });
28
29 it("compares shorthand versions numerically instead of lexically", () => {
30 expect(compareSemverVersions("20", "9")).toBeGreaterThan(0);
31 expect(compareSemverVersions("18", "18.0.0")).toBe(0);
32 });
33
34 it("compares prerelease versions before stable versions", () => {
35 expect(compareSemverVersions("1.0.0-Z", "1.0.0-a")).toBeLessThan(0);
36 expect(compareSemverVersions("1.0.0-alpha-1", "1.0.0-alpha1")).toBeLessThan(0);
37 expect(compareSemverVersions("1.0.0-9007199254740992", "1.0.0-9007199254740993")).toBeLessThan(
38 0,
39 );
40 expect(compareSemverVersions("2.1.111-beta.1", "2.1.111")).toBeLessThan(0);
41 });
42
43 it("preserves hyphens within prerelease identifiers", () => {
44 expect(normalizeSemverVersion("20-alpha-1")).toBe("20.0.0-alpha-1");
45 expect(parseSemver("1.2.3-alpha-1.2")).toEqual({
46 major: 1,
47 minor: 2,
48 patch: 3,
49 prerelease: ["alpha-1", "2"],
50 });
51 expect(parseSemver("1.2.3-x-y-z.--")?.prerelease).toEqual(["x-y-z", "--"]);
52 });
53
54 it("distinguishes releases whose identifiers differ after a hyphen", () => {
55 expect(compareSemverVersions("1.2.3-alpha-1", "1.2.3-alpha-2")).toBeLessThan(0);
56 expect(compareSemverVersions("1.2.3-alpha-2", "1.2.3-alpha-1")).toBeGreaterThan(0);
57 expect(compareSemverVersions("1.2.3-alpha-1.2", "1.2.3-alpha-1.10")).toBeLessThan(0);
58 expect(compareSemverVersions("1.2.3-alpha.1+build-2", "1.2.3-alpha.1+build-3")).toBe(0);
59 });
60
61 it("supports comparison comparators", () => {
62 expect(satisfiesSemverRange("24.9.0", ">=24.0 <24.10")).toBe(true);
63 expect(satisfiesSemverRange("24.10.0", ">=24.0 <24.10")).toBe(false);
64 for (const gap of [" ", "\t", "\n"]) {
65 const range = `>=${gap}24.0 <${gap}24.10`;
66 expect(satisfiesSemverRange("24.9.0", range)).toBe(true);
67 expect(satisfiesSemverRange("24.10.0", range)).toBe(false);
68 expect(satisfiesSemverRange("22.16.0", `^${gap}22.16 || >=${gap}24.10`)).toBe(true);
69 }
70 });
71
72 it("honors caret range upper bounds for zero-major versions", () => {
73 expect(satisfiesSemverRange("0.5.0", "^0")).toBe(true);
74 expect(satisfiesSemverRange("0.0.4", "^0.0")).toBe(true);
75 expect(satisfiesSemverRange("1.0.0", "^0")).toBe(false);
76 expect(satisfiesSemverRange("0.1.0", "^0.0")).toBe(false);
77 expect(satisfiesSemverRange("0.2.3", "^0.2.3")).toBe(true);
78 expect(satisfiesSemverRange("0.2.9", "^0.2.3")).toBe(true);
79 expect(satisfiesSemverRange("0.3.0", "^0.2.3")).toBe(false);
80 expect(satisfiesSemverRange("0.5.0", "^0.2.3")).toBe(false);
81 expect(satisfiesSemverRange("0.0.3", "^0.0.3")).toBe(true);
82 expect(satisfiesSemverRange("0.0.4", "^0.0.3")).toBe(false);
83 expect(satisfiesSemverRange("0.0.0", "^0.0.0")).toBe(true);
84 expect(satisfiesSemverRange("0.0.1", "^0.0.0")).toBe(false);
85 });
86
87 it("keeps the range checker stringifiable and executable as plain JavaScript", () => {
88 const source = satisfiesSemverRange.toString();
89 const recreated = Function(`return (${source});`)() as typeof satisfiesSemverRange;
90
91 expect(source).toContain("function satisfiesSemverRange");
92 expect(source).not.toContain(": string");
93 expect(source).not.toContain(": boolean");
94 expect(recreated("0.5.0", "^0")).toBe(true);
95 expect(recreated("0.0.4", "^0.0")).toBe(true);
96 expect(recreated("24.10.0", ">=24.10")).toBe(true);
97 expect(recreated("24.9.9", ">=24.10")).toBe(false);
98 });
99 });