Skip to content

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

7.8 KB
Raw Blame
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import {
	checkReleaseNotes,
	desktopVersion,
	inspectReleaseImages,
	markReleased,
	parseReleaseStatus,
	releaseNotesPath,
	requireReleaseNotes,
	requireUnreleasedNotes,
	uploadReleaseImages,
} from "./release-notes.mjs";

const roots = [];

function fixtureRoot(version, notesVersions = [], body = (v) => `# Vetta ${v}\n`) {
	const root = mkdtempSync(join(tmpdir(), "vetta-release-notes-"));
	roots.push(root);
	mkdirSync(join(root, "apps/desktop"), { recursive: true });
	writeFileSync(join(root, "apps/desktop/package.json"), JSON.stringify({ version }));
	mkdirSync(join(root, ".github/release-notes"), { recursive: true });
	for (const notesVersion of notesVersions) {
		writeFileSync(join(root, ".github/release-notes", `v${notesVersion}.md`), body(notesVersion));
	}
	return root;
}

afterEach(() => {
	for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
});

describe("release notes addressing", () => {
	it("maps a version to its release notes file", () => {
		expect(releaseNotesPath("0.5.58")).toBe(".github/release-notes/v0.5.58.md");
	});

	it("rejects a version that is not semver", () => {
		expect(() => releaseNotesPath("0.5")).toThrow(/Invalid release version/);
		expect(() => releaseNotesPath("v0.5.58")).toThrow(/Invalid release version/);
	});

	it("resolves the desktop version from the package manifest", () => {
		expect(desktopVersion(fixtureRoot("0.5.58"))).toBe("0.5.58");
	});

	it("returns the path when the notes exist", () => {
		const root = fixtureRoot("0.5.58", ["0.5.58"]);
		expect(requireReleaseNotes("0.5.58", root)).toBe(".github/release-notes/v0.5.58.md");
	});

	// 缺发布说明必须拦住发版:它就是 Release 的正文,补不上就没有正文可发。
	it("explains how to fix a missing release note", () => {
		const root = fixtureRoot("0.5.59", ["0.5.58"]);
		expect(() => requireReleaseNotes("0.5.59", root)).toThrow(/Missing release notes for v0\.5\.59/);
		expect(() => requireReleaseNotes("0.5.59", root)).toThrow(/release-notes\/README\.md/);
	});

	it("ships release notes for the version this repository is about to release", () => {
		expect(requireReleaseNotes(desktopVersion())).toMatch(/^\.github\/release-notes\/v\d+\.\d+\.\d+\.md$/);
	});
});

