Skip to content

Open-source, local-first AI agent for real work.

9.1 KB
Raw Blame
import { describe, expect, it } from "vitest";
import {
	parseReleaseNotes,
	releaseImageUrl,
	releaseNoteImages,
	releaseNoteTemplate,
	summarizeReleaseNotes,
	validateReleaseNotes,
} from "./release-notes-format.mjs";

const FOOTER = "**Full Changelog**: https://github.com/openvetta/open-vetta/compare/v0.6.13...v0.6.14";

/** 一份能通过门禁的双语说明;各用例只替换要测的那一段。 */
function note({ en = "- Plugins can now reuse the host's keycaps.", zh = "- 插件可以复用宿主的快捷键键帽。" } = {}) {
	return [
		"# Vetta 0.6.14",
		"<!-- release-status: unreleased -->",
		"",
		"## English",
		"",
		"### New",
		"",
		en,
		"",
		"## 中文",
		"",
		"### 新增",
		"",
		zh,
		"",
		FOOTER,
		"",
	].join("\n");
}

const validate = (text, options = {}) => validateReleaseNotes(text, { version: "0.6.14", previousTag: "v0.6.13", ...options });

describe("parseReleaseNotes", () => {
	it("按语言与小节拆分条目", () => {
		const parsed = parseReleaseNotes(note({ zh: "- 插件可以用宿主的键帽。\n- 设置页的快捷键更好读。" }));
		expect(parsed.version).toBe("0.6.14");
		expect(parsed.hasLanguageHeadings).toBe(true);
		expect(parsed.languages.en.total).toBe(1);
		expect(parsed.languages.zh.total).toBe(2);
		expect(parsed.languages.zh.sections.added).toHaveLength(2);
		expect(parsed.footerUrl).toBe("https://github.com/openvetta/open-vetta/compare/v0.6.13...v0.6.14");
	});

	// 旧文件是单语正文,解析结果为空即可——校验会明确要求改成双语结构。
	it("旧版单语说明不会被误认成双语", () => {
		const parsed = parseReleaseNotes("# Vetta 0.5.70\n<!-- release-status: released 2026-10-29 -->\n\n## 新增\n\n- 一条\n");
		expect(parsed.hasLanguageHeadings).toBe(false);
		expect(parsed.strayHeadings).toEqual([{ level: 2, heading: "新增", line: 4 }]);
	});
});

