import { readFileSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { EMPTY_SECTION_FALLBACK, compareVersions, 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: 'X..' is X and everything newer", () => { const batch = resolveChangelogRange(DESC_CONTENT, "0.3.0", undefined); expect(batch.versions).toEqual(["0.3.0", "0.4.0", "0.5.0"]); }); it("opens the bottom end: '..X' is the oldest through X", () => { const batch = resolveChangelogRange(DESC_CONTENT, undefined, "0.4.0"); expect(batch.versions).toEqual(["0.1.0", "0.2.0", "0.3.0", "0.4.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 into markdown headings 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."); // The entry's own "# vX.Y.Z" version header stays out (the embed title has it). expect(formatted).not.toContain("# v0.4.0"); }); 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 heading. expect(messages[0]).toContain("### Features"); }); }); describe("empty-section fallback", () => { it("renders a fully empty entry as a Fixes section carrying the generic line", () => { const content = [ "# Changelog", "", "# v0.8.3", "", "---", "", "# v0.8.2", "", "### Fixes", "", "- A real fix.", "", ].join("\n"); const resolved = resolveChangelog(content, "0.8.3"); expect(resolved.found).toBe(true); expect(resolved.messages).toHaveLength(1); expect(resolved.messages[0]).toContain("### Fixes"); expect(resolved.messages[0]).toContain(EMPTY_SECTION_FALLBACK); expect(resolved.messages[0]).not.toContain("---"); }); it("fills a section heading that has no items, leaving real sections alone", () => { const formatted = formatEntryDiscord("### Improvements\n\n### Fixes\n\n- A real fix.\n"); expect(formatted).toContain("### Improvements"); expect(formatted).toContain(`• ${EMPTY_SECTION_FALLBACK}`); expect(formatted).toContain("• A real fix."); }); it("does not invent content for entries that already have some", () => { const formatted = formatEntryDiscord(SAMPLE); expect(formatted).not.toContain(EMPTY_SECTION_FALLBACK); // Entry separators never leak into the post. expect(formatted).not.toContain("---"); }); it("keeps a real summary paragraph even without sections", () => { const formatted = formatEntryDiscord("Just a summary sentence.\n"); expect(formatted).toBe("Just a summary sentence."); expect(formatted).not.toContain(EMPTY_SECTION_FALLBACK); }); it("drops a raw body's own # version line (the embed title carries it)", () => { const formatted = formatEntryDiscord("# v9.9.9\n\nA summary.\n\n### Fixes\n\n- A fix.\n"); expect(formatted).not.toContain("v9.9.9"); expect(formatted).toContain("A summary."); expect(formatted).toContain("### Fixes"); }); it("renders the shipped empty v0.8.3 entry from the real file", () => { const content = readFileSync(CHANGELOG_PATH, "utf8"); const resolved = resolveChangelog(content, "0.8.3"); expect(resolved.found).toBe(true); expect(resolved.messages[0]).toContain(EMPTY_SECTION_FALLBACK); expect(resolved.messages[0]).not.toContain("---"); }); }); describe("edit-mode round trip (modal prefill -> post)", () => { it("renders the trimmed raw body exactly like the normal posting path", () => { const prefilled = parseChangelog(SAMPLE)[0].rawBody.trim(); // what the modal shows expect(splitForDiscord(prefilled)).toEqual(resolveChangelog(SAMPLE).messages); }); it("posts last-minute edits through the same renderer", () => { const edited = parseChangelog(SAMPLE)[0].rawBody .trim() .replace("- Fixed a bug.", "- Fixed a bug (hotfix)."); const rendered = splitForDiscord(edited).join("\n\n"); expect(rendered).toContain("Fixed a bug (hotfix)."); expect(rendered).toContain("### Fixes"); expect(rendered).not.toContain("- Fixed a bug.\n"); }); }); 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: the first entry is the highest version. const highest = entries.reduce( (max, entry) => (compareVersions(entry.version, max) > 0 ? entry.version : max), entries[0].version, ); expect(entries[0].version).toBe(highest); 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)); }); }); it("treats 'X..' as X and newer on the shipped file (reported bug)", () => { const content = readFileSync(CHANGELOG_PATH, "utf8"); const newest = parseChangelog(content)[0].version; const batch = resolveChangelogRange(content, "0.6.0", undefined); expect(batch.found).toBe(true); expect(batch.versions[0]).toBe("0.6.0"); expect(batch.versions[batch.versions.length - 1]).toBe(newest); expect(batch.versions.every((version) => compareVersions(version, "0.6.0") >= 0)).toBe(true); }); });