@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
|
@@ -1,248 +1,353 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""Loader tests: verify-then-serve behaviour of ``load_docs``."""
|
|
3
|
-
|
|
4
|
-
from __future__ import annotations
|
|
5
|
-
|
|
6
|
-
import json
|
|
7
|
-
import subprocess
|
|
8
|
-
import sys
|
|
9
|
-
from pathlib import Path
|
|
10
|
-
|
|
11
|
-
import pytest
|
|
12
|
-
|
|
13
|
-
import load_docs
|
|
14
|
-
import project_docs_contract as contract
|
|
15
|
-
|
|
16
|
-
SYNCED = "2026-07-30T12:00:00+00:00"
|
|
17
|
-
LOAD_TOOL = Path(__file__).resolve().parents[1] / "tools" / "load_docs.py"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def _write_doc(docs_root: Path, relative: str, title: str, status: str = "✅") -> None:
|
|
21
|
-
path = docs_root.joinpath(*relative.split("/"))
|
|
22
|
-
path.parent.mkdir(parents=True, exist_ok=True)
|
|
23
|
-
path.write_text(
|
|
24
|
-
"\n".join(
|
|
25
|
-
[
|
|
26
|
-
f"# {title}",
|
|
27
|
-
"",
|
|
28
|
-
"<!-- AUTO-GENERATED v3.0: folder-doc START -->",
|
|
29
|
-
f"<!-- Last synced: {SYNCED} by proj-doc-builder v4.0 -->",
|
|
30
|
-
"",
|
|
31
|
-
"**导航**: project_docs > **" + title.split(" — ")[0] + "**",
|
|
32
|
-
"",
|
|
33
|
-
"## 概述",
|
|
34
|
-
"",
|
|
35
|
-
f"{title} 用于验证加载器的行为,内容足够长以通过文档质量校验。",
|
|
36
|
-
"",
|
|
37
|
-
"## 依赖关系",
|
|
38
|
-
"",
|
|
39
|
-
"| 方向 | 模块 | 关系 | 文档 |",
|
|
40
|
-
"|------|------|------|------|",
|
|
41
|
-
"| ⬆ 上游 | index | 示例上游依赖说明文字 | [index.md](index.md) |",
|
|
42
|
-
"",
|
|
43
|
-
"## 状态",
|
|
44
|
-
"",
|
|
45
|
-
status,
|
|
46
|
-
"",
|
|
47
|
-
"<!-- AUTO-GENERATED v3.0: folder-doc END -->",
|
|
48
|
-
"",
|
|
49
|
-
]
|
|
50
|
-
),
|
|
51
|
-
encoding="utf-8",
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def _project(tmp_path: Path) -> Path:
|
|
56
|
-
project = tmp_path / "project"
|
|
57
|
-
docs = project / "project_docs"
|
|
58
|
-
docs.mkdir(parents=True)
|
|
59
|
-
_write_doc(docs, "index.md", "index — 工程总览")
|
|
60
|
-
_write_doc(docs, "pkg.md", "pkg — 示例包")
|
|
61
|
-
_write_doc(docs, "pkg/sub.md", "sub — 子模块")
|
|
62
|
-
_publish_manifest(docs)
|
|
63
|
-
return project
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
def _publish_manifest(docs: Path) -> dict:
|
|
67
|
-
manifest = contract.build_manifest(
|
|
68
|
-
docs,
|
|
69
|
-
last_synced=SYNCED,
|
|
70
|
-
docs_root_label="project_docs",
|
|
71
|
-
)
|
|
72
|
-
contract.atomic_write_json(docs / contract.MANIFEST_FILENAME, manifest)
|
|
73
|
-
return manifest
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def test_overview_reports_verified_project_shape(tmp_path: Path) -> None:
|
|
77
|
-
project = _project(tmp_path)
|
|
78
|
-
|
|
79
|
-
result, exit_code = load_docs.execute(project)
|
|
80
|
-
|
|
81
|
-
assert exit_code == load_docs.EXIT_OK
|
|
82
|
-
assert result["ok"] is True
|
|
83
|
-
assert result["verified"] is True
|
|
84
|
-
assert result["mode"] == "overview"
|
|
85
|
-
assert result["counts"] == {"documents": 3, "returned": 3}
|
|
86
|
-
assert result["manifest"]["total"] == 3
|
|
87
|
-
assert "project_docs/" in result["tree"]
|
|
88
|
-
paths = [item["path"] for item in result["documents"]]
|
|
89
|
-
assert paths == ["index.md", "pkg.md", "pkg/sub.md"]
|
|
90
|
-
assert "概述" in result["documents"][0]["sections"]
|
|
91
|
-
assert "状态" in result["documents"][0]["sections"]
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def test_overview_honours_limit_and_reports_omitted(tmp_path: Path) -> None:
|
|
95
|
-
project = _project(tmp_path)
|
|
96
|
-
|
|
97
|
-
result, exit_code = load_docs.execute(project, limit=1)
|
|
98
|
-
|
|
99
|
-
assert exit_code == load_docs.EXIT_OK
|
|
100
|
-
assert result["counts"] == {"documents": 3, "returned": 1}
|
|
101
|
-
assert result["omitted"]["documents"] == 2
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def test_overview_can_drop_sections_and_tree(tmp_path: Path) -> None:
|
|
105
|
-
project = _project(tmp_path)
|
|
106
|
-
|
|
107
|
-
result, exit_code = load_docs.execute(project, sections=0, include_tree=False)
|
|
108
|
-
|
|
109
|
-
assert exit_code == load_docs.EXIT_OK
|
|
110
|
-
assert "tree" not in result
|
|
111
|
-
assert all("sections" not in item for item in result["documents"])
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def test_target_resolves_source_directory_to_document(tmp_path: Path) -> None:
|
|
115
|
-
project = _project(tmp_path)
|
|
116
|
-
|
|
117
|
-
result, exit_code = load_docs.execute(project, mode="target", requested="pkg")
|
|
118
|
-
|
|
119
|
-
assert exit_code == load_docs.EXIT_OK
|
|
120
|
-
assert result["mode"] == "target"
|
|
121
|
-
assert result["doc"]["path"] == "pkg.md"
|
|
122
|
-
assert "# pkg — 示例包" in result["content"]
|
|
123
|
-
assert "状态" in result["available_sections"]
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
@pytest.mark.parametrize(
|
|
127
|
-
("requested", "expected"),
|
|
128
|
-
(
|
|
129
|
-
("pkg.md", "pkg.md"),
|
|
130
|
-
("project_docs/pkg.md", "pkg.md"),
|
|
131
|
-
("./pkg.md", "pkg.md"),
|
|
132
|
-
("pkg/", "pkg.md"),
|
|
133
|
-
("", "index.md"),
|
|
134
|
-
(".", "index.md"),
|
|
135
|
-
),
|
|
136
|
-
)
|
|
137
|
-
def test_target_normalizes_accepted_path_forms(
|
|
138
|
-
tmp_path: Path, requested: str, expected: str
|
|
139
|
-
) -> None:
|
|
140
|
-
project = _project(tmp_path)
|
|
141
|
-
|
|
142
|
-
result, exit_code = load_docs.execute(project, mode="target", requested=requested)
|
|
143
|
-
|
|
144
|
-
assert exit_code == load_docs.EXIT_OK
|
|
145
|
-
assert result["doc"]["path"] == expected
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
def test_target_section_returns_only_that_section(tmp_path: Path) -> None:
|
|
149
|
-
project = _project(tmp_path)
|
|
150
|
-
|
|
151
|
-
result, exit_code = load_docs.execute(
|
|
152
|
-
project, mode="target", requested="pkg", section="状态"
|
|
153
|
-
)
|
|
154
|
-
|
|
155
|
-
assert exit_code == load_docs.EXIT_OK
|
|
156
|
-
assert result["section"] == "状态"
|
|
157
|
-
assert result["content"].startswith("✅")
|
|
158
|
-
assert "## 概述" not in result["content"]
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
def test_target_unknown_section_lists_available_sections(tmp_path: Path) -> None:
|
|
162
|
-
project = _project(tmp_path)
|
|
163
|
-
|
|
164
|
-
result, exit_code = load_docs.execute(
|
|
165
|
-
project, mode="target", requested="pkg", section="不存在的章节"
|
|
166
|
-
)
|
|
167
|
-
|
|
168
|
-
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
169
|
-
assert result["errors"][0]["code"] == "section-not-found"
|
|
170
|
-
assert "概述" in result["available_sections"]
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
def test_target_refuses_unregistered_document(tmp_path: Path) -> None:
|
|
174
|
-
project = _project(tmp_path)
|
|
175
|
-
|
|
176
|
-
result, exit_code = load_docs.execute(project, mode="target", requested="nope")
|
|
177
|
-
|
|
178
|
-
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
179
|
-
assert result["errors"][0]["code"] == "target-not-registered"
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
def test_target_rejects_path_escape(tmp_path: Path) -> None:
|
|
183
|
-
project = _project(tmp_path)
|
|
184
|
-
|
|
185
|
-
result, exit_code = load_docs.execute(
|
|
186
|
-
project, mode="target", requested="../../etc/passwd"
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
assert exit_code == load_docs.EXIT_INPUT_ERROR
|
|
190
|
-
assert result["errors"][0]["code"] == "unsafe-target"
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
def test_stale_manifest_is_not_served(tmp_path: Path) -> None:
|
|
194
|
-
project = _project(tmp_path)
|
|
195
|
-
(project / "project_docs" / "pkg.md").write_text("# pkg — 被改过\n", encoding="utf-8")
|
|
196
|
-
|
|
197
|
-
result, exit_code = load_docs.execute(project)
|
|
198
|
-
|
|
199
|
-
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
200
|
-
assert result["ok"] is False
|
|
201
|
-
assert result["verified"] is False
|
|
202
|
-
assert result["errors"][0]["code"] == "manifest-stale"
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
def test_unregistered_extra_document_is_not_served(tmp_path: Path) -> None:
|
|
206
|
-
project = _project(tmp_path)
|
|
207
|
-
_write_doc(project / "project_docs", "extra.md", "extra — 未注册")
|
|
208
|
-
|
|
209
|
-
result, exit_code = load_docs.execute(project)
|
|
210
|
-
|
|
211
|
-
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
212
|
-
assert result["errors"][0]["code"] == "manifest-stale"
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
def test_missing_manifest_reports_actionable_error(tmp_path: Path) -> None:
|
|
216
|
-
project = tmp_path / "bare"
|
|
217
|
-
(project / "project_docs").mkdir(parents=True)
|
|
218
|
-
|
|
219
|
-
result, exit_code = load_docs.execute(project)
|
|
220
|
-
|
|
221
|
-
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
222
|
-
assert result["errors"][0]["code"] == "manifest-missing"
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
def test_cli_json_is_single_document_and_signals_failures(tmp_path: Path) -> None:
|
|
226
|
-
project = _project(tmp_path)
|
|
227
|
-
environment = {"PYTHONIOENCODING": "utf-8", "PYTHONDONTWRITEBYTECODE": "1"}
|
|
228
|
-
|
|
229
|
-
completed = subprocess.run(
|
|
230
|
-
[sys.executable, str(LOAD_TOOL), str(project), "--json"],
|
|
231
|
-
capture_output=True,
|
|
232
|
-
check=False,
|
|
233
|
-
env={**environment, "PATH": ""},
|
|
234
|
-
)
|
|
235
|
-
assert completed.returncode == load_docs.EXIT_OK, completed.stderr.decode("utf-8")
|
|
236
|
-
payload = json.loads(completed.stdout.decode("utf-8"))
|
|
237
|
-
assert payload["ok"] is True
|
|
238
|
-
assert payload["exit_code"] == load_docs.EXIT_OK
|
|
239
|
-
|
|
240
|
-
missing = subprocess.run(
|
|
241
|
-
[sys.executable, str(LOAD_TOOL), str(project), "--mode", "target", "--target", "nope", "--json"],
|
|
242
|
-
capture_output=True,
|
|
243
|
-
check=False,
|
|
244
|
-
env={**environment, "PATH": ""},
|
|
245
|
-
)
|
|
246
|
-
assert missing.returncode == load_docs.EXIT_STALE_OR_MISSING
|
|
247
|
-
failure = json.loads(missing.stdout.decode("utf-8"))
|
|
248
|
-
assert failure["errors"][0]["code"] == "target-not-registered"
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Loader tests: verify-then-serve behaviour of ``load_docs``."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
|
|
13
|
+
import load_docs
|
|
14
|
+
import project_docs_contract as contract
|
|
15
|
+
|
|
16
|
+
SYNCED = "2026-07-30T12:00:00+00:00"
|
|
17
|
+
LOAD_TOOL = Path(__file__).resolve().parents[1] / "tools" / "reader" / "load_docs.py"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _write_doc(docs_root: Path, relative: str, title: str, status: str = "✅") -> None:
|
|
21
|
+
path = docs_root.joinpath(*relative.split("/"))
|
|
22
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
23
|
+
path.write_text(
|
|
24
|
+
"\n".join(
|
|
25
|
+
[
|
|
26
|
+
f"# {title}",
|
|
27
|
+
"",
|
|
28
|
+
"<!-- AUTO-GENERATED v3.0: folder-doc START -->",
|
|
29
|
+
f"<!-- Last synced: {SYNCED} by proj-doc-builder v4.0 -->",
|
|
30
|
+
"",
|
|
31
|
+
"**导航**: project_docs > **" + title.split(" — ")[0] + "**",
|
|
32
|
+
"",
|
|
33
|
+
"## 概述",
|
|
34
|
+
"",
|
|
35
|
+
f"{title} 用于验证加载器的行为,内容足够长以通过文档质量校验。",
|
|
36
|
+
"",
|
|
37
|
+
"## 依赖关系",
|
|
38
|
+
"",
|
|
39
|
+
"| 方向 | 模块 | 关系 | 文档 |",
|
|
40
|
+
"|------|------|------|------|",
|
|
41
|
+
"| ⬆ 上游 | index | 示例上游依赖说明文字 | [index.md](index.md) |",
|
|
42
|
+
"",
|
|
43
|
+
"## 状态",
|
|
44
|
+
"",
|
|
45
|
+
status,
|
|
46
|
+
"",
|
|
47
|
+
"<!-- AUTO-GENERATED v3.0: folder-doc END -->",
|
|
48
|
+
"",
|
|
49
|
+
]
|
|
50
|
+
),
|
|
51
|
+
encoding="utf-8",
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _project(tmp_path: Path) -> Path:
|
|
56
|
+
project = tmp_path / "project"
|
|
57
|
+
docs = project / "project_docs"
|
|
58
|
+
docs.mkdir(parents=True)
|
|
59
|
+
_write_doc(docs, "index.md", "index — 工程总览")
|
|
60
|
+
_write_doc(docs, "pkg.md", "pkg — 示例包")
|
|
61
|
+
_write_doc(docs, "pkg/sub.md", "sub — 子模块")
|
|
62
|
+
_publish_manifest(docs)
|
|
63
|
+
return project
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _publish_manifest(docs: Path) -> dict:
|
|
67
|
+
manifest = contract.build_manifest(
|
|
68
|
+
docs,
|
|
69
|
+
last_synced=SYNCED,
|
|
70
|
+
docs_root_label="project_docs",
|
|
71
|
+
)
|
|
72
|
+
contract.atomic_write_json(docs / contract.MANIFEST_FILENAME, manifest)
|
|
73
|
+
return manifest
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def test_overview_reports_verified_project_shape(tmp_path: Path) -> None:
|
|
77
|
+
project = _project(tmp_path)
|
|
78
|
+
|
|
79
|
+
result, exit_code = load_docs.execute(project)
|
|
80
|
+
|
|
81
|
+
assert exit_code == load_docs.EXIT_OK
|
|
82
|
+
assert result["ok"] is True
|
|
83
|
+
assert result["verified"] is True
|
|
84
|
+
assert result["mode"] == "overview"
|
|
85
|
+
assert result["counts"] == {"documents": 3, "returned": 3}
|
|
86
|
+
assert result["manifest"]["total"] == 3
|
|
87
|
+
assert "project_docs/" in result["tree"]
|
|
88
|
+
paths = [item["path"] for item in result["documents"]]
|
|
89
|
+
assert paths == ["index.md", "pkg.md", "pkg/sub.md"]
|
|
90
|
+
assert "概述" in result["documents"][0]["sections"]
|
|
91
|
+
assert "状态" in result["documents"][0]["sections"]
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def test_overview_honours_limit_and_reports_omitted(tmp_path: Path) -> None:
|
|
95
|
+
project = _project(tmp_path)
|
|
96
|
+
|
|
97
|
+
result, exit_code = load_docs.execute(project, limit=1)
|
|
98
|
+
|
|
99
|
+
assert exit_code == load_docs.EXIT_OK
|
|
100
|
+
assert result["counts"] == {"documents": 3, "returned": 1}
|
|
101
|
+
assert result["omitted"]["documents"] == 2
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_overview_can_drop_sections_and_tree(tmp_path: Path) -> None:
|
|
105
|
+
project = _project(tmp_path)
|
|
106
|
+
|
|
107
|
+
result, exit_code = load_docs.execute(project, sections=0, include_tree=False)
|
|
108
|
+
|
|
109
|
+
assert exit_code == load_docs.EXIT_OK
|
|
110
|
+
assert "tree" not in result
|
|
111
|
+
assert all("sections" not in item for item in result["documents"])
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def test_target_resolves_source_directory_to_document(tmp_path: Path) -> None:
|
|
115
|
+
project = _project(tmp_path)
|
|
116
|
+
|
|
117
|
+
result, exit_code = load_docs.execute(project, mode="target", requested="pkg")
|
|
118
|
+
|
|
119
|
+
assert exit_code == load_docs.EXIT_OK
|
|
120
|
+
assert result["mode"] == "target"
|
|
121
|
+
assert result["doc"]["path"] == "pkg.md"
|
|
122
|
+
assert "# pkg — 示例包" in result["content"]
|
|
123
|
+
assert "状态" in result["available_sections"]
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@pytest.mark.parametrize(
|
|
127
|
+
("requested", "expected"),
|
|
128
|
+
(
|
|
129
|
+
("pkg.md", "pkg.md"),
|
|
130
|
+
("project_docs/pkg.md", "pkg.md"),
|
|
131
|
+
("./pkg.md", "pkg.md"),
|
|
132
|
+
("pkg/", "pkg.md"),
|
|
133
|
+
("", "index.md"),
|
|
134
|
+
(".", "index.md"),
|
|
135
|
+
),
|
|
136
|
+
)
|
|
137
|
+
def test_target_normalizes_accepted_path_forms(
|
|
138
|
+
tmp_path: Path, requested: str, expected: str
|
|
139
|
+
) -> None:
|
|
140
|
+
project = _project(tmp_path)
|
|
141
|
+
|
|
142
|
+
result, exit_code = load_docs.execute(project, mode="target", requested=requested)
|
|
143
|
+
|
|
144
|
+
assert exit_code == load_docs.EXIT_OK
|
|
145
|
+
assert result["doc"]["path"] == expected
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_target_section_returns_only_that_section(tmp_path: Path) -> None:
|
|
149
|
+
project = _project(tmp_path)
|
|
150
|
+
|
|
151
|
+
result, exit_code = load_docs.execute(
|
|
152
|
+
project, mode="target", requested="pkg", section="状态"
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
assert exit_code == load_docs.EXIT_OK
|
|
156
|
+
assert result["section"] == "状态"
|
|
157
|
+
assert result["content"].startswith("✅")
|
|
158
|
+
assert "## 概述" not in result["content"]
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def test_target_unknown_section_lists_available_sections(tmp_path: Path) -> None:
|
|
162
|
+
project = _project(tmp_path)
|
|
163
|
+
|
|
164
|
+
result, exit_code = load_docs.execute(
|
|
165
|
+
project, mode="target", requested="pkg", section="不存在的章节"
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
169
|
+
assert result["errors"][0]["code"] == "section-not-found"
|
|
170
|
+
assert "概述" in result["available_sections"]
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def test_target_refuses_unregistered_document(tmp_path: Path) -> None:
|
|
174
|
+
project = _project(tmp_path)
|
|
175
|
+
|
|
176
|
+
result, exit_code = load_docs.execute(project, mode="target", requested="nope")
|
|
177
|
+
|
|
178
|
+
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
179
|
+
assert result["errors"][0]["code"] == "target-not-registered"
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def test_target_rejects_path_escape(tmp_path: Path) -> None:
|
|
183
|
+
project = _project(tmp_path)
|
|
184
|
+
|
|
185
|
+
result, exit_code = load_docs.execute(
|
|
186
|
+
project, mode="target", requested="../../etc/passwd"
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
assert exit_code == load_docs.EXIT_INPUT_ERROR
|
|
190
|
+
assert result["errors"][0]["code"] == "unsafe-target"
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def test_stale_manifest_is_not_served(tmp_path: Path) -> None:
|
|
194
|
+
project = _project(tmp_path)
|
|
195
|
+
(project / "project_docs" / "pkg.md").write_text("# pkg — 被改过\n", encoding="utf-8")
|
|
196
|
+
|
|
197
|
+
result, exit_code = load_docs.execute(project)
|
|
198
|
+
|
|
199
|
+
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
200
|
+
assert result["ok"] is False
|
|
201
|
+
assert result["verified"] is False
|
|
202
|
+
assert result["errors"][0]["code"] == "manifest-stale"
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_unregistered_extra_document_is_not_served(tmp_path: Path) -> None:
|
|
206
|
+
project = _project(tmp_path)
|
|
207
|
+
_write_doc(project / "project_docs", "extra.md", "extra — 未注册")
|
|
208
|
+
|
|
209
|
+
result, exit_code = load_docs.execute(project)
|
|
210
|
+
|
|
211
|
+
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
212
|
+
assert result["errors"][0]["code"] == "manifest-stale"
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def test_missing_manifest_reports_actionable_error(tmp_path: Path) -> None:
|
|
216
|
+
project = tmp_path / "bare"
|
|
217
|
+
(project / "project_docs").mkdir(parents=True)
|
|
218
|
+
|
|
219
|
+
result, exit_code = load_docs.execute(project)
|
|
220
|
+
|
|
221
|
+
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
222
|
+
assert result["errors"][0]["code"] == "manifest-missing"
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def test_cli_json_is_single_document_and_signals_failures(tmp_path: Path) -> None:
|
|
226
|
+
project = _project(tmp_path)
|
|
227
|
+
environment = {"PYTHONIOENCODING": "utf-8", "PYTHONDONTWRITEBYTECODE": "1"}
|
|
228
|
+
|
|
229
|
+
completed = subprocess.run(
|
|
230
|
+
[sys.executable, str(LOAD_TOOL), str(project), "--json"],
|
|
231
|
+
capture_output=True,
|
|
232
|
+
check=False,
|
|
233
|
+
env={**environment, "PATH": ""},
|
|
234
|
+
)
|
|
235
|
+
assert completed.returncode == load_docs.EXIT_OK, completed.stderr.decode("utf-8")
|
|
236
|
+
payload = json.loads(completed.stdout.decode("utf-8"))
|
|
237
|
+
assert payload["ok"] is True
|
|
238
|
+
assert payload["exit_code"] == load_docs.EXIT_OK
|
|
239
|
+
|
|
240
|
+
missing = subprocess.run(
|
|
241
|
+
[sys.executable, str(LOAD_TOOL), str(project), "--mode", "target", "--target", "nope", "--json"],
|
|
242
|
+
capture_output=True,
|
|
243
|
+
check=False,
|
|
244
|
+
env={**environment, "PATH": ""},
|
|
245
|
+
)
|
|
246
|
+
assert missing.returncode == load_docs.EXIT_STALE_OR_MISSING
|
|
247
|
+
failure = json.loads(missing.stdout.decode("utf-8"))
|
|
248
|
+
assert failure["errors"][0]["code"] == "target-not-registered"
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
# ---------------------------------------------------------------------------
|
|
252
|
+
# v4.5: brief mode — cheap orientation for a fresh conversation
|
|
253
|
+
# ---------------------------------------------------------------------------
|
|
254
|
+
def test_brief_returns_abstract_and_per_document_summaries(tmp_path: Path) -> None:
|
|
255
|
+
project = _project(tmp_path)
|
|
256
|
+
|
|
257
|
+
result, exit_code = load_docs.execute(project, mode="brief")
|
|
258
|
+
|
|
259
|
+
assert exit_code == load_docs.EXIT_OK
|
|
260
|
+
assert result["mode"] == "brief"
|
|
261
|
+
assert result["verified"] is True
|
|
262
|
+
assert result["counts"] == {"documents": 3, "summarized": 3}
|
|
263
|
+
assert "用于验证加载器的行为" in result["abstract"]
|
|
264
|
+
assert [item["path"] for item in result["documents"]] == [
|
|
265
|
+
"index.md",
|
|
266
|
+
"pkg.md",
|
|
267
|
+
"pkg/sub.md",
|
|
268
|
+
]
|
|
269
|
+
subsystem_paths = [item["path"] for item in result["subsystems"]]
|
|
270
|
+
assert subsystem_paths == ["index.md", "pkg.md", "pkg/sub.md"]
|
|
271
|
+
# Summaries carry the 概述 body, not the whole document.
|
|
272
|
+
assert "## 依赖关系" not in result["documents"][0]["summary"]
|
|
273
|
+
assert result["hints"]["drill_down"].startswith("--mode target")
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def test_brief_and_overview_report_contract_version_from_per_file_entries(
|
|
277
|
+
tmp_path: Path,
|
|
278
|
+
) -> None:
|
|
279
|
+
"""The manifest stores ``doc_contract_version`` on each file entry, never at top
|
|
280
|
+
level. Both loaders must aggregate that field rather than read a phantom key."""
|
|
281
|
+
project = _project(tmp_path)
|
|
282
|
+
|
|
283
|
+
for mode in ("brief", "overview"):
|
|
284
|
+
result, exit_code = load_docs.execute(project, mode=mode)
|
|
285
|
+
assert exit_code == load_docs.EXIT_OK
|
|
286
|
+
versions = result["manifest"]["doc_contract_versions"]
|
|
287
|
+
assert versions == [contract.DOC_CONTRACT_VERSION]
|
|
288
|
+
assert result["manifest"]["doc_contract_version"] == contract.DOC_CONTRACT_VERSION
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def test_contract_versions_sort_numerically_not_lexically() -> None:
|
|
292
|
+
"""4.10 must come after 4.2, not before (naive ``sorted`` would misorder)."""
|
|
293
|
+
entries = [
|
|
294
|
+
{"doc_contract_version": "4.10"},
|
|
295
|
+
{"doc_contract_version": "4.2"},
|
|
296
|
+
{"doc_contract_version": "4.1"},
|
|
297
|
+
{"doc_contract_version": None},
|
|
298
|
+
{"doc_contract_version": ""},
|
|
299
|
+
]
|
|
300
|
+
assert load_docs._contract_versions(entries) == ["4.1", "4.2", "4.10"]
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def test_brief_bounds_summary_length_per_document(tmp_path: Path) -> None:
|
|
304
|
+
project = _project(tmp_path)
|
|
305
|
+
|
|
306
|
+
result, exit_code = load_docs.execute(
|
|
307
|
+
project, mode="brief", summary_chars=10, max_chars=0
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
assert exit_code == load_docs.EXIT_OK
|
|
311
|
+
for item in result["documents"]:
|
|
312
|
+
assert len(item["summary"]) <= 11 # 10 chars plus the ellipsis marker
|
|
313
|
+
assert item["summary"].endswith("…")
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def test_brief_reports_budget_omissions(tmp_path: Path) -> None:
|
|
317
|
+
project = _project(tmp_path)
|
|
318
|
+
|
|
319
|
+
result, exit_code = load_docs.execute(
|
|
320
|
+
project, mode="brief", summary_chars=200, max_chars=120
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
assert exit_code == load_docs.EXIT_OK
|
|
324
|
+
assert result["counts"]["summarized"] < result["counts"]["documents"]
|
|
325
|
+
assert result["omitted"]["reason"].startswith("max_chars=")
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def test_brief_refuses_a_stale_manifest(tmp_path: Path) -> None:
|
|
329
|
+
project = _project(tmp_path)
|
|
330
|
+
docs = project / "project_docs"
|
|
331
|
+
(docs / "pkg.md").write_text("# pkg\n\n## 概述\n\n改过但未同步。\n", encoding="utf-8")
|
|
332
|
+
|
|
333
|
+
result, exit_code = load_docs.execute(project, mode="brief")
|
|
334
|
+
|
|
335
|
+
assert exit_code == load_docs.EXIT_STALE_OR_MISSING
|
|
336
|
+
assert result["ok"] is False
|
|
337
|
+
assert result["errors"][0]["code"] == "manifest-stale"
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def test_brief_cli_emits_text_orientation(tmp_path: Path) -> None:
|
|
341
|
+
project = _project(tmp_path)
|
|
342
|
+
|
|
343
|
+
completed = subprocess.run(
|
|
344
|
+
[sys.executable, "-X", "utf8", str(LOAD_TOOL), str(project), "--mode", "brief"],
|
|
345
|
+
capture_output=True,
|
|
346
|
+
timeout=120,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
assert completed.returncode == load_docs.EXIT_OK
|
|
350
|
+
text = completed.stdout.decode("utf-8")
|
|
351
|
+
assert "工程速览" in text
|
|
352
|
+
assert "## 顶层子系统" in text
|
|
353
|
+
assert "深入:" in text
|
|
@@ -386,6 +386,34 @@ def test_atomic_json_is_stable_utf8_and_uses_unique_same_dir_temps(
|
|
|
386
386
|
assert not list(tmp_path.glob(".manifest.json.*.tmp"))
|
|
387
387
|
|
|
388
388
|
|
|
389
|
+
@pytest.mark.skipif(contract.os.name != "posix", reason="permission bits are POSIX-only")
|
|
390
|
+
def test_atomic_write_preserves_existing_file_mode(tmp_path: Path) -> None:
|
|
391
|
+
"""A replacement must keep the destination's permission bits, not silently switch to 0600."""
|
|
392
|
+
target = tmp_path / "doc.md"
|
|
393
|
+
target.write_bytes(b"old\n")
|
|
394
|
+
target.chmod(0o644)
|
|
395
|
+
contract.atomic_write_utf8(target, "new content\n")
|
|
396
|
+
assert target.read_bytes() == b"new content\n"
|
|
397
|
+
assert (target.stat().st_mode & 0o777) == 0o644
|
|
398
|
+
|
|
399
|
+
target.chmod(0o600)
|
|
400
|
+
contract.atomic_write_utf8(target, "again\n")
|
|
401
|
+
assert (target.stat().st_mode & 0o777) == 0o600
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
@pytest.mark.skipif(contract.os.name != "posix", reason="permission bits are POSIX-only")
|
|
405
|
+
def test_atomic_write_new_file_uses_umask_correct_mode(tmp_path: Path) -> None:
|
|
406
|
+
"""A brand-new file must respect the process umask, not be locked to mkstemp's 0600."""
|
|
407
|
+
import os as _os
|
|
408
|
+
target = tmp_path / "fresh.md"
|
|
409
|
+
saved = _os.umask(0); _os.umask(saved)
|
|
410
|
+
expected = 0o666 & ~saved
|
|
411
|
+
contract.atomic_write_utf8(target, "hello\n")
|
|
412
|
+
actual = target.stat().st_mode & 0o777
|
|
413
|
+
assert actual != 0o600, "file mode stayed at mkstemp's 0600"
|
|
414
|
+
assert actual == expected
|
|
415
|
+
|
|
416
|
+
|
|
389
417
|
def test_exclusive_lock_blocks_a_second_owner(tmp_path: Path) -> None:
|
|
390
418
|
lock_path = tmp_path / "manifest.lock"
|
|
391
419
|
with contract.exclusive_lock(lock_path, timeout=1.0):
|
|
@@ -535,7 +563,10 @@ def test_manifest_v2_roundtrip_and_stable_generation(tmp_path: Path) -> None:
|
|
|
535
563
|
second = contract.build_manifest(root, last_synced=SYNCED_2)
|
|
536
564
|
|
|
537
565
|
assert first["schema_version"] == 2
|
|
538
|
-
assert first["producer"] == {
|
|
566
|
+
assert first["producer"] == {
|
|
567
|
+
"name": contract.DEFAULT_PRODUCER_NAME,
|
|
568
|
+
"version": contract.DEFAULT_PRODUCER_VERSION,
|
|
569
|
+
}
|
|
539
570
|
assert first["docs_root"] == "project_docs"
|
|
540
571
|
assert first["total"] == 2
|
|
541
572
|
assert first["depth_counts"] == {"0": 1, "1": 1}
|
|
@@ -954,7 +954,7 @@ def test_candidate_commit_classifies_installed_post_replace_failure(
|
|
|
954
954
|
real_atomic_write = contract.atomic_write_json
|
|
955
955
|
|
|
956
956
|
def interrupt_after_install(path: Path, value: dict) -> Path:
|
|
957
|
-
|
|
957
|
+
real_atomic_write(path, value)
|
|
958
958
|
raise KeyboardInterrupt("after-install")
|
|
959
959
|
|
|
960
960
|
monkeypatch.setattr(contract, "atomic_write_json", interrupt_after_install)
|