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, resolveChangelogRange, 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); }); }); // Newest-first file so index order == reverse-chronological order. const DESC_CONTENT = ["# v0.5.0", "", "V5", "", "# v0.4.0", "", "V4", "", "# v0.3.0", "", "V3", "", "# v0.2.0", "", "V2", "", "# v0.1.0", "", "V1"].join( "\n", ); describe("resolveChangelogRange", () => { it("returns an inclusive slice, oldest-to-newest", () => { const batch = resolveChangelogRange(DESC_CONTENT, "0.3.0", "0.5.0"); expect(batch.found).toBe(true); expect(batch.versions).toEqual(["0.3.0", "0.4.0", "0.5.0"]); expect(batch.messages).toHaveLength(3); }); it("opens the top end (from here to the latest)", () => { const batch = resolveChangelogRange(DESC_CONTENT, "0.3.0", undefined); expect(batch.versions).toEqual(["0.1.0", "0.2.0", "0.3.0"]); }); it("opens the bottom end (from the oldest to here)", () => { const batch = resolveChangelogRange(DESC_CONTENT, undefined, "0.4.0"); expect(batch.versions).toEqual(["0.4.0", "0.5.0"]); }); it("normalizes a reversed range", () => { expect(resolveChangelogRange(DESC_CONTENT, "0.5.0", "0.3.0").versions).toEqual([ "0.3.0", "0.4.0", "0.5.0", ]); }); it("treats an empty range (..) as nothing to post", () => { expect(resolveChangelogRange(DESC_CONTENT, undefined, undefined).found).toBe(false); }); it("reports an unknown bound", () => { expect(resolveChangelogRange(DESC_CONTENT, "0.3.0", "9.9.9").found).toBe(false); }); it("ignores a leading v on either side", () => { expect(resolveChangelogRange(DESC_CONTENT, "v0.4.0", "v0.5.0").versions).toEqual([ "0.4.0", "0.5.0", ]); }); }); 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**:"); }); it("resolves an inclusive range on the shipped file", () => { const content = readFileSync(CHANGELOG_PATH, "utf8"); const batch = resolveChangelogRange(content, "0.4.0", "0.5.0"); expect(batch.found).toBe(true); expect(batch.versions).toEqual(["0.4.0", "0.4.1", "0.5.0"]); batch.messages.forEach((messages) => { expect(messages.length).toBeGreaterThanOrEqual(1); messages.forEach((message) => expect(message.length).toBeLessThanOrEqual(4000)); }); }); });