|
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | +import plugin, { semverMax, semverMin, semverOurs, semverTheirs } from "./index"; |
| 3 | +import { StrategyStatus } from "git-json-resolver"; |
| 4 | + |
| 5 | +const run = (fn: any, ours: any, theirs: any) => fn({ ours, theirs, base: undefined, path: [] }); |
| 6 | + |
| 7 | +const reset = (overrides: any = {}) => { |
| 8 | + // @ts-ignore -- ts gone mad |
| 9 | + plugin.init({ |
| 10 | + strict: true, |
| 11 | + normalize: true, |
| 12 | + fallback: "continue", |
| 13 | + preferValid: true, |
| 14 | + preferRange: false, |
| 15 | + workspacePattern: "", |
| 16 | + ...overrides, |
| 17 | + }); |
| 18 | +}; |
| 19 | +describe("git-json-resolver-semver", () => { |
| 20 | + beforeEach(() => reset()); |
| 21 | + |
| 22 | + describe("semver-max", () => { |
| 23 | + it("picks higher when both valid", () => { |
| 24 | + const r = run(semverMax, "1.2.3", "1.3.0"); |
| 25 | + expect(r.status).toBe(StrategyStatus.OK); |
| 26 | + expect(r.value).toBe("1.3.0"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("prefers valid when only one is valid", () => { |
| 30 | + const r = run(semverMax, "1.2.3", "banana"); |
| 31 | + expect(r.status).toBe(StrategyStatus.OK); |
| 32 | + expect(r.value).toBe("1.2.3"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("respects fallback=continue when both invalid", async () => { |
| 36 | + await reset({ preferValid: false, fallback: "continue" }); |
| 37 | + const r = run(semverMax, "foo", "bar"); |
| 38 | + expect(r.status).toBe(StrategyStatus.CONTINUE); |
| 39 | + }); |
| 40 | + |
| 41 | + it("respects fallback=ours/theirs/error", async () => { |
| 42 | + await reset({ preferValid: false, fallback: "ours" }); |
| 43 | + let r = run(semverMax, "foo", "bar"); |
| 44 | + expect(r.status).toBe(StrategyStatus.OK); |
| 45 | + expect(r.value).toBe("foo"); |
| 46 | + |
| 47 | + await reset({ preferValid: false, fallback: "theirs" }); |
| 48 | + r = run(semverMax, "foo", "bar"); |
| 49 | + expect(r.status).toBe(StrategyStatus.OK); |
| 50 | + expect(r.value).toBe("bar"); |
| 51 | + |
| 52 | + await reset({ preferValid: false, fallback: "error" }); |
| 53 | + r = run(semverMax, "foo", "bar"); |
| 54 | + expect(r.status).toBe(StrategyStatus.FAIL); |
| 55 | + }); |
| 56 | + |
| 57 | + it("strict=false allows prereleases/ranges", async () => { |
| 58 | + await reset({ strict: false }); |
| 59 | + const r1 = run(semverMax, "1.2.3", "1.2.3-beta.1"); |
| 60 | + expect(r1.status).toBe(StrategyStatus.OK); |
| 61 | + expect(r1.value).toBe("1.2.3"); // release > prerelease |
| 62 | + |
| 63 | + const r2 = run(semverMax, "^1.2.3", "1.3.0"); |
| 64 | + // validate("^1.2.3") is true in non-strict; compareVersions("^1.2.3", "1.3.0") |
| 65 | + // is not meaningful, so preferValid should push to 1.3.0 only when one side valid. |
| 66 | + // Here both are considered valid, so we trust compareVersions result. |
| 67 | + expect([StrategyStatus.OK, StrategyStatus.CONTINUE]).toContain(r2.status); |
| 68 | + }); |
| 69 | + |
| 70 | + it("strict=true rejects prereleases", async () => { |
| 71 | + await reset({ strict: true }); |
| 72 | + const r = run(semverMax, "1.2.3-beta.1", "1.2.3"); |
| 73 | + expect(r.status).toBe(StrategyStatus.OK); |
| 74 | + expect(r.value).toBe("1.2.3"); // only ours invalid → pick valid theirs via preferValid |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("semver-min", () => { |
| 79 | + it("picks lower when both valid", () => { |
| 80 | + const r = run(semverMin, "2.0.0", "2.1.0"); |
| 81 | + expect(r.status).toBe(StrategyStatus.OK); |
| 82 | + expect(r.value).toBe("2.0.0"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("prefers valid when only one is valid", () => { |
| 86 | + const r = run(semverMin, "banana", "2.1.0"); |
| 87 | + expect(r.status).toBe(StrategyStatus.OK); |
| 88 | + expect(r.value).toBe("2.1.0"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("strict=false handles prerelease ordering", async () => { |
| 92 | + await reset({ strict: false }); |
| 93 | + const r = run(semverMin, "1.2.3", "1.2.3-beta.1"); |
| 94 | + expect(r.status).toBe(StrategyStatus.OK); |
| 95 | + expect(r.value).toBe("1.2.3-beta.1"); // prerelease < release |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe("semver-ours / semver-theirs", () => { |
| 100 | + it("semver-ours returns ours when ours is valid", () => { |
| 101 | + const r = run(semverOurs, "1.0.0", "2.0.0"); |
| 102 | + expect(r.status).toBe(StrategyStatus.OK); |
| 103 | + expect(r.value).toBe("1.0.0"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("semver-ours falls back to valid theirs when ours invalid", () => { |
| 107 | + const r = run(semverOurs, "banana", "1.0.1"); |
| 108 | + expect(r.status).toBe(StrategyStatus.OK); |
| 109 | + expect(r.value).toBe("1.0.1"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("semver-theirs returns theirs when theirs is valid", () => { |
| 113 | + const r = run(semverTheirs, "1.2.3", "2.0.0"); |
| 114 | + expect(r.status).toBe(StrategyStatus.OK); |
| 115 | + expect(r.value).toBe("2.0.0"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("semver-theirs falls back to valid ours when theirs invalid", () => { |
| 119 | + const r = run(semverTheirs, "1.2.3", "carrot"); |
| 120 | + expect(r.status).toBe(StrategyStatus.OK); |
| 121 | + expect(r.value).toBe("1.2.3"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("falls back per config when neither valid", async () => { |
| 125 | + await reset({ preferValid: false, fallback: "theirs" }); |
| 126 | + const r = run(semverOurs, "foo", "bar"); |
| 127 | + expect(r.status).toBe(StrategyStatus.OK); |
| 128 | + expect(r.value).toBe("bar"); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments