@walvka-code/proj-doc-builder 4.0.0 → 4.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +290 -272
- package/README.zh-CN.md +258 -240
- package/package.json +6 -2
- package/skills/proj-doc-builder/SKILL.md +97 -35
- package/skills/proj-doc-builder/references/audit_script.md +103 -96
- package/skills/proj-doc-builder/references/batch-generation.md +55 -0
- package/skills/proj-doc-builder/references/checklist.md +20 -10
- package/skills/proj-doc-builder/references/exclusions.md +1 -1
- package/skills/proj-doc-builder/references/linking.md +83 -83
- package/skills/proj-doc-builder/references/templates.md +57 -17
- package/skills/proj-doc-builder/tests/claims.json +76 -0
- package/skills/proj-doc-builder/tests/conftest.py +17 -5
- package/skills/proj-doc-builder/tests/test_claims_registry.py +81 -0
- package/skills/proj-doc-builder/tests/test_e2e_fresh_project.py +156 -0
- package/skills/proj-doc-builder/tests/test_load_docs.py +353 -248
- package/skills/proj-doc-builder/tests/test_project_docs_contract.py +32 -1
- package/skills/proj-doc-builder/tests/test_scan_dirs.py +1 -1
- package/skills/proj-doc-builder/tests/test_stage1_stage2_upgrades.py +395 -0
- package/skills/proj-doc-builder/tests/test_sync_project_docs.py +2169 -2169
- package/skills/proj-doc-builder/tests/test_v41_upgrades.py +614 -0
- package/skills/proj-doc-builder/tests/test_v42_upgrades.py +422 -0
- package/skills/proj-doc-builder/tests/test_v43_upgrades.py +198 -0
- package/skills/proj-doc-builder/tests/test_v44_upgrades.py +361 -0
- package/skills/proj-doc-builder/tests/test_v45_upgrades.py +212 -0
- package/skills/proj-doc-builder/tests/test_validate_doc.py +172 -15
- package/skills/proj-doc-builder/tools/_paths.py +39 -0
- package/skills/proj-doc-builder/tools/{claude_doc_index.py → core/claude_doc_index.py} +504 -298
- package/skills/proj-doc-builder/tools/{compute_link.py → core/compute_link.py} +239 -233
- package/skills/proj-doc-builder/tools/core/proj_doc_config.py +460 -0
- package/skills/proj-doc-builder/tools/{project_docs_contract.py → core/project_docs_contract.py} +460 -53
- package/skills/proj-doc-builder/tools/phase4/__init__.py +31 -6
- package/skills/proj-doc-builder/tools/phase4/cas_storage.py +11 -4
- package/skills/proj-doc-builder/tools/phase4/engine.py +17 -2
- package/skills/proj-doc-builder/tools/phase4/journal.py +15 -8
- package/skills/proj-doc-builder/tools/phase4/platform_handles.py +12 -5
- package/skills/proj-doc-builder/tools/phase4_exit.py +153 -47
- package/skills/proj-doc-builder/tools/pipeline/gen_docs_from_scaffold.py +789 -0
- package/skills/proj-doc-builder/tools/{sync_project_docs.py → pipeline/sync_project_docs.py} +5531 -3639
- package/skills/proj-doc-builder/tools/{load_docs.py → reader/load_docs.py} +654 -380
- package/skills/proj-doc-builder/tools/{link_check.py → scan/link_check.py} +203 -152
- package/skills/proj-doc-builder/tools/{normalize_markers.py → scan/normalize_markers.py} +211 -205
- package/skills/proj-doc-builder/tools/{scan_dirs.py → scan/scan_dirs.py} +2735 -2405
- package/skills/proj-doc-builder/tools/{validate_doc.py → validate/validate_doc.py} +1911 -1299
- package/skills/proj-doc-builder/tests/__pycache__/conftest.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_claude_doc_index.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_link_tools.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_load_docs.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_normalize_markers.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_phase4_exit.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_project_docs_contract.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_scan_dirs.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tests/__pycache__/test_sync_project_docs.cpython-314-pytest-9.0.3.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/claude_doc_index.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/compute_link.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/link_check.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/load_docs.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/normalize_markers.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/phase4_exit.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/project_docs_contract.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/scan_dirs.cpython-314.pyc +0 -0
- package/skills/proj-doc-builder/tools/__pycache__/sync_project_docs.cpython-314.pyc +0 -0
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Comprehensive tests for v4.1 upgrades (all 20 items from real-world feedback)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import pytest
|
|
11
|
+
|
|
12
|
+
import claude_doc_index
|
|
13
|
+
import proj_doc_config
|
|
14
|
+
import project_docs_contract as contract
|
|
15
|
+
import scan_dirs
|
|
16
|
+
import sync_project_docs
|
|
17
|
+
import validate_doc
|
|
18
|
+
|
|
19
|
+
SYNCED = "2026-11-27T12:00:00+00:00"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _write(path: Path, text: str) -> Path:
|
|
23
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
24
|
+
path.write_text(text, encoding="utf-8")
|
|
25
|
+
return path
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _make_valid_claude_md() -> str:
|
|
29
|
+
sections = [
|
|
30
|
+
("doc-tree-instruction", "## 文档树索引\n\n- [index.md](project_docs/index.md)\n"),
|
|
31
|
+
("project-overview", "## 项目概览\n\n测试项目。\n"),
|
|
32
|
+
("common-commands", "## 常用命令\n\n```bash\npytest\n```\n"),
|
|
33
|
+
("architecture", "## 架构说明\n\n核心模块。\n"),
|
|
34
|
+
("test-structure", "## 测试结构\n\n单元测试。\n"),
|
|
35
|
+
("dependencies", "## 依赖关系\n\n无额外依赖。\n"),
|
|
36
|
+
]
|
|
37
|
+
parts = ["# CLAUDE.md\n"]
|
|
38
|
+
for name, body in sections:
|
|
39
|
+
parts.append(f"<!-- AUTO-GENERATED v3.0: {name} START -->\n{body}<!-- AUTO-GENERATED v3.0: {name} END -->\n")
|
|
40
|
+
return "\n".join(parts)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _make_minimal_valid_doc(
|
|
44
|
+
title: str,
|
|
45
|
+
*,
|
|
46
|
+
role_text: str = "本模块 `alpha.py` 属于核心计算组件,上游依赖 `config.py` 输入配置,下游输出给调用方。",
|
|
47
|
+
files_rows: list[tuple[str, str]] | None = None,
|
|
48
|
+
subdir_rows: list[tuple[str, str]] | None = None,
|
|
49
|
+
symbol_rows: list[tuple[str, str, str]] | None = None,
|
|
50
|
+
extra_links: str = "",
|
|
51
|
+
) -> str:
|
|
52
|
+
lines = [
|
|
53
|
+
f"# {title} — 模块说明",
|
|
54
|
+
"",
|
|
55
|
+
"## 系统角色",
|
|
56
|
+
"",
|
|
57
|
+
role_text,
|
|
58
|
+
"",
|
|
59
|
+
"## 文件清单",
|
|
60
|
+
"",
|
|
61
|
+
"| 文件 | 说明 |",
|
|
62
|
+
"|------|------|",
|
|
63
|
+
]
|
|
64
|
+
for fn, desc in files_rows or [("alpha.py", "核心逻辑实现")]:
|
|
65
|
+
lines.append(f"| `{fn}` | {desc} |")
|
|
66
|
+
if subdir_rows is not None:
|
|
67
|
+
lines.extend(["", "## 子目录", "", "| 目录 | 说明 |", "|------|------|"])
|
|
68
|
+
for sub, desc in subdir_rows:
|
|
69
|
+
lines.append(f"| [{sub}]({sub}.md) | {desc} |")
|
|
70
|
+
if symbol_rows is not None:
|
|
71
|
+
lines.extend(
|
|
72
|
+
[
|
|
73
|
+
"",
|
|
74
|
+
"## 关键符号",
|
|
75
|
+
"",
|
|
76
|
+
"| 符号 | 位置 | 说明 |",
|
|
77
|
+
"|------|------|------|",
|
|
78
|
+
]
|
|
79
|
+
)
|
|
80
|
+
for sym, loc, desc in symbol_rows:
|
|
81
|
+
lines.append(f"| `{sym}` | `{loc}` | {desc} |")
|
|
82
|
+
lines.extend(
|
|
83
|
+
[
|
|
84
|
+
"",
|
|
85
|
+
"## 依赖关系",
|
|
86
|
+
"",
|
|
87
|
+
"- 上游:`config.py`",
|
|
88
|
+
"- 下游:上层调用方",
|
|
89
|
+
]
|
|
90
|
+
)
|
|
91
|
+
if extra_links:
|
|
92
|
+
lines.extend(["", extra_links])
|
|
93
|
+
lines.extend(["", f"<!-- Last synced: {SYNCED} by proj-doc-builder v4.1 -->", ""])
|
|
94
|
+
return "\n".join(lines)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
# ---------------------------------------------------------------------------
|
|
98
|
+
# #1: --init one-shot initialization with heuristic exclusion suggestions
|
|
99
|
+
# ---------------------------------------------------------------------------
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_init_project_creates_docs_and_heuristic_config(tmp_path: Path) -> None:
|
|
103
|
+
_write(tmp_path / "src" / "app.py", "def run():\n return 1\n")
|
|
104
|
+
_write(tmp_path / "work" / "reports" / "r1.json", "{}\n")
|
|
105
|
+
# >100 direct files
|
|
106
|
+
big_dir = tmp_path / "bulk_assets"
|
|
107
|
+
big_dir.mkdir()
|
|
108
|
+
for i in range(105):
|
|
109
|
+
(big_dir / f"item_{i:03d}.dat").write_text("x", encoding="utf-8")
|
|
110
|
+
# High data ratio (>=80% .json/.md) with no code
|
|
111
|
+
data_dir = tmp_path / "session_dumps"
|
|
112
|
+
data_dir.mkdir()
|
|
113
|
+
for i in range(12):
|
|
114
|
+
(data_dir / f"dump_{i:02d}.json").write_text("{}", encoding="utf-8")
|
|
115
|
+
|
|
116
|
+
result, exit_code = sync_project_docs.init_project(tmp_path)
|
|
117
|
+
assert exit_code == sync_project_docs.EXIT_OK
|
|
118
|
+
assert result["ok"] is True
|
|
119
|
+
assert result["project_docs_created"] is True
|
|
120
|
+
assert result["config_created"] is True
|
|
121
|
+
assert (tmp_path / "project_docs").is_dir()
|
|
122
|
+
cfg_text = (tmp_path / ".projdoc.toml").read_text(encoding="utf-8")
|
|
123
|
+
suggested_paths = {item["path"] for item in result["suggested_excludes"]}
|
|
124
|
+
assert "work/reports" in suggested_paths
|
|
125
|
+
assert "bulk_assets" in suggested_paths
|
|
126
|
+
assert "session_dumps" in suggested_paths
|
|
127
|
+
assert "src" not in suggested_paths
|
|
128
|
+
assert 'strict_config = false' in cfg_text
|
|
129
|
+
|
|
130
|
+
# Re-running --init is idempotent and preserves existing .projdoc.toml
|
|
131
|
+
result2, exit_code2 = sync_project_docs.init_project(tmp_path)
|
|
132
|
+
assert exit_code2 == sync_project_docs.EXIT_OK
|
|
133
|
+
assert result2["project_docs_created"] is False
|
|
134
|
+
assert result2["config_created"] is False
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def test_init_project_creates_the_six_marker_claude_md(tmp_path: Path) -> None:
|
|
138
|
+
"""``--init`` must leave the project ready for a dry-run.
|
|
139
|
+
|
|
140
|
+
Without a root CLAUDE.md the very next ``sync_project_docs.py --dry-run`` fails
|
|
141
|
+
with "root CLAUDE.md cannot be resolved", so init has to scaffold it.
|
|
142
|
+
"""
|
|
143
|
+
_write(tmp_path / "src" / "app.py", "def run():\n return 1\n")
|
|
144
|
+
|
|
145
|
+
result, exit_code = sync_project_docs.init_project(tmp_path)
|
|
146
|
+
assert exit_code == sync_project_docs.EXIT_OK
|
|
147
|
+
assert result["claude_md_created"] is True
|
|
148
|
+
claude_path = tmp_path / "CLAUDE.md"
|
|
149
|
+
assert claude_path.is_file()
|
|
150
|
+
text = claude_path.read_text(encoding="utf-8")
|
|
151
|
+
for section in claude_doc_index.REQUIRED_SECTIONS:
|
|
152
|
+
assert claude_doc_index.section_start(section) in text
|
|
153
|
+
assert claude_doc_index.section_end(section) in text
|
|
154
|
+
# The skeleton must satisfy the index writer's own contract.
|
|
155
|
+
claude_doc_index.validate_required_sections(text)
|
|
156
|
+
|
|
157
|
+
# A second run preserves the file, including anything the user appended.
|
|
158
|
+
claude_path.write_text(text + "\n用户补充\n", encoding="utf-8")
|
|
159
|
+
result2, exit_code2 = sync_project_docs.init_project(tmp_path)
|
|
160
|
+
assert exit_code2 == sync_project_docs.EXIT_OK
|
|
161
|
+
assert result2["claude_md_created"] is False
|
|
162
|
+
assert "用户补充" in claude_path.read_text(encoding="utf-8")
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# ---------------------------------------------------------------------------
|
|
166
|
+
# #4 & #5: recommended_profile alignment & validator-aligned scaffold inventory
|
|
167
|
+
# ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_placeholder_directory_alignment_and_scaffold_inventory(tmp_path: Path) -> None:
|
|
171
|
+
# 0 files + 1 subdir => standard (NOT placeholder), matching validate_doc
|
|
172
|
+
assert proj_doc_config.is_placeholder_directory([], ["child"]) is False
|
|
173
|
+
assert proj_doc_config.is_placeholder_directory(["__init__.py"], ["child"]) is False
|
|
174
|
+
assert proj_doc_config.is_placeholder_directory([], []) is True
|
|
175
|
+
assert proj_doc_config.is_placeholder_directory(["__init__.py"], []) is True
|
|
176
|
+
|
|
177
|
+
_write(tmp_path / "CLAUDE.md", _make_valid_claude_md())
|
|
178
|
+
_write(tmp_path / "standard_data" / "NIST" / "sha2" / "vec.txt", "abc\n")
|
|
179
|
+
_write(tmp_path / "standard_data" / "NIST" / ".hidden_meta", "secret\n")
|
|
180
|
+
_write(tmp_path / "empty_pkg" / "__init__.py", "")
|
|
181
|
+
(tmp_path / "project_docs").mkdir()
|
|
182
|
+
|
|
183
|
+
report, _ = sync_project_docs.synchronize_project(tmp_path, apply=False)
|
|
184
|
+
scaffold_by_path = {
|
|
185
|
+
item["path"]: item for item in report["scan"]["worklist"]["scaffold"]
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
nist = scaffold_by_path["standard_data/NIST"]
|
|
189
|
+
assert nist["recommended_profile"] == "standard"
|
|
190
|
+
assert nist["profile_basis"]["subdirs"] == 1
|
|
191
|
+
assert nist["inventory_files"] == []
|
|
192
|
+
assert ".hidden_meta" not in nist["inventory_files"]
|
|
193
|
+
assert nist["inventory_subdirs"] == ["sha2"]
|
|
194
|
+
|
|
195
|
+
empty_pkg = scaffold_by_path["empty_pkg"]
|
|
196
|
+
assert empty_pkg["recommended_profile"] == "placeholder"
|
|
197
|
+
assert empty_pkg["profile_basis"]["has_init_py"] is True
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# ---------------------------------------------------------------------------
|
|
201
|
+
# #6: boundary directory skips check_4_subdir_table
|
|
202
|
+
# ---------------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_boundary_directory_skips_check_4(tmp_path: Path) -> None:
|
|
206
|
+
_write(tmp_path / "CLAUDE.md", "# Root\n")
|
|
207
|
+
_write(tmp_path / "firmware" / "CLAUDE.md", "# Subproject boundary\n")
|
|
208
|
+
_write(tmp_path / "firmware" / "main.c", "int main(void) { return 0; }\n")
|
|
209
|
+
_write(tmp_path / "firmware" / "drivers" / "uart.c", "void uart(void) {}\n")
|
|
210
|
+
|
|
211
|
+
doc = _write(
|
|
212
|
+
tmp_path / "project_docs" / "firmware.md",
|
|
213
|
+
_make_minimal_valid_doc(
|
|
214
|
+
"firmware",
|
|
215
|
+
role_text="本模块 `main.c` 为芯片固件边界子项目入口,上游接收构建系统参数,下游驱动硬件外设。",
|
|
216
|
+
files_rows=[("CLAUDE.md", "子项目入口说明"), ("main.c", "固件入口")],
|
|
217
|
+
subdir_rows=None, # No 子目录 section on boundary dir
|
|
218
|
+
),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
res = validate_doc.validate_doc(
|
|
222
|
+
doc,
|
|
223
|
+
tmp_path / "firmware",
|
|
224
|
+
project_docs_dir=tmp_path / "project_docs",
|
|
225
|
+
project_root=tmp_path,
|
|
226
|
+
)
|
|
227
|
+
check4 = next(c for c in res["checks"] if c["id"] == 4)
|
|
228
|
+
assert check4["result"] == "N/A"
|
|
229
|
+
assert "boundary" in check4["detail"]
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
# ---------------------------------------------------------------------------
|
|
233
|
+
# #7 & #8: AST symbol extraction (no function locals, includes imports/methods)
|
|
234
|
+
# and pure re-export __init__.py package handling
|
|
235
|
+
# ---------------------------------------------------------------------------
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_ast_symbols_exclude_function_locals_and_include_reexports(tmp_path: Path) -> None:
|
|
239
|
+
src = _write(
|
|
240
|
+
tmp_path / "pkg" / "core.py",
|
|
241
|
+
"""
|
|
242
|
+
from .helper import exported_helper as alias_helper
|
|
243
|
+
|
|
244
|
+
TOP_CONST = 42
|
|
245
|
+
|
|
246
|
+
class Engine:
|
|
247
|
+
def run_step(self):
|
|
248
|
+
local_var = 1
|
|
249
|
+
another_local = 2
|
|
250
|
+
return local_var + another_local
|
|
251
|
+
|
|
252
|
+
def compute(x):
|
|
253
|
+
temp_a = x + 1
|
|
254
|
+
temp_b = temp_a * 2
|
|
255
|
+
return temp_b
|
|
256
|
+
""".lstrip(),
|
|
257
|
+
)
|
|
258
|
+
symbols = validate_doc._extract_ast_symbols(src)
|
|
259
|
+
assert "TOP_CONST" in symbols
|
|
260
|
+
assert "Engine" in symbols
|
|
261
|
+
assert "run_step" in symbols
|
|
262
|
+
assert "compute" in symbols
|
|
263
|
+
assert "alias_helper" in symbols
|
|
264
|
+
# Function-local variables must NOT be extracted as symbols (#8)
|
|
265
|
+
assert "local_var" not in symbols
|
|
266
|
+
assert "another_local" not in symbols
|
|
267
|
+
assert "temp_a" not in symbols
|
|
268
|
+
assert "temp_b" not in symbols
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def test_pure_reexport_init_package_passes_check_6(tmp_path: Path) -> None:
|
|
272
|
+
_write(
|
|
273
|
+
tmp_path / "reexport_pkg" / "__init__.py",
|
|
274
|
+
"from .a import Foo\nfrom .b import Bar\n",
|
|
275
|
+
)
|
|
276
|
+
_write(tmp_path / "reexport_pkg" / "sub" / "a.py", "class Foo:\n pass\n")
|
|
277
|
+
_write(tmp_path / "project_docs" / "reexport_pkg" / "sub.md", "# sub\n")
|
|
278
|
+
doc = _write(
|
|
279
|
+
tmp_path / "project_docs" / "reexport_pkg.md",
|
|
280
|
+
_make_minimal_valid_doc(
|
|
281
|
+
"reexport_pkg",
|
|
282
|
+
role_text="本模块 `__init__.py` 聚合子包导出接口,上游供外部调用,下游委托 `sub` 实现。",
|
|
283
|
+
files_rows=[("__init__.py", "包导出入口")],
|
|
284
|
+
subdir_rows=[("reexport_pkg/sub", "子模块")],
|
|
285
|
+
symbol_rows=None,
|
|
286
|
+
),
|
|
287
|
+
)
|
|
288
|
+
res = validate_doc.validate_doc(
|
|
289
|
+
doc,
|
|
290
|
+
tmp_path / "reexport_pkg",
|
|
291
|
+
project_docs_dir=tmp_path / "project_docs",
|
|
292
|
+
project_root=tmp_path,
|
|
293
|
+
)
|
|
294
|
+
check6 = next(c for c in res["checks"] if c["id"] == 6)
|
|
295
|
+
assert check6["result"] == "N/A"
|
|
296
|
+
assert "re-export" in check6["detail"]
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
# ---------------------------------------------------------------------------
|
|
300
|
+
# #9 & #10: Configurable check_3 terms + backtick entities, phrase BANNED_WORDS
|
|
301
|
+
# ---------------------------------------------------------------------------
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def test_check_3_configurable_terms_and_banned_words_phrases(tmp_path: Path) -> None:
|
|
305
|
+
# BANNED_WORDS should not contain 30+ char sentences (#10)
|
|
306
|
+
assert all(len(phrase) < 25 for phrase in validate_doc.BANNED_WORDS)
|
|
307
|
+
|
|
308
|
+
_write(
|
|
309
|
+
tmp_path / ".projdoc.toml",
|
|
310
|
+
"""
|
|
311
|
+
[validation]
|
|
312
|
+
architecture_terms = ["硬件测试夹具"]
|
|
313
|
+
relation_terms = ["下发给"]
|
|
314
|
+
""".lstrip(),
|
|
315
|
+
)
|
|
316
|
+
_write(tmp_path / "rig" / "driver.py", "def send():\n pass\n")
|
|
317
|
+
doc = _write(
|
|
318
|
+
tmp_path / "project_docs" / "rig.md",
|
|
319
|
+
_make_minimal_valid_doc(
|
|
320
|
+
"rig",
|
|
321
|
+
role_text=(
|
|
322
|
+
"该目录作为硬件测试夹具,负责封装串口与协议控制流程,"
|
|
323
|
+
"把 `driver.py` 中构造的测试帧按既定时序下发给目标芯片验证板。"
|
|
324
|
+
),
|
|
325
|
+
files_rows=[("driver.py", "驱动")],
|
|
326
|
+
),
|
|
327
|
+
)
|
|
328
|
+
cfg = proj_doc_config.load_project_config(tmp_path)
|
|
329
|
+
res = validate_doc.validate_doc(
|
|
330
|
+
doc,
|
|
331
|
+
tmp_path / "rig",
|
|
332
|
+
project_docs_dir=tmp_path / "project_docs",
|
|
333
|
+
project_root=tmp_path,
|
|
334
|
+
project_config=cfg,
|
|
335
|
+
)
|
|
336
|
+
check3 = next(c for c in res["checks"] if c["id"] == 3)
|
|
337
|
+
assert check3["result"] == "PASS"
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
# ---------------------------------------------------------------------------
|
|
341
|
+
# #12 & #19: expected_failures whitelist -> known_issues, validation.by_check
|
|
342
|
+
# ---------------------------------------------------------------------------
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def test_expected_failures_whitelist_and_by_check_aggregation(tmp_path: Path) -> None:
|
|
346
|
+
_write(tmp_path / "CLAUDE.md", _make_valid_claude_md())
|
|
347
|
+
_write(
|
|
348
|
+
tmp_path / ".projdoc.toml",
|
|
349
|
+
"""
|
|
350
|
+
[[validation.expected_failures]]
|
|
351
|
+
path = "pkg.md"
|
|
352
|
+
check_id = 6
|
|
353
|
+
reason = "legacy generated symbols"
|
|
354
|
+
review_date = "2026-12-31"
|
|
355
|
+
""".lstrip(),
|
|
356
|
+
)
|
|
357
|
+
_write(tmp_path / "pkg" / "__init__.py", "")
|
|
358
|
+
_write(
|
|
359
|
+
tmp_path / "pkg" / "big.py",
|
|
360
|
+
"def f1():\n pass\ndef f2():\n pass\ndef f3():\n pass\n",
|
|
361
|
+
)
|
|
362
|
+
# Missing 关键符号 table => check_6 would normally fail, but is whitelisted
|
|
363
|
+
_write(
|
|
364
|
+
tmp_path / "project_docs" / "pkg.md",
|
|
365
|
+
_make_minimal_valid_doc(
|
|
366
|
+
"pkg",
|
|
367
|
+
role_text="本模块 `big.py` 负责核心计算逻辑与数据转换,上游读取配置参数,下游向调用层返回标准化计算结果。",
|
|
368
|
+
files_rows=[("__init__.py", "包初始化"), ("big.py", "核心脚本")],
|
|
369
|
+
symbol_rows=None,
|
|
370
|
+
),
|
|
371
|
+
)
|
|
372
|
+
cfg = proj_doc_config.load_project_config(tmp_path)
|
|
373
|
+
res = validate_doc.validate_doc(
|
|
374
|
+
tmp_path / "project_docs" / "pkg.md",
|
|
375
|
+
tmp_path / "pkg",
|
|
376
|
+
project_docs_dir=tmp_path / "project_docs",
|
|
377
|
+
project_root=tmp_path,
|
|
378
|
+
project_config=cfg,
|
|
379
|
+
)
|
|
380
|
+
check6 = next(c for c in res["checks"] if c["id"] == 6)
|
|
381
|
+
assert check6["result"] == "KNOWN_ISSUE"
|
|
382
|
+
assert res["known_issue_count"] == 1
|
|
383
|
+
assert res["known_issues"][0]["check_id"] == 6
|
|
384
|
+
|
|
385
|
+
report, _ = sync_project_docs.synchronize_project(tmp_path, apply=False)
|
|
386
|
+
val = report["validation"]
|
|
387
|
+
assert "by_check" in val
|
|
388
|
+
assert isinstance(val["by_check"], dict)
|
|
389
|
+
assert val["known_issue_count"] >= 1
|
|
390
|
+
|
|
391
|
+
# v4.5: the aggregate must cover every check id the validator declares
|
|
392
|
+
# (1-12), not the stale 1-9 range, otherwise advisory failures vanish.
|
|
393
|
+
expected_ids = {str(check_id) for check_id, *_rest in validate_doc.CHECKS}
|
|
394
|
+
assert set(val["by_check"]) == expected_ids
|
|
395
|
+
assert {"10", "11", "12"} <= set(val["by_check"])
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
# ---------------------------------------------------------------------------
|
|
399
|
+
# #2, #3, #11, #17: summary, first_run, scaffold-dump, diff
|
|
400
|
+
# ---------------------------------------------------------------------------
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def test_summary_first_run_scaffold_dump_and_diff(tmp_path: Path) -> None:
|
|
404
|
+
_write(tmp_path / "CLAUDE.md", _make_valid_claude_md())
|
|
405
|
+
_write(tmp_path / "src" / "main.py", "def main():\n return 0\n")
|
|
406
|
+
_write(
|
|
407
|
+
tmp_path / "project_docs" / "index.md",
|
|
408
|
+
_make_minimal_valid_doc(
|
|
409
|
+
"index",
|
|
410
|
+
role_text="项目根目录 `index.md` 汇总 `src` 组件,上游对接开发者,下游索引子模块。",
|
|
411
|
+
files_rows=[],
|
|
412
|
+
subdir_rows=[("src", "源码目录")],
|
|
413
|
+
),
|
|
414
|
+
)
|
|
415
|
+
_write(
|
|
416
|
+
tmp_path / "project_docs" / "src.md",
|
|
417
|
+
_make_minimal_valid_doc(
|
|
418
|
+
"src",
|
|
419
|
+
role_text="本模块 `main.py` 为应用入口,上游接收 CLI 参数,下游调度核心逻辑。",
|
|
420
|
+
files_rows=[("main.py", "入口脚本")],
|
|
421
|
+
),
|
|
422
|
+
)
|
|
423
|
+
dump_file = tmp_path / "out" / "scaffold.json"
|
|
424
|
+
report, _ = sync_project_docs.synchronize_project(
|
|
425
|
+
tmp_path,
|
|
426
|
+
apply=False,
|
|
427
|
+
include_diff=True,
|
|
428
|
+
scaffold_dump=dump_file,
|
|
429
|
+
)
|
|
430
|
+
# #3: first_run flag + notice
|
|
431
|
+
assert report["first_run"] is True
|
|
432
|
+
assert "first_run_notice" in report
|
|
433
|
+
|
|
434
|
+
# #2: compact summary
|
|
435
|
+
summary = sync_project_docs.build_dry_run_summary(report)
|
|
436
|
+
assert summary["total_worklist_directories"] >= 2
|
|
437
|
+
assert "top_level_distribution" in summary
|
|
438
|
+
assert "profile_distribution" in summary
|
|
439
|
+
assert "direct_files_distribution" in summary
|
|
440
|
+
|
|
441
|
+
# #11: scaffold dump
|
|
442
|
+
assert dump_file.is_file()
|
|
443
|
+
dumped = json.loads(dump_file.read_text(encoding="utf-8"))
|
|
444
|
+
assert isinstance(dumped["scaffold"], list)
|
|
445
|
+
assert len(dumped["scaffold"]) >= 2
|
|
446
|
+
|
|
447
|
+
# #17: diff
|
|
448
|
+
assert "diff" in report
|
|
449
|
+
assert "manifest_changes" in report["diff"]
|
|
450
|
+
assert report["diff"]["manifest_changes"]["added_count"] == 2
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
# ---------------------------------------------------------------------------
|
|
454
|
+
# #13 & #14: doc_contract_version, --upgrade-plan, --migrate-manifest
|
|
455
|
+
# ---------------------------------------------------------------------------
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def test_doc_contract_version_upgrade_plan_and_migrate_manifest(tmp_path: Path) -> None:
|
|
459
|
+
_write(tmp_path / "CLAUDE.md", _make_valid_claude_md())
|
|
460
|
+
docs = tmp_path / "project_docs"
|
|
461
|
+
_write(docs / "index.md", "# index — Root\n\nContent.\n")
|
|
462
|
+
manifest = contract.build_manifest(docs, last_synced=SYNCED)
|
|
463
|
+
assert manifest["files"][0]["doc_contract_version"] == contract.DOC_CONTRACT_VERSION
|
|
464
|
+
assert (
|
|
465
|
+
manifest["files"][0]["generator"]
|
|
466
|
+
== f"{contract.DEFAULT_PRODUCER_NAME}/{contract.DEFAULT_PRODUCER_VERSION}"
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
# Simulate an older v4.0 manifest without doc_contract_version
|
|
470
|
+
legacy_manifest = json.loads(json.dumps(manifest))
|
|
471
|
+
del legacy_manifest["files"][0]["doc_contract_version"]
|
|
472
|
+
del legacy_manifest["files"][0]["generator"]
|
|
473
|
+
orig_sha = legacy_manifest["files"][0]["sha256"]
|
|
474
|
+
contract.write_manifest(docs / contract.MANIFEST_FILENAME, legacy_manifest, use_lock=False)
|
|
475
|
+
|
|
476
|
+
# #13: upgrade-plan reports legacy document without modifying anything
|
|
477
|
+
plan, code = sync_project_docs.upgrade_plan_project(tmp_path)
|
|
478
|
+
assert code == sync_project_docs.EXIT_OK
|
|
479
|
+
assert plan["needs_upgrade_count"] == 1
|
|
480
|
+
assert plan["documents_needing_upgrade"][0]["current_version"] == "legacy"
|
|
481
|
+
|
|
482
|
+
# #14: migrate-manifest updates metadata in-place without changing sha256
|
|
483
|
+
mig_res, mig_code = sync_project_docs.migrate_manifest_project(tmp_path)
|
|
484
|
+
assert mig_code == sync_project_docs.EXIT_OK
|
|
485
|
+
assert mig_res["migration"]["updated_entries"] == 1
|
|
486
|
+
|
|
487
|
+
reloaded = contract.load_manifest(docs, verify_files=True)
|
|
488
|
+
assert reloaded["files"][0]["sha256"] == orig_sha
|
|
489
|
+
assert reloaded["files"][0]["doc_contract_version"] == contract.DOC_CONTRACT_VERSION
|
|
490
|
+
|
|
491
|
+
# After migration, upgrade-plan reports 0 needing upgrade
|
|
492
|
+
plan2, _ = sync_project_docs.upgrade_plan_project(tmp_path)
|
|
493
|
+
assert plan2["needs_upgrade_count"] == 0
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
# ---------------------------------------------------------------------------
|
|
497
|
+
# #15 & #20: scanner_version upgrade guard & doc_hash_cache in scan_state
|
|
498
|
+
# ---------------------------------------------------------------------------
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
def test_scanner_version_and_doc_hash_cache(tmp_path: Path) -> None:
|
|
502
|
+
_write(tmp_path / "pkg" / "a.py", "x = 1\n")
|
|
503
|
+
candidate = scan_dirs.build_candidate_state(tmp_path)
|
|
504
|
+
assert candidate["scanner_version"] == scan_dirs.SCANNER_VERSION
|
|
505
|
+
|
|
506
|
+
committed = {
|
|
507
|
+
**candidate,
|
|
508
|
+
"state_role": "committed",
|
|
509
|
+
"committed_at": SYNCED,
|
|
510
|
+
"directories": candidate["snapshot"]["directories"],
|
|
511
|
+
"total": candidate["full_total"],
|
|
512
|
+
"manifest_generation_id": "a" * 64,
|
|
513
|
+
"doc_hash_cache": {"pkg.md": "b" * 64},
|
|
514
|
+
}
|
|
515
|
+
validated = scan_dirs.validate_scan_state_v2(committed)
|
|
516
|
+
assert validated["scanner_version"] == scan_dirs.SCANNER_VERSION
|
|
517
|
+
assert validated["doc_hash_cache"] == {"pkg.md": "b" * 64}
|
|
518
|
+
|
|
519
|
+
# Older scanner_version triggers _scanner_version_changed == True
|
|
520
|
+
old_baseline = {
|
|
521
|
+
"schema_version": 2,
|
|
522
|
+
"scanner_version": "2.0",
|
|
523
|
+
}
|
|
524
|
+
assert sync_project_docs._scanner_version_changed(old_baseline) is True
|
|
525
|
+
assert sync_project_docs._scanner_version_changed(validated) is False
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
# ---------------------------------------------------------------------------
|
|
529
|
+
# #16: .projdoc.toml unknown keys warn by default, fail when strict_config=true
|
|
530
|
+
# ---------------------------------------------------------------------------
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
def test_projdoc_config_unknown_keys_warn_vs_strict(tmp_path: Path) -> None:
|
|
534
|
+
_write(
|
|
535
|
+
tmp_path / ".projdoc.toml",
|
|
536
|
+
"""
|
|
537
|
+
future_top_key = 123
|
|
538
|
+
|
|
539
|
+
[scan]
|
|
540
|
+
exlude_paths = ["typo/*"]
|
|
541
|
+
""".lstrip(),
|
|
542
|
+
)
|
|
543
|
+
cfg = proj_doc_config.load_project_config(tmp_path)
|
|
544
|
+
assert cfg.strict_config is False
|
|
545
|
+
assert len(cfg.warnings) >= 2
|
|
546
|
+
assert any("future_top_key" in w for w in cfg.warnings)
|
|
547
|
+
assert any("exlude_paths" in w for w in cfg.warnings)
|
|
548
|
+
|
|
549
|
+
_write(
|
|
550
|
+
tmp_path / ".projdoc.toml",
|
|
551
|
+
"""
|
|
552
|
+
strict_config = true
|
|
553
|
+
future_top_key = 123
|
|
554
|
+
""".lstrip(),
|
|
555
|
+
)
|
|
556
|
+
with pytest.raises(proj_doc_config.ConfigError):
|
|
557
|
+
proj_doc_config.load_project_config(tmp_path)
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def test_expected_failures_accepts_v4_5_advisory_check_ids(tmp_path: Path) -> None:
|
|
561
|
+
"""``check_id`` must accept the v4.5 advisory checks 10/11/12, not just 1..9."""
|
|
562
|
+
_write(
|
|
563
|
+
tmp_path / ".projdoc.toml",
|
|
564
|
+
"""
|
|
565
|
+
[[validation.expected_failures]]
|
|
566
|
+
path = "pkg.md"
|
|
567
|
+
check_id = 10
|
|
568
|
+
reason = "数据与状态 section not yet filled"
|
|
569
|
+
review_date = "2026-12-01"
|
|
570
|
+
|
|
571
|
+
[[validation.expected_failures]]
|
|
572
|
+
path = "pkg.md"
|
|
573
|
+
check_id = 12
|
|
574
|
+
reason = "变更与迁移注意 section deferred"
|
|
575
|
+
""".lstrip(),
|
|
576
|
+
)
|
|
577
|
+
cfg = proj_doc_config.load_project_config(tmp_path)
|
|
578
|
+
ids = {rule.check_id for rule in cfg.validation.expected_failures}
|
|
579
|
+
assert ids == {10, 12}
|
|
580
|
+
|
|
581
|
+
# 13 is still out of range
|
|
582
|
+
_write(
|
|
583
|
+
tmp_path / ".projdoc.toml",
|
|
584
|
+
"""
|
|
585
|
+
[[validation.expected_failures]]
|
|
586
|
+
path = "x.md"
|
|
587
|
+
check_id = 13
|
|
588
|
+
reason = "too far"
|
|
589
|
+
""".lstrip(),
|
|
590
|
+
)
|
|
591
|
+
with pytest.raises(proj_doc_config.ConfigError):
|
|
592
|
+
proj_doc_config.load_project_config(tmp_path)
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
# ---------------------------------------------------------------------------
|
|
596
|
+
# #18: legacy 2-arg _file_snapshot monkeypatch compatibility
|
|
597
|
+
# ---------------------------------------------------------------------------
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def test_scan_dirs_legacy_two_arg_file_snapshot_compat(
|
|
601
|
+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
602
|
+
) -> None:
|
|
603
|
+
_write(tmp_path / "pkg" / "mod.py", "def ok():\n return True\n")
|
|
604
|
+
orig = scan_dirs._file_snapshot
|
|
605
|
+
called: list[str] = []
|
|
606
|
+
|
|
607
|
+
def two_arg_wrapper(entry: Path, scan_root: Path) -> dict[str, Any]:
|
|
608
|
+
called.append(entry.name)
|
|
609
|
+
return orig(entry, scan_root)
|
|
610
|
+
|
|
611
|
+
monkeypatch.setattr(scan_dirs, "_file_snapshot", two_arg_wrapper)
|
|
612
|
+
result = scan_dirs.scan_directories(tmp_path)
|
|
613
|
+
assert any(item["path"] == "pkg" for item in result)
|
|
614
|
+
assert "mod.py" in called
|