import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; const ROOT = path.resolve(fileURLToPath(new URL("../..", import.meta.url))); const CANONICAL_DOCS = [ "docs/dev-rules/environment-setup.md", "docs/dev-rules/desktop-development.md", "docs/dev-rules/mobile-development.md", ]; const CONTRIBUTING_DOCS = ["CONTRIBUTING.md", "CONTRIBUTING.en.md"]; const CHECKED_DOCS = [...CONTRIBUTING_DOCS, ...CANONICAL_DOCS]; const COMMAND_CHECKED_DOCS = [...CHECKED_DOCS, "apps/desktop/README.md"]; const WORKSPACES = new Map([ ["desktop", "apps/desktop/package.json"], ["mobile", "apps/mobile/package.json"], ]); const PNPM_BUILTINS = new Set(["install", "--version"]); function readText(relativePath) { return fs.readFileSync(path.join(ROOT, relativePath), "utf8"); } function readJson(relativePath) { return JSON.parse(readText(relativePath)); } function readSupportedLocales(relativePath) { const declaration = readText(relativePath).match(/SUPPORTED_LOCALES\s*=\s*\[([^\]]*)\]/s)?.[1]; const locales = declaration ? [...declaration.matchAll(/["']([^"']+)["']/g)].map((match) => match[1]) : []; assert.ok(locales.length, `${relativePath} must declare SUPPORTED_LOCALES`); return locales; } function workflowJob(workflow, jobId) { const lines = workflow.split(/\r?\n/); const start = lines.findIndex((line) => line === ` ${jobId}:`); if (start === -1) return undefined; const endOffset = lines .slice(start + 1) .findIndex((line) => /^ [a-zA-Z0-9_-]+:$/.test(line)); const end = endOffset === -1 ? lines.length : start + 1 + endOffset; return lines.slice(start + 1, end).join("\n"); } function shellLines(relativePath) { const markdown = readText(relativePath); return [...markdown.matchAll(/```(?:bash|sh)?\r?\n([\s\S]*?)```/g)] .flatMap((match) => match[1].split(/\r?\n/)) .map((line) => line.trim()) .filter((line) => line.startsWith("pnpm ")); } function assertPnpmCommandExists(line, rootPackage) { const tokens = line.split(/\s+/); if (tokens[1] === "--filter") { const selector = tokens[2]; const command = tokens[3]; const workspaceManifest = WORKSPACES.get(selector); assert.ok(workspaceManifest, `unknown workspace selector in documented command: ${line}`); const workspacePackage = readJson(workspaceManifest); if (command === "exec") { const binary = tokens[4]; assert.ok( workspacePackage.dependencies?.[binary] || workspacePackage.devDependencies?.[binary], `documented binary '${binary}' is not declared by ${workspaceManifest}: ${line}`, ); return; } assert.ok( workspacePackage.scripts?.[command], `documented script '${command}' is missing from ${workspaceManifest}: ${line}`, ); return; } const command = tokens[1]; if (PNPM_BUILTINS.has(command)) return; assert.ok(rootPackage.scripts?.[command], `documented root script is missing: ${line}`); } /** * 去掉 fenced code block 与 inline code span,再抽链接。顺序不能反 —— fenced block * 内部含反引号,先剥 inline 会把围栏本身吃掉。 * * 这样 `[x](y)` 这类**演示 markdown 语法**的写法不会被当成真链接(design-rules/DESIGN.md * 里有多处);而 [`foo.md`](./foo.md) 这种「链接文本本身是 inline code」的常见写法,剥完 * 变成 [](./foo.md),目标仍在括号里,正则照常命中。 */ function stripCodeSpans(text) { return ( text // 围栏允许带缩进 —— 列表项里的围栏本就是缩进的(docs/design-rules/DESIGN.md // 与 docs/dev-rules/pi-remaining-work.md 各有一处)。只认行首 ``` 会漏掉它们, // 块内的示例链接就会被当成真链接。 .replace(/^[ \t]*```[\s\S]*?^[ \t]*```/gm, "") // inline code 用「等长反引号串」配对,这样内容本身含反引号的写法也能整段剥掉 // (docs/product-rules/task-and-conversation-naming.md 有 `` `${n} 个会话` ``)。 // 刻意不跨行:落单的反引号不应该让后面整篇文档漏检。 .replace(/(`+)(?:(?!\1)[^\n])*?\1/g, "") ); } function localMarkdownLinks(relativePath) { return [...stripCodeSpans(readText(relativePath)).matchAll(/\[[^\]]*\]\(([^)]+)\)/g)] .map((match) => match[1].trim()) .filter((target) => !/^(?:https?:|mailto:|#)/.test(target)); } /** * 需要做内链体检的全部文档:根目录与模块 AGENTS + `docs/` 下所有 markdown(递归)。 * * 覆盖面刻意做全 —— 悬空内链最容易出现在深层目录里:文档被删除时,引用它的索引 * (如 `docs/legal/README.md`、`docs/README.md`)常常漏改。只体检 CHECKED_DOCS 那几篇 * canonical 文档挡不住这类回归。 */ function listLinkCheckedDocs() { // Include newly added, unstaged module rules without traversing node_modules, // runtime binaries or generated build trees. Git is already required by the runner. const agentDocs = execFileSync("git", [ "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "AGENTS.md", ":(glob)apps/**/AGENTS.md", ":(glob)packages/**/AGENTS.md", ], { cwd: ROOT, encoding: "utf8" }).split("\0").filter((relativePath) => relativePath && fs.existsSync(path.join(ROOT, relativePath))); const out = ["AGENTS.md", ...agentDocs, ...CONTRIBUTING_DOCS]; const walk = (relativeDir) => { for (const entry of fs.readdirSync(path.join(ROOT, relativeDir), { withFileTypes: true })) { const relativePath = path.join(relativeDir, entry.name); if (entry.isDirectory()) walk(relativePath); else if (entry.name.endsWith(".md")) out.push(relativePath); } }; walk("docs"); return [...new Set(out)].sort(); } test("developer docs only document pnpm commands that exist", () => { const rootPackage = readJson("package.json"); for (const relativePath of COMMAND_CHECKED_DOCS) { for (const line of shellLines(relativePath)) { assertPnpmCommandExists(line, rootPackage); } } }); test("developer docs do not duplicate canonical command lines", () => { const owners = new Map(); for (const relativePath of CHECKED_DOCS) { for (const line of shellLines(relativePath)) { assert.equal( owners.get(line), undefined, `documented command is duplicated in ${owners.get(line)} and ${relativePath}: ${line}`, ); owners.set(line, relativePath); } } for (const relativePath of CONTRIBUTING_DOCS) { assert.equal(shellLines(relativePath).length, 0, `${relativePath} must link to canonical command docs`); } }); test("AGENTS and CONTRIBUTING route to the canonical developer docs", () => { const agents = readText("AGENTS.md"); const contributingDocs = CONTRIBUTING_DOCS.map(readText); for (const relativePath of CANONICAL_DOCS) { assert.match(agents, new RegExp(relativePath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))); for (const contributing of contributingDocs) { assert.match(contributing, new RegExp(relativePath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))); } } }); test("developer documentation links resolve", () => { for (const relativePath of listLinkCheckedDocs()) { const sourceDir = path.dirname(path.join(ROOT, relativePath)); for (const target of localMarkdownLinks(relativePath)) { const fileTarget = decodeURIComponent(target.split("#", 1)[0]); assert.ok( fs.existsSync(path.resolve(sourceDir, fileTarget)), `broken local link in ${relativePath}: ${target}`, ); } } }); // 规则依据:docs/dev-rules/protocol-compatibility.md「本页与协议记录的分工」。 // 失效后果:逐项协议记录重新堆进必读页,通用规则与 Review 清单被埋掉——拆分前该页 // 555 KB、194 节,文档抬头在第 1290 行,Review 清单在第 3967 行,没有 Agent 能先读完再动手。 test("protocol compatibility keeps rules on the front page and one record per file", () => { const frontPage = "docs/dev-rules/protocol-compatibility.md"; const recordsDir = "docs/dev-rules/protocol-compatibility"; const frontPageMaxBytes = 16 * 1024; const frontPageBytes = Buffer.byteLength(readText(frontPage)); assert.ok( frontPageBytes <= frontPageMaxBytes, `${frontPage} is the must-read rules page (${frontPageBytes} bytes > ${frontPageMaxBytes}). ` + `Put per-capability protocol records in ${recordsDir}/-.md instead of appending here.`, ); const records = fs.readdirSync(path.join(ROOT, recordsDir)); assert.ok(records.length, `${recordsDir} must hold the protocol records`); for (const name of records) { const relativePath = `${recordsDir}/${name}`; assert.match( name, /^[a-z0-9]+(?:-[a-z0-9]+)*\.md$/, `${relativePath}: record files are flat, lower-case -.md`, ); const text = readText(relativePath); const titles = text.replace(/^[ \t]*```[\s\S]*?^[ \t]*```/gm, "").match(/^# .+$/gm) ?? []; assert.ok( text.startsWith("# ") && titles.length === 1, `${relativePath}: one record per file, starting with its level-1 title`, ); } }); test("login-all-hifi embeds generated truth as a script-safe static literal", () => { const html = readText("docs/design-previews/login-all-hifi/index.html"); const match = html.match(/