test_debug_ita_query_book.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. """Real framework creation/publication/dispatch/execution, synthetic I/O only.
  2. No test-owned signal schema, expiry rule, account dispatcher or retry policy.
  3. The already-existing Redis fixture simulates a retained server key. Snapshot
  4. encoding/decoding, account binding, route/date/cooldown decisions and public
  5. Italy Query/Book remain the real implementations.
  6. """
  7. import contextlib
  8. import copy
  9. from datetime import date
  10. import io
  11. import itertools
  12. import json
  13. import tempfile
  14. from pathlib import Path
  15. from types import SimpleNamespace
  16. import unittest
  17. from unittest.mock import Mock, patch
  18. from tools import debug_ita_query_book as cli
  19. from test.ita.test_ita_plugin import OrderBookerGCO, FakeRetainedSignalRedis
  20. from sentinel import SentinelGCO
  21. from test.ita import test_ita_calendar_book_flow as book_flow
  22. from test.ita.test_ita_query_recovery import RecoveryPage, prepared_plugin
  23. from vs_types import AvailabilityStatus, GroupConfig, QueryWaitMode
  24. class ExistingFrameworkJointTests(unittest.TestCase):
  25. def setUp(self):
  26. # Fail closed if a future test accidentally stops isolating I/O.
  27. for target in ("socket.socket.connect", "socket.socket.connect_ex", "socket.create_connection",
  28. "requests.sessions.Session.request", "subprocess.Popen"):
  29. self.enterContext(patch(target, side_effect=AssertionError("Offline test attempted external I/O")))
  30. self.clock = {"now": 1000.0}
  31. self.retained = None
  32. self.query_page = RecoveryPage()
  33. self.query_page.plans = [[(2026, 8, []), (2026, 9, []), (2026, 10, [13, 20])]] * 10
  34. self.query, self.route = prepared_plugin(self.query_page)
  35. self.apt = self.route.appointment_type()
  36. self.apt.weight = 1
  37. self.query._transient_user_data_path = "/nonexistent/ita-framework-query"
  38. self.query.free_config.update(otp_mode="manual", final_submit_enabled=False)
  39. self.query.query = Mock(wraps=self.query.query)
  40. # Existing fixture is already on Calendar; restore its prepared marker
  41. # after the framework's real set_config clears per-session state.
  42. self.query.create_session = Mock(side_effect=lambda:
  43. self.query._record_calendar_prepared_route(self.query._resolve_route(self.apt), role="query"))
  44. self.query.cleanup = Mock()
  45. self.inputs = {"username": "applicant@example.test", "password": "synthetic-applicant-password",
  46. "expected_start_date": "2026-08-01", "expected_end_date": "2026-10-31"}
  47. self.order = {"id": 991, "order_id": "SYNTHETIC-ORDER", "status": "processing",
  48. "user_inputs": copy.deepcopy(self.inputs)}
  49. self.cloud = Mock()
  50. self.cloud.get_next_account.return_value = {
  51. "id": 21, "username": "pool-query@example.test", "password": "synthetic-pool-password"}
  52. self.cloud.get_vas_task_pop.return_value = self.order
  53. self.redis = Mock()
  54. self.redis.setex.side_effect = self.capture_redis_write
  55. self.redis.get.side_effect = lambda key: (
  56. self.retained.get(key) if self.retained is not None and key == self.signal_key else None)
  57. self.redis.delete.side_effect = self.delete_redis_key
  58. self.signal_key = "vs:signal:" + self.apt.routing_key
  59. self.executor = Mock()
  60. self.log = Mock()
  61. # Existing test pattern: isolate only method dependencies, not methods.
  62. # Use each loaded class's globals to avoid import-cache/stub ambiguity.
  63. dependencies = {
  64. "SafeRedisClient": Mock(return_value=self.redis),
  65. "VSCloudApi": SimpleNamespace(Instance=lambda: self.cloud, lock_account=Mock()),
  66. "time": SimpleNamespace(time=lambda: self.clock["now"], sleep=lambda _seconds: None),
  67. }
  68. for cls in (SentinelGCO, OrderBookerGCO):
  69. self.enterContext(patch.dict(cls.__init__.__globals__, dependencies))
  70. self.enterContext(patch.dict(OrderBookerGCO._execute_book_job.__globals__,
  71. ThreadPool=SimpleNamespace(getInstance=lambda: self.executor)))
  72. self.final = self.enterContext(patch("plugins.ita_plugin.submit_final",
  73. side_effect=AssertionError("Final submission forbidden")))
  74. # Query month navigation uses only the synthetic page above.
  75. self.enterContext(patch("plugins.ita_plugin.time.sleep"))
  76. self.query_config = GroupConfig(
  77. identifier="offline-query", need_account=True, appointment_types=[self.apt],
  78. plugin_config={"plugin_name": "ita_plugin"}, free_config=copy.deepcopy(self.query.free_config),
  79. sentinel={"account_pool_id": "fixture.query.pool", "account_cd": 10800,
  80. "signal_ttl": 30, "query_wait": {"mode": QueryWaitMode.Fixed, "fixed_wait": 1}})
  81. self.book_plugin, self.book_page = self.new_book_plugin()
  82. self.book_config = GroupConfig(
  83. identifier="offline-book", need_account=True, appointment_types=[self.apt],
  84. plugin_config={"plugin_name": "ita_plugin"}, free_config=copy.deepcopy(self.book_plugin.free_config),
  85. booker={"account_source": "order", "target_instances": 1, "booking_cooldown": 45})
  86. self.sentinel = SentinelGCO(self.query_config, {}, self.log)
  87. self.booker = OrderBookerGCO(self.book_config, {}, self.log)
  88. self.sentinel.m_factory.create = Mock(return_value=self.query)
  89. self.booker.m_factory.create = Mock(return_value=self.book_plugin)
  90. self.booker._execute_book_job = Mock(wraps=self.booker._execute_book_job)
  91. # Real GCO stop/cleanup is exercised; browser cleanup remains a fixture.
  92. self.addCleanup(self.sentinel.stop)
  93. self.addCleanup(self.booker.stop)
  94. def new_book_plugin(self):
  95. page = book_flow.FlowPage([book_flow.month("2026-10", {13: 0, 14: 1})])
  96. plugin, _, _ = book_flow.prepared_plugin(page, routing_key=self.apt.routing_key)
  97. plugin.free_config.update(otp_mode="cloud", final_submit_enabled=False)
  98. plugin.is_healthy = True
  99. plugin.cleanup = Mock()
  100. # Login/OTP are already independently tested; this fixture starts at
  101. # the prepared Calendar but binds it AFTER real order account wiring.
  102. plugin.create_session = Mock(side_effect=lambda:
  103. plugin._record_calendar_prepared_route(plugin._resolve_route(self.apt), role="book"))
  104. original_book = plugin.book
  105. plugin.observed_approval = None
  106. def observed_book(trigger, inputs):
  107. result, plugin.observed_approval = book_flow.book_with_approval(
  108. plugin, trigger, inputs, call=original_book,
  109. )
  110. return result
  111. plugin.book = Mock(side_effect=observed_book)
  112. return plugin, page
  113. def capture_redis_write(self, key, ttl, payload):
  114. self.assertEqual(key, self.signal_key)
  115. # Reuse existing retained-key fixture, not a new signal/TTL algorithm.
  116. self.retained = FakeRetainedSignalRedis(json.loads(payload), self.clock,
  117. ttl=self.clock["now"] + ttl)
  118. return True
  119. def delete_redis_key(self, key):
  120. if key == self.signal_key:
  121. self.retained = None
  122. return 1
  123. def spawn(self):
  124. self.sentinel._spawn_sentinel_worker()
  125. self.booker._spawn_worker(self.apt.routing_key)
  126. def publish_once(self, *, positive=True):
  127. # Bound the EXISTING loop to one cycle; do not reimplement its body.
  128. with patch.object(self.sentinel.m_stop_event, "is_set", side_effect=[False, True]):
  129. self.sentinel._monitor_loop()
  130. if positive:
  131. self.assertIsNotNone(self.retained, self.log.call_args_list)
  132. def trigger_once(self, page=None):
  133. with book_flow.browser_navigation(page or self.book_page), \
  134. patch.object(self.booker.m_stop_event, "is_set", side_effect=[False, True]):
  135. # Existing Thread creation and joins run as-is inside this loop.
  136. self.booker._booking_trigger_loop()
  137. def assert_no_final_or_success_report(self):
  138. self.final.assert_not_called()
  139. self.executor.enqueue.assert_not_called()
  140. self.cloud.update_vas_task.assert_not_called()
  141. self.cloud.push_weixin_text.assert_not_called()
  142. def test_real_framework_pool_query_json_order_book_rechecks_current_inventory(self):
  143. self.spawn()
  144. self.assertEqual(len(self.sentinel.m_tasks), 1)
  145. self.assertEqual(len(self.booker.m_tasks), 1)
  146. self.cloud.get_next_account.assert_called_once_with("fixture.query.pool", 10800)
  147. self.cloud.get_vas_task_pop.assert_called_once_with("auto." + self.apt.routing_key)
  148. self.assertEqual(self.query.config.account.username, "pool-query@example.test")
  149. self.assertEqual(self.query.config.account.id, 21)
  150. self.assertEqual(self.book_plugin.config.account.username, self.inputs["username"])
  151. self.assertEqual(self.book_plugin.config.account.password, self.inputs["password"])
  152. self.assertFalse(self.sentinel.m_tasks[0].book_allowed)
  153. self.assertEqual(self.booker.m_tasks[0].task_ref, 991)
  154. self.assertEqual(self.booker.m_tasks[0].acceptable_routing_keys, [self.apt.routing_key])
  155. self.assertIsNot(self.query.page, self.book_plugin.page)
  156. self.publish_once()
  157. self.query.query.assert_called_once_with(self.apt)
  158. self.redis.setex.assert_called_once()
  159. key, ttl, raw = self.redis.setex.call_args.args
  160. self.assertEqual((key, ttl), (self.signal_key, 30))
  161. payload = json.loads(raw)
  162. self.assertEqual(set(payload), {"group_id", "apt_type", "query_result", "timestamp"})
  163. self.assertEqual(payload["group_id"], self.query_config.identifier)
  164. self.assertEqual(payload["apt_type"], self.apt.model_dump())
  165. self.assertEqual(payload["query_result"]["earliest_date"], "2026-10-13")
  166. self.assertEqual(payload["query_result"]["availability_status"], "Available")
  167. self.assertNotIn("success", payload["query_result"])
  168. self.assertNotIn("password", raw)
  169. self.assertNotIn("pool-query", raw)
  170. self.assertEqual(self.query_page.forward_clicks, [(2026, 9), (2026, 10)])
  171. self.trigger_once()
  172. self.booker._execute_book_job.assert_called_once()
  173. self.book_plugin.book.assert_called_once()
  174. trigger, inputs = self.book_plugin.book.call_args.args
  175. self.assertEqual(trigger.apt_type, self.apt)
  176. self.assertIs(trigger.availability_status, AvailabilityStatus.Available)
  177. self.assertFalse(trigger.success) # Actual framework decode default.
  178. self.assertEqual(trigger.earliest_date.date(), date(2026, 10, 13))
  179. self.assertEqual(inputs, self.inputs)
  180. self.assertEqual(self.book_plugin.observed_approval.candidate.day, date(2026, 10, 14))
  181. self.assertEqual(self.book_page.refreshes, 1) # Applicant inventory is scanned once.
  182. self.assertGreater(len(self.book_page.requests), 0)
  183. self.assert_no_final_or_success_report()
  184. self.sentinel.stop()
  185. self.booker.stop()
  186. self.query.cleanup.assert_called_once()
  187. self.book_plugin.cleanup.assert_called_once()
  188. def test_no_account_or_login_failure_does_not_create_query_task(self):
  189. self.cloud.get_next_account.side_effect = RuntimeError("synthetic pool failure")
  190. self.sentinel._spawn_sentinel_worker()
  191. self.assertEqual(self.sentinel.m_tasks, [])
  192. self.sentinel.m_factory.create.assert_not_called()
  193. self.cloud.get_next_account.reset_mock(side_effect=True)
  194. self.query.create_session.side_effect = RuntimeError("synthetic login failure")
  195. self.sentinel._spawn_sentinel_worker()
  196. self.assertEqual(self.sentinel.m_tasks, [])
  197. self.query.cleanup.assert_called_once()
  198. self.assertGreater(self.sentinel.m_spawn_cooldown_until, self.clock["now"])
  199. def test_no_order_does_not_take_pool_account_for_book(self):
  200. self.cloud.get_vas_task_pop.return_value = None
  201. self.booker._spawn_worker(self.apt.routing_key)
  202. self.assertEqual(self.booker.m_tasks, [])
  203. self.booker.m_factory.create.assert_not_called()
  204. self.cloud.get_next_account.assert_not_called()
  205. def test_order_login_failure_uses_existing_cleanup_without_query_account_fallback(self):
  206. self.book_plugin.create_session.side_effect = RuntimeError("synthetic login failure")
  207. self.booker._spawn_worker(self.apt.routing_key)
  208. self.assertEqual(self.booker.m_tasks, [])
  209. self.assertEqual(self.booker.m_task_data_cache, {})
  210. self.book_plugin.cleanup.assert_called_once()
  211. self.cloud.get_next_account.assert_not_called()
  212. def test_no_availability_does_not_publish_or_trigger_book(self):
  213. self.query_page.outcomes = ["none"]
  214. self.spawn()
  215. self.publish_once(positive=False)
  216. self.trigger_once()
  217. self.redis.setex.assert_not_called()
  218. self.book_plugin.book.assert_not_called()
  219. self.cloud.slot_refresh_success.assert_called_once()
  220. self.assert_no_final_or_success_report()
  221. def test_bad_query_protocol_does_not_publish_a_positive_signal(self):
  222. self.query_page.outcomes = ["malformed"]
  223. self.spawn()
  224. self.publish_once(positive=False)
  225. self.trigger_once()
  226. self.redis.setex.assert_not_called()
  227. self.cloud.slot_refresh_fail.assert_called_once()
  228. self.book_plugin.book.assert_not_called()
  229. def test_server_expired_signal_does_not_trigger_book(self):
  230. self.spawn()
  231. self.publish_once()
  232. self.clock["now"] += self.query_config.sentinel.signal_ttl + 1
  233. self.trigger_once()
  234. self.book_plugin.book.assert_not_called()
  235. def test_same_signal_obeys_existing_task_cooldown_not_new_one_shot_rule(self):
  236. self.query_config.sentinel.signal_ttl = 180
  237. self.spawn()
  238. self.publish_once()
  239. self.trigger_once()
  240. self.clock["now"] += 1
  241. self.trigger_once()
  242. self.book_plugin.book.assert_called_once()
  243. self.clock["now"] += self.book_config.booker.booking_cooldown
  244. self.trigger_once()
  245. self.assertEqual(self.book_plugin.book.call_count, 2)
  246. self.assert_no_final_or_success_report()
  247. def test_unmatched_routing_key_is_filtered_by_real_trigger_loop(self):
  248. self.spawn()
  249. self.publish_once()
  250. self.booker.m_tasks[0].acceptable_routing_keys = ["slot.other.it.tourist"]
  251. self.trigger_once()
  252. self.book_plugin.book.assert_not_called()
  253. def test_date_outside_order_end_is_filtered_before_book(self):
  254. self.order["user_inputs"]["expected_end_date"] = "2026-10-12"
  255. self.spawn()
  256. self.publish_once()
  257. self.trigger_once()
  258. self.book_plugin.book.assert_not_called()
  259. def test_order_start_bound_reaches_real_book_selection(self):
  260. self.order["user_inputs"]["expected_start_date"] = "2026-10-15"
  261. self.spawn()
  262. self.publish_once()
  263. self.trigger_once()
  264. self.book_plugin.book.assert_called_once()
  265. self.assertIsNone(self.book_plugin.observed_approval)
  266. self.assert_no_final_or_success_report()
  267. def test_paused_order_is_removed_without_book(self):
  268. self.spawn()
  269. self.publish_once()
  270. self.order["status"] = "pause"
  271. self.trigger_once()
  272. self.book_plugin.book.assert_not_called()
  273. self.assertEqual(self.booker.m_tasks, [])
  274. self.book_plugin.cleanup.assert_called_once()
  275. def test_corrupt_redis_payload_uses_existing_delete_branch(self):
  276. self.spawn()
  277. self.publish_once()
  278. self.retained.payload = "invalid-json"
  279. self.trigger_once()
  280. self.redis.delete.assert_called_once_with(self.signal_key)
  281. self.book_plugin.book.assert_not_called()
  282. def test_one_shared_signal_can_trigger_two_separately_bound_orders(self):
  283. self.spawn()
  284. other, other_page = self.new_book_plugin()
  285. other_order = copy.deepcopy(self.order)
  286. other_order.update(id=992, order_id="SYNTHETIC-OTHER")
  287. other_order["user_inputs"]["username"] = "second-applicant@example.test"
  288. self.cloud.get_vas_task_pop.return_value = other_order
  289. self.booker.m_factory.create.return_value = other
  290. self.booker._spawn_worker(self.apt.routing_key)
  291. self.publish_once()
  292. # Both fixtures start directly in the positive month; no month-click
  293. # adapter is needed for this two-thread test, only no-delay sleeps.
  294. with patch.object(self.booker.m_stop_event, "is_set", side_effect=[False, True]):
  295. self.booker._booking_trigger_loop()
  296. self.book_plugin.book.assert_called_once()
  297. other.book.assert_called_once()
  298. self.assertEqual(other.config.account.username, "second-applicant@example.test")
  299. self.assertIsNot(self.book_page, other_page)
  300. self.assertEqual(other.observed_approval.candidate.day, date(2026, 10, 14))
  301. self.assertEqual(self.booker._execute_book_job.call_count, 2)
  302. self.cloud.get_next_account.assert_called_once()
  303. self.assert_no_final_or_success_report()
  304. def test_later_negative_query_keeps_existing_signal_until_redis_expiry(self):
  305. self.spawn()
  306. self.publish_once()
  307. original = self.retained.payload
  308. self.query_page.outcomes = ["none"]
  309. self.clock["now"] += 2
  310. self.publish_once(positive=False)
  311. self.assertEqual(self.query.query.call_count, 2)
  312. self.redis.setex.assert_called_once()
  313. self.redis.delete.assert_not_called()
  314. self.assertEqual(self.retained.payload, original)
  315. class OfflineCliTests(unittest.TestCase):
  316. def test_old_live_flag_is_rejected_before_loading_tests(self):
  317. with patch.object(cli.unittest.defaultTestLoader, "loadTestsFromNames") as load, \
  318. contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit) as caught:
  319. cli.main(["--run"])
  320. self.assertEqual(caught.exception.code, 2)
  321. load.assert_not_called()
  322. def test_cli_runs_only_existing_framework_suite_and_reports_offline(self):
  323. result = SimpleNamespace(testsRun=26, failures=[], errors=[], skipped=[], wasSuccessful=lambda: True)
  324. with patch.object(cli.unittest.defaultTestLoader, "loadTestsFromNames") as load, \
  325. patch.object(cli.unittest, "TextTestRunner") as runner, \
  326. contextlib.redirect_stdout(io.StringIO()) as output:
  327. runner.return_value.run.return_value = result
  328. self.assertEqual(cli.main([]), 0)
  329. load.assert_called_once_with(cli.TEST_NAMES)
  330. summary = json.loads(output.getvalue().split("ITA_QUERY_BOOK_FRAMEWORK_RESULT=")[1])
  331. self.assertTrue(summary["passed"])
  332. self.assertFalse(summary["liveAcceptance"])
  333. self.assertEqual(summary["externalBoundaries"], "simulated")
  334. def test_empty_skipped_or_failed_suite_does_not_pass(self):
  335. for count, skipped, successful in ((0, [], True), (1, ["fixture"], True), (1, [], False)):
  336. result = SimpleNamespace(testsRun=count, failures=[], errors=[], skipped=skipped,
  337. wasSuccessful=lambda: successful)
  338. with patch.object(cli.unittest.defaultTestLoader, "loadTestsFromNames"), \
  339. patch.object(cli.unittest, "TextTestRunner") as runner, \
  340. contextlib.redirect_stdout(io.StringIO()):
  341. runner.return_value.run.return_value = result
  342. self.assertEqual(cli.main(["--offline"]), 1)
  343. class LocalHarnessTests(unittest.TestCase):
  344. """Same real GCO fixtures; site/pool/Redis are synthetic in offline runs."""
  345. def setUp(self):
  346. from tools import ita_query_book_local as local
  347. self.local = local
  348. self.fixture = ExistingFrameworkJointTests()
  349. self.fixture.setUp()
  350. self.addCleanup(self.fixture.doCleanups)
  351. self.enterContext(contextlib.redirect_stdout(io.StringIO()))
  352. self.enterContext(patch.object(local, "validate_cloud_otp_environment"))
  353. f = self.fixture
  354. f.cloud.get_next_account.return_value.update(pool_name="fixture.query.pool", status="active")
  355. self.applicant = local.VSAccount(username=f.inputs["username"], password=f.inputs["password"])
  356. self.cloud = local.LocalCloudBoundary(f.cloud, f.query_config,
  357. SimpleNamespace(routing_key=f.route.routing_key, mail=None),
  358. SimpleNamespace(model_dump=lambda: {}), self.applicant,
  359. {key: f.inputs[key] for key in ("expected_start_date", "expected_end_date")})
  360. self.enterContext(patch.object(local, "CalendarRequestAudit", return_value=Mock(**{
  361. "settled_snapshot.return_value": {"otpRequests": 0, "avantiPosts": 0,
  362. "availabilityRequests": 1, "timeSlotRequests": 1, "finalRequests": 0}})))
  363. self.enterContext(patch.object(local.SentinelGCO, "_is_within_active_hours", return_value=True))
  364. self.enterContext(patch.object(local.OrderBookerGCO, "_is_within_active_hours", return_value=True))
  365. # Factories still belong to the GCO. Substitute only browser creation.
  366. real_observe = local.observe_factory
  367. def synthetic_factory(gco, role, stack, observed, on_created=None):
  368. gco.m_factory.create = Mock(return_value=f.query if role == "query" else f.book_plugin)
  369. return real_observe(gco, role, stack, observed, on_created=on_created)
  370. self.enterContext(patch.object(local, "observe_factory", side_effect=synthetic_factory))
  371. self.enterContext(book_flow.browser_navigation(f.book_page))
  372. def run_case(self):
  373. return self.local.run_framework_once(self.fixture.query_config, self.fixture.book_config,
  374. self.fixture.route, self.cloud, {})
  375. def test_one_real_framework_iteration_preserves_pool_order_signal_and_selection(self):
  376. report = self.run_case()
  377. self.assertTrue(report["passed"], report)
  378. self.assertEqual(report["outcome"], "pre_final")
  379. self.assertEqual((report["queryCalls"], report["bookCalls"]), (1, 1))
  380. self.assertTrue(report["signalPublished"])
  381. self.assertTrue(report["signalReadByBooker"])
  382. self.assertTrue(report["selectionVerified"])
  383. self.assertTrue(report["cleanupVerified"])
  384. self.assertFalse(report["cloudOrders"])
  385. self.fixture.cloud.get_next_account.assert_called_once_with("fixture.query.pool", 10800)
  386. self.fixture.cloud.get_vas_task_pop.assert_not_called()
  387. self.fixture.cloud.update_vas_task.assert_not_called()
  388. self.fixture.cloud.slot_snapshot_report.assert_not_called()
  389. self.assertNotEqual(self.fixture.query.config.account.username, self.fixture.book_plugin.config.account.username)
  390. self.assertEqual(self.fixture.book_plugin.config.account.username, self.applicant.username)
  391. self.assertEqual(self.fixture.book_plugin.book.call_args.args[1], self.cloud.order["user_inputs"])
  392. def test_no_availability_does_not_call_book(self):
  393. self.fixture.query_page.plans = [[(2026, 8, []), (2026, 9, []), (2026, 10, [])]] * 2
  394. report = self.run_case()
  395. self.assertFalse(report["passed"])
  396. self.assertEqual(report["outcome"], "no_availability")
  397. self.assertEqual(report["bookCalls"], 0)
  398. self.assertFalse(report["signalPublished"])
  399. self.assertTrue(report["cleanupVerified"])
  400. def test_failed_redis_write_is_not_a_successful_handoff(self):
  401. self.fixture.redis.setex.side_effect = lambda *_: False
  402. report = self.run_case()
  403. self.assertFalse(report["passed"])
  404. self.assertEqual(report["reason"], "redis_publication_failed")
  405. self.assertEqual(report["bookCalls"], 0)
  406. def test_expired_signal_does_not_trigger_or_get_republished(self):
  407. self.fixture.redis.get.side_effect = lambda *_: None
  408. report = self.run_case()
  409. self.assertTrue(report["signalPublished"])
  410. self.assertFalse(report["signalReadByBooker"])
  411. self.assertEqual(report["bookCalls"], 0)
  412. self.assertEqual(report["outcome"], "not_triggered_expired_or_date_filtered")
  413. self.fixture.redis.setex.assert_called_once()
  414. def test_order_date_range_filters_real_trigger_without_calling_book(self):
  415. self.cloud.order["user_inputs"]["expected_end_date"] = "2026-09-30"
  416. report = self.run_case()
  417. self.assertEqual(report["bookCalls"], 0)
  418. self.assertTrue(report["signalReadByBooker"])
  419. self.assertFalse(report["passed"])
  420. def test_pool_failure_is_redacted_and_does_not_retry(self):
  421. self.fixture.cloud.get_next_account.side_effect = RuntimeError("secret-password-and-body")
  422. report = self.run_case()
  423. self.assertEqual(report["reason"], "pool_allocation_failed")
  424. self.assertEqual(self.cloud.pool_calls, 1)
  425. self.assertNotIn("secret-password", json.dumps(report))
  426. self.assertTrue(report["cleanupVerified"])
  427. def test_same_account_cannot_masquerade_as_dual_account_acceptance(self):
  428. self.fixture.cloud.get_next_account.return_value["username"] = self.applicant.username
  429. report = self.run_case()
  430. self.assertFalse(report["passed"])
  431. self.assertEqual(report["reason"], "query_and_book_accounts_same_choose_other_applicant")
  432. self.assertEqual(report["queryCalls"], 0)
  433. def test_cloud_order_and_reporting_methods_are_not_forwarded(self):
  434. for name in ("update_vas_task", "push_weixin_text", "get_vas_task", "return_vas_task_to_queue"):
  435. with self.assertRaises(self.local.LocalAcceptanceError):
  436. getattr(self.cloud, name)
  437. getattr(self.fixture.cloud, name).assert_not_called()
  438. def test_unexpected_final_request_fails_even_after_selection(self):
  439. self.local.CalendarRequestAudit.return_value.settled_snapshot.return_value["finalRequests"] = 1
  440. report = self.run_case()
  441. self.assertFalse(report["passed"])
  442. self.assertEqual(report["reason"], "unexpected_final_request_do_not_rerun")
  443. def test_book_exception_does_not_escape_as_success_or_leak_body(self):
  444. self.fixture.book_plugin.book.side_effect = RuntimeError("secret-otp-body")
  445. report = self.run_case()
  446. self.assertEqual(report["reason"], "book_failed")
  447. self.assertEqual(report["bookErrorType"], "RuntimeError")
  448. self.assertNotIn("secret-otp", json.dumps(report))
  449. self.assertTrue(report["cleanupVerified"])
  450. def test_final_success_cannot_be_reported_by_prefinal_test(self):
  451. from vs_types import VSBookResult
  452. self.fixture.book_plugin.book.side_effect = None
  453. self.fixture.book_plugin.book.return_value = VSBookResult(success=True)
  454. report = self.run_case()
  455. self.assertFalse(report["passed"])
  456. self.assertEqual(report["reason"], "unexpected_book_success_before_final")
  457. self.fixture.cloud.update_vas_task.assert_not_called()
  458. self.assertGreater(self.cloud.forbidden_calls, 0)
  459. def test_book_login_failure_does_not_allocate_pool_account(self):
  460. self.fixture.book_plugin.create_session.side_effect = RuntimeError("secret-provider-response")
  461. report = self.run_case()
  462. self.assertEqual(report["reason"], "applicant_session_not_created")
  463. self.fixture.cloud.get_next_account.assert_not_called()
  464. self.assertTrue(report["cleanupVerified"])
  465. def test_interrupt_during_query_cleans_both_sessions_without_book(self):
  466. self.fixture.query.query.side_effect = KeyboardInterrupt()
  467. report = self.run_case()
  468. self.assertEqual(report["outcome"], "interrupted")
  469. self.assertTrue(report["cleanupVerified"])
  470. self.assertEqual(report["bookCalls"], 0)
  471. def test_cleanup_failure_invalidates_otherwise_successful_selection(self):
  472. self.fixture.book_plugin.cleanup.side_effect = RuntimeError("synthetic-cleanup-failure")
  473. report = self.run_case()
  474. self.assertTrue(report["selectionVerified"])
  475. self.assertFalse(report["passed"])
  476. self.assertFalse(report["cleanupVerified"])
  477. def test_repeated_otp_is_not_accepted_as_single_round(self):
  478. self.local.CalendarRequestAudit.return_value.settled_snapshot.return_value["otpRequests"] = 2
  479. report = self.run_case()
  480. self.assertFalse(report["passed"])
  481. self.assertEqual(report["reason"], "unexpected_repeated_preparation")
  482. class LocalFinalTests(unittest.TestCase):
  483. def setUp(self):
  484. from tools import ita_query_book_local as local
  485. self.local = local
  486. self.applicant = SimpleNamespace(
  487. username="Applicant@example.test", password="fixture-password")
  488. self.route = SimpleNamespace(
  489. service_id=9001, routing_key="slot.fixture.it.tourist",
  490. capability="single_schengen_tourism", lookahead_months=3)
  491. candidate = SimpleNamespace(
  492. day=date(2026, 10, 13), time_label="10:05 - 11:05", slots_free=1)
  493. self.approval = SimpleNamespace(candidate=candidate)
  494. self.plugin = SimpleNamespace(config=SimpleNamespace(account=self.applicant))
  495. self.audit = Mock()
  496. self.audit.settled_snapshot.return_value = {
  497. "otpRequests": 0, "avantiPosts": 0, "availabilityRequests": 0,
  498. "timeSlotRequests": 0, "finalRequests": 0,
  499. }
  500. self.observed = {
  501. "book": SimpleNamespace(plugin=self.plugin, audit=self.audit),
  502. "query": SimpleNamespace(plugin=object(), audit=self.audit),
  503. }
  504. self.control = local.LocalFinalSubmission(self.applicant, self.route)
  505. def submit(self, result):
  506. production = Mock(return_value=result)
  507. with patch.object(self.local, "verify_plugin_selection"):
  508. returned = self.control.submit(
  509. self.plugin, self.route, self.approval, self.observed, production)
  510. production.assert_called_once_with(self.plugin, self.route, self.approval)
  511. return returned
  512. def test_standard_success_delegates_once_without_private_fence(self):
  513. result = SimpleNamespace(success=True)
  514. self.assertIs(self.submit(result), result)
  515. self.assertTrue(self.control.used)
  516. self.assertTrue(self.control.dispatched)
  517. self.assertFalse(hasattr(self.control, "claim_path"))
  518. def test_standard_failure_remains_retryable_by_the_shared_booker(self):
  519. result = SimpleNamespace(success=False)
  520. self.assertIs(self.submit(result), result)
  521. self.assertTrue(self.control.dispatched)
  522. self.assertNotEqual(getattr(self.control, "failure_reason", None), "unknown_do_not_retry")
  523. def test_same_local_control_cannot_dispatch_twice_in_one_run(self):
  524. self.submit(SimpleNamespace(success=False))
  525. with self.assertRaises(self.local.LocalAcceptanceError):
  526. self.control.submit(
  527. self.plugin, self.route, self.approval, self.observed, Mock())
  528. def test_wrong_applicant_or_existing_final_request_is_rejected(self):
  529. production = Mock()
  530. self.plugin.config.account.username = "other@example.test"
  531. with self.assertRaises(self.local.LocalAcceptanceError):
  532. self.control.submit(
  533. self.plugin, self.route, self.approval, self.observed, production,
  534. )
  535. production.assert_not_called()
  536. class LocalFinalCompositionTests(unittest.TestCase):
  537. def setUp(self):
  538. from tools import ita_query_book_local as local
  539. self.local = local
  540. def test_noninteractive_failure_uses_ordinary_exit_code(self):
  541. args = SimpleNamespace(
  542. submit_once=True, local_run=True, local_preflight=False,
  543. book_account_file="fixture")
  544. group = SimpleNamespace(sentinel=SimpleNamespace(account_pool_id="fixture"))
  545. route = SimpleNamespace(routing_key="fixture", mail=None)
  546. applicant = SimpleNamespace(
  547. username="applicant@example.test", password="fixture-password")
  548. result = {
  549. "passed": False, "outcome": "book_failed_retryable",
  550. "finalDispatched": True,
  551. }
  552. with contextlib.redirect_stdout(io.StringIO()), \
  553. patch.object(self.local, "load_settings", return_value=(group, route, Mock(), {})), \
  554. patch("sys.stdin.isatty", return_value=False), \
  555. patch("builtins.input", side_effect=AssertionError("No interactive input")) as prompt, \
  556. patch.object(self.local, "check_dependencies", return_value=("fixture", "fixture")), \
  557. patch.object(self.local, "load_local_account", return_value=applicant), \
  558. patch.object(self.local, "validate_cloud_otp_environment"), \
  559. patch.object(self.local, "LocalFinalSubmission"), \
  560. patch.object(self.local, "build_groups", return_value=(Mock(), Mock())), \
  561. patch.object(self.local, "LocalCloudBoundary"), \
  562. patch.object(self.local.VSCloudApi, "Instance"), \
  563. patch.object(self.local, "private_redis", return_value=contextlib.nullcontext({})), \
  564. patch.object(self.local, "run_framework_once", return_value=result):
  565. self.assertEqual(self.local.main_local(args), 1)
  566. result.update(passed=True, outcome="confirmed")
  567. self.assertEqual(self.local.main_local(args), 0)
  568. prompt.assert_not_called()
  569. class LocalConfigurationTests(unittest.TestCase):
  570. def setUp(self):
  571. from tools import ita_query_book_local as local
  572. self.local = local
  573. self.enterContext(contextlib.redirect_stdout(io.StringIO()))
  574. def settings_fixture(self):
  575. args = SimpleNamespace(config="synthetic.json", group="synthetic-group", proxy="http://127.0.0.1:6268",
  576. expected_start_date=None, expected_end_date=None)
  577. group = {"identifier": args.group, "plugin_config": {"plugin_name": "ita_plugin"},
  578. "sentinel": {"account_pool_id": "synthetic-pool", "signal_ttl": 30},
  579. "appointment_types": [{"routing_key": "slot.other.it.tourist", "weight": 1}],
  580. "free_config": {"query_mode": "calendar", "otp_mode": "cloud", "final_submit_enabled": False,
  581. "apt_configs": {"slot.other.it.tourist": {
  582. "service_id": 9001, "capability": "single_schengen_tourism",
  583. "calendar_scan": {"lookahead_months": 3},
  584. "form": {"booking_type_control_id": "typeofbookingddl", "booking_type_value": "1",
  585. "booking_type_labels": ["Single booking"], "purpose_control_id": "ddls_0",
  586. "purpose_value": "42", "purpose_labels": ["Tourism"]},
  587. "mail": {"authorization_source": "account"}}}}}
  588. return args, group
  589. def test_real_settings_parser_accepts_other_region_without_fixed_mailbox(self):
  590. args, group = self.settings_fixture()
  591. with patch.object(self.local.Path, "read_text", return_value=json.dumps([group])), \
  592. patch.object(self.local.configure, "TEST_ACCOUNT", None):
  593. source, route, proxy, preferences = self.local.load_settings(args)
  594. self.assertEqual(route.service_id, 9001)
  595. self.assertEqual(source.sentinel.account_pool_id, "synthetic-pool")
  596. self.assertEqual(route.mail.mailbox_for("second@example.test"), "second@example.test")
  597. self.assertEqual(proxy.port, 6268)
  598. self.assertEqual(preferences, {})
  599. def test_invalid_settings_cannot_reach_dependencies_or_pool(self):
  600. args, template = self.settings_fixture()
  601. for changes in ({"query_mode": "services_probe"}, {"otp_mode": "manual"}, {"final_submit_enabled": True}):
  602. group = copy.deepcopy(template)
  603. group["free_config"].update(changes)
  604. with self.subTest(changes=changes), \
  605. patch.object(self.local.Path, "read_text", return_value=json.dumps([group])), \
  606. patch.object(self.local.configure, "TEST_ACCOUNT", None), \
  607. self.assertRaises(self.local.LocalAcceptanceError):
  608. self.local.load_settings(args)
  609. def test_public_proxy_and_reversed_date_range_are_rejected_before_run(self):
  610. args, group = self.settings_fixture()
  611. for proxy, start, end in (("http://remote.example.test:6268", None, None),
  612. (args.proxy, "2026-10-31", "2026-10-01")):
  613. args.proxy, args.expected_start_date, args.expected_end_date = proxy, start, end
  614. with patch.object(self.local.Path, "read_text", return_value=json.dumps([group])), \
  615. patch.object(self.local.configure, "TEST_ACCOUNT", None), \
  616. self.assertRaises(Exception):
  617. self.local.load_settings(args)
  618. def test_successful_preflight_never_prompts_allocates_or_launches(self):
  619. args = SimpleNamespace(local_preflight=True)
  620. group = SimpleNamespace(sentinel=SimpleNamespace(account_pool_id="synthetic-pool"))
  621. route = SimpleNamespace(routing_key="synthetic-route")
  622. with patch.object(self.local, "load_settings", return_value=(group, route, Mock(), {})), \
  623. patch.object(self.local, "check_dependencies", return_value=("redis-server", "/synthetic/chrome")), \
  624. patch.object(self.local, "_get_credentials") as credentials, \
  625. patch.object(self.local.VSCloudApi, "Instance") as api, \
  626. patch.object(self.local, "private_redis") as private_redis:
  627. self.assertEqual(self.local.main_local(args), 0)
  628. credentials.assert_not_called()
  629. api.assert_not_called()
  630. private_redis.assert_not_called()
  631. def test_default_cli_never_starts_local_run_and_explicit_mode_is_forwarded(self):
  632. with patch.object(self.local, "main_local", return_value=7) as live:
  633. self.assertEqual(cli.main(["--local-run", "--group", "my-route-group"]), 7)
  634. self.assertEqual(live.call_args.args[0].group, "my-route-group")
  635. self.assertTrue(live.call_args.args[0].local_run)
  636. def test_preflight_checks_dependencies_before_api_or_credentials(self):
  637. args = SimpleNamespace(local_preflight=True)
  638. with patch.object(self.local, "load_settings", return_value=(Mock(), Mock(), Mock(), {})), \
  639. patch.object(self.local, "check_dependencies", side_effect=self.local.LocalAcceptanceError("missing_dependency")), \
  640. patch.object(self.local, "_get_credentials") as credentials, \
  641. patch.object(self.local.VSCloudApi, "Instance") as api:
  642. self.assertEqual(self.local.main_local(args), 1)
  643. credentials.assert_not_called()
  644. api.assert_not_called()
  645. def test_group_copy_preserves_service_and_ttl_not_production_config(self):
  646. group = GroupConfig(identifier="source", sentinel={"account_pool_id": "test-pool", "signal_ttl": 37},
  647. appointment_types=[{"routing_key": "slot.other.it.tourist", "weight": 1}],
  648. free_config={"query_mode": "calendar", "otp_mode": "cloud", "final_submit_enabled": False,
  649. "persistent_browser_profile_path": "/never-touch", "apt_configs": {"some-route": {"service_id": 999}}})
  650. before = group.model_dump()
  651. query, book = self.local.build_groups(group, "synthetic")
  652. self.assertEqual(group.model_dump(), before)
  653. self.assertEqual(query.sentinel.signal_ttl, 37)
  654. self.assertEqual(query.sentinel.account_pool_id, "test-pool")
  655. self.assertEqual(query.free_config["apt_configs"], before["free_config"]["apt_configs"])
  656. self.assertEqual(book.booker.account_source, "order")
  657. self.assertEqual(book.free_config["query_mode"], "services_probe")
  658. self.assertNotIn("persistent_browser_profile_path", query.free_config)
  659. self.assertFalse(book.free_config["final_submit_enabled"])
  660. def test_final_flag_only_enables_applicant_copy_and_requires_live_cli(self):
  661. group = GroupConfig(identifier="source", free_config={"final_submit_enabled": False})
  662. group.appointment_types = [SimpleNamespace(weight=1)]
  663. query, book = self.local.build_groups(group, "fixture", submit_once=True)
  664. self.assertFalse(group.free_config["final_submit_enabled"])
  665. self.assertFalse(query.free_config["final_submit_enabled"])
  666. self.assertTrue(book.free_config["final_submit_enabled"])
  667. with patch.object(self.local, "main_local", return_value=0) as live:
  668. for args in (["--submit-once"], ["--local-preflight", "--submit-once"], ["--offline", "--submit-once"]):
  669. with self.assertRaises(SystemExit), contextlib.redirect_stderr(io.StringIO()):
  670. cli.main(args)
  671. live.assert_not_called()
  672. self.assertEqual(cli.main(["--local-run", "--submit-once"]), 0)
  673. self.assertTrue(live.call_args.args[0].submit_once)
  674. def test_final_requires_explicit_live_mode_before_pool_or_browser(self):
  675. args = SimpleNamespace(submit_once=True, local_run=False)
  676. with patch.object(self.local, "load_settings", return_value=(Mock(), Mock(), Mock(), {})), \
  677. patch.object(self.local, "check_dependencies") as dependencies, \
  678. patch.object(self.local.VSCloudApi, "Instance") as api:
  679. self.assertEqual(self.local.main_local(args), 1)
  680. dependencies.assert_not_called()
  681. api.assert_not_called()
  682. def test_private_redis_is_owned_socket_only_no_shared_flush(self):
  683. process = Mock()
  684. process.poll.return_value = None
  685. with patch.object(self.local.subprocess, "Popen", return_value=process) as popen, \
  686. patch.object(self.local.redis, "Redis") as redis:
  687. redis.return_value.ping.return_value = True
  688. with self.local.private_redis("/synthetic/redis-server") as config:
  689. self.assertTrue(config["unix_socket_path"].startswith("/tmp/ita-qb-"))
  690. self.assertNotIn("host", config)
  691. command = popen.call_args.args[0]
  692. self.assertEqual(command[command.index("--port") + 1], "0")
  693. self.assertEqual(command[command.index("--save") + 1], "")
  694. self.assertEqual(command[command.index("--appendonly") + 1], "no")
  695. process.terminate.assert_called_once()
  696. process.wait.assert_called_once_with(timeout=5)
  697. redis.return_value.flushdb.assert_not_called()
  698. redis.return_value.flushall.assert_not_called()
  699. self.assertFalse(self.local.Path(config["unix_socket_path"]).parent.exists())
  700. def test_private_redis_cleanup_also_runs_when_test_fails(self):
  701. process = Mock()
  702. process.poll.return_value = None
  703. with patch.object(self.local.subprocess, "Popen", return_value=process), \
  704. patch.object(self.local.redis, "Redis") as redis:
  705. redis.return_value.ping.return_value = True
  706. with self.assertRaisesRegex(RuntimeError, "synthetic"):
  707. with self.local.private_redis("/synthetic/redis-server"):
  708. raise RuntimeError("synthetic")
  709. process.terminate.assert_called_once()
  710. def test_private_redis_startup_failure_closes_client_and_never_yields(self):
  711. process = Mock()
  712. process.poll.return_value = 1
  713. with patch.object(self.local.subprocess, "Popen", return_value=process), \
  714. patch.object(self.local.redis, "Redis") as redis:
  715. with self.assertRaisesRegex(self.local.LocalAcceptanceError, "private_redis_exited"):
  716. with self.local.private_redis("/synthetic/redis-server"):
  717. self.fail("Dead Redis cannot be used")
  718. redis.return_value.close.assert_called_once()
  719. class LocalRedisIntegrationTests(unittest.TestCase):
  720. """Opt-in class: real disposable Redis, synthetic website/account I/O.
  721. Not included in the CLI's --offline TEST_NAMES, which promises no subprocesses.
  722. """
  723. def run_real_redis_case(self, *, expire=False):
  724. import shutil
  725. import socket
  726. import time
  727. from tools import ita_query_book_local as local
  728. from utils.safe_redis_cli import SafeRedisClient
  729. executable = shutil.which("redis-server")
  730. if not executable:
  731. self.skipTest("redis-server is needed for the explicitly selected Redis integration test")
  732. original_connect = socket.socket.connect
  733. real_sleep = time.sleep
  734. with local.private_redis(executable) as config:
  735. case = LocalHarnessTests()
  736. case.setUp()
  737. try:
  738. def socket_only(connection, address):
  739. if address != config["unix_socket_path"]:
  740. raise AssertionError("Redis integration attempted non-owned socket")
  741. return original_connect(connection, address)
  742. case.enterContext(patch.object(socket.socket, "connect", socket_only))
  743. for cls in (local.SentinelGCO, local.OrderBookerGCO):
  744. case.enterContext(patch.dict(cls.__init__.__globals__, {"SafeRedisClient": SafeRedisClient}))
  745. if expire:
  746. case.fixture.query_config.sentinel.signal_ttl = 1
  747. read = SafeRedisClient.get
  748. def after_expiry(client, key):
  749. real_sleep(1.1) # Fixture mocks time.sleep; use actual server wall clock here.
  750. return read(client, key)
  751. case.enterContext(patch.object(SafeRedisClient, "get", after_expiry))
  752. report = local.run_framework_once(case.fixture.query_config, case.fixture.book_config,
  753. case.fixture.route, case.cloud, config)
  754. finally:
  755. case.doCleanups()
  756. self.assertFalse(local.Path(config["unix_socket_path"]).parent.exists())
  757. return report
  758. def test_real_framework_publishes_and_consumes_real_redis(self):
  759. report = self.run_real_redis_case()
  760. self.assertTrue(report["passed"], report)
  761. self.assertTrue(report["signalPublished"])
  762. self.assertTrue(report["signalReadByBooker"])
  763. self.assertEqual(report["bookCalls"], 1)
  764. def test_real_redis_expiry_prevents_book_without_republishing(self):
  765. report = self.run_real_redis_case(expire=True)
  766. self.assertTrue(report["signalPublished"])
  767. self.assertFalse(report["signalReadByBooker"])
  768. self.assertEqual(report["bookCalls"], 0)
  769. self.assertFalse(report["passed"])
  770. if __name__ == "__main__":
  771. unittest.main()