import io import json import os import stat import tempfile import unittest from concurrent.futures import ThreadPoolExecutor from contextlib import redirect_stderr, redirect_stdout from pathlib import Path from unittest.mock import Mock, patch from plugins.ita_plugin import BOOKING_FORM_DISCOVERY_STOP_LABEL from tools.debug_ita_book import run_book_recheck_once from tools.debug_ita_monitor import run_monitor from tools.ita_local_handoff import ( ItaLocalHandoffError, ItaLocalSignalStaleError, claim_local_weak_signal, publish_local_weak_signal, ) from vs_types import AvailabilityStatus, VSBookResult, VSQueryResult SERVICE_ID = 1321 ROUTING_KEY = "slot.dub.it.tourist" class _FlowPage: url = "https://prenotami.esteri.it/Services/Booking/1321" def ele(self, _selector, timeout=None): del timeout return None def run_js(self, _script): return { "path": "/Services/Booking/1321", "form": { "method": "POST", "action_path": "/Services/Booking/1321", }, "field_count": 0, "fields": [], "button_count": 1, "buttons": [ { "tag": "button", "type": "submit", "id": "btnAvanti", "name": None, "text": "Avanti", } ], } class _FlowQueryPlugin: last_instance = None def __init__(self, group_id): self.group_id = group_id self.page = _FlowPage() self.query_calls = 0 self.book_calls = 0 self.cleanup_calls = 0 _FlowQueryPlugin.last_instance = self def set_log(self, logger): self.logger = logger def set_config(self, config): self.config = config def create_session(self): return None def query(self, apt_type): self.query_calls += 1 return VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable, ) def cleanup(self): self.cleanup_calls += 1 class _FlowBookPlugin: last_instance = None def __init__(self, group_id): self.group_id = group_id self.page = _FlowPage() self.query_calls = 0 self.book_calls = 0 self.cleanup_calls = 0 self.last_trigger = None _FlowBookPlugin.last_instance = self def set_log(self, logger): self.logger = logger def set_config(self, config): self.config = config def create_session(self): return None def book(self, trigger, user_inputs=None): del user_inputs self.last_trigger = trigger self.book_calls += 1 return VSBookResult( success=False, label=BOOKING_FORM_DISCOVERY_STOP_LABEL, ) def cleanup(self): self.cleanup_calls += 1 class ItaLocalHandoffStorageTests(unittest.TestCase): def test_signal_is_private_metadata_only_and_consumed_once(self): with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) / "handoff" signal_path = publish_local_weak_signal( service_id=SERVICE_ID, routing_key=ROUTING_KEY, issued_at=100.0, handoff_root=root, ) body = signal_path.read_text(encoding="utf-8") payload = json.loads(body) self.assertEqual(payload["service_id"], SERVICE_ID) self.assertEqual(payload["routing_key"], ROUTING_KEY) self.assertEqual(payload["evidence"], "booking-form") self.assertNotIn("username", body.lower()) self.assertNotIn("password", body.lower()) self.assertNotIn("cookie", body.lower()) if os.name == "posix": self.assertEqual(stat.S_IMODE(root.stat().st_mode), 0o700) self.assertEqual(stat.S_IMODE(signal_path.stat().st_mode), 0o600) claimed = claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=110.0, handoff_root=root, ) claimed_again = claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=110.0, handoff_root=root, ) self.assertIsNotNone(claimed) self.assertEqual(claimed.service_id, SERVICE_ID) self.assertIsNone(claimed_again) def test_stale_signal_is_rejected_and_removed(self): with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) / "handoff" publish_local_weak_signal( service_id=SERVICE_ID, routing_key=ROUTING_KEY, issued_at=100.0, handoff_root=root, ) with self.assertRaises(ItaLocalSignalStaleError): claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=131.0, handoff_root=root, ) self.assertIsNone( claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=131.0, handoff_root=root, ) ) def test_two_consumers_can_claim_the_signal_only_once(self): with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) / "handoff" publish_local_weak_signal( service_id=SERVICE_ID, routing_key=ROUTING_KEY, issued_at=100.0, handoff_root=root, ) def claim(): return claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=110.0, handoff_root=root, ) with ThreadPoolExecutor(max_workers=2) as pool: results = list(pool.map(lambda _index: claim(), range(2))) self.assertEqual(sum(item is not None for item in results), 1) def test_wrong_target_is_consumed_and_never_available_for_book(self): with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) / "handoff" publish_local_weak_signal( service_id=9999, routing_key=ROUTING_KEY, issued_at=100.0, handoff_root=root, ) with self.assertRaisesRegex( ItaLocalHandoffError, "service_id does not match", ): claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=110.0, handoff_root=root, ) self.assertIsNone( claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=110.0, handoff_root=root, ) ) class ItaLocalHandoffFlowTests(unittest.TestCase): def test_one_query_weak_signal_triggers_one_independent_book_recheck(self): query_stdout = io.StringIO() book_stdout = io.StringIO() stderr = io.StringIO() with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) / "handoff" def publish(): publish_local_weak_signal( service_id=SERVICE_ID, routing_key=ROUTING_KEY, issued_at=100.0, handoff_root=root, ) with ( patch( "tools.debug_ita_monitor.resolve_chrome_path", return_value=Path("/test/chrome"), ), patch( "tools.debug_ita_monitor._get_query_credentials", return_value=("query@example.test", "query-password"), ), patch( "tools.debug_ita_monitor._prepare_query_profile", return_value=Path("/offline/query-profile"), ), patch( "tools.debug_ita_monitor._bind_query_profile", return_value=Mock(), ), patch("tools.debug_ita_monitor.ItaPlugin", _FlowQueryPlugin), patch.dict("tools.debug_ita_monitor.os.environ", {}, clear=False), redirect_stdout(query_stdout), redirect_stderr(stderr), ): monitor_exit = run_monitor( {}, apply=True, non_interactive=True, min_wait=60, max_wait=60, max_probes=1, publish_local_signal=True, signal_publisher=publish, ) def claim(): return claim_local_weak_signal( expected_service_id=SERVICE_ID, expected_routing_key=ROUTING_KEY, now=110.0, handoff_root=root, ) with ( patch( "tools.debug_ita_book.resolve_chrome_path", return_value=Path("/test/chrome"), ), patch( "tools.debug_ita_book._get_credentials", return_value=("user@example.test", "user-password"), ), patch( "tools.debug_ita_book._assert_distinct_local_accounts" ), patch("tools.debug_ita_book.ItaPlugin", _FlowBookPlugin), patch.dict("tools.debug_ita_book.os.environ", {}, clear=False), redirect_stdout(book_stdout), redirect_stderr(stderr), ): book_exit = run_book_recheck_once( {}, non_interactive=True, wait_for_local_signal=True, signal_timeout=1, signal_claimer=claim, monotonic=lambda: 0.0, sleep=lambda _seconds: None, ) query_plugin = _FlowQueryPlugin.last_instance book_plugin = _FlowBookPlugin.last_instance self.assertEqual(monitor_exit, 4) self.assertEqual(book_exit, 4) self.assertIsNot(query_plugin, book_plugin) self.assertEqual(query_plugin.query_calls, 1) self.assertEqual(query_plugin.book_calls, 0) self.assertEqual(book_plugin.query_calls, 0) self.assertEqual(book_plugin.book_calls, 1) self.assertTrue(book_plugin.last_trigger.success) self.assertEqual( book_plugin.last_trigger.availability_status, AvailabilityStatus.NoneAvailable, ) self.assertEqual(book_plugin.cleanup_calls, 1) self.assertIn( "ITA_MONITOR_HANDOFF_PUBLISHED", query_stdout.getvalue(), ) self.assertIn("ITA_BOOK_SIGNAL_CLAIMED", book_stdout.getvalue()) self.assertIn( "stage=before-avanti submit_attempts=0", book_stdout.getvalue(), ) combined_output = query_stdout.getvalue() + book_stdout.getvalue() for secret in ( "query@example.test", "query-password", "user@example.test", "user-password", ): self.assertNotIn(secret, combined_output) self.assertIsNone(claim()) self.assertEqual(stderr.getvalue(), "") if __name__ == "__main__": unittest.main()