import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { formatEntryDiscord, parseChangelog, resolveChangelog, splitForDiscord, } from "@/bot/lib/changelogRender"; const CHANGELOG_PATH = path.resolve( path.dirname(fileURLToPath(import.meta.url)), "../../CHANGELOG.md", ); const SAMPLE = `# v0.4.0 Intro line describing the release. ### Features - **Feature one:** does a thing. - **Feature two:** does another. ### Fixes - Fixed a bug. `; describe("parseChangelog", () => { it("returns entries newest-first with clean body boundaries", () => { const content = [ "# Changelog", "", "# v0.5.0", "", "Fifth release.", "", "### Features", "", "- A.", "", "# v0.4.0", "", "Fourth release.", "", ].join("\n"); const entries = parseChangelog(content); expect(entries.map((e) => e.version)).toEqual(["0.5.0", "0.4.0"]); expect(entries[0].rawBody).toContain("Fifth release."); expect(entries[0].rawBody).toContain("### Features"); expect(entries[0].rawBody).not.toContain("# v0.4.0"); expect(entries[1].rawBody.trim()).toBe("Fourth release."); }); it("ignores the file title header", () => { expect(parseChangelog("# Changelog\n\n# v1.0.0\n\nBody\n")).toHaveLength(1); }); }); describe("resolveChangelog", () => { it("defaults to the newest (first) entry", () => { const content = ["# v0.3.0", "", "New.", "", "# v0.2.0", "", "Old."].join("\n"); const resolved = resolveChangelog(content); expect(resolved.found).toBe(true); expect(resolved.version).toBe("0.3.0"); }); it("matches a specific version and tolerates a leading v", () => { const content = ["# v0.2.0", "", "A.", "", "# v0.4.0", "", "B."].join("\n"); expect(resolveChangelog(content, "0.4.0").version).toBe("0.4.0"); expect(resolveChangelog(content, "v0.2.0").version).toBe("0.2.0"); }); it("reports when nothing matches", () => { expect(resolveChangelog("# v0.4.0\n\nBody\n", "9.9.9").found).toBe(false); expect(resolveChangelog("# Changelog\n", "").found).toBe(false); }); }); describe("formatEntryDiscord", () => { it("turns section headers bold and bullets into the bullet glyph", () => { const formatted = formatEntryDiscord(SAMPLE); expect(formatted).toContain("**Features**"); expect(formatted).toContain("**Fixes**"); expect(formatted).toContain("• **Feature one:** does a thing."); expect(formatted).not.toContain("# "); }); it("preserves inline markdown", () => { const formatted = formatEntryDiscord("Plain with **bold**, `code`, and [a link](https://example.com)."); expect(formatted).toContain("**bold**"); expect(formatted).toContain("`code`"); expect(formatted).toContain("[a link](https://example.com)"); }); }); describe("splitForDiscord", () => { it("keeps a short entry as a single message", () => { expect(splitForDiscord(SAMPLE)).toHaveLength(1); }); it("splits a long entry at block boundaries without losing bullets", () => { const bullets = Array.from({ length: 300 }, (_, i) => `- **Item ${i}:** descriptive text`).join("\n"); const body = `### Features\n\n${bullets}\n`; const messages = splitForDiscord(body); expect(messages.length).toBeGreaterThan(1); messages.forEach((message) => expect(message.length).toBeLessThanOrEqual(4000)); // Every original bullet survives across the split, so nothing was cut mid-item. const bulletsInMessages = messages .flatMap((message) => message.split("\n")) .filter((line) => line.startsWith("• ")); expect(bulletsInMessages).toHaveLength(300); // The first chunk leads with the section header. expect(messages[0]).toContain("**Features**"); }); }); describe("real CHANGELOG.md", () => { it("parses and renders the shipped entries", () => { const content = readFileSync(CHANGELOG_PATH, "utf8"); const entries = parseChangelog(content); expect(entries.length).toBeGreaterThan(10); // The file is newest-first. expect(entries[0].version).toBe("0.8.0"); const v040 = resolveChangelog(content, "0.4.0"); expect(v040.found).toBe(true); expect(v040.messages.length).toBeGreaterThan(0); expect(v040.messages[0]).toContain("**Features**"); expect(v040.messages[0]).toContain("• **Mission planning maps**:"); }); });