| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377 |
- import ast
- import hashlib
- import json
- import unittest
- from contextlib import redirect_stdout
- from io import StringIO
- from pathlib import Path
- from types import SimpleNamespace
- from unittest.mock import patch
- from plugins.ita_calendar_query import CALENDAR_GUARD_SCRIPT
- from plugins.ita_protocol import InventoryState, resolve_route_config
- from tools.debug_ita_calendar_gate import (
- BookingPreflightStage,
- CalendarGateError,
- CalendarGateEvidence,
- CalendarMonthScanResult,
- CalendarNextMonthUnavailableError,
- CalendarProbeResult,
- NEXT_MONTH_CONTROL_SCRIPT,
- OneShotAction,
- QCAL_TOOL_CONTRACT_VERSION,
- QCAL_TOOL_SOURCE_FINGERPRINT,
- ReservationSnapshot,
- _click_next_month,
- _enter_otp,
- _read_manual_otp,
- build_calendar_gate_config,
- listen_once,
- parse_availability_packet,
- read_reservation_snapshot,
- resolve_qcal_forward_months,
- run_calendar_gate,
- safe_avanti_failure_diagnostic,
- safe_next_month_diagnostic,
- safe_preflight_diagnostic,
- scan_calendar_horizon,
- validate_avanti_packet,
- validate_booking_preflight,
- validate_next_month_snapshot,
- validate_otp_packet,
- wait_for_booking_preflight,
- )
- def gate_env():
- return {
- "ITA_ROUTING_KEY": "slot.hkg.it.tourist",
- "ITA_SERVICE_ID": "4937",
- "ITA_CITY": "Hong Kong",
- "ITA_VISA_TYPE": "Tourist",
- "ITA_CALENDAR_LOOKAHEAD_MONTHS": "3",
- "ITA_BOOKING_TYPE_CONTROL_ID": "typeofbookingddl",
- "ITA_BOOKING_TYPE_VALUE": "1",
- "ITA_BOOKING_TYPE_LABELS": "Prenotazione Singola|Single booking",
- "ITA_PURPOSE_CONTROL_ID": "ddls_0",
- "ITA_PURPOSE_VALUE": "42",
- "ITA_PURPOSE_LABELS": "Turismo|Tourism",
- }
- def booking_preflight_snapshot(**changes):
- snapshot = {
- "pathMatches": True,
- "formPresent": True,
- "bookingTypeOptionMatched": True,
- "purposeOptionMatched": True,
- "bookingVisibleMatches": True,
- "bookingHiddenMatches": True,
- "companionsHiddenIsZero": True,
- "purposeVisibleMatches": True,
- "purposeHiddenIsPreSubmit": True,
- "privacyChecked": True,
- "otpInputCount": 1,
- "otpValuePresent": False,
- "otpButtonCount": 1,
- "otpDisabled": False,
- "avantiButtonCount": 1,
- "avantiDisabled": False,
- "turnstileCount": 1,
- "turnstileTokenPresent": True,
- "unknownRequiredCount": 0,
- }
- snapshot.update(changes)
- return snapshot
- class FakeResponse:
- def __init__(self, status, *, url="", raw_body="", headers=None):
- self.status = status
- self.url = url
- self.raw_body = raw_body
- self.headers = headers or {}
- class FakePacket:
- def __init__(
- self,
- *,
- method,
- url,
- status,
- response_url="",
- raw_body="",
- headers=None,
- post_data=None,
- ):
- self.method = method
- self.url = url
- self.response = FakeResponse(
- status,
- url=response_url,
- raw_body=raw_body,
- headers=headers,
- )
- self.request = SimpleNamespace(postData=post_data)
- class FakeListener:
- def __init__(self, packet=None):
- self.packet = packet
- self.starts = []
- self.waits = []
- self.stop_count = 0
- def start(self, target, method=None):
- self.starts.append((target, method))
- def wait(self, timeout=None):
- self.waits.append(timeout)
- return self.packet
- def stop(self):
- self.stop_count += 1
- class FakeReservationTab:
- def __init__(self, outputs):
- self.outputs = list(outputs)
- self.wait = SimpleNamespace(doc_loaded=lambda: None)
- self.close_count = 0
- def run_js(self, _script):
- if len(self.outputs) > 1:
- return self.outputs.pop(0)
- return self.outputs[0]
- def close(self):
- self.close_count += 1
- class CalendarGateConfigTests(unittest.TestCase):
- def test_builds_one_explicit_live_route_with_final_disabled(self):
- config = build_calendar_gate_config(
- gate_env(),
- username="offline@example.test",
- password="not-a-real-password",
- )
- free_config = config.free_config
- route = resolve_route_config(
- free_config["apt_configs"],
- "slot.hkg.it.tourist",
- )
- self.assertEqual(free_config["query_mode"], "services_probe")
- self.assertIs(free_config["final_submit_enabled"], False)
- self.assertEqual(route.service_id, 4937)
- self.assertEqual(route.capability.value, "single_schengen_tourism")
- self.assertEqual(route.calendar_scan.lookahead_months, 3)
- self.assertEqual(route.booking_type.control_id, "typeofbookingddl")
- self.assertEqual(route.booking_type.value, "1")
- self.assertEqual(route.purpose.value, "42")
- def test_missing_any_live_route_field_fails_closed(self):
- for missing in (
- "ITA_CALENDAR_LOOKAHEAD_MONTHS",
- "ITA_BOOKING_TYPE_CONTROL_ID",
- "ITA_BOOKING_TYPE_LABELS",
- "ITA_PURPOSE_VALUE",
- ):
- with self.subTest(missing=missing):
- env = gate_env()
- env.pop(missing)
- with self.assertRaisesRegex(CalendarGateError, missing):
- build_calendar_gate_config(
- env,
- username="offline@example.test",
- password="not-a-real-password",
- )
- def test_resolves_a_separate_bounded_discovery_horizon(self):
- self.assertEqual(resolve_qcal_forward_months({}), 1)
- self.assertEqual(
- resolve_qcal_forward_months({"ITA_QCAL_FORWARD_MONTHS": "12"}),
- 12,
- )
- for invalid in ("0", "13", "invalid", "1.5"):
- with self.subTest(invalid=invalid):
- with self.assertRaisesRegex(
- CalendarGateError,
- "ITA_QCAL_FORWARD_MONTHS",
- ):
- resolve_qcal_forward_months(
- {"ITA_QCAL_FORWARD_MONTHS": invalid}
- )
- class GateValueObjectTests(unittest.TestCase):
- def test_reservation_snapshot_requires_nonnegative_plain_integers(self):
- self.assertEqual(
- ReservationSnapshot(reservations=0, waitlist=1).total,
- 1,
- )
- for kwargs in (
- {"reservations": -1, "waitlist": 0},
- {"reservations": True, "waitlist": 0},
- {"reservations": 0, "waitlist": "0"},
- ):
- with self.subTest(kwargs=kwargs):
- with self.assertRaises(CalendarGateError):
- ReservationSnapshot(**kwargs)
- def test_gate_evidence_passes_only_the_exact_qcal_contract(self):
- empty = ReservationSnapshot(0, 0)
- evidence = CalendarGateEvidence(
- before=empty,
- after=empty,
- otp_requests=1,
- avanti_posts=1,
- availability_requests=2,
- final_requests=0,
- calendar_ready=True,
- )
- self.assertTrue(evidence.passed)
- variants = (
- {"after": ReservationSnapshot(1, 0)},
- {"otp_requests": 0},
- {"avanti_posts": 2},
- {"availability_requests": 1},
- {"final_requests": 1},
- {"calendar_ready": False},
- )
- for changes in variants:
- with self.subTest(changes=changes):
- values = {
- "before": evidence.before,
- "after": evidence.after,
- "otp_requests": evidence.otp_requests,
- "avanti_posts": evidence.avanti_posts,
- "availability_requests": evidence.availability_requests,
- "final_requests": evidence.final_requests,
- "calendar_ready": evidence.calendar_ready,
- }
- values.update(changes)
- self.assertFalse(CalendarGateEvidence(**values).passed)
- def test_one_shot_action_marks_attempt_before_calling(self):
- calls = []
- guard = OneShotAction("OTP")
- self.assertEqual(guard.run(lambda: calls.append("called") or 7), 7)
- self.assertEqual(calls, ["called"])
- with self.assertRaisesRegex(CalendarGateError, "OTP.*already attempted"):
- guard.run(lambda: calls.append("second"))
- self.assertEqual(calls, ["called"])
- failing = OneShotAction("AVANTI")
- with self.assertRaisesRegex(RuntimeError, "ambiguous"):
- failing.run(
- lambda: (_ for _ in ()).throw(RuntimeError("ambiguous"))
- )
- with self.assertRaisesRegex(CalendarGateError, "already attempted"):
- failing.run(lambda: None)
- class NetworkContractTests(unittest.TestCase):
- def test_listener_always_stops_and_performs_one_action(self):
- packet = object()
- listener = FakeListener(packet)
- page = SimpleNamespace(listen=listener)
- actions = []
- result = listen_once(
- page,
- target="/safe-endpoint",
- method="POST",
- action=lambda: actions.append("one"),
- timeout=9,
- )
- self.assertIs(result, packet)
- self.assertEqual(actions, ["one"])
- self.assertEqual(listener.starts, [("/safe-endpoint", "POST")])
- self.assertEqual(listener.waits, [9])
- self.assertEqual(listener.stop_count, 1)
- failing_listener = FakeListener(None)
- failing_page = SimpleNamespace(listen=failing_listener)
- with self.assertRaisesRegex(CalendarGateError, "not observed"):
- listen_once(
- failing_page,
- target="/safe-endpoint",
- method="GET",
- action=lambda: None,
- timeout=1,
- )
- self.assertEqual(failing_listener.stop_count, 1)
- raising_listener = FakeListener(packet)
- raising_page = SimpleNamespace(listen=raising_listener)
- with self.assertRaisesRegex(RuntimeError, "action failed"):
- listen_once(
- raising_page,
- target="/safe-endpoint",
- method="POST",
- action=lambda: (_ for _ in ()).throw(
- RuntimeError("action failed")
- ),
- timeout=1,
- )
- self.assertEqual(raising_listener.stop_count, 1)
- def test_otp_requires_exact_get_route_status_and_json_string(self):
- packet = FakePacket(
- method="GET",
- url=(
- "https://prenotami.esteri.it/BookingCalendar/GenerateOTP"
- "?ServiceID=4937"
- ),
- status=200,
- raw_body='"SENT"',
- )
- validate_otp_packet(packet, service_id=4937)
- bad_packets = (
- FakePacket(
- method="POST",
- url=packet.url,
- status=200,
- raw_body='"SENT"',
- ),
- FakePacket(
- method="GET",
- url=packet.url,
- status=201,
- raw_body='"SENT"',
- ),
- FakePacket(
- method="GET",
- url=packet.url,
- status=200,
- raw_body="SENT",
- ),
- FakePacket(
- method="GET",
- url=packet.url.replace("4937", "1321"),
- status=200,
- raw_body='"SENT"',
- ),
- )
- for bad in bad_packets:
- with self.subTest(packet=bad):
- with self.assertRaises(CalendarGateError):
- validate_otp_packet(bad, service_id=4937)
- def test_avanti_accepts_direct_302_to_the_observed_calendar(self):
- packet = FakePacket(
- method="POST",
- url="https://prenotami.esteri.it/Services/Booking/4937",
- status=302,
- headers={
- "Location": (
- "/BookingCalendar?selectedService="
- "Visti%20Nazionali%20e%20Schengen"
- )
- },
- )
- mode = validate_avanti_packet(
- packet,
- booking_path="/Services/Booking/4937",
- final_page_url=(
- "https://prenotami.esteri.it/BookingCalendar"
- "?selectedService=Visti"
- ),
- )
- self.assertEqual(mode, "direct_302")
- def test_avanti_accepts_only_a_triply_consistent_followed_200(self):
- packet = FakePacket(
- method="POST",
- url="https://prenotami.esteri.it/Services/Booking/4937",
- status=200,
- response_url=(
- "https://prenotami.esteri.it/BookingCalendar"
- "?selectedService=Visti"
- ),
- )
- mode = validate_avanti_packet(
- packet,
- booking_path="/Services/Booking/4937",
- final_page_url="https://prenotami.esteri.it/BookingCalendar",
- )
- self.assertEqual(mode, "followed_redirect_200")
- def test_avanti_rejects_inconsistent_redirect_evidence(self):
- cases = (
- {
- "status": 200,
- "response_url": "https://prenotami.esteri.it/Services",
- "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
- },
- {
- "status": 200,
- "response_url": "https://example.test/BookingCalendar",
- "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
- },
- {
- "status": 200,
- "response_url": (
- "https://prenotami.esteri.it:444/BookingCalendar"
- ),
- "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
- },
- {
- "status": 200,
- "response_url": "https://prenotami.esteri.it/BookingCalendar",
- "final_page_url": "https://prenotami.esteri.it/Services",
- },
- {
- "status": 201,
- "response_url": "https://prenotami.esteri.it/BookingCalendar",
- "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
- },
- )
- for case in cases:
- with self.subTest(case=case):
- bad = FakePacket(
- method="POST",
- url=(
- "https://prenotami.esteri.it/Services/Booking/4937"
- ),
- status=case["status"],
- response_url=case["response_url"],
- )
- with self.assertRaises(CalendarGateError):
- validate_avanti_packet(
- bad,
- booking_path="/Services/Booking/4937",
- final_page_url=case["final_page_url"],
- )
- def test_avanti_rejects_bad_direct_redirects_and_requests(self):
- for method, request_url, location in (
- (
- "POST",
- "https://prenotami.esteri.it/Services/Booking/4937",
- "/Services",
- ),
- (
- "POST",
- "https://prenotami.esteri.it/Services/Booking/4937",
- "https://example.test/BookingCalendar",
- ),
- (
- "POST",
- "https://prenotami.esteri.it/Services/Booking/4937?retry=1",
- "/BookingCalendar",
- ),
- (
- "GET",
- "https://prenotami.esteri.it/Services/Booking/4937",
- "/BookingCalendar",
- ),
- (
- "POST",
- "https://prenotami.esteri.it/Services/Booking/1321",
- "/BookingCalendar",
- ),
- (
- "POST",
- "https://example.test/Services/Booking/4937",
- "/BookingCalendar",
- ),
- (
- "POST",
- "https://prenotami.esteri.it/Services/Booking/4937",
- "",
- ),
- ):
- with self.subTest(
- method=method,
- request_url=request_url,
- location=location,
- ):
- packet = FakePacket(
- method=method,
- url=request_url,
- status=302,
- headers={"Location": location},
- )
- with self.assertRaises(CalendarGateError):
- validate_avanti_packet(
- packet,
- booking_path="/Services/Booking/4937",
- final_page_url=(
- "https://prenotami.esteri.it/BookingCalendar"
- ),
- )
- def test_avanti_failure_diagnostic_is_strictly_allowlisted(self):
- packet = FakePacket(
- method="POST",
- url="https://prenotami.esteri.it/Services/Booking/4937",
- status=200,
- response_url=(
- "https://prenotami.esteri.it/Services/Booking/4937"
- "?private-response-query"
- ),
- raw_body="private-response-body",
- headers={
- "Location": (
- "/BookingCalendar?private-location-query"
- )
- },
- post_data="private-form-data",
- )
- raw = {
- "path": "/Services/Booking/4937",
- "bookingFormPresent": True,
- "otpValuePresent": True,
- "otpInvalidVisible": False,
- "avantiDisabled": False,
- "turnstileTokenPresent": False,
- "visibleValidationCount": 1,
- "visibleConfirmCount": 0,
- "finalRequestCount": 0,
- "mustNotLeak": "private-runtime-value",
- }
- diagnostic = safe_avanti_failure_diagnostic(
- packet,
- raw,
- booking_path="/Services/Booking/4937",
- )
- self.assertEqual(
- set(diagnostic),
- {
- "outcome",
- "contract",
- "sourceFingerprint",
- "snapshotType",
- "bookingPostObserved",
- "requestMatches",
- "responseStatus",
- "responsePath",
- "locationPath",
- "browserPath",
- "bookingFormPresent",
- "otpValuePresent",
- "otpInvalidVisible",
- "avantiDisabled",
- "turnstileReady",
- "visibleValidationCount",
- "visibleConfirmCount",
- "finalRequestCount",
- },
- )
- self.assertEqual(diagnostic["outcome"], "booking_form_returned")
- self.assertIs(diagnostic["bookingPostObserved"], True)
- self.assertIs(diagnostic["requestMatches"], True)
- self.assertEqual(
- diagnostic["responseStatus"],
- {"type": "int", "value": 200},
- )
- self.assertEqual(diagnostic["responsePath"], "booking")
- self.assertEqual(diagnostic["locationPath"], "calendar")
- self.assertEqual(diagnostic["browserPath"], "booking")
- self.assertEqual(
- diagnostic["otpValuePresent"],
- {"type": "bool", "value": True},
- )
- serialized = json.dumps(diagnostic)
- for private_value in (
- "private-response-query",
- "private-response-body",
- "private-location-query",
- "private-form-data",
- "private-runtime-value",
- ):
- self.assertNotIn(private_value, serialized)
- def test_avanti_failure_diagnostic_degrades_unknown_values(self):
- diagnostic = safe_avanti_failure_diagnostic(
- None,
- "private-runtime-value",
- booking_path="/Services/Booking/4937",
- )
- self.assertEqual(
- diagnostic["outcome"],
- "booking_post_not_observed",
- )
- self.assertIs(diagnostic["bookingPostObserved"], False)
- self.assertEqual(diagnostic["responsePath"], "missing")
- self.assertEqual(diagnostic["browserPath"], "missing")
- self.assertEqual(
- diagnostic["otpValuePresent"],
- {"type": "NoneType", "value": None},
- )
- self.assertNotIn(
- "private-runtime-value",
- json.dumps(diagnostic),
- )
- def test_availability_packet_reuses_the_strict_protocol_parser(self):
- inner = json.dumps(
- [
- {
- "DateLibere": "01/10/2026 00:00:00",
- "SlotLiberi": 0,
- "SlotRimanenti": -1,
- },
- {
- "DateLibere": "02/10/2026 00:00:00",
- "SlotLiberi": 1,
- "SlotRimanenti": 0,
- },
- ]
- )
- packet = FakePacket(
- method="POST",
- url=(
- "https://prenotami.esteri.it/BookingCalendar/"
- "RetrieveCalendarAvailability"
- ),
- status=200,
- raw_body=json.dumps(inner),
- post_data=json.dumps(
- {"_Servizio": "runtime-only", "selectedDay": "01/10/2026"}
- ),
- )
- result = parse_availability_packet(packet)
- self.assertEqual(result.item_count, 2)
- self.assertEqual(result.available_count, 1)
- self.assertEqual(result.selected_day, "01/10/2026")
- self.assertEqual(result.selected_month, (2026, 10))
- self.assertEqual(result.days[0].state, InventoryState.UNAVAILABLE)
- self.assertEqual(result.days[1].state, InventoryState.AVAILABLE)
- packet.request.postData = json.dumps(
- {
- "_Servizio": "runtime-only",
- "selectedDay": "01/10/2026",
- "guessed": True,
- }
- )
- with self.assertRaisesRegex(CalendarGateError, "request keys"):
- parse_availability_packet(packet)
- class NextMonthControlTests(unittest.TestCase):
- def test_snapshot_requires_one_actionable_control_and_exact_shape(self):
- ready = {
- "pathMatches": True,
- "candidateCount": 4,
- "actionableCount": 1,
- "clicked": False,
- }
- self.assertIs(
- validate_next_month_snapshot(
- ready,
- expected_clicked=False,
- ),
- ready,
- )
- unready = {**ready, "actionableCount": 0}
- validate_next_month_snapshot(
- unready,
- expected_clicked=False,
- allow_unready=True,
- )
- for changed, message in (
- ({**ready, "actionableCount": 2}, "uniquely actionable"),
- ({**ready, "candidateCount": True}, "candidateCount"),
- ({**ready, "clicked": True}, "acknowledgement"),
- ({**ready, "private": "value"}, "unexpected fields"),
- ):
- with self.subTest(changed=changed):
- with self.assertRaisesRegex(CalendarGateError, message):
- validate_next_month_snapshot(
- changed,
- expected_clicked=False,
- )
- def test_click_waits_then_atomically_clicks_once(self):
- outputs = [
- {
- "pathMatches": True,
- "candidateCount": 4,
- "actionableCount": 0,
- "clicked": False,
- },
- {
- "pathMatches": True,
- "candidateCount": 4,
- "actionableCount": 1,
- "clicked": False,
- },
- {
- "pathMatches": True,
- "candidateCount": 4,
- "actionableCount": 1,
- "clicked": True,
- },
- ]
- calls = []
- def run_js(script, should_click):
- calls.append((script, should_click))
- return outputs.pop(0)
- stdout = StringIO()
- with redirect_stdout(stdout):
- with patch("tools.debug_ita_calendar_gate.time.sleep"):
- _click_next_month(
- SimpleNamespace(run_js=run_js),
- timeout=1,
- )
- self.assertEqual(
- [should_click for _script, should_click in calls],
- [False, False, True],
- )
- self.assertTrue(
- all(script == NEXT_MONTH_CONTROL_SCRIPT for script, _ in calls)
- )
- self.assertEqual(
- stdout.getvalue().count("ITA_QCAL_NEXT_MONTH_DIAGNOSTIC="),
- 1,
- )
- self.assertIn('"clicked": {"type": "bool", "value": true}', stdout.getvalue())
- def test_click_uses_a_distinct_error_for_an_unavailable_boundary(self):
- with self.assertRaises(CalendarNextMonthUnavailableError):
- _click_next_month(
- SimpleNamespace(run_js=lambda _script, _click: None),
- timeout=0,
- )
- def test_diagnostic_allowlist_never_serializes_unknown_values(self):
- raw = {
- "pathMatches": True,
- "candidateCount": 4,
- "actionableCount": 1,
- "clicked": False,
- "private": "private-runtime-value",
- }
- diagnostic = safe_next_month_diagnostic(raw)
- self.assertEqual(
- set(diagnostic),
- {
- "contract",
- "sourceFingerprint",
- "snapshotType",
- "pathMatches",
- "candidateCount",
- "actionableCount",
- "clicked",
- },
- )
- self.assertNotIn("private-runtime-value", json.dumps(diagnostic))
- class CalendarHorizonScanTests(unittest.TestCase):
- @staticmethod
- def packet(month: int, available: bool = False) -> FakePacket:
- slots_free = 1 if available else 0
- slots_remaining = 0 if available else -1
- inner = json.dumps(
- [
- {
- "DateLibere": f"01/{month:02d}/2026 00:00:00",
- "SlotLiberi": slots_free,
- "SlotRimanenti": slots_remaining,
- }
- ]
- )
- return FakePacket(
- method="POST",
- url=(
- "https://prenotami.esteri.it/BookingCalendar/"
- "RetrieveCalendarAvailability"
- ),
- status=200,
- raw_body=json.dumps(inner),
- post_data=json.dumps(
- {
- "_Servizio": "runtime-only",
- "selectedDay": f"01/{month:02d}/2026",
- }
- ),
- )
- def test_scans_the_initial_month_plus_the_forward_limit(self):
- packets = [
- self.packet(8),
- self.packet(9, available=True),
- self.packet(10),
- ]
- actions = []
- guards = []
- def fake_listen(_page, *, action, **_kwargs):
- action()
- return packets.pop(0)
- stdout = StringIO()
- with redirect_stdout(stdout):
- with patch(
- "tools.debug_ita_calendar_gate.listen_once",
- side_effect=fake_listen,
- ), patch(
- "tools.debug_ita_calendar_gate._refresh_calendar",
- side_effect=lambda _page: actions.append("refresh"),
- ), patch(
- "tools.debug_ita_calendar_gate._click_next_month",
- side_effect=lambda _page: actions.append("next"),
- ), patch(
- "tools.debug_ita_calendar_gate._calendar_guard",
- side_effect=lambda _page: guards.append("guard"),
- ), patch("tools.debug_ita_calendar_gate.time.sleep"):
- result = scan_calendar_horizon(
- SimpleNamespace(),
- forward_months=2,
- )
- self.assertEqual(actions, ["refresh", "next", "next"])
- self.assertEqual(guards, ["guard", "guard", "guard"])
- self.assertEqual(result.forward_clicks, 2)
- self.assertEqual(result.availability_requests, 3)
- self.assertEqual(result.stop_reason, "forward_limit_reached")
- self.assertEqual(result.probes[1].available_count, 1)
- self.assertEqual(result.probes[-1].available_count, 0)
- self.assertEqual(result.safe_dict()["monthsWithAvailability"], 1)
- self.assertEqual(
- result.safe_dict()["earliestAvailableDate"],
- "2026-09-01",
- )
- self.assertEqual(result.safe_dict()["furthestMonth"], "2026-10")
- self.assertIn("ITA_QCAL_MONTH_SCAN_RESULT=", stdout.getvalue())
- def test_stops_cleanly_when_the_native_next_control_is_unavailable(self):
- packets = [self.packet(8)]
- def fake_listen(_page, *, action, **_kwargs):
- action()
- return packets.pop(0)
- with patch(
- "tools.debug_ita_calendar_gate.listen_once",
- side_effect=fake_listen,
- ), patch(
- "tools.debug_ita_calendar_gate._refresh_calendar",
- ), patch(
- "tools.debug_ita_calendar_gate._click_next_month",
- side_effect=CalendarNextMonthUnavailableError("bounded-stop"),
- ), patch(
- "tools.debug_ita_calendar_gate._calendar_guard",
- ), patch("tools.debug_ita_calendar_gate.time.sleep"):
- result = scan_calendar_horizon(
- SimpleNamespace(),
- forward_months=12,
- )
- self.assertEqual(result.forward_clicks, 0)
- self.assertEqual(result.stop_reason, "next_control_unavailable")
- self.assertTrue(result.safe_dict()["nextControlUnavailable"])
- def test_result_rejects_skipped_or_excess_months(self):
- august = CalendarProbeResult(
- days=(),
- selected_day="01/08/2026",
- )
- october = CalendarProbeResult(
- days=(),
- selected_day="01/10/2026",
- )
- with self.assertRaisesRegex(CalendarGateError, "exactly one month"):
- CalendarMonthScanResult(
- probes=(august, october),
- requested_forward_months=2,
- stop_reason="next_control_unavailable",
- )
- with self.assertRaisesRegex(CalendarGateError, "ended before"):
- CalendarMonthScanResult(
- probes=(august,),
- requested_forward_months=2,
- stop_reason="forward_limit_reached",
- )
- def test_result_accepts_an_exact_cross_year_transition(self):
- december = CalendarProbeResult(
- days=(),
- selected_day="01/12/2026",
- )
- january = CalendarProbeResult(
- days=(),
- selected_day="01/01/2027",
- )
- result = CalendarMonthScanResult(
- probes=(december, january),
- requested_forward_months=1,
- stop_reason="forward_limit_reached",
- )
- self.assertEqual(result.safe_dict()["startMonth"], "2026-12")
- self.assertEqual(result.safe_dict()["furthestMonth"], "2027-01")
- self.assertIsNone(result.safe_dict()["earliestAvailableDate"])
- class BookingPreflightTests(unittest.TestCase):
- def test_diagnostic_is_stage_specific_and_strictly_allowlisted(self):
- raw = booking_preflight_snapshot(otpDisabled=1)
- raw["mustNotLeak"] = "private-runtime-value"
- diagnostic = safe_preflight_diagnostic(
- raw,
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- outcome="rejected",
- )
- self.assertEqual(
- set(diagnostic),
- {
- "contract",
- "sourceFingerprint",
- "outcome",
- "stage",
- "snapshotType",
- "otpDisabled",
- "otpValuePresent",
- "avantiDisabled",
- "turnstileReady",
- },
- )
- self.assertEqual(
- diagnostic["contract"],
- QCAL_TOOL_CONTRACT_VERSION,
- )
- self.assertEqual(
- diagnostic["sourceFingerprint"],
- QCAL_TOOL_SOURCE_FINGERPRINT,
- )
- self.assertEqual(diagnostic["stage"], "after_otp_send")
- self.assertEqual(
- diagnostic["otpDisabled"],
- {"type": "int", "value": None},
- )
- self.assertEqual(
- diagnostic["otpValuePresent"],
- {"type": "bool", "value": False},
- )
- self.assertNotIn(
- "private-runtime-value",
- json.dumps(diagnostic),
- )
- invalid_outcome = safe_preflight_diagnostic(
- raw,
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- outcome="private-runtime-value",
- )
- self.assertEqual(invalid_outcome["outcome"], "invalid")
- self.assertNotIn(
- "private-runtime-value",
- json.dumps(invalid_outcome),
- )
- def test_rejected_preflight_logs_shape_without_runtime_values(self):
- raw = booking_preflight_snapshot(otpDisabled=1)
- raw["mustNotLeak"] = "private-runtime-value"
- page = SimpleNamespace(run_js=lambda _script: raw)
- stdout = StringIO()
- with redirect_stdout(stdout):
- with self.assertRaisesRegex(CalendarGateError, "otpDisabled"):
- wait_for_booking_preflight(
- page,
- script="safe-script",
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- timeout=1,
- )
- output = stdout.getvalue()
- self.assertIn("ITA_QCAL_PREFLIGHT_DIAGNOSTIC=", output)
- self.assertIn('"stage": "after_otp_send"', output)
- self.assertIn('"type": "int"', output)
- self.assertNotIn("private-runtime-value", output)
- def test_preflight_requires_exact_safe_shape_without_reading_tokens(self):
- raw = booking_preflight_snapshot()
- validate_booking_preflight(raw)
- for key in (
- "pathMatches",
- "purposeHiddenIsPreSubmit",
- "privacyChecked",
- "turnstileTokenPresent",
- "otpDisabled",
- "otpValuePresent",
- "avantiDisabled",
- ):
- with self.subTest(key=key):
- changed = dict(raw)
- changed[key] = key in {
- "otpDisabled",
- "otpValuePresent",
- "avantiDisabled",
- }
- with self.assertRaisesRegex(CalendarGateError, key):
- validate_booking_preflight(changed)
- changed = dict(raw)
- changed["unknownRequiredCount"] = 1
- with self.assertRaisesRegex(CalendarGateError, "unknownRequiredCount"):
- validate_booking_preflight(changed)
- def test_post_otp_accepts_cooldown_without_relaxing_structure(self):
- cooldown = booking_preflight_snapshot(
- otpDisabled=True,
- otpValuePresent=True,
- )
- with self.assertRaisesRegex(CalendarGateError, "otpDisabled"):
- validate_booking_preflight(cooldown)
- validate_booking_preflight(
- cooldown,
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- )
- validate_booking_preflight(
- booking_preflight_snapshot(otpValuePresent=True),
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- )
- for key, value in (
- ("otpDisabled", None),
- ("otpValuePresent", False),
- ("otpButtonCount", 0),
- ("formPresent", False),
- ("unknownRequiredCount", 1),
- ):
- with self.subTest(key=key):
- changed = dict(cooldown)
- changed[key] = value
- with self.assertRaisesRegex(CalendarGateError, key):
- validate_booking_preflight(
- changed,
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- )
- def test_dynamic_wait_rechecks_token_without_relaxing_structure(self):
- base = booking_preflight_snapshot(
- avantiDisabled=True,
- turnstileTokenPresent=False,
- )
- ready = dict(base)
- ready["avantiDisabled"] = False
- ready["turnstileTokenPresent"] = True
- outputs = [base, ready]
- page = SimpleNamespace(run_js=lambda _script: outputs.pop(0))
- with patch("tools.debug_ita_calendar_gate.time.sleep"):
- result = wait_for_booking_preflight(
- page,
- script="safe-script",
- timeout=1,
- )
- self.assertIs(result, ready)
- def test_post_otp_wait_accepts_disabled_send_button(self):
- base = booking_preflight_snapshot(
- otpDisabled=True,
- otpValuePresent=True,
- avantiDisabled=True,
- turnstileTokenPresent=False,
- )
- ready = dict(base)
- ready["avantiDisabled"] = False
- ready["turnstileTokenPresent"] = True
- outputs = [base, ready]
- page = SimpleNamespace(run_js=lambda _script: outputs.pop(0))
- stdout = StringIO()
- with redirect_stdout(stdout):
- with patch("tools.debug_ita_calendar_gate.time.sleep"):
- result = wait_for_booking_preflight(
- page,
- script="safe-script",
- stage=BookingPreflightStage.AFTER_OTP_SEND,
- timeout=1,
- )
- self.assertIs(result, ready)
- self.assertIn('"outcome": "ready"', stdout.getvalue())
- self.assertIn('"stage": "after_otp_send"', stdout.getvalue())
- self.assertIn('"type": "bool"', stdout.getvalue())
- class OtpEntryTests(unittest.TestCase):
- def test_manual_otp_is_visible_and_still_validated(self):
- prompt = StringIO()
- with patch("builtins.input", return_value="123456") as input_mock:
- with redirect_stdout(prompt):
- result = _read_manual_otp()
- self.assertEqual(result, "123456")
- input_mock.assert_called_once_with(
- "Italy booking OTP "
- "(visible; terminal scrollback may retain it): "
- )
- self.assertNotIn("123456", prompt.getvalue())
- for invalid in ("", "123", "1234567890123", "12a456"):
- with self.subTest(invalid=invalid):
- with patch("builtins.input", return_value=invalid):
- with self.assertRaisesRegex(
- CalendarGateError,
- "OTP must be 4-12 digits",
- ):
- _read_manual_otp()
- def test_otp_entry_focuses_before_typing_and_compares_in_page(self):
- events = []
- class FakeOtpElement:
- def input(self, value, clear=False):
- events.append(("input", value, clear))
- def run_js(self, script, value):
- events.append(("compare", script, value))
- return True
- otp_element = FakeOtpElement()
- plugin = SimpleNamespace(
- page=SimpleNamespace(
- ele=lambda selector, timeout=0: (
- otp_element if selector == "#otp-input" else None
- )
- ),
- mouse=SimpleNamespace(
- human_click_ele=lambda element: events.append(
- ("click", element)
- )
- ),
- )
- stdout = StringIO()
- with redirect_stdout(stdout):
- _enter_otp(plugin, "123456")
- self.assertEqual(events[0], ("click", otp_element))
- self.assertEqual(events[1], ("input", "123456", True))
- self.assertEqual(events[2][0], "compare")
- self.assertIn("this.value === arguments[0]", events[2][1])
- self.assertEqual(events[2][2], "123456")
- self.assertIn(
- "ITA_QCAL_OTP_DOM_WRITE_VERIFIED matched=true",
- stdout.getvalue(),
- )
- self.assertNotIn("123456", stdout.getvalue())
- def test_otp_entry_fails_before_avanti_when_readback_differs(self):
- otp_element = SimpleNamespace(
- input=lambda _value, clear=False: None,
- run_js=lambda _script, _value: False,
- )
- plugin = SimpleNamespace(
- page=SimpleNamespace(ele=lambda _selector, timeout=0: otp_element),
- mouse=SimpleNamespace(human_click_ele=lambda _element: None),
- )
- with self.assertRaisesRegex(
- CalendarGateError,
- "OTP DOM write verification failed",
- ):
- _enter_otp(plugin, "123456")
- class ToolContractTests(unittest.TestCase):
- def test_contract_is_logged_before_route_or_credentials_are_read(self):
- tool_path = (
- Path(__file__).resolve().parents[1]
- / "tools"
- / "debug_ita_calendar_gate.py"
- )
- shared_path = (
- Path(__file__).resolve().parents[1]
- / "plugins"
- / "ita_calendar_query.py"
- )
- plugin_path = tool_path.parents[1] / "plugins" / "ita_plugin.py"
- protocol_path = tool_path.parents[1] / "plugins" / "ita_protocol.py"
- expected_fingerprint = hashlib.sha256(
- tool_path.read_bytes()
- + (tool_path.parents[1] / "plugins" / "ita_calendar_session.py").read_bytes()
- + shared_path.read_bytes()
- + plugin_path.read_bytes() + protocol_path.read_bytes()
- + (tool_path.parents[1] / "plugins" / "ita_calendar_book.py").read_bytes()
- + (tool_path.parents[1] / "tools" / "debug_ita_soak.py").read_bytes()
- + (tool_path.parents[1] / "toolkit" / "backoff.py").read_bytes()
- ).hexdigest()[:12]
- self.assertEqual(
- QCAL_TOOL_SOURCE_FINGERPRINT,
- expected_fingerprint,
- )
- stdout = StringIO()
- with patch(
- "tools.debug_ita_calendar_gate.resolve_debug_route",
- side_effect=CalendarGateError("offline-stop"),
- ):
- with redirect_stdout(stdout):
- with self.assertRaisesRegex(
- CalendarGateError,
- "offline-stop",
- ):
- run_calendar_gate({})
- self.assertEqual(
- stdout.getvalue().strip(),
- "ITA_QCAL_TOOL_CONTRACT "
- f"version={QCAL_TOOL_CONTRACT_VERSION} "
- f"source_sha256={QCAL_TOOL_SOURCE_FINGERPRINT}",
- )
- class ReservationSnapshotTests(unittest.TestCase):
- def test_reads_both_initialized_datatables_and_always_closes_tab(self):
- loading = {
- "path": "/Reservation",
- "authenticated": True,
- "tableCount": 2,
- "ready": False,
- "reservations": None,
- "waitlist": None,
- }
- ready = {
- **loading,
- "ready": True,
- "reservations": 2,
- "waitlist": 1,
- }
- tab = FakeReservationTab([loading, ready, ready])
- opened_urls = []
- page = SimpleNamespace(
- new_tab=lambda url: opened_urls.append(url) or tab
- )
- with patch("tools.debug_ita_calendar_gate.time.sleep"):
- result = read_reservation_snapshot(
- page,
- timeout=1,
- settle_seconds=0,
- )
- self.assertEqual(result, ReservationSnapshot(2, 1))
- self.assertEqual(opened_urls, ["https://prenotami.esteri.it/Reservation"])
- self.assertEqual(tab.close_count, 1)
- def test_invalid_counts_fail_and_close_tab(self):
- tab = FakeReservationTab(
- [
- {
- "path": "/Reservation",
- "authenticated": True,
- "tableCount": 2,
- "ready": True,
- "reservations": True,
- "waitlist": 0,
- }
- ]
- )
- page = SimpleNamespace(new_tab=lambda _url: tab)
- with self.assertRaises(CalendarGateError):
- read_reservation_snapshot(
- page,
- timeout=1,
- settle_seconds=0,
- )
- self.assertEqual(tab.close_count, 1)
- class SourceSafetyTests(unittest.TestCase):
- def test_gate_source_never_clicks_or_submits_the_final_control(self):
- path = (
- Path(__file__).resolve().parents[1]
- / "tools"
- / "debug_ita_calendar_gate.py"
- )
- source = path.read_text(encoding="utf-8")
- tree = ast.parse(source)
- violations = []
- one_shot_calls = []
- for node in ast.walk(tree):
- if not isinstance(node, ast.Call):
- continue
- function = node.func
- function_name = (
- function.attr
- if isinstance(function, ast.Attribute)
- else function.id
- if isinstance(function, ast.Name)
- else ""
- )
- string_arguments = {
- child.value
- for child in ast.walk(node)
- if isinstance(child, ast.Constant)
- and isinstance(child.value, str)
- }
- if function_name in {"submit", "click"} and any(
- "btnPrenotaNoOtp" in value
- or "InsertNewBooking" in value
- for value in string_arguments
- ):
- violations.append((function_name, node.lineno))
- if function_name == "human_click_ele" and any(
- "btnPrenotaNoOtp" in value
- for value in string_arguments
- ):
- violations.append((function_name, node.lineno))
- if function_name == "run" and isinstance(function, ast.Attribute):
- if isinstance(function.value, ast.Name):
- one_shot_calls.append(function.value.id)
- self.assertEqual(violations, [])
- self.assertEqual(one_shot_calls.count("otp_guard"), 1)
- self.assertEqual(one_shot_calls.count("avanti_guard"), 1)
- self.assertIn("button.disabled = true", CALENDAR_GUARD_SCRIPT)
- self.assertIn('OneShotAction("OTP")', source)
- self.assertIn('OneShotAction("AVANTI")', source)
- self.assertIn(
- '#datetimepicker',
- NEXT_MONTH_CONTROL_SCRIPT,
- )
- self.assertIn(
- 'th.dtpicker-next',
- NEXT_MONTH_CONTROL_SCRIPT,
- )
- for forbidden_selector in (
- "btnPrenotaNoOtp",
- "InsertNewBooking",
- ".fascia",
- ".events a",
- "a.next",
- "data-action",
- "fetch(",
- "XMLHttpRequest",
- ):
- self.assertNotIn(
- forbidden_selector,
- NEXT_MONTH_CONTROL_SCRIPT,
- )
- if __name__ == "__main__":
- unittest.main()
|