test_debug_ita_calendar_gate.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. import ast
  2. import hashlib
  3. import json
  4. import unittest
  5. from contextlib import redirect_stdout
  6. from io import StringIO
  7. from pathlib import Path
  8. from types import SimpleNamespace
  9. from unittest.mock import patch
  10. from plugins.ita_plugin import CALENDAR_GUARD_SCRIPT
  11. from plugins.ita_plugin import resolve_route_config
  12. from tools.debug_ita_calendar_gate import (
  13. BookingPreflightStage,
  14. CalendarGateMode,
  15. CalendarGateError,
  16. CalendarGateEvidence,
  17. CalendarMonthScanResult,
  18. CalendarNextMonthUnavailableError,
  19. CalendarProbeResult,
  20. NEXT_MONTH_CONTROL_SCRIPT,
  21. OneShotAction,
  22. QCAL_TOOL_CONTRACT_VERSION,
  23. QCAL_TOOL_SOURCE_FINGERPRINT,
  24. ReservationSnapshot,
  25. _click_next_month,
  26. _enter_otp,
  27. _read_manual_otp,
  28. build_calendar_gate_config,
  29. compare_availability_transport,
  30. listen_once,
  31. parse_availability_packet,
  32. read_reservation_snapshot,
  33. resolve_qcal_forward_months,
  34. resolve_qcal_mode,
  35. run_calendar_gate,
  36. safe_avanti_failure_diagnostic,
  37. safe_next_month_diagnostic,
  38. safe_preflight_diagnostic,
  39. scan_calendar_horizon,
  40. transport_comparison_report,
  41. validate_avanti_packet,
  42. validate_booking_preflight,
  43. validate_next_month_snapshot,
  44. validate_otp_packet,
  45. wait_for_booking_preflight,
  46. )
  47. def gate_env():
  48. return {
  49. "ITA_ROUTING_KEY": "slot.hkg.it.tourist",
  50. "ITA_SERVICE_ID": "4937",
  51. "ITA_CITY": "Hong Kong",
  52. "ITA_VISA_TYPE": "Tourist",
  53. "ITA_CALENDAR_LOOKAHEAD_MONTHS": "3",
  54. "ITA_BOOKING_TYPE_CONTROL_ID": "typeofbookingddl",
  55. "ITA_BOOKING_TYPE_VALUE": "1",
  56. "ITA_BOOKING_TYPE_LABELS": "Prenotazione Singola|Single booking",
  57. "ITA_PURPOSE_CONTROL_ID": "ddls_0",
  58. "ITA_PURPOSE_VALUE": "42",
  59. "ITA_PURPOSE_LABELS": "Turismo|Tourism",
  60. }
  61. def booking_preflight_snapshot(**changes):
  62. snapshot = {
  63. "formReady": True,
  64. "turnstileReady": True,
  65. "otpValuePresent": False,
  66. "otpSendEnabled": True,
  67. "avantiEnabled": True,
  68. }
  69. snapshot.update(changes)
  70. return snapshot
  71. class FakeResponse:
  72. def __init__(self, status, *, url="", raw_body="", headers=None):
  73. self.status = status
  74. self.url = url
  75. self.raw_body = raw_body
  76. self.headers = headers or {}
  77. class FakePacket:
  78. def __init__(
  79. self,
  80. *,
  81. method,
  82. url,
  83. status,
  84. response_url="",
  85. raw_body="",
  86. headers=None,
  87. post_data=None,
  88. ):
  89. self.method = method
  90. self.url = url
  91. self.response = FakeResponse(
  92. status,
  93. url=response_url,
  94. raw_body=raw_body,
  95. headers=headers,
  96. )
  97. self.request = SimpleNamespace(postData=post_data)
  98. class FakeListener:
  99. def __init__(self, packet=None):
  100. self.packet = packet
  101. self.starts = []
  102. self.waits = []
  103. self.stop_count = 0
  104. def start(self, target, method=None):
  105. self.starts.append((target, method))
  106. def wait(self, timeout=None):
  107. self.waits.append(timeout)
  108. return self.packet
  109. def stop(self):
  110. self.stop_count += 1
  111. class FakeReservationTab:
  112. def __init__(self, outputs):
  113. self.outputs = list(outputs)
  114. self.wait = SimpleNamespace(doc_loaded=lambda: None)
  115. self.close_count = 0
  116. def run_js(self, _script):
  117. if len(self.outputs) > 1:
  118. return self.outputs.pop(0)
  119. return self.outputs[0]
  120. def close(self):
  121. self.close_count += 1
  122. class CalendarGateConfigTests(unittest.TestCase):
  123. def test_builds_one_explicit_live_route_with_final_disabled(self):
  124. config = build_calendar_gate_config(
  125. gate_env(),
  126. username="offline@example.test",
  127. password="not-a-real-password",
  128. )
  129. free_config = config.free_config
  130. route = resolve_route_config(
  131. free_config["apt_configs"],
  132. "slot.hkg.it.tourist",
  133. )
  134. self.assertEqual(free_config["query_mode"], "services_probe")
  135. self.assertIs(free_config["final_submit_enabled"], False)
  136. self.assertEqual(route.service_id, 4937)
  137. self.assertEqual(route.capability.value, "single_schengen_tourism")
  138. self.assertEqual(route.lookahead_months, 3)
  139. self.assertEqual(route.booking_type.control_id, "typeofbookingddl")
  140. self.assertEqual(route.booking_type.value, "1")
  141. self.assertEqual(route.purpose.value, "42")
  142. def test_missing_any_live_route_field_fails_closed(self):
  143. for missing in (
  144. "ITA_CALENDAR_LOOKAHEAD_MONTHS",
  145. "ITA_BOOKING_TYPE_CONTROL_ID",
  146. "ITA_BOOKING_TYPE_LABELS",
  147. "ITA_PURPOSE_VALUE",
  148. ):
  149. with self.subTest(missing=missing):
  150. env = gate_env()
  151. env.pop(missing)
  152. with self.assertRaisesRegex(CalendarGateError, missing):
  153. build_calendar_gate_config(
  154. env,
  155. username="offline@example.test",
  156. password="not-a-real-password",
  157. )
  158. def test_resolves_a_separate_bounded_discovery_horizon(self):
  159. self.assertEqual(resolve_qcal_forward_months({}), 1)
  160. self.assertEqual(
  161. resolve_qcal_forward_months({"ITA_QCAL_FORWARD_MONTHS": "12"}),
  162. 12,
  163. )
  164. for invalid in ("0", "13", "invalid", "1.5"):
  165. with self.subTest(invalid=invalid):
  166. with self.assertRaisesRegex(
  167. CalendarGateError,
  168. "ITA_QCAL_FORWARD_MONTHS",
  169. ):
  170. resolve_qcal_forward_months(
  171. {"ITA_QCAL_FORWARD_MONTHS": invalid}
  172. )
  173. def test_transport_comparison_is_explicit_and_default_remains_discovery(self):
  174. self.assertIs(resolve_qcal_mode({}), CalendarGateMode.DISCOVERY_HORIZON)
  175. self.assertIs(
  176. resolve_qcal_mode({"ITA_QCAL_MODE": "transport_compare"}),
  177. CalendarGateMode.TRANSPORT_COMPARE,
  178. )
  179. with self.assertRaisesRegex(CalendarGateError, "transport_compare"):
  180. resolve_qcal_mode({"ITA_QCAL_MODE": "browser_fetch"})
  181. class GateValueObjectTests(unittest.TestCase):
  182. def test_reservation_snapshot_requires_nonnegative_plain_integers(self):
  183. self.assertEqual(
  184. ReservationSnapshot(reservations=0, waitlist=1).total,
  185. 1,
  186. )
  187. for kwargs in (
  188. {"reservations": -1, "waitlist": 0},
  189. {"reservations": True, "waitlist": 0},
  190. {"reservations": 0, "waitlist": "0"},
  191. ):
  192. with self.subTest(kwargs=kwargs):
  193. with self.assertRaises(CalendarGateError):
  194. ReservationSnapshot(**kwargs)
  195. def test_gate_evidence_passes_only_the_exact_qcal_contract(self):
  196. empty = ReservationSnapshot(0, 0)
  197. evidence = CalendarGateEvidence(
  198. before=empty,
  199. after=empty,
  200. otp_requests=1,
  201. avanti_posts=1,
  202. availability_requests=2,
  203. final_requests=0,
  204. calendar_ready=True,
  205. )
  206. self.assertTrue(evidence.passed)
  207. variants = (
  208. {"after": ReservationSnapshot(1, 0)},
  209. {"otp_requests": 0},
  210. {"avanti_posts": 2},
  211. {"availability_requests": 1},
  212. {"final_requests": 1},
  213. {"calendar_ready": False},
  214. )
  215. for changes in variants:
  216. with self.subTest(changes=changes):
  217. values = {
  218. "before": evidence.before,
  219. "after": evidence.after,
  220. "otp_requests": evidence.otp_requests,
  221. "avanti_posts": evidence.avanti_posts,
  222. "availability_requests": evidence.availability_requests,
  223. "final_requests": evidence.final_requests,
  224. "calendar_ready": evidence.calendar_ready,
  225. }
  226. values.update(changes)
  227. self.assertFalse(CalendarGateEvidence(**values).passed)
  228. def test_one_shot_action_marks_attempt_before_calling(self):
  229. calls = []
  230. guard = OneShotAction("OTP")
  231. self.assertEqual(guard.run(lambda: calls.append("called") or 7), 7)
  232. self.assertEqual(calls, ["called"])
  233. with self.assertRaisesRegex(CalendarGateError, "OTP.*already attempted"):
  234. guard.run(lambda: calls.append("second"))
  235. self.assertEqual(calls, ["called"])
  236. failing = OneShotAction("AVANTI")
  237. with self.assertRaisesRegex(RuntimeError, "ambiguous"):
  238. failing.run(
  239. lambda: (_ for _ in ()).throw(RuntimeError("ambiguous"))
  240. )
  241. with self.assertRaisesRegex(CalendarGateError, "already attempted"):
  242. failing.run(lambda: None)
  243. class NetworkContractTests(unittest.TestCase):
  244. def test_listener_always_stops_and_performs_one_action(self):
  245. packet = object()
  246. listener = FakeListener(packet)
  247. page = SimpleNamespace(listen=listener)
  248. actions = []
  249. result = listen_once(
  250. page,
  251. target="/safe-endpoint",
  252. method="POST",
  253. action=lambda: actions.append("one"),
  254. timeout=9,
  255. )
  256. self.assertIs(result, packet)
  257. self.assertEqual(actions, ["one"])
  258. self.assertEqual(listener.starts, [("/safe-endpoint", "POST")])
  259. self.assertEqual(listener.waits, [9])
  260. self.assertEqual(listener.stop_count, 1)
  261. failing_listener = FakeListener(None)
  262. failing_page = SimpleNamespace(listen=failing_listener)
  263. with self.assertRaisesRegex(CalendarGateError, "not observed"):
  264. listen_once(
  265. failing_page,
  266. target="/safe-endpoint",
  267. method="GET",
  268. action=lambda: None,
  269. timeout=1,
  270. )
  271. self.assertEqual(failing_listener.stop_count, 1)
  272. raising_listener = FakeListener(packet)
  273. raising_page = SimpleNamespace(listen=raising_listener)
  274. with self.assertRaisesRegex(RuntimeError, "action failed"):
  275. listen_once(
  276. raising_page,
  277. target="/safe-endpoint",
  278. method="POST",
  279. action=lambda: (_ for _ in ()).throw(
  280. RuntimeError("action failed")
  281. ),
  282. timeout=1,
  283. )
  284. self.assertEqual(raising_listener.stop_count, 1)
  285. def test_otp_requires_exact_get_route_status_and_json_string(self):
  286. packet = FakePacket(
  287. method="GET",
  288. url=(
  289. "https://prenotami.esteri.it/BookingCalendar/GenerateOTP"
  290. "?ServiceID=4937"
  291. ),
  292. status=200,
  293. raw_body='"SENT"',
  294. )
  295. validate_otp_packet(packet, service_id=4937)
  296. bad_packets = (
  297. FakePacket(
  298. method="POST",
  299. url=packet.url,
  300. status=200,
  301. raw_body='"SENT"',
  302. ),
  303. FakePacket(
  304. method="GET",
  305. url=packet.url,
  306. status=201,
  307. raw_body='"SENT"',
  308. ),
  309. FakePacket(
  310. method="GET",
  311. url=packet.url,
  312. status=200,
  313. raw_body="SENT",
  314. ),
  315. FakePacket(
  316. method="GET",
  317. url=packet.url.replace("4937", "1321"),
  318. status=200,
  319. raw_body='"SENT"',
  320. ),
  321. )
  322. for bad in bad_packets:
  323. with self.subTest(packet=bad):
  324. with self.assertRaises(CalendarGateError):
  325. validate_otp_packet(bad, service_id=4937)
  326. def test_avanti_accepts_direct_302_to_the_observed_calendar(self):
  327. packet = FakePacket(
  328. method="POST",
  329. url="https://prenotami.esteri.it/Services/Booking/4937",
  330. status=302,
  331. headers={
  332. "Location": (
  333. "/BookingCalendar?selectedService="
  334. "Visti%20Nazionali%20e%20Schengen"
  335. )
  336. },
  337. )
  338. mode = validate_avanti_packet(
  339. packet,
  340. booking_path="/Services/Booking/4937",
  341. final_page_url=(
  342. "https://prenotami.esteri.it/BookingCalendar"
  343. "?selectedService=Visti"
  344. ),
  345. )
  346. self.assertEqual(mode, "direct_302")
  347. def test_avanti_accepts_only_a_triply_consistent_followed_200(self):
  348. packet = FakePacket(
  349. method="POST",
  350. url="https://prenotami.esteri.it/Services/Booking/4937",
  351. status=200,
  352. response_url=(
  353. "https://prenotami.esteri.it/BookingCalendar"
  354. "?selectedService=Visti"
  355. ),
  356. )
  357. mode = validate_avanti_packet(
  358. packet,
  359. booking_path="/Services/Booking/4937",
  360. final_page_url="https://prenotami.esteri.it/BookingCalendar",
  361. )
  362. self.assertEqual(mode, "followed_redirect_200")
  363. def test_avanti_rejects_inconsistent_redirect_evidence(self):
  364. cases = (
  365. {
  366. "status": 200,
  367. "response_url": "https://prenotami.esteri.it/Services",
  368. "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
  369. },
  370. {
  371. "status": 200,
  372. "response_url": "https://example.test/BookingCalendar",
  373. "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
  374. },
  375. {
  376. "status": 200,
  377. "response_url": (
  378. "https://prenotami.esteri.it:444/BookingCalendar"
  379. ),
  380. "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
  381. },
  382. {
  383. "status": 200,
  384. "response_url": "https://prenotami.esteri.it/BookingCalendar",
  385. "final_page_url": "https://prenotami.esteri.it/Services",
  386. },
  387. {
  388. "status": 201,
  389. "response_url": "https://prenotami.esteri.it/BookingCalendar",
  390. "final_page_url": "https://prenotami.esteri.it/BookingCalendar",
  391. },
  392. )
  393. for case in cases:
  394. with self.subTest(case=case):
  395. bad = FakePacket(
  396. method="POST",
  397. url=(
  398. "https://prenotami.esteri.it/Services/Booking/4937"
  399. ),
  400. status=case["status"],
  401. response_url=case["response_url"],
  402. )
  403. with self.assertRaises(CalendarGateError):
  404. validate_avanti_packet(
  405. bad,
  406. booking_path="/Services/Booking/4937",
  407. final_page_url=case["final_page_url"],
  408. )
  409. def test_avanti_rejects_bad_direct_redirects_and_requests(self):
  410. for method, request_url, location in (
  411. (
  412. "POST",
  413. "https://prenotami.esteri.it/Services/Booking/4937",
  414. "/Services",
  415. ),
  416. (
  417. "POST",
  418. "https://prenotami.esteri.it/Services/Booking/4937",
  419. "https://example.test/BookingCalendar",
  420. ),
  421. (
  422. "POST",
  423. "https://prenotami.esteri.it/Services/Booking/4937?retry=1",
  424. "/BookingCalendar",
  425. ),
  426. (
  427. "GET",
  428. "https://prenotami.esteri.it/Services/Booking/4937",
  429. "/BookingCalendar",
  430. ),
  431. (
  432. "POST",
  433. "https://prenotami.esteri.it/Services/Booking/1321",
  434. "/BookingCalendar",
  435. ),
  436. (
  437. "POST",
  438. "https://example.test/Services/Booking/4937",
  439. "/BookingCalendar",
  440. ),
  441. (
  442. "POST",
  443. "https://prenotami.esteri.it/Services/Booking/4937",
  444. "",
  445. ),
  446. ):
  447. with self.subTest(
  448. method=method,
  449. request_url=request_url,
  450. location=location,
  451. ):
  452. packet = FakePacket(
  453. method=method,
  454. url=request_url,
  455. status=302,
  456. headers={"Location": location},
  457. )
  458. with self.assertRaises(CalendarGateError):
  459. validate_avanti_packet(
  460. packet,
  461. booking_path="/Services/Booking/4937",
  462. final_page_url=(
  463. "https://prenotami.esteri.it/BookingCalendar"
  464. ),
  465. )
  466. def test_avanti_failure_diagnostic_is_strictly_allowlisted(self):
  467. packet = FakePacket(
  468. method="POST",
  469. url="https://prenotami.esteri.it/Services/Booking/4937",
  470. status=200,
  471. response_url=(
  472. "https://prenotami.esteri.it/Services/Booking/4937"
  473. "?private-response-query"
  474. ),
  475. raw_body="private-response-body",
  476. headers={
  477. "Location": (
  478. "/BookingCalendar?private-location-query"
  479. )
  480. },
  481. post_data="private-form-data",
  482. )
  483. raw = {
  484. "path": "/Services/Booking/4937",
  485. "bookingFormPresent": True,
  486. "otpValuePresent": True,
  487. "otpInvalidVisible": False,
  488. "avantiDisabled": False,
  489. "turnstileTokenPresent": False,
  490. "visibleValidationCount": 1,
  491. "visibleConfirmCount": 0,
  492. "finalRequestCount": 0,
  493. "mustNotLeak": "private-runtime-value",
  494. }
  495. diagnostic = safe_avanti_failure_diagnostic(
  496. packet,
  497. raw,
  498. booking_path="/Services/Booking/4937",
  499. )
  500. self.assertEqual(
  501. set(diagnostic),
  502. {
  503. "outcome",
  504. "contract",
  505. "sourceFingerprint",
  506. "snapshotType",
  507. "bookingPostObserved",
  508. "requestMatches",
  509. "responseStatus",
  510. "responsePath",
  511. "locationPath",
  512. "browserPath",
  513. "bookingFormPresent",
  514. "otpValuePresent",
  515. "otpInvalidVisible",
  516. "avantiDisabled",
  517. "turnstileReady",
  518. "visibleValidationCount",
  519. "visibleConfirmCount",
  520. "finalRequestCount",
  521. },
  522. )
  523. self.assertEqual(diagnostic["outcome"], "booking_form_returned")
  524. self.assertIs(diagnostic["bookingPostObserved"], True)
  525. self.assertIs(diagnostic["requestMatches"], True)
  526. self.assertEqual(
  527. diagnostic["responseStatus"],
  528. {"type": "int", "value": 200},
  529. )
  530. self.assertEqual(diagnostic["responsePath"], "booking")
  531. self.assertEqual(diagnostic["locationPath"], "calendar")
  532. self.assertEqual(diagnostic["browserPath"], "booking")
  533. self.assertEqual(
  534. diagnostic["otpValuePresent"],
  535. {"type": "bool", "value": True},
  536. )
  537. serialized = json.dumps(diagnostic)
  538. for private_value in (
  539. "private-response-query",
  540. "private-response-body",
  541. "private-location-query",
  542. "private-form-data",
  543. "private-runtime-value",
  544. ):
  545. self.assertNotIn(private_value, serialized)
  546. def test_avanti_failure_diagnostic_degrades_unknown_values(self):
  547. diagnostic = safe_avanti_failure_diagnostic(
  548. None,
  549. "private-runtime-value",
  550. booking_path="/Services/Booking/4937",
  551. )
  552. self.assertEqual(
  553. diagnostic["outcome"],
  554. "booking_post_not_observed",
  555. )
  556. self.assertIs(diagnostic["bookingPostObserved"], False)
  557. self.assertEqual(diagnostic["responsePath"], "missing")
  558. self.assertEqual(diagnostic["browserPath"], "missing")
  559. self.assertEqual(
  560. diagnostic["otpValuePresent"],
  561. {"type": "NoneType", "value": None},
  562. )
  563. self.assertNotIn(
  564. "private-runtime-value",
  565. json.dumps(diagnostic),
  566. )
  567. def test_availability_packet_reuses_the_strict_protocol_parser(self):
  568. inner = json.dumps(
  569. [
  570. {
  571. "DateLibere": "01/10/2026 00:00:00",
  572. "SlotLiberi": 0,
  573. "SlotRimanenti": -1,
  574. },
  575. {
  576. "DateLibere": "02/10/2026 00:00:00",
  577. "SlotLiberi": 1,
  578. "SlotRimanenti": 0,
  579. },
  580. ]
  581. )
  582. packet = FakePacket(
  583. method="POST",
  584. url=(
  585. "https://prenotami.esteri.it/BookingCalendar/"
  586. "RetrieveCalendarAvailability"
  587. ),
  588. status=200,
  589. raw_body=json.dumps(inner),
  590. post_data=json.dumps(
  591. {"_Servizio": "runtime-only", "selectedDay": "01/10/2026"}
  592. ),
  593. )
  594. result = parse_availability_packet(packet)
  595. self.assertEqual(result.item_count, 2)
  596. self.assertEqual(result.available_count, 1)
  597. self.assertEqual(result.selected_day, "01/10/2026")
  598. self.assertEqual(result.selected_month, (2026, 10))
  599. self.assertFalse(result.days[0].is_available)
  600. self.assertTrue(result.days[1].is_available)
  601. packet.request.postData = json.dumps(
  602. {
  603. "_Servizio": "runtime-only",
  604. "selectedDay": "01/10/2026",
  605. "guessed": True,
  606. }
  607. )
  608. with self.assertRaisesRegex(CalendarGateError, "request keys"):
  609. parse_availability_packet(packet)
  610. def test_browser_fetch_transport_matches_the_native_month_without_leaking_service(self):
  611. inner = json.dumps(
  612. [
  613. {
  614. "DateLibere": "01/10/2026 00:00:00",
  615. "SlotLiberi": 0,
  616. "SlotRimanenti": -1,
  617. },
  618. {
  619. "DateLibere": "02/10/2026 00:00:00",
  620. "SlotLiberi": 1,
  621. "SlotRimanenti": 0,
  622. },
  623. ]
  624. )
  625. raw_body = json.dumps(inner)
  626. packet = FakePacket(
  627. method="POST",
  628. url="https://prenotami.esteri.it/BookingCalendar/RetrieveCalendarAvailability",
  629. status=200,
  630. raw_body=raw_body,
  631. post_data=json.dumps(
  632. {"_Servizio": "private-runtime-id", "selectedDay": "01/10/2026"}
  633. ),
  634. )
  635. native = parse_availability_packet(packet)
  636. calls = []
  637. def fetch(*args, **kwargs):
  638. calls.append((args, kwargs))
  639. return SimpleNamespace(
  640. status_code=200,
  641. url=packet.url,
  642. text=raw_body,
  643. )
  644. report = compare_availability_transport(
  645. SimpleNamespace(), packet, native, fetch=fetch
  646. )
  647. self.assertEqual(
  648. report,
  649. {
  650. "month": "2026-10",
  651. "dayCount": 2,
  652. "availableCount": 1,
  653. "equivalent": True,
  654. },
  655. )
  656. self.assertEqual(calls[0][0][1:3], ("POST", packet.url))
  657. self.assertEqual(calls[0][1]["timeout"], 30)
  658. self.assertEqual(
  659. calls[0][1]["json_data"],
  660. {"_Servizio": "private-runtime-id", "selectedDay": "01/10/2026"},
  661. )
  662. self.assertNotIn("private-runtime-id", json.dumps(report))
  663. def test_browser_fetch_transport_rejects_inventory_status_and_raw_body_changes(self):
  664. inner = json.dumps(
  665. [{
  666. "DateLibere": "01/10/2026 00:00:00",
  667. "SlotLiberi": 0,
  668. "SlotRimanenti": -1,
  669. }]
  670. )
  671. packet = FakePacket(
  672. method="POST",
  673. url="https://prenotami.esteri.it/BookingCalendar/RetrieveCalendarAvailability",
  674. status=200,
  675. raw_body=json.dumps(inner),
  676. post_data={"_Servizio": "private-runtime-id", "selectedDay": "01/10/2026"},
  677. )
  678. native = parse_availability_packet(packet)
  679. changed = json.dumps(json.dumps([{
  680. "DateLibere": "01/10/2026 00:00:00",
  681. "SlotLiberi": 1,
  682. "SlotRimanenti": 0,
  683. }]))
  684. cases = (
  685. (
  686. SimpleNamespace(status_code=200, url=packet.url, text=changed),
  687. "inventory differed",
  688. ),
  689. (
  690. SimpleNamespace(status_code=429, url=packet.url, text="private-rate-body"),
  691. "rate limited",
  692. ),
  693. (
  694. SimpleNamespace(status_code=401, url=packet.url, text="private-auth-body"),
  695. "authentication expired",
  696. ),
  697. (
  698. SimpleNamespace(status_code=403, url=packet.url, text="private-denied-body"),
  699. "access was denied",
  700. ),
  701. (
  702. SimpleNamespace(status_code=503, url=packet.url, text="private-transient-body"),
  703. "temporarily failed",
  704. ),
  705. (
  706. SimpleNamespace(status_code=200, url="https://iam.esteri.it/signin/", text="private-login"),
  707. "response URL changed",
  708. ),
  709. (
  710. SimpleNamespace(status_code=200, url=packet.url, text="private-malformed-body"),
  711. "response rejected",
  712. ),
  713. )
  714. for response, expected in cases:
  715. with self.subTest(expected=expected):
  716. with self.assertRaisesRegex(CalendarGateError, expected) as caught:
  717. compare_availability_transport(
  718. SimpleNamespace(), packet, native,
  719. fetch=lambda *_args, **_kwargs: response,
  720. )
  721. self.assertNotIn("private", str(caught.exception))
  722. def test_transport_report_requires_two_reads_per_month_and_no_business_writes(self):
  723. comparisons = [
  724. {"month": "2026-08", "dayCount": 2, "availableCount": 0, "equivalent": True},
  725. {"month": "2026-09", "dayCount": 3, "availableCount": 1, "equivalent": True},
  726. ]
  727. before = {
  728. "otpRequests": 1,
  729. "avantiPosts": 1,
  730. "availabilityRequests": 0,
  731. "finalRequests": 0,
  732. }
  733. after = {**before, "availabilityRequests": 4}
  734. report = transport_comparison_report(
  735. comparisons, before, after, native_requests=2
  736. )
  737. self.assertTrue(report["coverageComplete"])
  738. self.assertTrue(report["requestContractMatched"])
  739. self.assertEqual(report["emptyMonths"], 1)
  740. self.assertEqual(report["positiveMonths"], 1)
  741. self.assertEqual(
  742. report["compareRequestDelta"],
  743. {
  744. "otpRequests": 0,
  745. "avantiPosts": 0,
  746. "availabilityRequests": 4,
  747. "finalRequests": 0,
  748. },
  749. )
  750. for changed in (
  751. {**after, "otpRequests": 2},
  752. {**after, "avantiPosts": 2},
  753. {**after, "finalRequests": 1},
  754. {**after, "availabilityRequests": 3},
  755. ):
  756. with self.subTest(changed=changed):
  757. bad = transport_comparison_report(
  758. comparisons, before, changed, native_requests=2
  759. )
  760. self.assertFalse(bad["requestContractMatched"])
  761. class NextMonthControlTests(unittest.TestCase):
  762. def test_snapshot_requires_one_actionable_control_and_exact_shape(self):
  763. ready = {
  764. "pathMatches": True,
  765. "candidateCount": 4,
  766. "actionableCount": 1,
  767. "clicked": False,
  768. }
  769. self.assertIs(
  770. validate_next_month_snapshot(
  771. ready,
  772. expected_clicked=False,
  773. ),
  774. ready,
  775. )
  776. unready = {**ready, "actionableCount": 0}
  777. validate_next_month_snapshot(
  778. unready,
  779. expected_clicked=False,
  780. allow_unready=True,
  781. )
  782. for changed, message in (
  783. ({**ready, "actionableCount": 2}, "uniquely actionable"),
  784. ({**ready, "candidateCount": True}, "candidateCount"),
  785. ({**ready, "clicked": True}, "acknowledgement"),
  786. ({**ready, "private": "value"}, "unexpected fields"),
  787. ):
  788. with self.subTest(changed=changed):
  789. with self.assertRaisesRegex(CalendarGateError, message):
  790. validate_next_month_snapshot(
  791. changed,
  792. expected_clicked=False,
  793. )
  794. def test_click_waits_then_atomically_clicks_once(self):
  795. outputs = [
  796. {
  797. "pathMatches": True,
  798. "candidateCount": 4,
  799. "actionableCount": 0,
  800. "clicked": False,
  801. },
  802. {
  803. "pathMatches": True,
  804. "candidateCount": 4,
  805. "actionableCount": 1,
  806. "clicked": False,
  807. },
  808. {
  809. "pathMatches": True,
  810. "candidateCount": 4,
  811. "actionableCount": 1,
  812. "clicked": True,
  813. },
  814. ]
  815. calls = []
  816. def run_js(script, should_click):
  817. calls.append((script, should_click))
  818. return outputs.pop(0)
  819. stdout = StringIO()
  820. with redirect_stdout(stdout):
  821. with patch("tools.debug_ita_calendar_gate.time.sleep"):
  822. _click_next_month(
  823. SimpleNamespace(run_js=run_js),
  824. timeout=1,
  825. )
  826. self.assertEqual(
  827. [should_click for _script, should_click in calls],
  828. [False, False, True],
  829. )
  830. self.assertTrue(
  831. all(script == NEXT_MONTH_CONTROL_SCRIPT for script, _ in calls)
  832. )
  833. self.assertEqual(
  834. stdout.getvalue().count("ITA_QCAL_NEXT_MONTH_DIAGNOSTIC="),
  835. 1,
  836. )
  837. self.assertIn('"clicked": {"type": "bool", "value": true}', stdout.getvalue())
  838. def test_click_uses_a_distinct_error_for_an_unavailable_boundary(self):
  839. with self.assertRaises(CalendarNextMonthUnavailableError):
  840. _click_next_month(
  841. SimpleNamespace(run_js=lambda _script, _click: None),
  842. timeout=0,
  843. )
  844. def test_diagnostic_allowlist_never_serializes_unknown_values(self):
  845. raw = {
  846. "pathMatches": True,
  847. "candidateCount": 4,
  848. "actionableCount": 1,
  849. "clicked": False,
  850. "private": "private-runtime-value",
  851. }
  852. diagnostic = safe_next_month_diagnostic(raw)
  853. self.assertEqual(
  854. set(diagnostic),
  855. {
  856. "contract",
  857. "sourceFingerprint",
  858. "snapshotType",
  859. "pathMatches",
  860. "candidateCount",
  861. "actionableCount",
  862. "clicked",
  863. },
  864. )
  865. self.assertNotIn("private-runtime-value", json.dumps(diagnostic))
  866. class CalendarHorizonScanTests(unittest.TestCase):
  867. @staticmethod
  868. def packet(month: int, available: bool = False) -> FakePacket:
  869. slots_free = 1 if available else 0
  870. slots_remaining = 0 if available else -1
  871. inner = json.dumps(
  872. [
  873. {
  874. "DateLibere": f"01/{month:02d}/2026 00:00:00",
  875. "SlotLiberi": slots_free,
  876. "SlotRimanenti": slots_remaining,
  877. }
  878. ]
  879. )
  880. return FakePacket(
  881. method="POST",
  882. url=(
  883. "https://prenotami.esteri.it/BookingCalendar/"
  884. "RetrieveCalendarAvailability"
  885. ),
  886. status=200,
  887. raw_body=json.dumps(inner),
  888. post_data=json.dumps(
  889. {
  890. "_Servizio": "runtime-only",
  891. "selectedDay": f"01/{month:02d}/2026",
  892. }
  893. ),
  894. )
  895. def test_scans_the_initial_month_plus_the_forward_limit(self):
  896. packets = [
  897. self.packet(8),
  898. self.packet(9, available=True),
  899. self.packet(10),
  900. ]
  901. actions = []
  902. guards = []
  903. def fake_listen(_page, *, action, **_kwargs):
  904. action()
  905. return packets.pop(0)
  906. stdout = StringIO()
  907. with redirect_stdout(stdout):
  908. with patch(
  909. "tools.debug_ita_calendar_gate.listen_once",
  910. side_effect=fake_listen,
  911. ), patch(
  912. "tools.debug_ita_calendar_gate._refresh_calendar",
  913. side_effect=lambda _page: actions.append("refresh"),
  914. ), patch(
  915. "tools.debug_ita_calendar_gate._click_next_month",
  916. side_effect=lambda _page: actions.append("next"),
  917. ), patch(
  918. "tools.debug_ita_calendar_gate._calendar_guard",
  919. side_effect=lambda _page: guards.append("guard"),
  920. ), patch("tools.debug_ita_calendar_gate.time.sleep"):
  921. result = scan_calendar_horizon(
  922. SimpleNamespace(),
  923. forward_months=2,
  924. )
  925. self.assertEqual(actions, ["refresh", "next", "next"])
  926. self.assertEqual(guards, ["guard", "guard", "guard"])
  927. self.assertEqual(result.forward_clicks, 2)
  928. self.assertEqual(result.availability_requests, 3)
  929. self.assertEqual(result.stop_reason, "forward_limit_reached")
  930. self.assertEqual(result.probes[1].available_count, 1)
  931. self.assertEqual(result.probes[-1].available_count, 0)
  932. self.assertEqual(result.safe_dict()["monthsWithAvailability"], 1)
  933. self.assertEqual(
  934. result.safe_dict()["earliestAvailableDate"],
  935. "2026-09-01",
  936. )
  937. self.assertEqual(result.safe_dict()["furthestMonth"], "2026-10")
  938. self.assertIn("ITA_QCAL_MONTH_SCAN_RESULT=", stdout.getvalue())
  939. def test_transport_observer_receives_each_native_packet_after_guard(self):
  940. packets = [self.packet(8), self.packet(9, available=True)]
  941. events = []
  942. def fake_listen(_page, *, action, **_kwargs):
  943. action()
  944. return packets.pop(0)
  945. with patch(
  946. "tools.debug_ita_calendar_gate.listen_once",
  947. side_effect=fake_listen,
  948. ), patch(
  949. "tools.debug_ita_calendar_gate._refresh_calendar",
  950. side_effect=lambda _page: events.append("refresh"),
  951. ), patch(
  952. "tools.debug_ita_calendar_gate._click_next_month",
  953. side_effect=lambda _page: events.append("next"),
  954. ), patch(
  955. "tools.debug_ita_calendar_gate._calendar_guard",
  956. side_effect=lambda _page: events.append("guard"),
  957. ), patch("tools.debug_ita_calendar_gate.time.sleep"):
  958. result = scan_calendar_horizon(
  959. SimpleNamespace(),
  960. forward_months=1,
  961. packet_observer=lambda packet, probe: events.append(
  962. (packet.url, probe.selected_month)
  963. ),
  964. )
  965. self.assertEqual(result.availability_requests, 2)
  966. self.assertEqual(events[0:3], [
  967. "refresh",
  968. "guard",
  969. (
  970. "https://prenotami.esteri.it/BookingCalendar/RetrieveCalendarAvailability",
  971. (2026, 8),
  972. ),
  973. ])
  974. self.assertEqual(events[3], "next")
  975. self.assertEqual(events[4], "guard")
  976. self.assertEqual(events[5][1], (2026, 9))
  977. def test_invalid_transport_observer_fails_before_browser_actions(self):
  978. page = SimpleNamespace()
  979. with self.assertRaisesRegex(CalendarGateError, "packet_observer"):
  980. scan_calendar_horizon(page, forward_months=1, packet_observer=object())
  981. def test_stops_cleanly_when_the_native_next_control_is_unavailable(self):
  982. packets = [self.packet(8)]
  983. def fake_listen(_page, *, action, **_kwargs):
  984. action()
  985. return packets.pop(0)
  986. with patch(
  987. "tools.debug_ita_calendar_gate.listen_once",
  988. side_effect=fake_listen,
  989. ), patch(
  990. "tools.debug_ita_calendar_gate._refresh_calendar",
  991. ), patch(
  992. "tools.debug_ita_calendar_gate._click_next_month",
  993. side_effect=CalendarNextMonthUnavailableError("bounded-stop"),
  994. ), patch(
  995. "tools.debug_ita_calendar_gate._calendar_guard",
  996. ), patch("tools.debug_ita_calendar_gate.time.sleep"):
  997. result = scan_calendar_horizon(
  998. SimpleNamespace(),
  999. forward_months=12,
  1000. )
  1001. self.assertEqual(result.forward_clicks, 0)
  1002. self.assertEqual(result.stop_reason, "next_control_unavailable")
  1003. self.assertTrue(result.safe_dict()["nextControlUnavailable"])
  1004. def test_result_rejects_skipped_or_excess_months(self):
  1005. august = CalendarProbeResult(
  1006. days=(),
  1007. selected_day="01/08/2026",
  1008. )
  1009. october = CalendarProbeResult(
  1010. days=(),
  1011. selected_day="01/10/2026",
  1012. )
  1013. with self.assertRaisesRegex(CalendarGateError, "exactly one month"):
  1014. CalendarMonthScanResult(
  1015. probes=(august, october),
  1016. requested_forward_months=2,
  1017. stop_reason="next_control_unavailable",
  1018. )
  1019. with self.assertRaisesRegex(CalendarGateError, "ended before"):
  1020. CalendarMonthScanResult(
  1021. probes=(august,),
  1022. requested_forward_months=2,
  1023. stop_reason="forward_limit_reached",
  1024. )
  1025. def test_result_accepts_an_exact_cross_year_transition(self):
  1026. december = CalendarProbeResult(
  1027. days=(),
  1028. selected_day="01/12/2026",
  1029. )
  1030. january = CalendarProbeResult(
  1031. days=(),
  1032. selected_day="01/01/2027",
  1033. )
  1034. result = CalendarMonthScanResult(
  1035. probes=(december, january),
  1036. requested_forward_months=1,
  1037. stop_reason="forward_limit_reached",
  1038. )
  1039. self.assertEqual(result.safe_dict()["startMonth"], "2026-12")
  1040. self.assertEqual(result.safe_dict()["furthestMonth"], "2027-01")
  1041. self.assertIsNone(result.safe_dict()["earliestAvailableDate"])
  1042. class BookingPreflightTests(unittest.TestCase):
  1043. def test_diagnostic_is_stage_specific_and_strictly_allowlisted(self):
  1044. raw = booking_preflight_snapshot(otpSendEnabled=1)
  1045. raw["mustNotLeak"] = "private-runtime-value"
  1046. diagnostic = safe_preflight_diagnostic(
  1047. raw,
  1048. stage=BookingPreflightStage.AFTER_OTP_SEND,
  1049. outcome="rejected",
  1050. )
  1051. self.assertEqual(
  1052. set(diagnostic),
  1053. {
  1054. "contract",
  1055. "sourceFingerprint",
  1056. "outcome",
  1057. "stage",
  1058. "snapshotType",
  1059. "formReady",
  1060. "otpValuePresent",
  1061. "otpSendEnabled",
  1062. "avantiEnabled",
  1063. "turnstileReady",
  1064. },
  1065. )
  1066. self.assertEqual(
  1067. diagnostic["contract"],
  1068. QCAL_TOOL_CONTRACT_VERSION,
  1069. )
  1070. self.assertEqual(
  1071. diagnostic["sourceFingerprint"],
  1072. QCAL_TOOL_SOURCE_FINGERPRINT,
  1073. )
  1074. self.assertEqual(diagnostic["stage"], "after_otp_send")
  1075. self.assertEqual(
  1076. diagnostic["otpSendEnabled"],
  1077. {"type": "int", "value": None},
  1078. )
  1079. self.assertEqual(
  1080. diagnostic["otpValuePresent"],
  1081. {"type": "bool", "value": False},
  1082. )
  1083. self.assertNotIn(
  1084. "private-runtime-value",
  1085. json.dumps(diagnostic),
  1086. )
  1087. invalid_outcome = safe_preflight_diagnostic(
  1088. raw,
  1089. stage=BookingPreflightStage.AFTER_OTP_SEND,
  1090. outcome="private-runtime-value",
  1091. )
  1092. self.assertEqual(invalid_outcome["outcome"], "invalid")
  1093. self.assertNotIn(
  1094. "private-runtime-value",
  1095. json.dumps(invalid_outcome),
  1096. )
  1097. def test_rejected_preflight_logs_shape_without_runtime_values(self):
  1098. raw = booking_preflight_snapshot(otpSendEnabled=1)
  1099. raw["mustNotLeak"] = "private-runtime-value"
  1100. page = SimpleNamespace(run_js=lambda _script: raw)
  1101. stdout = StringIO()
  1102. with redirect_stdout(stdout):
  1103. with self.assertRaisesRegex(CalendarGateError, "otpSendEnabled"):
  1104. wait_for_booking_preflight(
  1105. page,
  1106. script="safe-script",
  1107. stage=BookingPreflightStage.AFTER_OTP_SEND,
  1108. timeout=1,
  1109. )
  1110. output = stdout.getvalue()
  1111. self.assertIn("ITA_QCAL_PREFLIGHT_DIAGNOSTIC=", output)
  1112. self.assertIn('"stage": "after_otp_send"', output)
  1113. self.assertIn('"type": "int"', output)
  1114. self.assertNotIn("private-runtime-value", output)
  1115. def test_timeout_diagnostic_stays_in_the_debug_gate(self):
  1116. raw = booking_preflight_snapshot(turnstileReady=False)
  1117. raw["mustNotLeak"] = "private-runtime-value"
  1118. page = SimpleNamespace(run_js=lambda _script: raw)
  1119. stdout = StringIO()
  1120. with patch("plugins.ita_plugin.time.monotonic", side_effect=[0, 0, 2]), \
  1121. patch("plugins.ita_plugin.time.sleep"), redirect_stdout(stdout), \
  1122. self.assertRaisesRegex(CalendarGateError, "turnstile_token_pending"):
  1123. wait_for_booking_preflight(page, script="safe-script", timeout=1)
  1124. output = stdout.getvalue()
  1125. self.assertIn("ITA_QCAL_PREFLIGHT_DIAGNOSTIC=", output)
  1126. self.assertIn('"outcome": "timeout"', output)
  1127. self.assertNotIn("private-runtime-value", output)
  1128. def test_preflight_requires_exact_safe_shape_without_reading_tokens(self):
  1129. raw = booking_preflight_snapshot()
  1130. validate_booking_preflight(raw)
  1131. for key in ("formReady", "turnstileReady", "otpValuePresent",
  1132. "otpSendEnabled", "avantiEnabled"):
  1133. with self.subTest(key=key):
  1134. changed = dict(raw)
  1135. changed[key] = not changed[key]
  1136. with self.assertRaisesRegex(CalendarGateError, key):
  1137. validate_booking_preflight(changed)
  1138. def test_post_otp_accepts_cooldown_without_relaxing_structure(self):
  1139. cooldown = booking_preflight_snapshot(
  1140. otpSendEnabled=False,
  1141. otpValuePresent=True,
  1142. )
  1143. with self.assertRaisesRegex(CalendarGateError, "otpValuePresent"):
  1144. validate_booking_preflight(cooldown)
  1145. validate_booking_preflight(
  1146. cooldown,
  1147. after_otp=True,
  1148. )
  1149. for key, value in (
  1150. ("otpSendEnabled", True),
  1151. ("otpValuePresent", False),
  1152. ("formReady", False),
  1153. ):
  1154. with self.subTest(key=key):
  1155. changed = dict(cooldown)
  1156. changed[key] = value
  1157. with self.assertRaisesRegex(CalendarGateError, key):
  1158. validate_booking_preflight(
  1159. changed,
  1160. after_otp=True,
  1161. )
  1162. def test_dynamic_wait_rechecks_token_without_relaxing_structure(self):
  1163. base = booking_preflight_snapshot(
  1164. avantiEnabled=False,
  1165. turnstileReady=False,
  1166. )
  1167. ready = dict(base)
  1168. ready["avantiEnabled"] = True
  1169. ready["turnstileReady"] = True
  1170. outputs = [base, ready]
  1171. page = SimpleNamespace(run_js=lambda _script: outputs.pop(0))
  1172. with patch("tools.debug_ita_calendar_gate.time.sleep"):
  1173. result = wait_for_booking_preflight(
  1174. page,
  1175. script="safe-script",
  1176. timeout=1,
  1177. )
  1178. self.assertIs(result, ready)
  1179. def test_post_otp_wait_accepts_disabled_send_button(self):
  1180. base = booking_preflight_snapshot(
  1181. otpSendEnabled=False,
  1182. otpValuePresent=True,
  1183. avantiEnabled=False,
  1184. turnstileReady=False,
  1185. )
  1186. ready = dict(base)
  1187. ready["avantiEnabled"] = True
  1188. ready["turnstileReady"] = True
  1189. outputs = [base, ready]
  1190. page = SimpleNamespace(run_js=lambda _script: outputs.pop(0))
  1191. stdout = StringIO()
  1192. with redirect_stdout(stdout):
  1193. with patch("tools.debug_ita_calendar_gate.time.sleep"):
  1194. result = wait_for_booking_preflight(
  1195. page,
  1196. script="safe-script",
  1197. stage=BookingPreflightStage.AFTER_OTP_SEND,
  1198. timeout=1,
  1199. )
  1200. self.assertIs(result, ready)
  1201. self.assertIn('"outcome": "ready"', stdout.getvalue())
  1202. self.assertIn('"stage": "after_otp_send"', stdout.getvalue())
  1203. self.assertIn('"type": "bool"', stdout.getvalue())
  1204. class OtpEntryTests(unittest.TestCase):
  1205. def test_manual_otp_is_visible_and_still_validated(self):
  1206. prompt = StringIO()
  1207. with patch("builtins.input", return_value="123456") as input_mock:
  1208. with redirect_stdout(prompt):
  1209. result = _read_manual_otp()
  1210. self.assertEqual(result, "123456")
  1211. input_mock.assert_called_once_with(
  1212. "Italy booking OTP "
  1213. "(visible; terminal scrollback may retain it): "
  1214. )
  1215. self.assertNotIn("123456", prompt.getvalue())
  1216. for invalid in ("", "123", "1234567890123", "12a456"):
  1217. with self.subTest(invalid=invalid):
  1218. with patch("builtins.input", return_value=invalid):
  1219. with self.assertRaisesRegex(
  1220. CalendarGateError,
  1221. "OTP must be 4-12 digits",
  1222. ):
  1223. _read_manual_otp()
  1224. def test_otp_entry_focuses_before_typing_and_compares_in_page(self):
  1225. events = []
  1226. class FakeOtpElement:
  1227. def input(self, value, clear=False):
  1228. events.append(("input", value, clear))
  1229. def run_js(self, script, value):
  1230. events.append(("compare", script, value))
  1231. return True
  1232. otp_element = FakeOtpElement()
  1233. plugin = SimpleNamespace(
  1234. page=SimpleNamespace(
  1235. ele=lambda selector, timeout=0: (
  1236. otp_element if selector == "#otp-input" else None
  1237. )
  1238. ),
  1239. mouse=SimpleNamespace(
  1240. human_click_ele=lambda element: events.append(
  1241. ("click", element)
  1242. )
  1243. ),
  1244. )
  1245. stdout = StringIO()
  1246. with redirect_stdout(stdout):
  1247. _enter_otp(plugin, "123456")
  1248. self.assertEqual(events[0], ("click", otp_element))
  1249. self.assertEqual(events[1], ("input", "123456", True))
  1250. self.assertEqual(events[2][0], "compare")
  1251. self.assertIn("this.value === arguments[0]", events[2][1])
  1252. self.assertEqual(events[2][2], "123456")
  1253. self.assertIn(
  1254. "ITA_QCAL_OTP_DOM_WRITE_VERIFIED matched=true",
  1255. stdout.getvalue(),
  1256. )
  1257. self.assertNotIn("123456", stdout.getvalue())
  1258. def test_otp_entry_fails_before_avanti_when_readback_differs(self):
  1259. otp_element = SimpleNamespace(
  1260. input=lambda _value, clear=False: None,
  1261. run_js=lambda _script, _value: False,
  1262. )
  1263. plugin = SimpleNamespace(
  1264. page=SimpleNamespace(ele=lambda _selector, timeout=0: otp_element),
  1265. mouse=SimpleNamespace(human_click_ele=lambda _element: None),
  1266. )
  1267. with self.assertRaisesRegex(
  1268. CalendarGateError,
  1269. "OTP DOM write verification failed",
  1270. ):
  1271. _enter_otp(plugin, "123456")
  1272. class ToolContractTests(unittest.TestCase):
  1273. def test_contract_is_logged_before_route_or_credentials_are_read(self):
  1274. tool_path = (
  1275. Path(__file__).resolve().parents[2]
  1276. / "tools"
  1277. / "debug_ita_calendar_gate.py"
  1278. )
  1279. shared_path = (
  1280. Path(__file__).resolve().parents[2]
  1281. / "plugins"
  1282. / "ita_plugin.py"
  1283. )
  1284. plugin_path = tool_path.parents[1] / "plugins" / "ita_plugin.py"
  1285. protocol_path = tool_path.parents[1] / "plugins" / "ita_plugin.py"
  1286. expected_fingerprint = hashlib.sha256(
  1287. tool_path.read_bytes()
  1288. + (tool_path.parents[1] / "plugins" / "ita_plugin.py").read_bytes()
  1289. + shared_path.read_bytes()
  1290. + plugin_path.read_bytes() + protocol_path.read_bytes()
  1291. + (tool_path.parents[1] / "plugins" / "ita_plugin.py").read_bytes()
  1292. + (tool_path.parents[1] / "tools" / "debug_ita_soak.py").read_bytes()
  1293. + (tool_path.parents[1] / "toolkit" / "backoff.py").read_bytes()
  1294. ).hexdigest()[:12]
  1295. self.assertEqual(
  1296. QCAL_TOOL_SOURCE_FINGERPRINT,
  1297. expected_fingerprint,
  1298. )
  1299. stdout = StringIO()
  1300. with patch(
  1301. "tools.debug_ita_calendar_gate.resolve_debug_route",
  1302. side_effect=CalendarGateError("offline-stop"),
  1303. ):
  1304. with redirect_stdout(stdout):
  1305. with self.assertRaisesRegex(
  1306. CalendarGateError,
  1307. "offline-stop",
  1308. ):
  1309. run_calendar_gate({})
  1310. self.assertEqual(
  1311. stdout.getvalue().strip(),
  1312. "ITA_QCAL_TOOL_CONTRACT "
  1313. f"version={QCAL_TOOL_CONTRACT_VERSION} "
  1314. f"source_sha256={QCAL_TOOL_SOURCE_FINGERPRINT}",
  1315. )
  1316. class ReservationSnapshotTests(unittest.TestCase):
  1317. def test_reads_both_initialized_datatables_and_always_closes_tab(self):
  1318. loading = {
  1319. "path": "/Reservation",
  1320. "authenticated": True,
  1321. "tableCount": 2,
  1322. "ready": False,
  1323. "reservations": None,
  1324. "waitlist": None,
  1325. }
  1326. ready = {
  1327. **loading,
  1328. "ready": True,
  1329. "reservations": 2,
  1330. "waitlist": 1,
  1331. }
  1332. tab = FakeReservationTab([loading, ready, ready])
  1333. opened_urls = []
  1334. page = SimpleNamespace(
  1335. new_tab=lambda url: opened_urls.append(url) or tab
  1336. )
  1337. with patch("tools.debug_ita_calendar_gate.time.sleep"):
  1338. result = read_reservation_snapshot(
  1339. page,
  1340. timeout=1,
  1341. settle_seconds=0,
  1342. )
  1343. self.assertEqual(result, ReservationSnapshot(2, 1))
  1344. self.assertEqual(opened_urls, ["https://prenotami.esteri.it/Reservation"])
  1345. self.assertEqual(tab.close_count, 1)
  1346. def test_invalid_counts_fail_and_close_tab(self):
  1347. tab = FakeReservationTab(
  1348. [
  1349. {
  1350. "path": "/Reservation",
  1351. "authenticated": True,
  1352. "tableCount": 2,
  1353. "ready": True,
  1354. "reservations": True,
  1355. "waitlist": 0,
  1356. }
  1357. ]
  1358. )
  1359. page = SimpleNamespace(new_tab=lambda _url: tab)
  1360. with self.assertRaises(CalendarGateError):
  1361. read_reservation_snapshot(
  1362. page,
  1363. timeout=1,
  1364. settle_seconds=0,
  1365. )
  1366. self.assertEqual(tab.close_count, 1)
  1367. class SourceSafetyTests(unittest.TestCase):
  1368. def test_gate_source_never_clicks_or_submits_the_final_control(self):
  1369. path = (
  1370. Path(__file__).resolve().parents[2]
  1371. / "tools"
  1372. / "debug_ita_calendar_gate.py"
  1373. )
  1374. source = path.read_text(encoding="utf-8")
  1375. tree = ast.parse(source)
  1376. violations = []
  1377. one_shot_calls = []
  1378. for node in ast.walk(tree):
  1379. if not isinstance(node, ast.Call):
  1380. continue
  1381. function = node.func
  1382. function_name = (
  1383. function.attr
  1384. if isinstance(function, ast.Attribute)
  1385. else function.id
  1386. if isinstance(function, ast.Name)
  1387. else ""
  1388. )
  1389. string_arguments = {
  1390. child.value
  1391. for child in ast.walk(node)
  1392. if isinstance(child, ast.Constant)
  1393. and isinstance(child.value, str)
  1394. }
  1395. if function_name in {"submit", "click"} and any(
  1396. "btnPrenotaNoOtp" in value
  1397. or "InsertNewBooking" in value
  1398. for value in string_arguments
  1399. ):
  1400. violations.append((function_name, node.lineno))
  1401. if function_name == "human_click_ele" and any(
  1402. "btnPrenotaNoOtp" in value
  1403. for value in string_arguments
  1404. ):
  1405. violations.append((function_name, node.lineno))
  1406. if function_name == "run" and isinstance(function, ast.Attribute):
  1407. if isinstance(function.value, ast.Name):
  1408. one_shot_calls.append(function.value.id)
  1409. self.assertEqual(violations, [])
  1410. self.assertEqual(one_shot_calls.count("otp_guard"), 1)
  1411. self.assertEqual(one_shot_calls.count("avanti_guard"), 1)
  1412. self.assertIn("button.disabled = true", CALENDAR_GUARD_SCRIPT)
  1413. self.assertIn('OneShotAction("OTP")', source)
  1414. self.assertIn('OneShotAction("AVANTI")', source)
  1415. self.assertIn(
  1416. '#datetimepicker',
  1417. NEXT_MONTH_CONTROL_SCRIPT,
  1418. )
  1419. self.assertIn(
  1420. 'th.dtpicker-next',
  1421. NEXT_MONTH_CONTROL_SCRIPT,
  1422. )
  1423. for forbidden_selector in (
  1424. "btnPrenotaNoOtp",
  1425. "InsertNewBooking",
  1426. ".fascia",
  1427. ".events a",
  1428. "a.next",
  1429. "data-action",
  1430. "fetch(",
  1431. "XMLHttpRequest",
  1432. ):
  1433. self.assertNotIn(
  1434. forbidden_selector,
  1435. NEXT_MONTH_CONTROL_SCRIPT,
  1436. )
  1437. if __name__ == "__main__":
  1438. unittest.main()