-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease-notes.test.mjs
More file actions
386 lines (353 loc) · 12.6 KB
/
Copy pathrelease-notes.test.mjs
File metadata and controls
386 lines (353 loc) · 12.6 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { describe, expect, test, vi } from 'vitest';
import {
collectReleaseData,
compareSemVer,
parseArguments,
parseConventionalCommit,
parseGitLog,
parseSemVer,
renderReleaseNotes,
runGit,
selectPreviousTag,
} from './release-notes.mjs';
const repositoryUrl = 'https://github.com/pmndrs/upscaler';
describe('SemVer', () => {
test('parses stable and prerelease versions without losing numeric precision', () => {
expect(parseSemVer('v1.2.3')).toEqual({
major: '1',
minor: '2',
patch: '3',
prerelease: [],
build: [],
});
expect(parseSemVer('1.2.3-beta.9007199254740993+metal.3')).toEqual({
major: '1',
minor: '2',
patch: '3',
prerelease: ['beta', '9007199254740993'],
build: ['metal', '3'],
});
});
test.each([
'',
'1.2',
'1.2.3.4',
'01.2.3',
'1.02.3',
'1.2.03',
'1.2.3-01',
'1.2.3-beta..1',
'version-1.2.3',
])('rejects invalid version %j', (value) => {
expect(() => parseSemVer(value)).toThrow(/invalid semver/i);
});
test('orders core versions and ignores build metadata', () => {
expect(compareSemVer('1.9.0', '1.10.0')).toBeLessThan(0);
expect(compareSemVer('2.0.0', '1.999.999')).toBeGreaterThan(0);
expect(compareSemVer('1.2.3+first', '1.2.3+second')).toBe(0);
});
test('implements SemVer prerelease precedence', () => {
const ordered = [
'1.0.0-alpha',
'1.0.0-alpha.1',
'1.0.0-alpha.beta',
'1.0.0-beta',
'1.0.0-beta.2',
'1.0.0-beta.11',
'1.0.0-rc.1',
'1.0.0',
];
for (let index = 1; index < ordered.length; index++)
expect(compareSemVer(ordered[index - 1], ordered[index])).toBeLessThan(0);
});
test('compares large numeric identifiers as integers rather than floats', () => {
expect(
compareSemVer(
'1.0.0-alpha.9007199254740992',
'1.0.0-alpha.9007199254740993',
),
).toBeLessThan(0);
expect(
compareSemVer('9007199254740992.0.0', '9007199254740993.0.0'),
).toBeLessThan(0);
});
});
describe('previous comparison tag selection', () => {
test('stable targets select the highest prior stable tag', () => {
expect(
selectPreviousTag('v1.0.0', [
'v0.9.0',
'v1.0.0-beta.1',
'v0.10.0',
'not-a-version',
'1.0.0-rc.1',
'v1.0.0',
]),
).toBe('v0.10.0');
});
test('prerelease targets select the highest prior stable or prerelease tag', () => {
expect(
selectPreviousTag('v1.0.0-rc.2', [
'v0.9.0',
'v1.0.0-beta.2',
'v1.0.0-rc.1',
'v1.0.0',
'v1.0.0-rc.2',
]),
).toBe('v1.0.0-rc.1');
});
test('returns null when no valid prior comparison tag exists', () => {
expect(selectPreviousTag('v0.1.0', ['v0.1.0', 'latest', '0.0.1'])).toBeNull();
});
});
describe('Conventional Commit parsing', () => {
test.each([
['feat: add temporal guides', 'Features', undefined],
['feat(guides): expose temporal products', 'Features', 'guides'],
['fix: preserve ping-pong identity', 'Fixes', undefined],
['fix(node): register the dependency', 'Fixes', 'node'],
['perf: fuse the detector', 'Performance', undefined],
['perf(rcas): avoid repeated inversion', 'Performance', 'rcas'],
])('classifies %s', (subject, category, scope) => {
expect(parseConventionalCommit({ subject, body: '' })).toMatchObject({
category,
scope,
description: subject.slice(subject.indexOf(':') + 1).trim(),
breaking: false,
});
});
test('classifies subject markers and breaking footers as breaking changes', () => {
expect(
parseConventionalCommit({
subject: 'feat(api)!: replace dispatch options',
body: '',
}),
).toMatchObject({
category: 'Breaking Changes',
scope: 'api',
description: 'replace dispatch options',
breaking: true,
});
expect(
parseConventionalCommit({
subject: 'fix: correct the output domain',
body: 'BREAKING CHANGE: output is now linear HDR',
}),
).toMatchObject({
category: 'Breaking Changes',
breaking: true,
});
});
test.each([
'docs: explain temporal guides',
'chore: update fixtures',
'ci: exercise packed output',
'test: cover ordering',
'refactor: rename local helper',
'release: v0.2.0',
'Merge pull request #11 from pmndrs/releases',
'plain non-conventional subject',
])('omits non-actionable commit %j', (subject) => {
expect(parseConventionalCommit({ subject, body: '' })).toBeNull();
});
});
describe('release-note rendering', () => {
const release = {
tag: 'v0.3.0',
previousTag: 'v0.2.0',
repositoryUrl,
commits: [
{
sha: 'ddddddd4444444',
subject: 'perf(rcas): avoid repeated inversion',
body: '',
},
{
sha: 'bbbbbbb2222222',
subject: 'feat: add release previews',
body: '',
},
{
sha: 'aaaaaaa1111111',
subject: 'feat(guides): expose temporal products',
body: '',
},
{
sha: 'ccccccc3333333',
subject: 'fix(history): preserve the latest write',
body: '',
},
{
sha: 'eeeeeee5555555',
subject: 'feat(api)!: replace the dispatch contract',
body: '',
},
{
sha: 'fffffff6666666',
subject: 'docs: document the new contract',
body: '',
},
],
};
test('renders non-empty sections in a fixed order and preserves commit order', () => {
expect(renderReleaseNotes(release)).toBe(
[
'## Breaking Changes',
'',
'- **api:** replace the dispatch contract',
` ([eeeeeee](${repositoryUrl}/commit/eeeeeee5555555))`,
'',
'## Features',
'',
'- add release previews',
` ([bbbbbbb](${repositoryUrl}/commit/bbbbbbb2222222))`,
'- **guides:** expose temporal products',
` ([aaaaaaa](${repositoryUrl}/commit/aaaaaaa1111111))`,
'',
'## Fixes',
'',
'- **history:** preserve the latest write',
` ([ccccccc](${repositoryUrl}/commit/ccccccc3333333))`,
'',
'## Performance',
'',
'- **rcas:** avoid repeated inversion',
` ([ddddddd](${repositoryUrl}/commit/ddddddd4444444))`,
'',
`**Full Changelog:** ${repositoryUrl}/compare/v0.2.0...v0.3.0`,
'',
].join('\n'),
);
});
test('renders breaking commits once and omits non-actionable sections', () => {
const notes = renderReleaseNotes({
...release,
commits: [release.commits[4], release.commits[5]],
});
expect(notes.match(/replace the dispatch contract/g)).toHaveLength(1);
expect(notes).not.toContain('## Features');
expect(notes).not.toContain('document the new contract');
});
test('omits the comparison link when there is no prior tag', () => {
const notes = renderReleaseNotes({
...release,
previousTag: null,
commits: [release.commits[1]],
});
expect(notes).toContain('## Features');
expect(notes).not.toContain('Full Changelog');
expect(notes).not.toContain('/compare/');
});
test('produces identical Markdown for identical release data', () => {
expect(renderReleaseNotes(release)).toBe(renderReleaseNotes(release));
});
});
describe('read-only Git data collection', () => {
test('collects reachable tags and the selected range through argument arrays', () => {
const controlBody = 'body with record byte \x1e and unit byte \x1f';
const runGit = vi.fn((args) => {
if (args[0] === 'rev-parse') return 'abc123\n';
if (args[0] === 'tag') return 'v0.1.0\nv0.2.0-beta.1\nv0.2.0\n';
if (args[0] === 'log')
return [
`abcdef0123456789\0feat(cli): add notes preview\0${controlBody}\0`,
'fedcba9876543210\0fix: preserve ranges\0\0',
].join('');
throw new Error(`Unexpected git arguments: ${args.join(' ')}`);
});
expect(collectReleaseData('v0.2.0', { runGit, repositoryUrl })).toEqual({
tag: 'v0.2.0',
previousTag: 'v0.1.0',
repositoryUrl,
commits: [
{
sha: 'abcdef0123456789',
subject: 'feat(cli): add notes preview',
body: controlBody,
},
{
sha: 'fedcba9876543210',
subject: 'fix: preserve ranges',
body: '',
},
],
});
expect(runGit.mock.calls.map(([args]) => args)).toEqual([
['rev-parse', '--verify', 'v0.2.0^{commit}'],
['tag', '--merged', 'v0.2.0', '--list', 'v*'],
[
'log',
'-z',
'--reverse',
'--format=%H%x00%s%x00%b',
'v0.1.0..v0.2.0',
],
]);
});
test('collects all target history when there is no prior tag', () => {
const runGit = vi.fn((args) => {
if (args[0] === 'tag') return 'v0.1.0\n';
if (args[0] === 'log') return '';
return 'abc123\n';
});
expect(
collectReleaseData('v0.1.0', { runGit, repositoryUrl }).previousTag,
).toBeNull();
expect(runGit.mock.calls.at(-1)[0]).toEqual([
'log',
'-z',
'--reverse',
'--format=%H%x00%s%x00%b',
'v0.1.0',
]);
});
test('rejects a CLI target that is not a v-prefixed SemVer tag before running Git', () => {
const runGit = vi.fn();
expect(() =>
collectReleaseData('0.2.0', { runGit, repositoryUrl }),
).toThrow(/v-prefixed semver tag/i);
expect(runGit).not.toHaveBeenCalled();
});
test('preserves legal commit-message control characters in fixed NUL fields', () => {
const body = 'first line\x1e\nsecond line\x01\nBREAKING CHANGE: keep framing';
const subject = 'feat(parser): preserve \x1f control bytes';
const log = `abcdef0123456789\0${subject}\0${body}\0`;
expect(parseGitLog(log)).toEqual([
{
sha: 'abcdef0123456789',
subject,
body,
},
]);
});
test('rejects an incomplete fixed-field Git record', () => {
expect(() => parseGitLog('abcdef\0feat: incomplete\0')).toThrow(
/fixed-field git log/i,
);
});
test('sets a generous buffer for long release histories', () => {
const execute = vi.fn(() => 'git output');
expect(runGit(['log', 'v0.1.0..v0.2.0'], execute)).toBe('git output');
expect(execute).toHaveBeenCalledWith(
'git',
['log', 'v0.1.0..v0.2.0'],
{
encoding: 'utf8',
maxBuffer: 64 * 1024 * 1024,
stdio: ['ignore', 'pipe', 'pipe'],
},
);
});
});
describe('preview CLI arguments', () => {
test('accepts only a single v-prefixed SemVer tag option', () => {
expect(parseArguments(['--tag', 'v0.2.0'])).toEqual({ tag: 'v0.2.0' });
expect(() => parseArguments([])).toThrow(/usage/i);
expect(() => parseArguments(['--tag', '0.2.0'])).toThrow(
/v-prefixed semver tag/i,
);
expect(() => parseArguments(['--tag', 'v0.2.0', '--other'])).toThrow(
/usage/i,
);
});
});