Repository navigation
Expand file tree
/
Copy pathchangelog-gen.test.ts
More file actions
338 lines (268 loc) · 10.2 KB
/
Copy pathchangelog-gen.test.ts
File metadata and controls
338 lines (268 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
import * as assert from "@std/assert";
import {
deduplicateCommits,
generateChangelogSection,
groupBySection,
insertIntoChangelog,
parseConventionalCommit,
stripTakeSuffix,
} from "./changelog-gen.ts";
import type { ConventionalCommit } from "./changelog-gen.ts";
// =============================================================================
// parseConventionalCommit
// =============================================================================
Deno.test("parseConventionalCommit - feat with scope", () => {
const result = parseConventionalCommit(
"feat(auth): add login system",
"abc123",
);
assert.assertExists(result);
assert.assertEquals(result.type, "feat");
assert.assertEquals(result.scope, "auth");
assert.assertEquals(result.message, "add login system");
assert.assertEquals(result.hash, "abc123");
});
Deno.test("parseConventionalCommit - fix without scope", () => {
const result = parseConventionalCommit(
"fix: resolve crash on startup",
"def456",
);
assert.assertExists(result);
assert.assertEquals(result.type, "fix");
assert.assertEquals(result.scope, undefined);
assert.assertEquals(result.message, "resolve crash on startup");
});
Deno.test("parseConventionalCommit - breaking change with !", () => {
const result = parseConventionalCommit(
"feat(api)!: remove deprecated endpoint",
"ghi789",
);
assert.assertExists(result);
assert.assertEquals(result.type, "feat");
assert.assertEquals(result.scope, "api");
assert.assertEquals(result.message, "remove deprecated endpoint");
});
Deno.test("parseConventionalCommit - non-conventional returns undefined", () => {
const result = parseConventionalCommit("random commit message", "jkl012");
assert.assertEquals(result, undefined);
});
Deno.test("parseConventionalCommit - chore type", () => {
const result = parseConventionalCommit(
"chore(deps): bump versions",
"mno345",
);
assert.assertExists(result);
assert.assertEquals(result.type, "chore");
});
// =============================================================================
// stripTakeSuffix
// =============================================================================
Deno.test("stripTakeSuffix - roman numeral", () => {
assert.assertEquals(
stripTakeSuffix("authentication system (take II)."),
"authentication system",
);
});
Deno.test("stripTakeSuffix - roman numeral III", () => {
assert.assertEquals(
stripTakeSuffix("authentication system (take III)"),
"authentication system",
);
});
Deno.test("stripTakeSuffix - arabic number", () => {
assert.assertEquals(
stripTakeSuffix("styling issues (take 3)"),
"styling issues",
);
});
Deno.test("stripTakeSuffix - no take suffix", () => {
assert.assertEquals(
stripTakeSuffix("normal commit message"),
"normal commit message",
);
});
Deno.test("stripTakeSuffix - case insensitive", () => {
assert.assertEquals(
stripTakeSuffix("feature (Take IV)"),
"feature",
);
});
// =============================================================================
// deduplicateCommits
// =============================================================================
Deno.test("deduplicateCommits - collapses take series", () => {
const commits: ConventionalCommit[] = [
{
type: "feat",
scope: "auth",
message: "authentication system.",
hash: "a",
},
{
type: "feat",
scope: "auth",
message: "authentication system (take II).",
hash: "b",
},
{ type: "fix", scope: "ui", message: "styling issues.", hash: "c" },
{
type: "feat",
scope: "auth",
message: "authentication system (take III).",
hash: "d",
},
];
const result = deduplicateCommits(commits);
assert.assertEquals(result.length, 2);
assert.assertEquals(result[0]!.message, "authentication system.");
assert.assertEquals(result[1]!.message, "styling issues.");
});
Deno.test("deduplicateCommits - different types are not deduped", () => {
const commits: ConventionalCommit[] = [
{ type: "feat", scope: undefined, message: "add feature", hash: "a" },
{ type: "fix", scope: undefined, message: "add feature", hash: "b" },
];
const result = deduplicateCommits(commits);
assert.assertEquals(result.length, 2);
});
Deno.test("deduplicateCommits - empty input", () => {
const result = deduplicateCommits([]);
assert.assertEquals(result.length, 0);
});
// =============================================================================
// groupBySection
// =============================================================================
Deno.test("groupBySection - maps types to sections", () => {
const commits: ConventionalCommit[] = [
{ type: "feat", scope: undefined, message: "new feature", hash: "a" },
{ type: "fix", scope: undefined, message: "bug fix", hash: "b" },
{ type: "refactor", scope: undefined, message: "clean up", hash: "c" },
{ type: "chore", scope: undefined, message: "bump deps", hash: "d" },
];
const groups = groupBySection(commits);
assert.assertEquals(groups.size, 3);
assert.assertEquals(groups.get("Added")!.length, 1);
assert.assertEquals(groups.get("Fixed")!.length, 1);
assert.assertEquals(groups.get("Changed")!.length, 1);
assert.assertEquals(groups.has("Removed"), false);
});
Deno.test("groupBySection - skips chore, ci, test", () => {
const commits: ConventionalCommit[] = [
{ type: "chore", scope: undefined, message: "bump deps", hash: "a" },
{ type: "ci", scope: undefined, message: "fix pipeline", hash: "b" },
{ type: "test", scope: undefined, message: "add tests", hash: "c" },
];
const groups = groupBySection(commits);
assert.assertEquals(groups.size, 0);
});
// =============================================================================
// generateChangelogSection
// =============================================================================
Deno.test("generateChangelogSection - produces markdown", () => {
const commits: ConventionalCommit[] = [
{ type: "feat", scope: "auth", message: "add login", hash: "a" },
{ type: "fix", scope: undefined, message: "fix crash", hash: "b" },
];
const section = generateChangelogSection("4.1.2", commits);
assert.assertStringIncludes(section, "## 4.1.2 -");
assert.assertStringIncludes(section, "### Added");
assert.assertStringIncludes(section, "- **auth:** add login");
assert.assertStringIncludes(section, "### Fixed");
assert.assertStringIncludes(section, "- fix crash");
});
Deno.test("generateChangelogSection - maintenance section when all skipped", () => {
const commits: ConventionalCommit[] = [
{ type: "chore", scope: undefined, message: "bump", hash: "a" },
];
const section = generateChangelogSection("1.0.0", commits);
assert.assertStringIncludes(section, "## 1.0.0 -");
assert.assertStringIncludes(section, "_Maintenance release._");
});
Deno.test("generateChangelogSection - section ordering", () => {
const commits: ConventionalCommit[] = [
{ type: "fix", scope: undefined, message: "bug", hash: "a" },
{ type: "feat", scope: undefined, message: "feature", hash: "b" },
{ type: "revert", scope: undefined, message: "undo", hash: "c" },
{ type: "refactor", scope: undefined, message: "clean", hash: "d" },
];
const section = generateChangelogSection("1.0.0", commits);
const addedIdx = section.indexOf("### Added");
const changedIdx = section.indexOf("### Changed");
const fixedIdx = section.indexOf("### Fixed");
const removedIdx = section.indexOf("### Removed");
// Order: Added, Changed, Fixed, Removed
assert.assert(addedIdx < changedIdx);
assert.assert(changedIdx < fixedIdx);
assert.assert(fixedIdx < removedIdx);
});
// =============================================================================
// insertIntoChangelog
// =============================================================================
Deno.test("insertIntoChangelog - inserts after [Unreleased]", () => {
const existing = `# Changelog
## [Unreleased]
### Added
- something in progress
## 4.1.1 - 2024-07-16
### Fixed
- old fix
`;
const newSection = `## 4.1.2 - 2025-03-14
### Added
- new feature`;
const result = insertIntoChangelog(existing, newSection);
assert.assertStringIncludes(result, "## [Unreleased]");
assert.assertStringIncludes(result, "## 4.1.2 - 2025-03-14");
assert.assertStringIncludes(result, "## 4.1.1 - 2024-07-16");
// New section should come between [Unreleased] and 4.1.1
const unreleasedIdx = result.indexOf("## [Unreleased]");
const newIdx = result.indexOf("## 4.1.2");
const oldIdx = result.indexOf("## 4.1.1");
assert.assert(unreleasedIdx < newIdx);
assert.assert(newIdx < oldIdx);
});
Deno.test("insertIntoChangelog - no [Unreleased] section", () => {
const existing = `# Changelog
## 4.1.1 - 2024-07-16
### Fixed
- old fix
`;
const newSection = `## 4.1.2 - 2025-03-14
### Added
- new feature`;
const result = insertIntoChangelog(existing, newSection);
assert.assertStringIncludes(result, "## 4.1.2 - 2025-03-14");
// Should be inserted before the existing section
const newIdx = result.indexOf("## 4.1.2");
const oldIdx = result.indexOf("## 4.1.1");
assert.assert(newIdx < oldIdx);
});
Deno.test("insertIntoChangelog - replaces existing section for same version", () => {
const existing = `# Changelog
## [Unreleased]
## 4.1.2 - 2025-03-14
### Added
- old entry from failed release
## 4.1.1 - 2024-07-16
### Fixed
- old fix
`;
const newSection = `## 4.1.2 - 2025-03-14
### Added
- updated entry after pipeline fix
### Fixed
- new bug fix`;
const result = insertIntoChangelog(existing, newSection, "4.1.2");
// Should contain the new content, not the old
assert.assertStringIncludes(result, "updated entry after pipeline fix");
assert.assertStringIncludes(result, "new bug fix");
assert.assert(!result.includes("old entry from failed release"));
// Should still have other sections
assert.assertStringIncludes(result, "## [Unreleased]");
assert.assertStringIncludes(result, "## 4.1.1 - 2024-07-16");
// Only one 4.1.2 heading
const first = result.indexOf("## 4.1.2");
const second = result.indexOf("## 4.1.2", first + 1);
assert.assertEquals(second, -1);
});