|
1 | 1 | import { describe, expect, it, vi } from "vitest"; |
2 | | -import { verifyRelease } from "../../../scripts/release/calver-plugin.cjs"; |
| 2 | +import { |
| 3 | + analyzeCommits, |
| 4 | + mapCalverReleaseType, |
| 5 | + verifyRelease, |
| 6 | +} from "../../../scripts/release/calver-plugin.cjs"; |
3 | 7 |
|
4 | 8 | describe("calver plugin", () => { |
5 | | - it("sets nextRelease.version in verifyRelease for main", async () => { |
6 | | - expect(verifyRelease).toBeTypeOf("function"); |
| 9 | + it("maps releasable commits to patch within same month", () => { |
| 10 | + expect( |
| 11 | + mapCalverReleaseType({ |
| 12 | + branchName: "main", |
| 13 | + releaseType: "minor", |
| 14 | + lastVersion: "2026.4.8", |
| 15 | + nowIso: "2026-04-20T10:00:00.000Z", |
| 16 | + }), |
| 17 | + ).toBe("patch"); |
7 | 18 |
|
| 19 | + expect( |
| 20 | + mapCalverReleaseType({ |
| 21 | + branchName: "next", |
| 22 | + releaseType: "major", |
| 23 | + lastVersion: "2026.4.8-next.6", |
| 24 | + nowIso: "2026-04-20T10:00:00.000Z", |
| 25 | + }), |
| 26 | + ).toBe("patch"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("maps releasable commits to minor on month rollover", () => { |
| 30 | + expect( |
| 31 | + mapCalverReleaseType({ |
| 32 | + branchName: "main", |
| 33 | + releaseType: "patch", |
| 34 | + lastVersion: "2026.4.8", |
| 35 | + nowIso: "2026-05-01T00:00:00.000Z", |
| 36 | + }), |
| 37 | + ).toBe("minor"); |
| 38 | + |
| 39 | + expect( |
| 40 | + mapCalverReleaseType({ |
| 41 | + branchName: "next", |
| 42 | + releaseType: "patch", |
| 43 | + lastVersion: "2026.4.8-next.6", |
| 44 | + nowIso: "2026-05-01T00:00:00.000Z", |
| 45 | + }), |
| 46 | + ).toBe("minor"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("preserves null release type", () => { |
| 50 | + expect( |
| 51 | + mapCalverReleaseType({ |
| 52 | + branchName: "main", |
| 53 | + releaseType: null, |
| 54 | + lastVersion: "2026.4.8", |
| 55 | + nowIso: "2026-04-20T10:00:00.000Z", |
| 56 | + }), |
| 57 | + ).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("delegates commit analysis and normalizes to patch cadence", async () => { |
| 61 | + const pluginConfig = { |
| 62 | + preset: "conventionalcommits", |
| 63 | + releaseRules: [{ type: "chore", release: false }], |
| 64 | + }; |
| 65 | + |
| 66 | + const context = { |
| 67 | + branch: { name: "next" }, |
| 68 | + commits: [{ message: "feat(labels): add project labels" }], |
| 69 | + cwd: process.cwd(), |
| 70 | + env: process.env, |
| 71 | + logger: { log: vi.fn<(message: string) => void>() }, |
| 72 | + options: {}, |
| 73 | + lastRelease: { version: "2026.4.8-next.6" }, |
| 74 | + }; |
| 75 | + |
| 76 | + await expect(analyzeCommits(pluginConfig, context)).resolves.toBe("patch"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("rolls over to minor on month change during analyzeCommits", async () => { |
8 | 80 | vi.useFakeTimers(); |
9 | | - vi.setSystemTime(new Date("2026-04-20T10:00:00.000Z")); |
| 81 | + vi.setSystemTime(new Date("2026-05-01T00:00:00.000Z")); |
| 82 | + |
| 83 | + const pluginConfig = { |
| 84 | + preset: "conventionalcommits", |
| 85 | + releaseRules: [{ type: "chore", release: false }], |
| 86 | + }; |
10 | 87 |
|
11 | 88 | const context = { |
12 | | - lastRelease: { version: "2026.4.5" }, |
13 | | - branch: { name: "main" }, |
| 89 | + branch: { name: "next" }, |
| 90 | + commits: [{ message: "fix(labels): repair filters" }], |
| 91 | + cwd: process.cwd(), |
| 92 | + env: process.env, |
14 | 93 | logger: { log: vi.fn<(message: string) => void>() }, |
15 | | - nextRelease: { version: "0.0.0" }, |
| 94 | + options: {}, |
| 95 | + lastRelease: { version: "2026.4.8-next.6" }, |
16 | 96 | }; |
17 | 97 |
|
18 | | - await verifyRelease({}, context); |
| 98 | + await expect(analyzeCommits(pluginConfig, context)).resolves.toBe("minor"); |
19 | 99 |
|
20 | | - expect(context.nextRelease.version).toBe("2026.4.6"); |
21 | | - expect(context.logger.log).toHaveBeenCalledWith( |
22 | | - "calver-plugin: forcing next release version to 2026.4.6", |
| 100 | + vi.useRealTimers(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("fails when semantic-release next version diverges from calver", async () => { |
| 104 | + vi.useFakeTimers(); |
| 105 | + vi.setSystemTime(new Date("2026-04-20T10:00:00.000Z")); |
| 106 | + |
| 107 | + const context = { |
| 108 | + lastRelease: { version: "2026.4.8-next.6" }, |
| 109 | + branch: { name: "next" }, |
| 110 | + logger: { log: vi.fn<(message: string) => void>() }, |
| 111 | + nextRelease: { version: "2026.5.0-next.1" }, |
| 112 | + }; |
| 113 | + |
| 114 | + await expect(verifyRelease({}, context)).rejects.toThrow( |
| 115 | + "semantic-release computed 2026.5.0-next.1 but calver requires 2026.4.8-next.7", |
23 | 116 | ); |
24 | 117 |
|
25 | 118 | vi.useRealTimers(); |
26 | 119 | }); |
27 | 120 |
|
28 | | - it("increments prerelease counter on next branch", async () => { |
| 121 | + it("passes month-rollover semantic-release version", async () => { |
| 122 | + vi.useFakeTimers(); |
| 123 | + vi.setSystemTime(new Date("2026-05-01T00:00:00.000Z")); |
| 124 | + |
| 125 | + const context = { |
| 126 | + lastRelease: { version: "2026.4.8" }, |
| 127 | + branch: { name: "main" }, |
| 128 | + logger: { log: vi.fn<(message: string) => void>() }, |
| 129 | + nextRelease: { version: "2026.5.0" }, |
| 130 | + }; |
| 131 | + |
| 132 | + await expect(verifyRelease({}, context)).resolves.toBeUndefined(); |
| 133 | + |
| 134 | + vi.useRealTimers(); |
| 135 | + }); |
| 136 | + |
| 137 | + it("passes when semantic-release next version matches calver", async () => { |
29 | 138 | vi.useFakeTimers(); |
30 | 139 | vi.setSystemTime(new Date("2026-04-20T10:00:00.000Z")); |
31 | 140 |
|
32 | 141 | const context = { |
33 | | - lastRelease: { version: "2026.4.6-next.2" }, |
| 142 | + lastRelease: { version: "2026.4.8-next.6" }, |
34 | 143 | branch: { name: "next" }, |
35 | 144 | logger: { log: vi.fn<(message: string) => void>() }, |
36 | | - nextRelease: { version: "0.0.0" }, |
| 145 | + nextRelease: { version: "2026.4.8-next.7" }, |
37 | 146 | }; |
38 | 147 |
|
39 | | - await verifyRelease({}, context); |
40 | | - |
41 | | - expect(context.nextRelease.version).toBe("2026.4.6-next.3"); |
| 148 | + await expect(verifyRelease({}, context)).resolves.toBeUndefined(); |
42 | 149 | expect(context.logger.log).toHaveBeenCalledWith( |
43 | | - "calver-plugin: forcing next release version to 2026.4.6-next.3", |
| 150 | + "calver-plugin: verified semantic-release version 2026.4.8-next.7", |
44 | 151 | ); |
45 | 152 |
|
46 | 153 | vi.useRealTimers(); |
|
0 commit comments