describe("release notes finalisation gate", () => {
	const READY = [
		"# Vetta 0.5.70",
		"<!-- release-status: unreleased -->",
		"",
		"## English",
		"",
		"### New",
		"",
		"- A keycap component.",
		"",
		"## 中文",
		"",
		"### 新增",
		"",
		"- 新的键帽组件。",
		"",
		"**Full Changelog**: https://github.com/openvetta/open-vetta/compare/v0.5.69...v0.5.70",
		"",
	].join("\n");

	it("accepts a bilingual note that has been finalised", () => {
		expect(checkReleaseNotes("0.5.70", fixtureRoot("0.5.70", ["0.5.70"], () => READY))).toBe(".github/release-notes/v0.5.70.md");
	});

	// 定稿门禁就是流水线 quality 阶段跑的同一个函数:这里失败,发版会拦在签名之前。
	it("explains what is still missing in a single-language note", () => {
		const root = fixtureRoot("0.5.70", ["0.5.70"], () => "# Vetta 0.5.70\n<!-- release-status: unreleased -->\n\n## 新增\n\n- 一条\n");
		expect(() => checkReleaseNotes("0.5.70", root)).toThrow(/发布说明还没定稿/);
		expect(() => checkReleaseNotes("0.5.70", root)).toThrow(/## English/);
	});

	it("still refuses a released note", () => {
		const root = fixtureRoot("0.5.70", ["0.5.70"], () => READY.replace("unreleased", "released 2026-11-02"));
		expect(() => checkReleaseNotes("0.5.70", root)).toThrow(/已于 2026-11-02 发布/);
	});
});

describe("release status marker", () => {
	const unreleased = (v) => `# Vetta ${v}\n<!-- release-status: unreleased -->\n\n## 新增\n\n- 一条\n`;

	it("reads the three states", () => {
		expect(parseReleaseStatus("# Vetta 0.5.70\n<!-- release-status: unreleased -->\n")).toEqual({ state: "unreleased", date: null });
		expect(parseReleaseStatus("# Vetta 0.5.69\n<!-- release-status: released 2026-10-29 -->\n")).toEqual({
			state: "released",
			date: "2026-10-29",
		});
		expect(parseReleaseStatus("# Vetta 0.5.60\n")).toEqual({ state: "unmarked", date: null });
	});

	// 旧文件没有标记:当作已发布,宁可拦住发版,也不让人往已发布的说明里续写。
	it("refuses to release a version that is released or unmarked", () => {
		const unmarked = fixtureRoot("0.5.60", ["0.5.60"]);
		expect(() => requireUnreleasedNotes("0.5.60", unmarked)).toThrow(/没有 release-status 标记/);
		const released = fixtureRoot("0.5.69", ["0.5.69"], (v) => `# Vetta ${v}\n<!-- release-status: released 2026-10-29 -->\n`);
		expect(() => requireUnreleasedNotes("0.5.69", released)).toThrow(/已于 2026-10-29 发布/);
	});

	it("marks an unreleased version as released, once", () => {
		const root = fixtureRoot("0.5.70", ["0.5.70"], unreleased);
		markReleased("0.5.70", "2026-11-02", root);
		const text = readFileSync(join(root, ".github/release-notes/v0.5.70.md"), "utf8");
		expect(text).toContain("<!-- release-status: released 2026-11-02 -->");
		expect(text).toContain("- 一条");
		expect(() => markReleased("0.5.70", "2026-11-03", root)).toThrow(/已于 2026-11-02 发布/);
		expect(() => markReleased("0.5.70", "11/03", root)).toThrow(/Invalid release date/);
	});
});

describe("release note images", () => {
	const url = (name) => `https://github.com/openvetta/open-vetta/releases/download/v0.6.34/${name}`;
	const withImages = (names) => (v) =>
		[`# Vetta ${v}`, "<!-- release-status: unreleased -->", "", "## 中文", "", ...names.map((n) => `![介绍](${url(n)})`), ""].join("\n");
	const stage = (root, name, bytes) => {
		mkdirSync(join(root, ".github/release-notes/images/v0.6.34"), { recursive: true });
		writeFileSync(join(root, ".github/release-notes/images/v0.6.34", name), Buffer.alloc(bytes));
	};

	it("没有配图时什么都不传", async () => {
		const root = fixtureRoot("0.6.34", ["0.6.34"], withImages([]));
		const calls = [];
		await expect(uploadReleaseImages("0.6.34", { root, github: (args) => calls.push(args) })).resolves.toEqual([]);
		expect(calls).toEqual([]);
	});

	it("引用的图片本地缺失或过大都要报出来", () => {
		const root = fixtureRoot("0.6.34", ["0.6.34"], withImages(["a.webp", "b.webp"]));
		stage(root, "b.webp", 2 * 1024 * 1024);
		const { problems } = inspectReleaseImages("0.6.34", root);
		expect(problems.join("\n")).toContain("本地没有 .github/release-notes/images/v0.6.34/a.webp");
		expect(problems.join("\n")).toContain("超过 1024KB");
	});

	it("上传到本版 Release 并逐个确认能打开", async () => {
		const root = fixtureRoot("0.6.34", ["0.6.34"], withImages(["whats-new-en.webp", "whats-new-zh.webp"]));
		stage(root, "whats-new-en.webp", 100);
		stage(root, "whats-new-zh.webp", 100);
		const calls = [];
		const checked = [];
		const images = await uploadReleaseImages("0.6.34", {
			root,
			github: (args) => calls.push(args),
			reachable: async (u) => (checked.push(u), true),
		});
		expect(images.map((image) => image.name)).toEqual(["whats-new-en.webp", "whats-new-zh.webp"]);
		expect(calls).toHaveLength(1);
		expect(calls[0].slice(0, 2)).toEqual(["release", "upload"]);
		expect(calls[0][2]).toBe("v0.6.34");
		expect(calls[0].at(-1)).toBe("--clobber");
		expect(checked).toEqual([url("whats-new-en.webp"), url("whats-new-zh.webp")]);
	});

	it("上传后打不开就失败,不当作完成", async () => {
		const root = fixtureRoot("0.6.34", ["0.6.34"], withImages(["a.webp"]));
		stage(root, "a.webp", 100);
		await expect(
			uploadReleaseImages("0.6.34", { root, github: () => {}, reachable: async () => false }),
		).rejects.toThrow(/上传后仍打不开/);
	});
});