@walvka-code/proj-doc-builder 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +290 -3
- package/README.zh-CN.md +258 -0
- package/cordis.patch.yml +8 -0
- package/icon.svg +11 -0
- package/index.js +164 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +59 -4
- package/skills/proj-doc-builder/SKILL.md +412 -0
- package/skills/proj-doc-builder/references/audit_script.md +103 -0
- package/skills/proj-doc-builder/references/batch-generation.md +55 -0
- package/skills/proj-doc-builder/references/checklist.md +90 -0
- package/skills/proj-doc-builder/references/exclusions.md +79 -0
- package/skills/proj-doc-builder/references/linking.md +83 -0
- package/skills/proj-doc-builder/references/migration.md +32 -0
- package/skills/proj-doc-builder/references/phase4.md +200 -0
- package/skills/proj-doc-builder/references/templates.md +212 -0
- package/skills/proj-doc-builder/tests/claims.json +76 -0
- package/skills/proj-doc-builder/tests/conftest.py +23 -0
- package/skills/proj-doc-builder/tests/test_claims_registry.py +81 -0
- package/skills/proj-doc-builder/tests/test_claude_doc_index.py +157 -0
- package/skills/proj-doc-builder/tests/test_e2e_fresh_project.py +156 -0
- package/skills/proj-doc-builder/tests/test_link_tools.py +279 -0
- package/skills/proj-doc-builder/tests/test_load_docs.py +353 -0
- package/skills/proj-doc-builder/tests/test_normalize_markers.py +120 -0
- package/skills/proj-doc-builder/tests/test_phase4_exit.py +3895 -0
- package/skills/proj-doc-builder/tests/test_project_docs_contract.py +812 -0
- package/skills/proj-doc-builder/tests/test_scan_dirs.py +1159 -0
- 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 -0
- 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 +712 -0
- package/skills/proj-doc-builder/tools/_paths.py +39 -0
- package/skills/proj-doc-builder/tools/core/claude_doc_index.py +504 -0
- package/skills/proj-doc-builder/tools/core/compute_link.py +239 -0
- package/skills/proj-doc-builder/tools/core/proj_doc_config.py +460 -0
- package/skills/proj-doc-builder/tools/core/project_docs_contract.py +2725 -0
- package/skills/proj-doc-builder/tools/phase4/__init__.py +82 -0
- package/skills/proj-doc-builder/tools/phase4/cas_storage.py +25 -0
- package/skills/proj-doc-builder/tools/phase4/engine.py +35 -0
- package/skills/proj-doc-builder/tools/phase4/journal.py +29 -0
- package/skills/proj-doc-builder/tools/phase4/platform_handles.py +27 -0
- package/skills/proj-doc-builder/tools/phase4_exit.py +8565 -0
- package/skills/proj-doc-builder/tools/pipeline/gen_docs_from_scaffold.py +789 -0
- package/skills/proj-doc-builder/tools/pipeline/sync_project_docs.py +5531 -0
- package/skills/proj-doc-builder/tools/reader/load_docs.py +654 -0
- package/skills/proj-doc-builder/tools/scan/link_check.py +203 -0
- package/skills/proj-doc-builder/tools/scan/normalize_markers.py +211 -0
- package/skills/proj-doc-builder/tools/scan/scan_dirs.py +2735 -0
- package/skills/proj-doc-builder/tools/validate/validate_doc.py +1911 -0
|
@@ -0,0 +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" / "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
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Tests for exact, idempotent Last synced marker normalization."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
import normalize_markers as normalizer
|
|
11
|
+
|
|
12
|
+
BAD = (
|
|
13
|
+
"<!-- Last synced: 2026-07-23T16:32:05+08:00 "
|
|
14
|
+
"by proj-doc-builder v4.0 --> v3.1 -->"
|
|
15
|
+
)
|
|
16
|
+
GOOD = "<!-- Last synced: 2026-07-23T16:32:05+08:00 " "by proj-doc-builder v4.0 -->"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _write(path: Path, text: str) -> None:
|
|
20
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
21
|
+
path.write_text(text, encoding="utf-8")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_normalize_only_exact_historical_tail() -> None:
|
|
25
|
+
content = "\n".join(
|
|
26
|
+
[
|
|
27
|
+
"# Doc",
|
|
28
|
+
BAD,
|
|
29
|
+
"自由文本 v3.1 --> 不应改变",
|
|
30
|
+
"<!-- Last synced: malformed --> other -->",
|
|
31
|
+
]
|
|
32
|
+
)
|
|
33
|
+
normalized, changes = normalizer.normalize_marker_content(content, path="a.md")
|
|
34
|
+
assert GOOD in normalized
|
|
35
|
+
assert BAD not in normalized
|
|
36
|
+
assert "自由文本 v3.1 --> 不应改变" in normalized
|
|
37
|
+
assert "<!-- Last synced: malformed --> other -->" in normalized
|
|
38
|
+
assert changes == [
|
|
39
|
+
{
|
|
40
|
+
"path": "a.md",
|
|
41
|
+
"line": 2,
|
|
42
|
+
"before": BAD,
|
|
43
|
+
"after": GOOD,
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_normalization_preserves_crlf() -> None:
|
|
49
|
+
content = f"# Doc\r\n{BAD}\r\nnext\r\n"
|
|
50
|
+
normalized, changes = normalizer.normalize_marker_content(content)
|
|
51
|
+
assert changes
|
|
52
|
+
assert normalized == f"# Doc\r\n{GOOD}\r\nnext\r\n"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_normalization_is_idempotent() -> None:
|
|
56
|
+
once, changes = normalizer.normalize_marker_content(BAD)
|
|
57
|
+
twice, second_changes = normalizer.normalize_marker_content(once)
|
|
58
|
+
assert changes
|
|
59
|
+
assert once == GOOD
|
|
60
|
+
assert twice == once
|
|
61
|
+
assert second_changes == []
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_collect_excludes_quarantine_and_apply_is_explicit(tmp_path: Path) -> None:
|
|
65
|
+
root = tmp_path / "project_docs"
|
|
66
|
+
_write(root / "bad.md", f"# Bad\n{BAD}\n")
|
|
67
|
+
_write(root / "ok.md", f"# OK\n{GOOD}\n")
|
|
68
|
+
_write(root / ".quarantine" / "receipt" / "old.md", f"# Old\n{BAD}\n")
|
|
69
|
+
|
|
70
|
+
changes = normalizer.collect_marker_changes(root)
|
|
71
|
+
assert [(change.path, change.line) for change in changes] == [("bad.md", 2)]
|
|
72
|
+
assert BAD in (root / "bad.md").read_text(encoding="utf-8")
|
|
73
|
+
|
|
74
|
+
assert normalizer.apply_marker_changes(root, changes) == ["bad.md"]
|
|
75
|
+
assert GOOD in (root / "bad.md").read_text(encoding="utf-8")
|
|
76
|
+
assert normalizer.collect_marker_changes(root) == []
|
|
77
|
+
assert BAD in (root / ".quarantine" / "receipt" / "old.md").read_text(
|
|
78
|
+
encoding="utf-8"
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_apply_rejects_concurrent_change(tmp_path: Path) -> None:
|
|
83
|
+
root = tmp_path / "project_docs"
|
|
84
|
+
path = root / "bad.md"
|
|
85
|
+
_write(path, f"# Bad\n{BAD}\n")
|
|
86
|
+
changes = normalizer.collect_marker_changes(root)
|
|
87
|
+
path.write_text("# changed concurrently\n", encoding="utf-8")
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
normalizer.apply_marker_changes(root, changes)
|
|
91
|
+
except normalizer.MarkerNormalizationError as exc:
|
|
92
|
+
assert "已变化" in str(exc)
|
|
93
|
+
else: # pragma: no cover
|
|
94
|
+
raise AssertionError("concurrent modification must fail")
|
|
95
|
+
assert path.read_text(encoding="utf-8") == "# changed concurrently\n"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def test_collect_rejects_inventory_to_read_symlink_swap(
|
|
99
|
+
tmp_path: Path,
|
|
100
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
101
|
+
) -> None:
|
|
102
|
+
root = tmp_path / "project_docs"
|
|
103
|
+
victim = root / "a.md"
|
|
104
|
+
replacement = root / "b.md"
|
|
105
|
+
_write(victim, f"# A\n{BAD}\n")
|
|
106
|
+
_write(replacement, f"# B\n{GOOD}\n")
|
|
107
|
+
original_discover = normalizer.discover_documents
|
|
108
|
+
|
|
109
|
+
def swapping_discover(project_docs: Path) -> list[tuple[str, Path]]:
|
|
110
|
+
documents = original_discover(project_docs)
|
|
111
|
+
try:
|
|
112
|
+
victim.unlink()
|
|
113
|
+
victim.symlink_to(replacement)
|
|
114
|
+
except (NotImplementedError, OSError) as exc:
|
|
115
|
+
pytest.skip(f"symlink unavailable: {exc}")
|
|
116
|
+
return documents
|
|
117
|
+
|
|
118
|
+
monkeypatch.setattr(normalizer, "discover_documents", swapping_discover)
|
|
119
|
+
with pytest.raises(normalizer.ProjectDocsContractError):
|
|
120
|
+
normalizer.collect_marker_changes(root)
|