1
0
Fork 0
polaris-task-force/tests/int/changelog.int.spec.ts

452 lines
16 KiB
TypeScript

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,
MAX_AUTOCOMPLETE_CHOICES,
PENDING_TOKEN,
buildRangeChoices,
changelogEmbedTitle,
compareVersions,
formatEntryDiscord,
parseChangelog,
resolveChangelog,
resolveChangelogRange,
resolvePendingChangelog,
splitForDiscord,
versionFromEmbedTitle,
} 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);
});
it("counts the pending versions from a posted watermark", () => {
const content = readFileSync(CHANGELOG_PATH, "utf8");
const entries = parseChangelog(content);
const index = entries.findIndex((entry) => entry.version === "0.15.3");
expect(index).toBeGreaterThan(0);
const batch = resolvePendingChangelog(content, "0.15.3");
expect(batch.found).toBe(true);
// The file is newest-first, so everything newer is the slice above it.
expect(batch.versions).toEqual(
entries
.slice(0, index)
.map((entry) => entry.version)
.reverse(),
);
expect(batch.versions.length).toBe(index);
expect(batch.versions[batch.versions.length - 1]).toBe(entries[0].version);
});
});
describe("changelog embed titles", () => {
it("round-trips a version through the title a post carries", () => {
expect(changelogEmbedTitle("0.21.1")).toBe("Changelog v0.21.1");
expect(versionFromEmbedTitle(changelogEmbedTitle("0.21.1"))).toBe("0.21.1");
});
it("ignores anything that is not an exact changelog title", () => {
// A near miss would move the watermark and silently skip real releases.
expect(versionFromEmbedTitle("Changelog v0.21")).toBeNull();
expect(versionFromEmbedTitle("Changelog v0.21.1 (edited)")).toBeNull();
expect(versionFromEmbedTitle("Releases: Changelog v0.21.1")).toBeNull();
expect(versionFromEmbedTitle(undefined)).toBeNull();
expect(versionFromEmbedTitle(null)).toBeNull();
});
});
describe("resolvePendingChangelog", () => {
const content = [
"# Changelog",
"",
"# v0.4.0",
"",
"Newest.",
"",
"# v0.3.0",
"",
"Middle.",
"",
"# v0.2.0",
"",
"Oldest.",
"",
].join("\n");
it("returns everything newer than the watermark, oldest first", () => {
const batch = resolvePendingChangelog(content, "0.2.0");
expect(batch.found).toBe(true);
expect(batch.versions).toEqual(["0.3.0", "0.4.0"]);
expect(batch.messages).toHaveLength(2);
expect(batch.messages[0].join("\n")).toContain("Middle.");
});
it("accepts a leading v on the watermark", () => {
expect(resolvePendingChangelog(content, "v0.3.0").versions).toEqual(["0.4.0"]);
});
it("reports nothing pending once the newest version is posted", () => {
const batch = resolvePendingChangelog(content, "0.4.0");
expect(batch.found).toBe(true);
expect(batch.versions).toEqual([]);
});
it("refuses an unknown or missing watermark instead of posting the whole file", () => {
expect(resolvePendingChangelog(content, "0.9.9")).toEqual({
found: false,
versions: [],
messages: [],
});
expect(resolvePendingChangelog(content, null)).toEqual({
found: false,
versions: [],
messages: [],
});
expect(resolvePendingChangelog(content, " ")).toEqual({
found: false,
versions: [],
messages: [],
});
});
});
describe("buildRangeChoices", () => {
const content = [
"# v0.4.0",
"",
"Newest.",
"",
"# v0.3.0",
"",
"Middle.",
"",
"# v0.2.0",
"",
"Oldest.",
"",
].join("\n");
it("leads with pending and labels every version by its state", () => {
const choices = buildRangeChoices(content, "", "0.2.0");
expect(choices[0]).toEqual({ name: "pending · v0.3.0 → v0.4.0 (2)", value: PENDING_TOKEN });
expect(choices.slice(1)).toEqual([
{ name: "v0.4.0 · pending", value: "v0.4.0" },
{ name: "v0.3.0 · pending", value: "v0.3.0" },
{ name: "v0.2.0 · posted", value: "v0.2.0" },
]);
});
it("offers no pending entry when the watermark is unknown", () => {
const choices = buildRangeChoices(content, "", null);
expect(choices.some((choice) => choice.value === PENDING_TOKEN)).toBe(false);
expect(choices[0]).toEqual({ name: "v0.4.0", value: "v0.4.0" });
});
it("offers no pending entry when nothing is pending", () => {
const choices = buildRangeChoices(content, "", "0.4.0");
expect(choices.some((choice) => choice.value === PENDING_TOKEN)).toBe(false);
expect(choices[0].name).toBe("v0.4.0 · posted");
});
it("filters on what has been typed, with or without the v", () => {
expect(buildRangeChoices(content, "0.3", "0.2.0")).toEqual([
{ name: "v0.3.0 · pending", value: "v0.3.0" },
]);
expect(buildRangeChoices(content, "v0.2", "0.2.0")).toEqual([
{ name: "v0.2.0 · posted", value: "v0.2.0" },
]);
expect(buildRangeChoices(content, "pend", "0.2.0")).toEqual([
{ name: "pending · v0.3.0 → v0.4.0 (2)", value: PENDING_TOKEN },
]);
});
it("caps the list at Discord's 25-choice limit, pending included", () => {
const many = Array.from({ length: 40 }, (_, i) => `# v0.${39 - i}.0\n\nEntry.\n`).join("\n");
const withPending = buildRangeChoices(many, "", "0.0.0");
expect(withPending).toHaveLength(MAX_AUTOCOMPLETE_CHOICES);
expect(withPending[0]).toEqual({
name: "pending · v0.1.0 → v0.39.0 (39)",
value: PENDING_TOKEN,
});
const plain = buildRangeChoices(many, "", "0.39.0");
expect(plain).toHaveLength(MAX_AUTOCOMPLETE_CHOICES);
expect(plain[0].value).toBe("v0.39.0");
});
});