describe("validateReleaseNotes", () => {
	it("接受定稿好的双语说明", () => {
		expect(validate(note())).toEqual({ problems: [], warnings: [], stats: { en: 1, zh: 1 } });
	});

	it("要求中英两段都在", () => {
		const single = "# Vetta 0.6.14\n<!-- release-status: unreleased -->\n\n## 新增\n\n- 一条\n";
		expect(validate(single).problems.join()).toMatch(/## English.*## 中文/s);
	});

	it("要求两段条数一一对应", () => {
		expect(validate(note({ zh: "- 一条。\n- 两条。" })).problems.join()).toMatch(/中英条目数不一致:English 1 条 \/ 中文 2 条/);
		expect(validate(note({ en: "" })).problems.join()).toMatch(/中英两段都要有条目:English 0 条 \/ 中文 1 条/);
	});

	it("超过条数上限就要求合并成主题", () => {
		const zh = Array.from({ length: 7 }, (_, i) => `- 第 ${i + 1} 件小事。`).join("\n");
		const en = Array.from({ length: 7 }, (_, i) => `- Small change ${i + 1}.`).join("\n");
		expect(validate(note({ zh, en })).problems.join()).toMatch(/中文 有 7 条,超过 6 条上限/);
	});

	it("单条过长说明还没写完", () => {
		const zh = `- ${"很长的一句话。".repeat(20)}`;
		const en = `- ${"A very long sentence. ".repeat(20)}`;
		const problems = validate(note({ zh, en })).problems.join();
		expect(problems).toMatch(/超过 90 字上限/);
		expect(problems).toMatch(/超过 240 字上限/);
	});

	it("拦下用户读不懂的内部标识", () => {
		const problems = validate(
			note({
				zh: "- 重构了快捷键渲染,改了 `release-notes.mjs`,见 #123。",
				en: "- Rewrote the updater refactor path.",
			}),
		).problems.join("\n");
		expect(problems).toMatch(/文件名「`release-notes\.mjs`」/);
		expect(problems).toMatch(/issue \/ PR 编号「#123」/);
		expect(problems).toMatch(/内部术语「重构」/);
		expect(problems).toMatch(/内部术语「refactor」/);
	});

	it("要求文末 Full Changelog 指向上一版到本版", () => {
		const withoutFooter = note().replace(`${FOOTER}\n`, "");
		expect(validate(withoutFooter).problems.join()).toMatch(/缺一行 `\*\*Full Changelog\*\*/);

		const wrongStart = note().replace("compare/v0.6.13...v0.6.14", "compare/v0.6.12...v0.6.14");
		expect(validate(wrongStart).problems.join()).toMatch(/起点应是上一版 v0\.6\.13/);

		const wrongEnd = note().replace("v0.6.14", "v0.6.15");
		expect(validate(wrongEnd).problems.join()).toMatch(/终点应是 v0\.6\.14/);
	});

	it("拒绝未知小节,条目必须落在小节里", () => {
		const unknownSection = note().replace("### 新增", "### 变化");
		expect(validate(unknownSection).problems.join()).toMatch(/不是允许的小节/);

		const strayBullet = note().replace("### 新增\n\n- 插件可以复用宿主的快捷键键帽。", "- 插件可以复用宿主的快捷键键帽。");
		expect(validate(strayBullet).problems.join()).toMatch(/不在任何小节里/);
	});

	it("占位文字仍要拦,但正文里正常使用「占位」不算", () => {
		expect(validate(note({ zh: "- TODO 补一条。", en: "- TODO fill this in." })).problems.join()).toMatch(/占位文字/);
		expect(validate(note({ zh: "- 不是仓库的项目既不占位,也能手动打开面板。", en: "- Non-repository folders still open." })).problems).toEqual([]);
	});
});

describe("summarizeReleaseNotes", () => {
	it("给预检摘要用的一句话", () => {
		expect(summarizeReleaseNotes(note({ zh: "- 一。\n- 二。" }))).toBe("中文 2 条(新增 2)/ English 1 条");
	});

	it("有配图时带上张数", () => {
		const zh = `![介绍图](${releaseImageUrl("0.6.14", "whats-new-zh.webp")})\n\n- 一。`;
		expect(summarizeReleaseNotes(note({ zh }))).toBe("中文 1 条(新增 1)/ English 1 条,配图 1 张");
	});
});

describe("配图", () => {
	const enImage = `![What's new](${releaseImageUrl("0.6.14", "whats-new-en.webp")})`;
	const zhImage = `![本版介绍](${releaseImageUrl("0.6.14", "whats-new-zh.webp")})`;

	it("不带图照常通过:配图是可选的", () => {
		expect(validate(note()).problems).toEqual([]);
		expect(releaseNoteImages(note())).toEqual([]);
	});

	it("中英各配一张,放在段首或条目之间都能通过,且不算进条目数", () => {
		const text = note({
			en: `- Plugins can now reuse the host's keycaps.\n\n${enImage}`,
			zh: `${zhImage}\n\n- 插件可以复用宿主的快捷键键帽。`,
		});
		expect(validate(text).problems).toEqual([]);
		const parsed = parseReleaseNotes(text);
		expect(parsed.languages.en.total).toBe(1);
		expect(parsed.languages.zh.images).toEqual([
			{ alt: "本版介绍", url: releaseImageUrl("0.6.14", "whats-new-zh.webp"), line: expect.any(Number) },
		]);
		expect(releaseNoteImages(text).map((image) => image.name)).toEqual(["whats-new-en.webp", "whats-new-zh.webp"]);
	});

	it("外面包一层链接的图片同样认得", () => {
		const url = releaseImageUrl("0.6.14", "whats-new-zh.webp");
		const text = note({ zh: `[![本版介绍](${url})](${url})\n\n- 插件可以复用宿主的快捷键键帽。` });
		expect(validate(text).problems).toEqual([]);
		expect(releaseNoteImages(text)).toEqual([{ tag: "v0.6.14", name: "whats-new-zh.webp", url }]);
	});

	it("两段共用同一张图只上传一次", () => {
		const shared = `![Vetta 0.6.14](${releaseImageUrl("0.6.14", "banner.webp")})`;
		const text = note({ en: `${shared}\n\n- Plugins can now reuse the host's keycaps.`, zh: `${shared}\n\n- 插件可以复用宿主的快捷键键帽。` });
		expect(releaseNoteImages(text).map((image) => image.name)).toEqual(["banner.webp"]);
	});

	it("地址只能是本版 Release 附件", () => {
		const external = note({ zh: "![介绍](https://example.com/a.webp)\n\n- 插件可以复用宿主的快捷键键帽。" });
		expect(validate(external).problems.join("\n")).toContain("不是本仓库的 Release 附件");

		const otherVersion = note({ zh: `![介绍](${releaseImageUrl("0.6.13", "a.webp")})\n\n- 插件可以复用宿主的快捷键键帽。` });
		expect(validate(otherVersion).problems.join("\n")).toContain("应为本版 v0.6.14 的附件");
	});

	it("要求说明文字,且每段有张数上限", () => {
		const noAlt = note({ zh: `![](${releaseImageUrl("0.6.14", "a.webp")})\n\n- 插件可以复用宿主的快捷键键帽。` });
		expect(validate(noAlt).problems.join("\n")).toContain("缺少说明文字");

		const many = ["a", "b", "c", "d"].map((n) => `![图 ${n}](${releaseImageUrl("0.6.14", `${n}.webp`)})`).join("\n\n");
		const tooMany = note({ zh: `${many}\n\n- 插件可以复用宿主的快捷键键帽。` });
		expect(validate(tooMany).problems.join("\n")).toContain("超过 3 张上限");
	});

	it("语言段之外的图片要拦下", () => {
		const text = note().replace("## English", `${enImage}\n\n## English`);
		expect(validate(text).problems.join("\n")).toContain("不在 `## English` / `## 中文` 段里");
	});
});

describe("releaseNoteTemplate", () => {
	// 骨架只负责结构和 Footer:条目空着,定稿门禁会拦住直接发版。
	it("生成的结构能解析,但没条目时过不了门禁", () => {
		const text = releaseNoteTemplate({ version: "0.6.14", previousVersion: "0.6.13" });
		expect(parseReleaseNotes(text).hasLanguageHeadings).toBe(true);
		expect(validateReleaseNotes(text, { version: "0.6.14", previousTag: "v0.6.13" }).problems.join()).toMatch(/没有任何条目/);
	});
});