/** * Post-render assertions for docs/current/tools-manual/index.html. * * Everything here is checked against the freshly built model + HTML string, so * `--check` (byte compare) and `--verify` (semantic) stay independent: a stale * file passes --verify and fails --check, which is the intended split. */ import fs from "node:fs"; import path from "node:path"; import { CATEGORIES } from "./config.mjs"; const SIZE_FAIL = 3 * 1024 * 1024; const SIZE_WARN = 1.5 * 1024 * 1024; function collectIds(html) { const ids = []; for (const match of html.matchAll(/\sid="([^"]+)"/g)) ids.push(match[1]); return ids; } function readPackageScripts(root, rel) { try { const pkg = JSON.parse(fs.readFileSync(path.join(root, rel), "utf8")); return pkg.scripts ?? {}; } catch { return {}; } } /** Extract `window.__SEARCH_INDEX__ = [ ... ];` without eval. */ function readSearchIndex(html) { const marker = "window.__SEARCH_INDEX__ = "; const start = html.indexOf(marker); if (start < 0) return null; const end = html.indexOf(";", start); if (end < 0) return null; try { return JSON.parse(html.slice(start + marker.length, end)); } catch { return null; } } export function verify({ root, html, model }) { const errors = []; const warnings = []; const fail = (message) => errors.push(message); const warn = (message) => warnings.push(message); // 1. Every documented path must exist on disk. for (const chapter of model.chapters) { if (!fs.existsSync(path.join(root, chapter.path))) fail(`章节路径不存在:${chapter.path}`); } for (const directory of model.directories) { if (!fs.existsSync(path.join(root, directory.relPath))) fail(`目录记录不存在:${directory.relPath}`); } // 2. Anchors: present, unique, and reachable from the search index. const ids = collectIds(html); const idSet = new Set(ids); const duplicates = [...new Set(ids.filter((id, index) => ids.indexOf(id) !== index))]; if (duplicates.length) fail(`锚点重复:${duplicates.slice(0, 8).join(", ")}`); const expectedAnchors = [ ...model.chapters.map((chapter) => chapter.anchor), ...model.families.map((family) => family.anchor), ...model.records.filter((record) => !record.familyId).map((record) => record.anchor), ...model.directories.map((directory) => directory.anchor), ]; const missing = expectedAnchors.filter((anchor) => !idSet.has(anchor)); if (missing.length) fail(`缺少锚点 ${missing.length} 个,例如:${missing.slice(0, 8).join(", ")}`); for (const section of ["overview", "quickstart", "contracts", "topology", "core", "inventory", "npm", "env", "troubleshooting", "maintenance", "appendix"]) { if (!idSet.has(section)) fail(`缺少章节锚点:#${section}`); } // 3. Counts must agree between scan, model and markup. const rowCount = (html.match(/data-cat="/g) ?? []).length; if (rowCount !== model.stats.rows) fail(`索引行数 ${rowCount} 与模型统计 ${model.stats.rows} 不一致`); if (model.chapters.length < 30) fail(`手写章节仅 ${model.chapters.length} 个,低于计划下限 30`); if (model.stats.files < 1000) warn(`扫描文件数仅 ${model.stats.files},检查 SCAN_ROOTS 是否漏目录`); for (const category of CATEGORIES) { const declared = model.stats.byCategory?.[category.id] ?? 0; if (!declared) continue; const rendered = (html.match(new RegExp(`data-cat="${category.id}"`, "g")) ?? []).length; if (rendered !== declared) fail(`分类 ${category.id} 行数 ${rendered} 与统计 ${declared} 不一致`); } // 4. Search index coverage: every file name must be findable by substring. const index = readSearchIndex(html); if (!index) { fail("未找到或无法解析 window.__SEARCH_INDEX__"); } else { const minimum = model.chapters.length + model.families.length + model.records.filter((record) => !record.familyId).length + model.directories.length; if (index.length < minimum) fail(`搜索索引 ${index.length} 条,少于应有 ${minimum} 条`); const hrefs = new Set(index.map((entry) => String(entry[1]).slice(1))); const unreachable = model.chapters.filter((chapter) => !hrefs.has(chapter.anchor)); if (unreachable.length) fail(`章节未进入搜索索引:${unreachable.slice(0, 5).map((c) => c.id).join(", ")}`); for (const entry of index) { if (!Array.isArray(entry) || entry.length < 3) { fail("搜索索引条目结构异常(应为 [标题, 链接, 分类, 关键词])"); break; } } } // 5. Self-containment: no external or linked assets. for (const [pattern, label] of [ [/]+\ssrc=/i, "外部