Open History to see the last commit
1import { describe, expect, test } from "bun:test";
2import type { Commit } from "changelogithub";
3import { renderMarkdown, type RenderConfig } from "./render.ts";
4β
5// Minimal config that matches the defaults used by `release-notes.ts`.
6const baseConfig: RenderConfig = {
7 titles: { breakingChanges: "π¨ Breaking Changes" },
8 types: {
9 feat: { title: "π Features" },
10 fix: { title: "π Bug Fixes" },
11 perf: { title: "π Performance" },
12 } as RenderConfig["types"],
13 capitalize: true,
14 emoji: true,
15 baseUrl: "github.com",
16 repo: "alchemy-run/alchemy-effect",
17 from: "v1",
18 to: "v2",
19};
20β
21function makeCommit(input: {
22 type: string;
23 scope?: string;
24 description: string;
25 hash?: string;
26 pr?: string;
27 authors?: Array<{ login?: string; name: string }>;
28 isBreaking?: boolean;
29}): Commit {
30 const references: Commit["references"] = [];
31 if (input.hash) references.push({ type: "hash", value: input.hash });
32 if (input.pr) references.push({ type: "pull-request", value: input.pr });
33 return {
34 message: `${input.type}${input.scope ? `(${input.scope})` : ""}: ${input.description}`,
35 body: "",
36 shortHash: (input.hash ?? "").slice(0, 7),
37 author: { name: "t", email: "t@t" },
38 authors: [],
39 description: input.description,
40 type: input.type,
41 scope: input.scope ?? "",
42 references,
43 isBreaking: input.isBreaking ?? false,
44 resolvedAuthors: input.authors,
45 } as Commit;
46}
47β
48const COMPARE_LINE =
49 "##### [View changes on GitHub](https://github.com/alchemy-run/alchemy-effect/compare/v1...v2)";
50β
51describe("renderMarkdown", () => {
52 test("renders empty when no commits", () => {
53 const md = renderMarkdown([], baseConfig);
54 expect(md).toBe(`*No significant changes*\n\n${COMPARE_LINE}`);
55 });
56β
57 test("nests commits that share a top-level scope via `/`", () => {
58 const commits = [
59 makeCommit({
60 type: "fix",
61 scope: "aws/lambda",
62 description: "scope public url invokes",
63 hash: "abcdef123456",
64 }),
65 makeCommit({
66 type: "fix",
67 scope: "aws/lambda",
68 description: "second lambda fix",
69 hash: "1111111111",
70 }),
71 makeCommit({
72 type: "fix",
73 scope: "aws/s3",
74 description: "handle presigned URLs",
75 hash: "2222222222",
76 }),
77 makeCommit({
78 type: "fix",
79 scope: "aws",
80 description: "top-level aws fix",
81 hash: "3333333333",
82 }),
83 ];
84β
85 const md = renderMarkdown(commits, baseConfig);
86β
87 // Single shared `aws` header for all four commits.
88 expect(md.match(/- \*\*aws\*\*:/g) ?? []).toHaveLength(1);
89 // `lambda` has two commits -> nested list with header.
90 expect(md).toContain(" - **lambda**:\n");
91 expect(md).toContain(" - Second lambda fix");
92 expect(md).toContain(" - Scope public url invokes");
93 // `s3` also emits a header + nested bullet (matches changelogithub's
94 // `group: true` default: if any sibling has >1 commits, every sibling
95 // gets a header for visual alignment).
96 expect(md).toContain(" - **s3**:\n - Handle presigned URLs");
97 // Plain `fix(aws)` shows as a bare bullet under the aws header.
98 expect(md).toContain(" - Top-level aws fix");
99 // No stray flat `**aws/lambda**` or `**aws/s3**` groups.
100 expect(md).not.toContain("**aws/lambda**");
101 expect(md).not.toContain("**aws/s3**");
102 });
103β
104 test("keeps single-segment scopes flat (byte-compatible with changelogithub)", () => {
105 const commits = [
106 makeCommit({
107 type: "fix",
108 scope: "Cloudflare",
109 description: "apply Cloudflare Access headers to HTTP requests",
110 hash: "fd329e70aaaa",
111 pr: "#160",
112 authors: [{ login: "jacobiajohnson", name: "jj" }],
113 }),
114 ];
115β
116 const md = renderMarkdown(commits, baseConfig);
117β
118 expect(md).toContain(
119 "- **Cloudflare**: Apply Cloudflare Access headers to HTTP requests - by @jacobiajohnson in https://github.com/alchemy-run/alchemy-effect/issues/160 [<samp>(fd329)</samp>](https://github.com/alchemy-run/alchemy-effect/commit/fd329e70aaaa)",
120 );
121 expect(md).not.toContain("- **Cloudflare**:\n");
122 });
123β
124 test("nests multi-commit single-segment scopes with a header", () => {
125 const commits = [
126 makeCommit({
127 type: "fix",
128 scope: "Cloudflare",
129 description: "first",
130 hash: "aaaaaaa",
131 }),
132 makeCommit({
133 type: "fix",
134 scope: "Cloudflare",
135 description: "second",
136 hash: "bbbbbbb",
137 }),
138 ];
139β
140 const md = renderMarkdown(commits, baseConfig);
141β
142 expect(md).toContain("- **Cloudflare**:\n");
143 expect(md).toMatch(/- \*\*Cloudflare\*\*:\n\s+- Second[\s\S]+- First/);
144 });
145β
146 test("renders unscoped commits as plain bullets at the top of their type", () => {
147 const commits = [
148 makeCommit({ type: "fix", description: "no scope", hash: "abcabca" }),
149 makeCommit({
150 type: "fix",
151 scope: "core",
152 description: "core thing",
153 hash: "deadbee",
154 }),
155 ];
156β
157 const md = renderMarkdown(commits, baseConfig);
158β
159 const bugFixesBlock = md.slice(md.indexOf("π Bug Fixes"));
160 const noScopeIdx = bugFixesBlock.indexOf("- No scope");
161 const coreIdx = bugFixesBlock.indexOf("- **core**:");
162 expect(noScopeIdx).toBeGreaterThan(-1);
163 expect(coreIdx).toBeGreaterThan(-1);
164 expect(noScopeIdx).toBeLessThan(coreIdx);
165 });
166β
167 test("groups types under their titles and partitions breaking changes", () => {
168 const commits = [
169 makeCommit({
170 type: "feat",
171 scope: "api",
172 description: "new endpoint",
173 hash: "feat0000",
174 }),
175 makeCommit({
176 type: "fix",
177 scope: "api",
178 description: "bug",
179 hash: "fix00000",
180 }),
181 makeCommit({
182 type: "feat",
183 scope: "api",
184 description: "breaking change",
185 hash: "brk00000",
186 isBreaking: true,
187 }),
188 ];
189β
190 const md = renderMarkdown(commits, baseConfig);
191β
192 const breakingIdx = md.indexOf("π¨ Breaking Changes");
193 const featuresIdx = md.indexOf("π Features");
194 const fixesIdx = md.indexOf("π Bug Fixes");
195 expect(breakingIdx).toBeGreaterThan(-1);
196 expect(featuresIdx).toBeGreaterThan(breakingIdx);
197 expect(fixesIdx).toBeGreaterThan(featuresIdx);
198 // Breaking change must not also appear under Features.
199 const featuresBlock = md.slice(featuresIdx, fixesIdx);
200 expect(featuresBlock).not.toContain("Breaking change");
201 });
202β
203 test("honors scopeMap before splitting on `/`", () => {
204 const commits = [
205 makeCommit({
206 type: "fix",
207 scope: "aws-lambda",
208 description: "mapped",
209 hash: "map00000",
210 }),
211 ];
212β
213 const md = renderMarkdown(commits, {
214 ...baseConfig,
215 scopeMap: { "aws-lambda": "aws/lambda" },
216 });
217β
218 expect(md).toContain("- **aws**:");
219 expect(md).toContain(" - **lambda**: Mapped");
220 });
221β
222 test("splits scopes with leading/trailing whitespace and empty segments", () => {
223 const commits = [
224 makeCommit({
225 type: "fix",
226 scope: " aws / lambda ",
227 description: "trims segments",
228 hash: "trim0000",
229 }),
230 makeCommit({
231 type: "fix",
232 scope: "aws//s3",
233 description: "skips empty segments",
234 hash: "skip0000",
235 }),
236 ];
237β
238 const md = renderMarkdown(commits, baseConfig);
239 expect(md).toContain("- **aws**:");
240 // Both children are single-commit leaves with no other siblings forcing
241 // a header, so they collapse inline.
242 expect(md).toContain(" - **lambda**: Trims segments");
243 expect(md).toContain(" - **s3**: Skips empty segments");
244 });
245β
246 test("collapses single-commit scopes inline when no sibling forces a header", () => {
247 const commits = [
248 makeCommit({
249 type: "fix",
250 scope: "core",
251 description: "just one",
252 hash: "core0000",
253 }),
254 makeCommit({
255 type: "fix",
256 scope: "website",
257 description: "also one",
258 hash: "web00000",
259 }),
260 ];
261 const md = renderMarkdown(commits, baseConfig);
262 expect(md).toContain("- **core**: Just one");
263 expect(md).toContain("- **website**: Also one");
264 expect(md).not.toContain("- **core**:\n");
265 });
266β
267 test("forces headers on all siblings when any has multiple commits", () => {
268 // `group: true` default behavior: `website` is a single-commit leaf but
269 // gets a header because `core` has multiple commits in the same section.
270 const commits = [
271 makeCommit({
272 type: "fix",
273 scope: "core",
274 description: "first core",
275 hash: "c1c1c1c",
276 }),
277 makeCommit({
278 type: "fix",
279 scope: "core",
280 description: "second core",
281 hash: "c2c2c2c",
282 }),
283 makeCommit({
284 type: "fix",
285 scope: "website",
286 description: "solo website",
287 hash: "w1w1w1w",
288 }),
289 ];
290 const md = renderMarkdown(commits, baseConfig);
291 expect(md).toContain("- **core**:\n");
292 expect(md).toContain("- **website**:\n");
293 expect(md).not.toContain("- **website**: Solo website");
294 });
295β
296 test("renders three-level nesting", () => {
297 const commits = [
298 makeCommit({
299 type: "fix",
300 scope: "aws/lambda/urls",
301 description: "first",
302 hash: "aaa0000",
303 }),
304 makeCommit({
305 type: "fix",
306 scope: "aws/lambda/urls",
307 description: "second",
308 hash: "bbb0000",
309 }),
310 ];
311β
312 const md = renderMarkdown(commits, baseConfig);
313β
314 expect(md).toContain("- **aws**:");
315 expect(md).toContain(" - **lambda**:");
316 expect(md).toContain(" - **urls**:");
317 expect(md).toContain(" - First");
318 expect(md).toContain(" - Second");
319 });
320β
321 test("formats multiple authors with `and`", () => {
322 const commits = [
323 makeCommit({
324 type: "fix",
325 scope: "core",
326 description: "thing",
327 hash: "a1b2c3d",
328 authors: [{ login: "alice", name: "Alice" }, { name: "Bob" }],
329 }),
330 ];
331β
332 const md = renderMarkdown(commits, baseConfig);
333 expect(md).toContain("by @alice and **Bob**");
334 });
335β
336 test("strips emojis from section titles when emoji=false", () => {
337 const commits = [
338 makeCommit({ type: "fix", description: "x", hash: "1234567" }),
339 ];
340 const md = renderMarkdown(commits, { ...baseConfig, emoji: false });
341 expect(md).toContain("### Bug Fixes");
342 expect(md).not.toContain("π");
343 });
344β
345 test("respects capitalize=false", () => {
346 const commits = [
347 makeCommit({
348 type: "fix",
349 scope: "core",
350 description: "lowercase description",
351 hash: "1234567",
352 }),
353 ];
354 const md = renderMarkdown(commits, { ...baseConfig, capitalize: false });
355 expect(md).toContain("- **core**: lowercase description");
356 });
357});