import unittest

from resolve_route import parse_code_map, resolve_subject_id


class ParseCodeMapTests(unittest.TestCase):
    def test_resolves_requested_identifiers_and_strips_comments(self):
        text = """
REPO-012-WEB=https://example.invalid/guanghu-ice-heart
ICE-GL-ZY001=eternal-lake-heart/heartbeat-core/zhuyuan-persona-system/INDEX.hdlp   # current
LL-004=tcs-core/LL-004-LAKE-LAMP-WAKE-PATH.hdlp
"""

        self.assertEqual(
            parse_code_map(text, ("ICE-GL-ZY001", "LL-004")),
            {
                "ICE-GL-ZY001": "eternal-lake-heart/heartbeat-core/zhuyuan-persona-system/INDEX.hdlp",
                "LL-004": "tcs-core/LL-004-LAKE-LAMP-WAKE-PATH.hdlp",
            },
        )

    def test_ignores_unrequested_identifiers(self):
        self.assertEqual(parse_code_map("OTHER=some/path", ("LL-004",)), {})


class SubjectAliasTests(unittest.TestCase):
    def setUp(self):
        self.alias_map = {
            "mappings": [{
                "canonical_id": "ICE-P-ZY001",
                "subject_kind": "persona_system",
                "aliases": ["ICE-GL-ZY001", "ICE-PZY-001"],
                "route_id": "FD-PERSONA-LOGIN-001",
                "current_path": "current/zhuyuan",
            }],
            "conflicts": [{"id": "ICE-PCA-001", "state": "BLOCKED_DUPLICATE_SUBJECT"}],
        }

    def test_legacy_persona_id_redirects_to_canonical_id(self):
        resolved = resolve_subject_id("ICE-GL-ZY001", self.alias_map)
        self.assertEqual(resolved["canonical_id"], "ICE-P-ZY001")
        self.assertTrue(resolved["redirected"])
        self.assertEqual(resolved["route_id"], "FD-PERSONA-LOGIN-001")

    def test_canonical_persona_id_does_not_redirect(self):
        resolved = resolve_subject_id("ICE-P-ZY001", self.alias_map)
        self.assertEqual(resolved["status"], "RESOLVED")
        self.assertFalse(resolved["redirected"])

    def test_human_id_is_not_rewritten_as_persona(self):
        resolved = resolve_subject_id("ICE-GL∞", self.alias_map)
        self.assertEqual(resolved["status"], "NOT_FOUND_NO_GUESS")
        self.assertIsNone(resolved["canonical_id"])

    def test_conflicted_id_is_rejected_without_guessing(self):
        resolved = resolve_subject_id("ICE-PCA-001", self.alias_map)
        self.assertEqual(resolved["status"], "CONFLICT_REJECTED")
        self.assertIsNone(resolved["canonical_id"])


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