import json
import unittest
from pathlib import Path

from route_recall import DEFAULT_MAP, ROOT, recall


class RouteRecallTest(unittest.TestCase):
    def test_expected_routes(self):
        cases = {
            "六节点灾备": "ZY-CONTRIB-20260720-001",
            "TestFlight Windows": "ZY-CONTRIB-20260719-001",
            "短剧 Seedance": "ZY-CONTRIB-20260720-002",
            "铸渊恢复路径": "ZY-CONTRIB-20260720-003",
            "HoloLake 0.2.0 工具回执 Windows安装包": "ZY-CONTRIB-20260722-001",
        }
        for query, expected in cases.items():
            with self.subTest(query=query):
                result = recall(query)
                self.assertEqual(result["status"], "FOUND_CONFIDENT_PATH")
                self.assertEqual(result["matches"][0]["contribution_id"], expected)
                self.assertFalse(result["matches"][0]["grants_execution_authority"])

    def test_unknown_does_not_guess(self):
        self.assertEqual(recall("完全不存在的火星工程")["status"], "NO_TRUSTED_PATH")

    def test_all_canonical_paths_exist(self):
        data = json.loads(DEFAULT_MAP.read_text(encoding="utf-8"))
        missing = [path for item in data["contributions"] for path in item["canonical_paths"] if not (ROOT / path).exists()]
        self.assertEqual(missing, [])


if __name__ == "__main__":
    unittest.main()
