"""规范索引维护 Skill 的确定性行为测试。""" import json from pathlib import Path import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] SKILL = ROOT / "plugins" / "skill" / "skills" / "guidance-edit" SCRIPT = SKILL / "scripts" / "check_index.py" class GuidanceEditTest(unittest.TestCase): """验证索引检查器和读写职责边界。""" def run_check(self, standards: Path) -> subprocess.CompletedProcess[str]: """以 JSON 模式执行检查器。""" return subprocess.run( [sys.executable, str(SCRIPT), str(standards), "--json"], check=False, capture_output=True, text=True, encoding="utf-8", ) def test_clean_index_is_reachable(self) -> None: """根索引能够逐级访问所有规范时应检查通过。""" with tempfile.TemporaryDirectory() as temp: standards = Path(temp) (standards / "frontend").mkdir() (standards / "index.md").write_text( "[前端](frontend/index.md)\n", encoding="utf-8" ) (standards / "frontend" / "index.md").write_text( "[组件](components.md)\n", encoding="utf-8" ) (standards / "frontend" / "components.md").write_text( "# 组件\n", encoding="utf-8" ) result = self.run_check(standards) self.assertEqual(0, result.returncode, result.stderr) report = json.loads(result.stdout) self.assertEqual([], report["unindexed"]) self.assertEqual([], report["broken"]) def test_reports_unindexed_broken_and_cycle(self) -> None: """未索引文件、失效链接和循环引用应同时报告。""" with tempfile.TemporaryDirectory() as temp: standards = Path(temp) (standards / "index.md").write_text( "[主题](topic.md)\n[缺失](missing.md)\n", encoding="utf-8" ) (standards / "topic.md").write_text( "[返回](index.md)\n", encoding="utf-8" ) (standards / "orphan.md").write_text("# 未索引\n", encoding="utf-8") result = self.run_check(standards) self.assertEqual(1, result.returncode) report = json.loads(result.stdout) self.assertEqual(["orphan.md"], report["unindexed"]) self.assertEqual("missing.md", report["broken"][0]["target"]) self.assertTrue(report["cycles"]) def test_missing_entry_is_input_error(self) -> None: """入口不存在时应返回输入错误,而不是覆盖问题。""" with tempfile.TemporaryDirectory() as temp: result = self.run_check(Path(temp)) self.assertEqual(2, result.returncode) self.assertIn("索引入口不存在", result.stderr) def test_skill_separates_read_and_write_modes(self) -> None: """检查默认只读,所有写入均需预览与确认。""" content = (SKILL / "SKILL.md").read_text(encoding="utf-8") self.assertIn("检查默认只读", content) self.assertIn("经用户确认后写入", content) self.assertIn("不强制创建固定层级", content) if __name__ == "__main__": unittest.main()