Static scan of tools/ and web/tools/ (1691 files, no tool execution):
purpose/usage/env extraction with provenance badges, family folding
(67 families covering 953 files), 16 directory records, 42 hand-written
chapters plus 4 environment contracts, and a 1996-entry search index.
- tools/tools-manual/{config,scan,extract,chapters,render,verify}.mjs
- tools/build-tools-manual.mjs (--check/--verify/--stats)
- tools/check-tools-manual.mjs (Playwright, desktop + mobile)
- docs/current/tools-manual/index.html (1.08 MB, self-contained)
- web/package.json: manual:tools, check:tools-manual
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
150 lines
6.2 KiB
JavaScript
150 lines
6.2 KiB
JavaScript
/**
|
||
* 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(";</script>", 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 [
|
||
[/<script[^>]+\ssrc=/i, "外部 <script src>"],
|
||
[/<link[^>]+rel=["']?stylesheet/i, "外部样式表 <link rel=stylesheet>"],
|
||
[/@import/i, "CSS @import"],
|
||
[/https?:\/\/(?!(?:localhost|127\.0\.0\.1)\b)/i, "http(s):// 外部引用"],
|
||
[/<img\b/i, "图片引用"],
|
||
]) {
|
||
if (pattern.test(html)) fail(`页面不是自包含:含${label}`);
|
||
}
|
||
|
||
// 6. Template leakage.
|
||
for (const [pattern, label] of [
|
||
[/\[object Object\]/, "[object Object]"],
|
||
[/\bundefined\b/, "undefined"],
|
||
[/\bNaN\b/, "NaN"],
|
||
[/\$\{/, "未替换的模板占位符 ${"],
|
||
]) {
|
||
if (pattern.test(html)) fail(`输出含模板泄漏:${label}`);
|
||
}
|
||
|
||
// 7. npm table rows must exist in a real package.json.
|
||
const rootScripts = readPackageScripts(root, "package.json");
|
||
const webScripts = readPackageScripts(root, "web/package.json");
|
||
for (const entry of model.npmCatalog) {
|
||
const table = entry.cwd === "web" ? webScripts : rootScripts;
|
||
if (!Object.prototype.hasOwnProperty.call(table, entry.name)) fail(`npm 脚本不存在:${entry.cwd}/package.json → ${entry.name}`);
|
||
}
|
||
|
||
// 8. Size budget.
|
||
const bytes = Buffer.byteLength(html);
|
||
if (bytes > SIZE_FAIL) fail(`页面体积 ${(bytes / 1048576).toFixed(2)} MB,超过 3 MB 上限`);
|
||
else if (bytes > SIZE_WARN) warn(`页面体积 ${(bytes / 1048576).toFixed(2)} MB,超过 1.5 MB 建议值`);
|
||
|
||
// 9. Environment dictionary sanity: every known variable should be described.
|
||
for (const name of model.envUsage.keys()) {
|
||
if (!/^[A-Z][A-Z0-9_]*$/.test(name)) warn(`环境变量名可疑:${name}`);
|
||
}
|
||
|
||
return { ok: errors.length === 0, errors, warnings, bytes, rows: rowCount, searchEntries: index?.length ?? 0 };
|
||
}
|