test_rebuild_index_contract.py
4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""rebuild_index 的分组契约。
注意:`src.tools.wiki_tools` 可以直接导入,不需要给 agentscope 打桩。
"""
import os
import tempfile
import unittest
from pathlib import Path
import frontmatter
from src.tools import wiki_tools
from src.utils.ingest_contract import load_ingest_contract
def _write_page(wiki_dir: Path, folder: str, slug: str, page_type: str) -> None:
path = wiki_dir / folder / f"{slug}.md"
path.parent.mkdir(parents=True, exist_ok=True)
post = frontmatter.Post("正文", type=page_type, title=slug, series="测试", brand=["问界"])
with path.open("w", encoding="utf-8") as handle:
frontmatter.dump(post, handle)
class RebuildIndexGroupingTest(unittest.TestCase):
def test_singular_alias_groups_with_canonical_type(self):
"""单数别名(product)与规范名(products)必须归进同一分组,不能分裂成两组。
这是有意为之的设计:摄入时模型偶尔写单数,靠别名归一。
注意 troubleshooting 在契约里**本身就是单数**,是个例外,勿"顺手改成复数"。
"""
with tempfile.TemporaryDirectory() as temp_dir:
wiki_dir = Path(temp_dir)
_write_page(wiki_dir, "products", "plural", "products")
_write_page(wiki_dir, "products", "singular", "product")
_write_page(wiki_dir, "troubleshooting", "repair", "troubleshooting")
count = wiki_tools.rebuild_index(os.fspath(wiki_dir))
index = (wiki_dir / "index.md").read_text(encoding="utf-8")
self.assertEqual(count, 3)
self.assertIn("## 产品(products/)", index)
self.assertIn("## 系统排障(troubleshooting/)", index)
self.assertIn("[[plural]]", index)
self.assertIn("[[singular]]", index)
self.assertNotIn("## 未分类", index, "合法 type 不该掉进未分类")
self.assertLess(
index.index("## 产品"), index.index("## 系统排障"),
"分组顺序应遵循 _INDEX_TYPE_GROUPS 的声明顺序",
)
def test_unknown_type_falls_into_uncategorized(self):
"""契约里没有的 type 要掉进「未分类」,而不是被静默塞进某个正常分组。"""
with tempfile.TemporaryDirectory() as temp_dir:
wiki_dir = Path(temp_dir)
_write_page(wiki_dir, "weird", "odd", "troubleshootings") # 复数形式不在契约里
wiki_tools.rebuild_index(os.fspath(wiki_dir))
index = (wiki_dir / "index.md").read_text(encoding="utf-8")
self.assertIn("## 未分类", index)
self.assertIn("[[odd]]", index)
class TypeTableConsistencyTest(unittest.TestCase):
def test_index_groups_match_contract_canonical_types(self):
"""wiki_tools 的 index 分组表与 ingest 契约的 canonical types 必须一一对应。
这两处各自维护了一份 type 表(wiki_tools._INDEX_TYPE_GROUPS/_INDEX_TYPE_ALIAS
vs ingest_contract.type_aliases)。一旦漂移,index.md 的分组与 lint 的校验就会
互相打架(页面通过 lint 却在 index 里掉进未分类,或反之)。本用例是那道护栏。
"""
contract_canonical = set(load_ingest_contract().type_aliases.values())
index_group_keys = {key for key, _heading in wiki_tools._INDEX_TYPE_GROUPS}
self.assertEqual(
index_group_keys,
contract_canonical,
"index 分组与契约 canonical types 已漂移:"
f"仅契约有={sorted(contract_canonical - index_group_keys)},"
f"仅 index 有={sorted(index_group_keys - contract_canonical)}",
)
def test_alias_normalization_agrees_with_contract(self):
"""对契约认识的每个别名,wiki_tools 的归一结果必须与契约一致。"""
contract = load_ingest_contract()
for raw, canonical in contract.type_aliases.items():
with self.subTest(raw=raw):
self.assertEqual(
wiki_tools._normalize_index_type(raw),
canonical,
f"别名 {raw!r} 在 wiki_tools 与契约中归一结果不同",
)
if __name__ == "__main__":
unittest.main()