返回 CodeWhale
install-guide.test.ts
根目录 / web / lib / install-guide.test.ts
1 import { readFileSync } from "node:fs";
2 import { describe, expect, it } from "vitest";
3 import { buildInstallGuide, installAnchorErrors, renderInstallGuideModule } from "../scripts/install-guide-lib.mjs";
4 import { CHINESE_INSTALL_GUIDE, INSTALL_GUIDE } from "./install-guide.generated";
5
6 describe("canonical installation guide", () => {
7 it("ships both current source guides with their historical release receipts", () => {
8 const source = readFileSync(new URL("../../docs/INSTALL.md", import.meta.url), "utf8");
9 const chineseSource = readFileSync(new URL("../../docs/zh_hans/INSTALL.md", import.meta.url), "utf8");
10 expect(readFileSync(new URL("./install-guide.generated.ts", import.meta.url), "utf8"))
11 .toBe(renderInstallGuideModule(source, chineseSource));
12 expect(INSTALL_GUIDE.chunks.map((chunk) => chunk.text).join("\n"))
13 .toContain("0.10.0");
14 expect(CHINESE_INSTALL_GUIDE.chunks.map((chunk) => chunk.text).join("\n"))
15 .toContain("使用当前源码参与测试与贡献");
16 expect(CHINESE_INSTALL_GUIDE.sourceHash).not.toBe(INSTALL_GUIDE.sourceHash);
17 });
18
19 it("keeps top-level command bytes intact and nested code inside its list", () => {
20 const guide = buildInstallGuide('# Setup\n\n```sh\nprintf "$HOME & <path>"\n```\n\n1. In a list:\n\n ```sh\n echo nested\n ```\n');
21 expect(guide.chunks[1]).toEqual({ kind: "code", text: 'printf "$HOME & <path>"', lang: "sh" });
22 expect(guide.chunks[2].text).toMatch(/<ol>[\s\S]*<li>[\s\S]*<pre><code[\s\S]*echo nested[\s\S]*<\/li>[\s\S]*<\/ol>/);
23 });
24
25 it("resolves relative docs and preserves duplicate-heading and legacy anchors", () => {
26 const guide = buildInstallGuide('# Start\n\n## Notes\n\n## Notes\n\n<a id="legacy"></a>\n\n[Keys](KEYBINDINGS.md) [Notes](INSTALL.md#notes-1) [Old](#legacy)\n');
27 expect(guide.anchors).toEqual(["start", "notes", "notes-1", "legacy"]);
28 expect(guide.chunks[0].text).toContain('href="https://github.com/codewhale-hq/CodeWhale/blob/main/docs/KEYBINDINGS.md"');
29 expect(guide.chunks[0].text).toContain('href="#notes-1"');
30 expect(installAnchorErrors("INSTALL.md#notes-1 INSTALL.md#missing", guide.anchors)).toEqual(["missing"]);
31 });
32
33 it("fails generation on a broken internal link", () => {
34 expect(() => buildInstallGuide("# Setup\n\n[Missing](#gone)"))
35 .toThrow("Missing INSTALL.md anchors: gone");
36 });
37
38 it("keeps Chinese anchors and relative source links in the Chinese document", () => {
39 const guide = buildInstallGuide('# 安装\n\n<a id="通过-cargo-安装"></a>\n\n[镜像](CNB_MIRROR.md) [英文](../INSTALL.md) [源码](INSTALL.md#通过-cargo-安装)\n', { sourcePath: "zh_hans/INSTALL.md" });
40 const html = guide.chunks.map((chunk) => chunk.text).join("\n");
41 expect(guide.anchors).toEqual(["安装", "通过-cargo-安装"]);
42 expect(html).toContain('href="https://github.com/codewhale-hq/CodeWhale/blob/main/docs/zh_hans/CNB_MIRROR.md"');
43 expect(html).toContain('href="https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md"');
44 expect(html).toContain('href="#%E9%80%9A%E8%BF%87-cargo-%E5%AE%89%E8%A3%85"');
45 });
46
47 it("rejects broken Chinese fragments and attributes on a Unicode anchor", () => {
48 expect(() => buildInstallGuide('# 安装\n\n[缺失](#不存在)', { sourcePath: "zh_hans/INSTALL.md" }))
49 .toThrow("Missing INSTALL.md anchors: 不存在");
50 expect(() => buildInstallGuide('<a id="安装" onclick="alert(1)"></a>'))
51 .toThrow("Unsupported raw HTML");
52 });
53
54 it("gives scrollable tables distinct, escaped names from their headers", () => {
55 const table = '| "Need" | Reason |\n| --- | --- |\n| curl | Download |\n';
56 const guide = buildInstallGuide(`# Setup\n\n${table}\n${table}`);
57 const html = guide.chunks.map((chunk) => chunk.text).join("\n");
58 const names = [...html.matchAll(/role="region" aria-label="([^"]+)"/g)].map((match) => match[1]);
59 expect(names).toEqual([
60 "Installation table 1: &quot;Need&quot; / Reason",
61 "Installation table 2: &quot;Need&quot; / Reason",
62 ]);
63 });
64
65 it.each(["javascript:alert(1)", "data:text/html,hello", "file:///tmp/private"])("rejects unsafe URL %s", (href) => {
66 expect(() => buildInstallGuide(`# Setup\n\n[link](${href})`)).toThrow("Unsupported install-guide link protocol");
67 });
68
69 it("rejects arbitrary raw HTML instead of publishing it", () => {
70 expect(() => buildInstallGuide('# Setup\n\n<img src="x" onerror="alert(1)">'))
71 .toThrow("Unsupported raw HTML");
72 });
73 });
74
74 lines TYPESCRIPT