import ast import io import json import os import stat import subprocess import sys import tempfile import unittest from contextlib import redirect_stderr, redirect_stdout from pathlib import Path from unittest.mock import patch from DrissionPage.errors import BrowserConnectError, PageDisconnectedError from plugins.ita_plugin import ItaTargetServiceLinkMissingError from tools.debug_ita_session import DebugConfigurationError from tools.debug_ita_soak import ( DAY_SECONDS, DEFAULT_MAX_SESSION_ATTEMPTS, DEFAULT_MAX_WAIT_SECONDS, DEFAULT_MIN_WAIT_SECONDS, run_soak, write_soak_status, ) from vs_types import ( AvailabilityStatus, SessionExpiredOrInvalidError, VSQueryResult, ) class FakeClock: def __init__(self): self.now = 0.0 def monotonic(self): return self.now def wall_time(self): return 1_800_000_000.0 + self.now def sleep(self, seconds): self.now += seconds class FakeSoakPage: url = "https://prenotami.esteri.it/Services/Booking/1321" title = "Index - Prenot@Mi" 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": 0, "buttons": [], } class FakeSoakPlugin: session_behaviors = [["none"]] create_behaviors = ["ok"] instances = [] def __init__(self, group_id): self.group_id = group_id session_index = len(self.instances) configured_index = min( session_index, len(self.session_behaviors) - 1, ) self.behaviors = list(self.session_behaviors[configured_index]) create_index = min( session_index, len(self.create_behaviors) - 1, ) self.create_behavior = self.create_behaviors[create_index] self.query_calls = 0 self.create_session_calls = 0 self.cleanup_calls = 0 self.page = FakeSoakPage() self.last_booking_form_structure = None self.last_service_list_structure = None self.instances.append(self) def set_log(self, logger): self.logger = logger def set_config(self, config): self.config = config def create_session(self): self.create_session_calls += 1 if self.create_behavior == "connect-error": raise BrowserConnectError("offline browser connection failed") def query(self, apt_type): index = min(self.query_calls, len(self.behaviors) - 1) behavior = self.behaviors[index] self.query_calls += 1 if behavior == "missing-link": self.last_service_list_structure = { "path": "/Services", "ready_state": "complete", "table_count": 1, "row_count": 0, "booking_link_count": 0, "target_link_count": 0, } raise ItaTargetServiceLinkMissingError( "Italy target service link is missing after the bounded wait" ) if behavior == "expired": raise SessionExpiredOrInvalidError("offline session expired") if behavior == "disconnected": raise PageDisconnectedError("offline browser disconnected") if behavior == "weak": return VSQueryResult( success=True, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable, ) if behavior == "error": raise RuntimeError( "private@example.test token abcdefghijklmnopqrstuvwxyz123456" ) return VSQueryResult( success=False, apt_type=apt_type, availability_status=AvailabilityStatus.NoneAvailable, ) def cleanup(self): self.cleanup_calls += 1 class DebugItaSoakTests(unittest.TestCase): def setUp(self): FakeSoakPlugin.session_behaviors = [["none"]] FakeSoakPlugin.create_behaviors = ["ok"] FakeSoakPlugin.instances = [] @staticmethod def _run( *, duration_seconds=120, min_wait=60, max_wait=60, max_session_attempts=DEFAULT_MAX_SESSION_ATTEMPTS, restart_wait=60, sleep=None, ): clock = FakeClock() statuses = [] stdout = io.StringIO() stderr = io.StringIO() if sleep is None: sleep = clock.sleep with ( patch( "tools.debug_ita_soak.resolve_chrome_path", return_value=Path("/test/chrome"), ), patch( "tools.debug_ita_soak._get_query_credentials", return_value=("query@example.test", "query-password"), ) as credential_reader, patch( "tools.debug_ita_soak._prepare_query_profile", return_value=Path("/offline/query-profile"), ) as profile_preparer, patch.dict("tools.debug_ita_soak.os.environ", {}, clear=False), redirect_stdout(stdout), redirect_stderr(stderr), ): exit_code = run_soak( {}, apply=True, non_interactive=True, duration_seconds=duration_seconds, min_wait=min_wait, max_wait=max_wait, max_session_attempts=max_session_attempts, restart_wait=restart_wait, randint=lambda _minimum, _maximum: max_wait, sleep=sleep, monotonic=clock.monotonic, wall_time=clock.wall_time, plugin_factory=FakeSoakPlugin, status_writer=lambda body: statuses.append(dict(body)), ) return ( exit_code, stdout.getvalue(), stderr.getvalue(), clock, statuses, credential_reader, profile_preparer, ) def test_accelerated_full_day_uses_one_session_and_completes(self): result = self._run( duration_seconds=DAY_SECONDS, min_wait=DEFAULT_MAX_WAIT_SECONDS, max_wait=DEFAULT_MAX_WAIT_SECONDS, ) exit_code, stdout, stderr, clock, statuses, _, _ = result plugin = FakeSoakPlugin.instances[0] self.assertEqual(exit_code, 0) self.assertEqual(clock.now, DAY_SECONDS) self.assertEqual(len(FakeSoakPlugin.instances), 1) self.assertEqual(plugin.create_session_calls, 1) self.assertEqual(plugin.query_calls, 288) self.assertEqual(plugin.cleanup_calls, 1) self.assertIn( "ITA_SOAK_COMPLETED duration_seconds=86400 probes=288", stdout, ) self.assertIn("ITA_SOAK_CLEANUP_OK", stdout) self.assertEqual(statuses[-1]["state"], "completed") self.assertEqual(statuses[-1]["elapsed_seconds"], DAY_SECONDS) self.assertEqual(statuses[-1]["exit_code"], 0) self.assertEqual(stderr, "") def test_accelerated_full_day_survives_many_normal_session_expirations(self): FakeSoakPlugin.session_behaviors = [ ["none", "none", "expired"] for _ in range(200) ] result = self._run( duration_seconds=DAY_SECONDS, min_wait=DEFAULT_MAX_WAIT_SECONDS, max_wait=DEFAULT_MAX_WAIT_SECONDS, ) exit_code, stdout, stderr, clock, statuses, _, _ = result self.assertEqual(exit_code, 0) self.assertEqual(clock.now, DAY_SECONDS) self.assertGreaterEqual(statuses[-1]["session_expirations"], 96) self.assertGreaterEqual(statuses[-1]["session_attempts"], 97) self.assertEqual( statuses[-1]["consecutive_session_expirations"], 0, ) self.assertEqual(statuses[-1]["state"], "completed") self.assertIn("ITA_SOAK_COMPLETED duration_seconds=86400", stdout) self.assertNotIn("max-session-attempts-reached", stdout) self.assertEqual(stderr, "") def test_one_missing_link_is_recorded_then_next_probe_can_recover(self): FakeSoakPlugin.session_behaviors = [["missing-link", "none"]] exit_code, stdout, stderr, _, statuses, _, _ = self._run() self.assertEqual(exit_code, 0) self.assertEqual(FakeSoakPlugin.instances[0].query_calls, 2) self.assertIn("ITA_SOAK_TRANSIENT", stdout) self.assertIn("consecutive=1 total=1", stdout) self.assertIn("ITA_SERVICE_LIST_STRUCTURE=", stdout) self.assertEqual(statuses[-1]["transient_errors"], 1) self.assertEqual(stderr, "") def test_second_consecutive_missing_link_stops_without_third_query(self): FakeSoakPlugin.session_behaviors = [ ["missing-link", "missing-link", "none"] ] exit_code, stdout, stderr, _, statuses, _, _ = self._run( duration_seconds=DAY_SECONDS ) self.assertEqual(exit_code, 1) self.assertEqual(FakeSoakPlugin.instances[0].query_calls, 2) self.assertIn( "ITA_SOAK_FAILED reason=consecutive-target-link-misses", stdout, ) self.assertEqual(statuses[-1]["state"], "failed") self.assertEqual(statuses[-1]["exit_code"], 1) self.assertEqual(stderr, "") def test_expired_session_is_cleaned_then_recreated_after_safe_wait(self): FakeSoakPlugin.session_behaviors = [["expired"], ["none"]] exit_code, stdout, stderr, clock, statuses, _, _ = self._run() self.assertEqual(exit_code, 0) self.assertEqual(len(FakeSoakPlugin.instances), 2) self.assertEqual(FakeSoakPlugin.instances[0].cleanup_calls, 1) self.assertEqual(FakeSoakPlugin.instances[1].cleanup_calls, 1) self.assertEqual(clock.now, 120) self.assertIn("ITA_SOAK_SESSION_EXPIRED", stdout) self.assertIn("session_attempts=2", stdout) self.assertEqual(statuses[-1]["session_attempts"], 2) self.assertEqual(stderr, "") def test_second_consecutive_session_expiration_stops_without_third_session(self): FakeSoakPlugin.session_behaviors = [ ["expired"], ["expired"], ["none"], ] exit_code, stdout, stderr, _, statuses, _, _ = self._run( duration_seconds=DAY_SECONDS ) self.assertEqual(exit_code, 1) self.assertEqual(len(FakeSoakPlugin.instances), 2) self.assertIn( "ITA_SOAK_FAILED reason=consecutive-session-expirations", stdout, ) self.assertEqual(statuses[-1]["session_expirations"], 2) self.assertEqual( statuses[-1]["consecutive_session_expirations"], 2, ) self.assertEqual(statuses[-1]["state"], "failed") self.assertEqual(stderr, "") def test_reauthentication_rate_limit_stops_dense_login_loop(self): FakeSoakPlugin.session_behaviors = [ ["none", "expired"] for _ in range(10) ] exit_code, stdout, stderr, _, statuses, _, _ = self._run( duration_seconds=DAY_SECONDS ) self.assertEqual(exit_code, 1) self.assertIn( "ITA_SOAK_FAILED reason=reauthentication-rate-limit", stdout, ) self.assertEqual(statuses[-1]["session_expirations"], 7) self.assertEqual( statuses[-1]["session_reauthentications_last_hour"], 7, ) self.assertEqual(statuses[-1]["state"], "failed") self.assertEqual(stderr, "") def test_disconnected_browser_is_cleaned_then_recreated_after_safe_wait(self): FakeSoakPlugin.session_behaviors = [["disconnected"], ["none"]] exit_code, stdout, stderr, clock, statuses, _, _ = self._run() self.assertEqual(exit_code, 0) self.assertEqual(len(FakeSoakPlugin.instances), 2) self.assertEqual(FakeSoakPlugin.instances[0].cleanup_calls, 1) self.assertEqual(FakeSoakPlugin.instances[1].cleanup_calls, 1) self.assertEqual(clock.now, 120) self.assertIn("ITA_SOAK_BROWSER_DISCONNECTED", stdout) self.assertIn("consecutive=1 total=1", stdout) self.assertEqual(statuses[-1]["browser_disconnects"], 1) self.assertEqual(statuses[-1]["session_attempts"], 2) self.assertEqual(stderr, "") def test_second_consecutive_browser_disconnect_stops_without_third_session(self): FakeSoakPlugin.session_behaviors = [ ["disconnected"], ["disconnected"], ["none"], ] exit_code, stdout, stderr, _, statuses, _, _ = self._run( duration_seconds=DAY_SECONDS ) self.assertEqual(exit_code, 1) self.assertEqual(len(FakeSoakPlugin.instances), 2) self.assertEqual(FakeSoakPlugin.instances[0].cleanup_calls, 1) self.assertEqual(FakeSoakPlugin.instances[1].cleanup_calls, 1) self.assertIn( "ITA_SOAK_FAILED reason=consecutive-browser-disconnects", stdout, ) self.assertEqual(statuses[-1]["browser_disconnects"], 2) self.assertEqual(statuses[-1]["state"], "failed") self.assertEqual(statuses[-1]["exit_code"], 1) self.assertEqual(stderr, "") def test_one_browser_connect_failure_is_retried_after_safe_wait(self): FakeSoakPlugin.create_behaviors = ["connect-error", "ok"] FakeSoakPlugin.session_behaviors = [["none"], ["none"]] exit_code, stdout, stderr, clock, statuses, _, _ = self._run() self.assertEqual(exit_code, 0) self.assertEqual(len(FakeSoakPlugin.instances), 2) self.assertEqual(FakeSoakPlugin.instances[0].cleanup_calls, 1) self.assertEqual(FakeSoakPlugin.instances[1].cleanup_calls, 1) self.assertEqual(clock.now, 120) self.assertIn("ITA_SOAK_BROWSER_CONNECT_FAILED", stdout) self.assertIn("consecutive=1 total=1", stdout) self.assertEqual(statuses[-1]["browser_connect_failures"], 1) self.assertEqual(statuses[-1]["session_attempts"], 2) self.assertEqual(stderr, "") def test_second_browser_connect_failure_stops_without_third_attempt(self): FakeSoakPlugin.create_behaviors = [ "connect-error", "connect-error", "ok", ] exit_code, stdout, stderr, _, statuses, _, _ = self._run( duration_seconds=DAY_SECONDS ) self.assertEqual(exit_code, 1) self.assertEqual(len(FakeSoakPlugin.instances), 2) self.assertIn( "ITA_SOAK_FAILED reason=consecutive-browser-connect-failures", stdout, ) self.assertEqual(statuses[-1]["browser_connect_failures"], 2) self.assertEqual(statuses[-1]["state"], "failed") self.assertEqual(statuses[-1]["exit_code"], 1) self.assertEqual(stderr, "") def test_weak_signal_stops_immediately_without_waiting_or_booking(self): FakeSoakPlugin.session_behaviors = [["weak", "none"]] exit_code, stdout, stderr, clock, statuses, _, _ = self._run( duration_seconds=DAY_SECONDS ) self.assertEqual(exit_code, 4) self.assertEqual(clock.now, 0) self.assertEqual(FakeSoakPlugin.instances[0].query_calls, 1) self.assertIn("ITA_SOAK_WEAK_SIGNAL", stdout) self.assertEqual(statuses[-1]["state"], "weak_signal") self.assertEqual(statuses[-1]["exit_code"], 4) self.assertEqual(stderr, "") def test_default_preflight_reads_no_credentials_and_makes_no_session(self): stdout = io.StringIO() stderr = io.StringIO() with ( patch( "tools.debug_ita_soak._get_query_credentials", side_effect=AssertionError("credentials must not be read"), ), redirect_stdout(stdout), redirect_stderr(stderr), ): exit_code = run_soak( {}, apply=False, non_interactive=True, duration_seconds=DAY_SECONDS, min_wait=DEFAULT_MIN_WAIT_SECONDS, max_wait=DEFAULT_MAX_WAIT_SECONDS, ) self.assertEqual(exit_code, 0) self.assertEqual(FakeSoakPlugin.instances, []) self.assertIn("ITA_SOAK_PREFLIGHT_OK", stdout.getvalue()) self.assertIn("external_request=false", stdout.getvalue()) self.assertEqual(stderr.getvalue(), "") def test_duration_over_one_day_is_rejected_before_credentials(self): with ( patch( "tools.debug_ita_soak._get_query_credentials", side_effect=AssertionError("credentials must not be read"), ), self.assertRaises(DebugConfigurationError), ): run_soak( {}, apply=True, non_interactive=True, duration_seconds=DAY_SECONDS + 1, min_wait=DEFAULT_MIN_WAIT_SECONDS, max_wait=DEFAULT_MAX_WAIT_SECONDS, ) def test_status_file_is_private_and_contains_no_credentials(self): with tempfile.TemporaryDirectory() as temp_dir: status_path = Path(temp_dir) / "ita_soak" / "status.json" write_soak_status( { "state": "running", "service_id": 1321, "elapsed_seconds": 60, "probes": 1, }, status_path=status_path, ) body = status_path.read_text(encoding="utf-8") self.assertEqual(json.loads(body)["state"], "running") self.assertNotIn("username", body.lower()) self.assertNotIn("password", body.lower()) self.assertNotIn("cookie", body.lower()) if os.name == "posix": self.assertEqual( stat.S_IMODE(status_path.parent.stat().st_mode), 0o700, ) self.assertEqual( stat.S_IMODE(status_path.stat().st_mode), 0o600, ) def test_cli_default_is_zero_network_preflight(self): repo_root = Path(__file__).resolve().parents[1] completed = subprocess.run( [sys.executable, str(repo_root / "tools" / "debug_ita_soak.py")], cwd=repo_root, check=False, capture_output=True, text=True, ) self.assertEqual(completed.returncode, 0) self.assertIn("ITA_SOAK_PREFLIGHT_OK", completed.stdout) self.assertIn("duration_seconds=86400", completed.stdout) self.assertIn("external_request=false", completed.stdout) self.assertEqual(completed.stderr, "") def test_source_has_one_query_call_and_no_book_call(self): source_path = ( Path(__file__).resolve().parents[1] / "tools" / "debug_ita_soak.py" ) tree = ast.parse(source_path.read_text(encoding="utf-8")) query_calls = [] book_calls = [] for node in ast.walk(tree): if not isinstance(node, ast.Call): continue if not isinstance(node.func, ast.Attribute): continue if node.func.attr == "query": query_calls.append(node) if node.func.attr == "book": book_calls.append(node) self.assertEqual(len(query_calls), 1) self.assertEqual(book_calls, []) if __name__ == "__main__": unittest.main()