72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
"""`dwg_review.checklist` 单元测试。"""
|
|||
|
|
|
||
|
|
import json
|
||
|
|
|
||
|
|
from dwg_review.checklist import (
|
||
|
|
DEFAULT_CHECKLIST,
|
||
|
|
build_system_prompt,
|
||
|
|
checklist_summary,
|
||
|
|
load_checklist,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_valid_checklist(tmp_path):
|
||
|
|
data = {
|
||
|
|
"name": "自定义清单",
|
||
|
|
"categories": [{"title": "类别A", "severity": "high", "checks": ["检查1"]}],
|
||
|
|
}
|
||
|
|
path = tmp_path / "cl.json"
|
||
|
|
path.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
||
|
|
loaded = load_checklist(path)
|
||
|
|
assert loaded["name"] == "自定义清单"
|
||
|
|
assert loaded["categories"][0]["title"] == "类别A"
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_missing_file_falls_back(tmp_path, capsys):
|
||
|
|
loaded = load_checklist(tmp_path / "nope.json")
|
||
|
|
assert loaded is DEFAULT_CHECKLIST
|
||
|
|
assert "无法加载审查清单" in capsys.readouterr().out
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_malformed_structure_falls_back(tmp_path, capsys):
|
||
|
|
path = tmp_path / "cl.json"
|
||
|
|
path.write_text(json.dumps({"name": "缺 categories"}), encoding="utf-8")
|
||
|
|
assert load_checklist(path) is DEFAULT_CHECKLIST
|
||
|
|
assert "结构异常" in capsys.readouterr().out
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_invalid_json_falls_back(tmp_path, capsys):
|
||
|
|
path = tmp_path / "cl.json"
|
||
|
|
path.write_text("{ not json", encoding="utf-8")
|
||
|
|
assert load_checklist(path) is DEFAULT_CHECKLIST
|
||
|
|
assert "无法加载审查清单" in capsys.readouterr().out
|
||
|
|
|
||
|
|
|
||
|
|
def test_load_empty_categories_falls_back(tmp_path):
|
||
|
|
path = tmp_path / "cl.json"
|
||
|
|
path.write_text(json.dumps({"categories": []}), encoding="utf-8")
|
||
|
|
assert load_checklist(path) is DEFAULT_CHECKLIST
|
||
|
|
|
||
|
|
|
||
|
|
def test_build_system_prompt_includes_categories_and_checks():
|
||
|
|
prompt = build_system_prompt(DEFAULT_CHECKLIST)
|
||
|
|
assert DEFAULT_CHECKLIST["name"] in prompt
|
||
|
|
for cat in DEFAULT_CHECKLIST["categories"]:
|
||
|
|
assert cat["title"] in prompt
|
||
|
|
assert f"严重度:{cat['severity']}" in prompt
|
||
|
|
for chk in cat["checks"]:
|
||
|
|
assert chk in prompt
|
||
|
|
|
||
|
|
|
||
|
|
def test_build_system_prompt_tool_hint_toggle():
|
||
|
|
with_tools = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=True)
|
||
|
|
without = build_system_prompt(DEFAULT_CHECKLIST, allow_tools=False)
|
||
|
|
assert "只读" in with_tools and "取证工具" in with_tools
|
||
|
|
assert "只读" not in without
|
||
|
|
|
||
|
|
|
||
|
|
def test_checklist_summary_mentions_name_and_count():
|
||
|
|
summary = checklist_summary(DEFAULT_CHECKLIST)
|
||
|
|
assert DEFAULT_CHECKLIST["name"] in summary
|
||
|
|
assert str(len(DEFAULT_CHECKLIST["categories"])) in summary
|
||
|
|
assert DEFAULT_CHECKLIST["categories"][0]["title"] in summary
|