import ast import json import unittest from pathlib import Path from plugins.ita_plugin import ItaPlugin from tools.debug_ita_query import ( DEFAULT_ITA_ROUTING_KEY, DEFAULT_ITA_SERVICE_ID, build_query_config, resolve_debug_route, ) from vs_types import AppointmentType, BizLogicError, VSPlgConfig REPO_ROOT = Path(__file__).resolve().parents[2] ITALY_GROUP_ID = "pernotami.ie.it" def appointment_type(routing_key, *, city): return AppointmentType( routing_key=routing_key, country="Italy", city=city, visa_type="Tourist", ) def probe_route(service_id): return { "service_id": service_id, "capability": "services_probe_only", } class ItaPluginRouteWiringTests(unittest.TestCase): @staticmethod def _plugin(apt_configs): plugin = ItaPlugin.__new__(ItaPlugin) plugin.page = None plugin.root_workspace = "/nonexistent/ita-route-wiring" plugin.tunnel = None plugin.free_config = { "query_mode": "services_probe", "final_submit_enabled": False, "apt_configs": apt_configs, } return plugin def test_two_sede_fixtures_use_the_same_route_resolver(self): first_key = "slot.dub.it.tourist" second_key = "slot.hkg.it.tourist" plugin = self._plugin( { first_key: probe_route(1321), second_key: probe_route(4937), } ) first = plugin._resolve_route( appointment_type(first_key, city="Dublin") ) second = plugin._resolve_route( appointment_type(second_key, city="Hong Kong") ) self.assertEqual(first.routing_key, first_key) self.assertEqual(first.service_id, 1321) self.assertEqual(second.routing_key, second_key) self.assertEqual(second.service_id, 4937) def test_top_level_service_id_is_not_a_runtime_fallback(self): plugin = self._plugin({}) plugin.free_config["service_id"] = 1321 with self.assertRaisesRegex( BizLogicError, "route configuration rejected.*route_missing", ): plugin._resolve_route( appointment_type("slot.sample.it.tourist", city="Sample") ) def test_unknown_capability_fails_closed(self): routing_key = "slot.sample.it.tourist" plugin = self._plugin( { routing_key: { "service_id": 100, "capability": "guessed_live", } } ) with self.assertRaisesRegex( BizLogicError, "route configuration rejected.*capability_unsupported", ): plugin._resolve_route( appointment_type(routing_key, city="Sample") ) def test_cs3_accepts_calendar_mode_but_keeps_final_disabled(self): plugin = ItaPlugin.__new__(ItaPlugin) plugin.page = None plugin._transient_user_data_path = "/nonexistent/transient" plugin.root_workspace = "/nonexistent/root" plugin.tunnel = None plugin.set_config( VSPlgConfig(free_config={"query_mode": "calendar"}) ) self.assertEqual(plugin.free_config["query_mode"], "calendar") for free_config, expected in ( ({"query_mode": "unknown"}, "query_mode"), ({"final_submit_enabled": True}, "final submission requires explicit OTP mode on an applicant Book configuration"), ): with self.subTest(free_config=free_config): with self.assertRaisesRegex(BizLogicError, expected): plugin.set_config(VSPlgConfig(free_config=free_config)) class ItaDebugRouteTests(unittest.TestCase): def test_default_route_is_only_a_backward_compatible_default(self): route = resolve_debug_route({}) self.assertEqual(route.routing_key, DEFAULT_ITA_ROUTING_KEY) self.assertEqual(route.service_id, DEFAULT_ITA_SERVICE_ID) def test_explicit_route_changes_key_service_and_display_metadata_together(self): route = resolve_debug_route( { "ITA_ROUTING_KEY": "slot.sample.it.tourist", "ITA_SERVICE_ID": "9001", "ITA_CITY": "Sample City", "ITA_VISA_TYPE": "Short-stay tourism", } ) config = build_query_config( { "ITA_ROUTING_KEY": route.routing_key, "ITA_SERVICE_ID": str(route.service_id), "ITA_CITY": route.city, "ITA_VISA_TYPE": route.visa_type, }, username="offline-test@example.test", password="offline-password", ) apt_type = route.appointment_type() self.assertEqual(apt_type.routing_key, route.routing_key) self.assertEqual(apt_type.city, route.city) self.assertEqual( config.free_config["apt_configs"][route.routing_key], probe_route(route.service_id), ) self.assertNotIn("service_id", config.free_config) self.assertEqual(config.free_config["query_mode"], "services_probe") self.assertIs(config.free_config["final_submit_enabled"], False) def test_partial_route_override_fails_closed(self): for env in ( {"ITA_SERVICE_ID": "4937"}, {"ITA_ROUTING_KEY": "slot.hkg.it.tourist"}, { "ITA_ROUTING_KEY": "slot.hkg.it.tourist", "ITA_SERVICE_ID": "4937", "ITA_CITY": "Hong Kong", }, ): with self.subTest(env=env): with self.assertRaisesRegex( ValueError, "route override must set", ): resolve_debug_route(env) def test_invalid_explicit_service_id_is_rejected(self): for value in ("0", "-1", "not-an-integer"): with self.subTest(value=value): with self.assertRaisesRegex(ValueError, "ITA_SERVICE_ID"): resolve_debug_route( { "ITA_ROUTING_KEY": "slot.sample.it.tourist", "ITA_SERVICE_ID": value, "ITA_CITY": "Sample City", "ITA_VISA_TYPE": "Short-stay tourism", } ) class ItaProductionConfigParityTests(unittest.TestCase): @staticmethod def _italy_group(path): groups = json.loads(path.read_text(encoding="utf-8")) return next( group for group in groups if group["identifier"] == ITALY_GROUP_ID ) def test_sentinel_and_booker_share_the_same_safe_route_contract(self): sentinel = self._italy_group( REPO_ROOT / "config" / "config_sentinel.json" ) booker = self._italy_group( REPO_ROOT / "config" / "config_booker.json" ) for group in (sentinel, booker): free_config = group["free_config"] self.assertNotIn("service_id", free_config) self.assertEqual(free_config["query_mode"], "services_probe") self.assertIs(free_config["final_submit_enabled"], False) self.assertEqual( sentinel["free_config"]["apt_configs"], booker["free_config"]["apt_configs"], ) routing_key = sentinel["appointment_types"][0]["routing_key"] self.assertIn(routing_key, sentinel["free_config"]["apt_configs"]) def test_runtime_code_has_no_sede_or_service_id_control_branch(self): paths = [ REPO_ROOT / "plugins" / "ita_plugin.py", REPO_ROOT / "tools" / "debug_ita_query.py", REPO_ROOT / "tools" / "debug_ita_book.py", REPO_ROOT / "tools" / "debug_ita_monitor.py", REPO_ROOT / "tools" / "debug_ita_soak.py", REPO_ROOT / "tools" / "debug_ita_calendar_gate.py", REPO_ROOT / "tools" / "ita_local_handoff.py", ] forbidden_constants = {1321, 4937, "Dublin", "Hong Kong", "hkg"} violations = [] for path in paths: tree = ast.parse(path.read_text(encoding="utf-8")) for node in ast.walk(tree): if not isinstance(node, (ast.If, ast.IfExp)): continue for child in ast.walk(node.test): if ( isinstance(child, ast.Constant) and child.value in forbidden_constants ): violations.append((path.name, node.lineno, child.value)) self.assertEqual(violations, []) if __name__ == "__main__": unittest.main()