@walvka-code/proj-doc-builder 0.0.0-stage → 4.0.0
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 +271 -2
- package/README.zh-CN.md +240 -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 +55 -4
- package/skills/proj-doc-builder/SKILL.md +350 -0
- package/skills/proj-doc-builder/references/audit_script.md +96 -0
- package/skills/proj-doc-builder/references/checklist.md +80 -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 +172 -0
- 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/tests/conftest.py +11 -0
- package/skills/proj-doc-builder/tests/test_claude_doc_index.py +157 -0
- package/skills/proj-doc-builder/tests/test_link_tools.py +279 -0
- package/skills/proj-doc-builder/tests/test_load_docs.py +248 -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 +781 -0
- package/skills/proj-doc-builder/tests/test_scan_dirs.py +1159 -0
- package/skills/proj-doc-builder/tests/test_sync_project_docs.py +2169 -0
- package/skills/proj-doc-builder/tests/test_validate_doc.py +555 -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
- package/skills/proj-doc-builder/tools/claude_doc_index.py +298 -0
- package/skills/proj-doc-builder/tools/compute_link.py +233 -0
- package/skills/proj-doc-builder/tools/link_check.py +152 -0
- package/skills/proj-doc-builder/tools/load_docs.py +380 -0
- package/skills/proj-doc-builder/tools/normalize_markers.py +205 -0
- package/skills/proj-doc-builder/tools/phase4/__init__.py +57 -0
- package/skills/proj-doc-builder/tools/phase4/cas_storage.py +18 -0
- package/skills/proj-doc-builder/tools/phase4/engine.py +20 -0
- package/skills/proj-doc-builder/tools/phase4/journal.py +22 -0
- package/skills/proj-doc-builder/tools/phase4/platform_handles.py +20 -0
- package/skills/proj-doc-builder/tools/phase4_exit.py +8459 -0
- package/skills/proj-doc-builder/tools/project_docs_contract.py +2318 -0
- package/skills/proj-doc-builder/tools/scan_dirs.py +2405 -0
- package/skills/proj-doc-builder/tools/sync_project_docs.py +3639 -0
- package/skills/proj-doc-builder/tools/validate_doc.py +1299 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Test import setup for standalone proj-doc-builder tools."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
TOOLS_DIR = Path(__file__).resolve().parents[1] / "tools"
|
|
10
|
+
if str(TOOLS_DIR) not in sys.path:
|
|
11
|
+
sys.path.insert(0, str(TOOLS_DIR))
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Tests for deterministic CLAUDE document-tree generation."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
import claude_doc_index as indexer
|
|
11
|
+
|
|
12
|
+
SYNCED = "2026-07-30T09:00:00+08:00"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _manifest() -> dict:
|
|
16
|
+
return {
|
|
17
|
+
"schema_version": 2,
|
|
18
|
+
"producer": {"name": "proj-doc-builder", "version": "4.0"},
|
|
19
|
+
"last_synced": SYNCED,
|
|
20
|
+
"files": [
|
|
21
|
+
{
|
|
22
|
+
"path": "alpha/child.md",
|
|
23
|
+
"title": "child — 子文档",
|
|
24
|
+
"status": "🔶",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"path": "alpha.md",
|
|
28
|
+
"title": "alpha — 主模块",
|
|
29
|
+
"status": "✅",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"path": "Beta.md",
|
|
33
|
+
"title": "Beta",
|
|
34
|
+
"status": "⬜",
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _claude(newline: str = "\n") -> str:
|
|
41
|
+
blocks = ["# CLAUDE.md", "manual-before"]
|
|
42
|
+
for section in indexer.REQUIRED_SECTIONS:
|
|
43
|
+
blocks.extend(
|
|
44
|
+
[
|
|
45
|
+
indexer.section_start(section),
|
|
46
|
+
f"old-{section}",
|
|
47
|
+
indexer.section_end(section),
|
|
48
|
+
f"manual-after-{section}",
|
|
49
|
+
]
|
|
50
|
+
)
|
|
51
|
+
return newline.join(blocks) + newline
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_render_tree_is_complete_and_deterministic() -> None:
|
|
55
|
+
rendered = indexer.render_document_tree(_manifest())
|
|
56
|
+
assert rendered.splitlines() == [
|
|
57
|
+
"project_docs/",
|
|
58
|
+
"├── alpha — 主模块 ✅",
|
|
59
|
+
"│ └── child — 子文档 🔶",
|
|
60
|
+
"└── Beta ⬜",
|
|
61
|
+
]
|
|
62
|
+
assert indexer.render_document_tree(_manifest()) == rendered
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_tree_merges_case_only_file_directory_spelling() -> None:
|
|
66
|
+
manifest = _manifest()
|
|
67
|
+
manifest["files"].extend(
|
|
68
|
+
[
|
|
69
|
+
{
|
|
70
|
+
"path": "pkg/Worker_Node/RSA.md",
|
|
71
|
+
"title": "RSA",
|
|
72
|
+
"status": "✅",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"path": "pkg/worker_node.md",
|
|
76
|
+
"title": "Worker_Node — 装配测试",
|
|
77
|
+
"status": "✅",
|
|
78
|
+
},
|
|
79
|
+
]
|
|
80
|
+
)
|
|
81
|
+
rendered = indexer.render_document_tree(manifest)
|
|
82
|
+
assert "Worker_Node — 装配测试 ✅" in rendered
|
|
83
|
+
assert "└── RSA ✅" in rendered
|
|
84
|
+
assert "worker_node — Worker_Node" not in rendered
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def test_replace_changes_only_doc_tree_block() -> None:
|
|
88
|
+
original = _claude()
|
|
89
|
+
updated = indexer.replace_doc_tree_section(original, _manifest())
|
|
90
|
+
|
|
91
|
+
assert "old-doc-tree-instruction" not in updated
|
|
92
|
+
assert "old-project-overview" in updated
|
|
93
|
+
assert "manual-before" in updated
|
|
94
|
+
assert f"Last synced: {SYNCED}" in updated
|
|
95
|
+
for section in indexer.REQUIRED_SECTIONS:
|
|
96
|
+
assert updated.count(indexer.section_start(section)) == 1
|
|
97
|
+
assert updated.count(indexer.section_end(section)) == 1
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_replace_preserves_crlf_outside_replaced_block() -> None:
|
|
101
|
+
original = _claude("\r\n")
|
|
102
|
+
updated = indexer.replace_doc_tree_section(original, _manifest())
|
|
103
|
+
assert "\r\nmanual-after-doc-tree-instruction\r\n" in updated
|
|
104
|
+
assert "\n" in updated
|
|
105
|
+
assert updated.replace("\r\n", "").find("\n") == -1
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_marker_missing_duplicate_and_reversed_fail_closed() -> None:
|
|
109
|
+
original = _claude()
|
|
110
|
+
with pytest.raises(indexer.ClaudeIndexError, match="START=0"):
|
|
111
|
+
indexer.validate_required_sections(
|
|
112
|
+
original.replace(indexer.section_start("architecture"), "")
|
|
113
|
+
)
|
|
114
|
+
with pytest.raises(indexer.ClaudeIndexError, match="START=2"):
|
|
115
|
+
indexer.validate_required_sections(
|
|
116
|
+
original + indexer.section_start("architecture")
|
|
117
|
+
)
|
|
118
|
+
start = indexer.section_start("dependencies")
|
|
119
|
+
end = indexer.section_end("dependencies")
|
|
120
|
+
reversed_content = original.replace(
|
|
121
|
+
f"{start}\nold-dependencies\n{end}",
|
|
122
|
+
f"{end}\nold-dependencies\n{start}",
|
|
123
|
+
)
|
|
124
|
+
with pytest.raises(indexer.ClaudeIndexError, match="precedes"):
|
|
125
|
+
indexer.validate_required_sections(reversed_content)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def test_update_is_dry_run_by_default_and_idempotent(tmp_path: Path) -> None:
|
|
129
|
+
path = tmp_path / "CLAUDE.md"
|
|
130
|
+
path.write_text(_claude(), encoding="utf-8")
|
|
131
|
+
before = path.read_bytes()
|
|
132
|
+
|
|
133
|
+
dry = indexer.update_claude_doc_index(path, _manifest())
|
|
134
|
+
assert dry["changed"] is True
|
|
135
|
+
assert path.read_bytes() == before
|
|
136
|
+
|
|
137
|
+
applied = indexer.update_claude_doc_index(path, _manifest(), apply=True)
|
|
138
|
+
assert applied["changed"] is True
|
|
139
|
+
second = indexer.update_claude_doc_index(path, _manifest())
|
|
140
|
+
assert second["changed"] is False
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def test_render_tree_pins_index_first() -> None:
|
|
144
|
+
manifest = _manifest()
|
|
145
|
+
manifest["files"].append(
|
|
146
|
+
{
|
|
147
|
+
"path": "index.md",
|
|
148
|
+
"title": "index — 项目全息总览",
|
|
149
|
+
"status": "✅",
|
|
150
|
+
}
|
|
151
|
+
)
|
|
152
|
+
rendered = indexer.render_document_tree(manifest)
|
|
153
|
+
lines = rendered.splitlines()
|
|
154
|
+
assert lines[0] == "project_docs/"
|
|
155
|
+
assert lines[1] == "├── index — 项目全息总览 ✅"
|
|
156
|
+
assert "├── alpha — 主模块 ✅" in lines
|
|
157
|
+
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Tests for the shared-resolver link CLIs."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
import compute_link
|
|
15
|
+
import link_check
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _write(path: Path, content: str) -> None:
|
|
19
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
20
|
+
path.write_text(content, encoding="utf-8")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _docs(tmp_path: Path) -> Path:
|
|
24
|
+
root = tmp_path / "project_docs"
|
|
25
|
+
_write(root / "index.md", "# Index\n\n[child](section/child)\n")
|
|
26
|
+
_write(
|
|
27
|
+
root / "section" / "child.md",
|
|
28
|
+
"# Child\n\n[root](../index.md#top)\n[code](../../src/tool.py)\n",
|
|
29
|
+
)
|
|
30
|
+
return root
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_compute_relative_link_accepts_legacy_project_docs_prefix(
|
|
34
|
+
tmp_path: Path,
|
|
35
|
+
) -> None:
|
|
36
|
+
root = _docs(tmp_path)
|
|
37
|
+
assert (
|
|
38
|
+
compute_link.compute_relative_link(
|
|
39
|
+
"project_docs/section/child.md",
|
|
40
|
+
"project_docs/index.md",
|
|
41
|
+
str(root),
|
|
42
|
+
must_exist=True,
|
|
43
|
+
)
|
|
44
|
+
== "../index.md"
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def test_batch_compute_validates_shape_and_uses_posix(tmp_path: Path) -> None:
|
|
49
|
+
root = _docs(tmp_path)
|
|
50
|
+
result = compute_link.batch_compute(
|
|
51
|
+
[{"from": "index.md", "to": "section/child.md"}],
|
|
52
|
+
str(root),
|
|
53
|
+
must_exist=True,
|
|
54
|
+
)
|
|
55
|
+
assert result == [
|
|
56
|
+
{
|
|
57
|
+
"from": "index.md",
|
|
58
|
+
"to": "section/child.md",
|
|
59
|
+
"link": "section/child.md",
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
with pytest.raises(compute_link.JsonContractError):
|
|
63
|
+
compute_link.batch_compute([{"from": "index.md"}], str(root))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_validate_doc_links_reports_invalid_and_missing(tmp_path: Path) -> None:
|
|
67
|
+
root = _docs(tmp_path)
|
|
68
|
+
_write(
|
|
69
|
+
root / "bad.md",
|
|
70
|
+
"# Bad\n\n[missing](missing)\n[outside](../../outside.md)\n",
|
|
71
|
+
)
|
|
72
|
+
issues = compute_link.validate_doc_links("bad.md", str(root))
|
|
73
|
+
assert [(item["kind"], item["target"]) for item in issues] == [
|
|
74
|
+
("missing", "missing"),
|
|
75
|
+
("invalid", "../../outside.md"),
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_link_check_is_read_only_and_excludes_quarantine(tmp_path: Path) -> None:
|
|
80
|
+
root = _docs(tmp_path)
|
|
81
|
+
_write(root / ".quarantine" / "r" / "old.md", "# Old\n\n[bad](missing.md)\n")
|
|
82
|
+
before = sorted(path.relative_to(root).as_posix() for path in root.rglob("*"))
|
|
83
|
+
|
|
84
|
+
count, issues = link_check.check_links(root)
|
|
85
|
+
|
|
86
|
+
after = sorted(path.relative_to(root).as_posix() for path in root.rglob("*"))
|
|
87
|
+
assert count == 0
|
|
88
|
+
assert issues == []
|
|
89
|
+
assert before == after
|
|
90
|
+
assert not (root / "_manifest.json").exists()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_link_check_excludes_only_root_quarantine(tmp_path: Path) -> None:
|
|
94
|
+
root = _docs(tmp_path)
|
|
95
|
+
_write(root / ".quarantine" / "r" / "old.md", "# Old\n\n[bad](missing.md)\n")
|
|
96
|
+
nested = "section/.quarantine/current.md"
|
|
97
|
+
_write(root / nested, "# Current\n\n[bad](missing.md)\n")
|
|
98
|
+
|
|
99
|
+
count, issues = link_check.check_links(root)
|
|
100
|
+
|
|
101
|
+
assert count == 1
|
|
102
|
+
assert issues[0]["doc"] == nested
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_link_check_reports_encoded_escape_and_missing(tmp_path: Path) -> None:
|
|
106
|
+
root = _docs(tmp_path)
|
|
107
|
+
_write(
|
|
108
|
+
root / "bad.md",
|
|
109
|
+
"# Bad\n\n[missing](none)\n[escape](%2E%2E/%2E%2E/out.md)\n",
|
|
110
|
+
)
|
|
111
|
+
count, issues = link_check.check_links(root)
|
|
112
|
+
assert count == 2
|
|
113
|
+
assert {(item["kind"], item["target"]) for item in issues} == {
|
|
114
|
+
("missing", "none"),
|
|
115
|
+
("invalid", "%2E%2E/%2E%2E/out.md"),
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_link_check_json_stdout_is_single_document(tmp_path: Path, capsys) -> None:
|
|
120
|
+
root = _docs(tmp_path)
|
|
121
|
+
assert link_check.main(["--project-docs", str(root), "--json"]) == 0
|
|
122
|
+
captured = capsys.readouterr()
|
|
123
|
+
assert captured.err == ""
|
|
124
|
+
payload = json.loads(captured.out)
|
|
125
|
+
assert payload["ok"] is True
|
|
126
|
+
assert payload["manifest_written"] is False
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def test_link_check_reports_invalid_utf8_as_json_error(tmp_path: Path, capsys) -> None:
|
|
130
|
+
root = _docs(tmp_path)
|
|
131
|
+
(root / "corrupt.md").write_bytes(b"# valid\n\xff")
|
|
132
|
+
assert link_check.main(["--project-docs", str(root), "--json"]) == 2
|
|
133
|
+
captured = capsys.readouterr()
|
|
134
|
+
assert captured.err == ""
|
|
135
|
+
payload = json.loads(captured.out)
|
|
136
|
+
assert payload["ok"] is False
|
|
137
|
+
assert "UTF-8" in payload["error"]
|
|
138
|
+
assert not (root / "_manifest.json").exists()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_manifest_only_is_rejected_without_writing(tmp_path: Path, capsys) -> None:
|
|
142
|
+
root = _docs(tmp_path)
|
|
143
|
+
assert (
|
|
144
|
+
link_check.main(["--project-docs", str(root), "--manifest-only", "--json"]) == 2
|
|
145
|
+
)
|
|
146
|
+
payload = json.loads(capsys.readouterr().out)
|
|
147
|
+
assert payload["ok"] is False
|
|
148
|
+
assert not (root / "_manifest.json").exists()
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def test_compute_link_json_mode_reports_mode_errors_cleanly(
|
|
152
|
+
tmp_path: Path, capsys
|
|
153
|
+
) -> None:
|
|
154
|
+
root = _docs(tmp_path)
|
|
155
|
+
assert compute_link.main(["--project-docs", str(root), "--json"]) == 2
|
|
156
|
+
captured = capsys.readouterr()
|
|
157
|
+
assert captured.err == ""
|
|
158
|
+
payload = json.loads(captured.out)
|
|
159
|
+
assert payload["ok"] is False
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_batch_output_is_atomic_json(tmp_path: Path) -> None:
|
|
163
|
+
root = _docs(tmp_path)
|
|
164
|
+
batch = tmp_path / "pairs.json"
|
|
165
|
+
output = tmp_path / "result.json"
|
|
166
|
+
batch.write_text(
|
|
167
|
+
json.dumps({"pairs": [{"from": "index.md", "to": "section/child.md"}]}),
|
|
168
|
+
encoding="utf-8",
|
|
169
|
+
)
|
|
170
|
+
assert (
|
|
171
|
+
compute_link.main(
|
|
172
|
+
[
|
|
173
|
+
"--project-docs",
|
|
174
|
+
str(root),
|
|
175
|
+
"--batch",
|
|
176
|
+
str(batch),
|
|
177
|
+
"--output",
|
|
178
|
+
str(output),
|
|
179
|
+
"--must-exist",
|
|
180
|
+
]
|
|
181
|
+
)
|
|
182
|
+
== 0
|
|
183
|
+
)
|
|
184
|
+
assert (
|
|
185
|
+
json.loads(output.read_text(encoding="utf-8"))[0]["link"] == "section/child.md"
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def test_link_check_rejects_symlink_escape(tmp_path: Path) -> None:
|
|
190
|
+
root = _docs(tmp_path)
|
|
191
|
+
outside = tmp_path / "outside.md"
|
|
192
|
+
_write(outside, "# Outside\n")
|
|
193
|
+
link = root / "escape.md"
|
|
194
|
+
try:
|
|
195
|
+
link.symlink_to(outside)
|
|
196
|
+
except (NotImplementedError, OSError) as exc:
|
|
197
|
+
pytest.skip(f"symlink unavailable: {exc}")
|
|
198
|
+
|
|
199
|
+
with pytest.raises(link_check.PathValidationError):
|
|
200
|
+
link_check.check_links(root)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def test_link_tools_reject_symlinked_docs_root(tmp_path: Path) -> None:
|
|
204
|
+
real_root = tmp_path / "real_docs"
|
|
205
|
+
_write(real_root / "index.md", "# Index\n")
|
|
206
|
+
linked_root = tmp_path / "project_docs"
|
|
207
|
+
try:
|
|
208
|
+
linked_root.symlink_to(real_root, target_is_directory=True)
|
|
209
|
+
except (NotImplementedError, OSError) as exc:
|
|
210
|
+
pytest.skip(f"directory symlink unavailable: {exc}")
|
|
211
|
+
|
|
212
|
+
with pytest.raises(link_check.PathValidationError, match="root"):
|
|
213
|
+
link_check.check_links(linked_root)
|
|
214
|
+
with pytest.raises(link_check.PathValidationError, match="root"):
|
|
215
|
+
compute_link.compute_relative_link(
|
|
216
|
+
"index.md",
|
|
217
|
+
"index.md",
|
|
218
|
+
str(linked_root),
|
|
219
|
+
must_exist=True,
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def test_link_check_rejects_inventory_to_read_symlink_swap(
|
|
224
|
+
tmp_path: Path,
|
|
225
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
226
|
+
) -> None:
|
|
227
|
+
root = tmp_path / "project_docs"
|
|
228
|
+
victim = root / "a.md"
|
|
229
|
+
replacement = root / "b.md"
|
|
230
|
+
_write(victim, "# A\n\n[missing](missing.md)\n")
|
|
231
|
+
_write(replacement, "# B\n")
|
|
232
|
+
original_discover = link_check.discover_active_markdown_paths
|
|
233
|
+
swapped = False
|
|
234
|
+
|
|
235
|
+
def swapping_discover(root_value: Path) -> list[str]:
|
|
236
|
+
nonlocal swapped
|
|
237
|
+
paths = original_discover(root_value)
|
|
238
|
+
if not swapped:
|
|
239
|
+
try:
|
|
240
|
+
victim.unlink()
|
|
241
|
+
victim.symlink_to(replacement)
|
|
242
|
+
except (NotImplementedError, OSError) as exc:
|
|
243
|
+
pytest.skip(f"symlink unavailable: {exc}")
|
|
244
|
+
swapped = True
|
|
245
|
+
return paths
|
|
246
|
+
|
|
247
|
+
monkeypatch.setattr(
|
|
248
|
+
link_check,
|
|
249
|
+
"discover_active_markdown_paths",
|
|
250
|
+
swapping_discover,
|
|
251
|
+
)
|
|
252
|
+
with pytest.raises(link_check.ProjectDocsContractError):
|
|
253
|
+
link_check.check_links(root)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def test_link_check_json_forces_utf8_under_ascii_environment(tmp_path: Path) -> None:
|
|
257
|
+
root = tmp_path / "project_docs"
|
|
258
|
+
_write(root / "中文.md", "# 中文\n\n[缺失](不存在.md)\n")
|
|
259
|
+
environment = dict(os.environ)
|
|
260
|
+
environment["PYTHONIOENCODING"] = "ascii"
|
|
261
|
+
environment["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
262
|
+
|
|
263
|
+
completed = subprocess.run(
|
|
264
|
+
[
|
|
265
|
+
sys.executable,
|
|
266
|
+
str(Path(link_check.__file__)),
|
|
267
|
+
"--project-docs",
|
|
268
|
+
str(root),
|
|
269
|
+
"--json",
|
|
270
|
+
],
|
|
271
|
+
check=False,
|
|
272
|
+
capture_output=True,
|
|
273
|
+
env=environment,
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
payload = json.loads(completed.stdout.decode("utf-8"))
|
|
277
|
+
assert completed.returncode == 1
|
|
278
|
+
assert completed.stderr == b""
|
|
279
|
+
assert payload["broken"][0]["doc"] == "中文.md"
|
|
@@ -0,0 +1,248 @@
|
|
|
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"
|