test_ita_reservation.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. """Synthetic fixtures reproducing observed markup; never calls the website."""
  2. import copy
  3. import json
  4. import shutil
  5. import subprocess
  6. import traceback
  7. import unittest
  8. from datetime import date
  9. from html.parser import HTMLParser
  10. from types import SimpleNamespace
  11. from unittest.mock import Mock, patch
  12. from plugins.ita_booking_final import ReservationState
  13. from plugins.ita_reservation import (
  14. DETAIL_READ_SCRIPT, LIST_READ_SCRIPT, DATE_DIAGNOSTIC_SCRIPT, HEADERS, ReservationReadError,
  15. parse_reservation_detail, parse_reservation_listing,
  16. read_reservation_detail, read_reservation_listing, read_reservation_date_diagnostic,
  17. )
  18. def detail():
  19. return {"path": "/ReservationDetail/Index/synthetic-link", "statusCode": "3", "fields": {
  20. "Servizio Consolare": "Visti", "Servizio Erogato": "Visti Nazionali e Schengen",
  21. "Codice Prenotazione": "SYNTHETIC-REFERENCE", "Tipo Prenotazione": "Prenotazione Singola",
  22. "Data": "13 ottobre 2026", "Ora": "10:25", "Stato": "Annullata",
  23. }}
  24. def listing():
  25. return {"path": "/Reservation", "tables": [
  26. {"id": "dataTableReservations", "structuralEmpty": False, "headers": [*HEADERS, ""], "count": 1,
  27. "info": {"page": 0, "pages": 1, "start": 0, "end": 1, "length": 10,
  28. "recordsTotal": 1, "recordsDisplay": 1, "serverSide": False},
  29. "rows": [{"reference": "SYNTHETIC-REFERENCE", "detailUrl": "https://prenotami.esteri.it/ReservationDetail/Index/synthetic-link",
  30. "office": "Visti", "service": "Visti Nazionali e Schengen", "date": "13 ottobre 2026",
  31. "time": "10:25", "status": "Annullata"}]},
  32. {"id": "dataTableReservationsWL", "structuralEmpty": True},
  33. ]}
  34. def confirmed_detail():
  35. """Observed 2026-08-31 business shape; identifiers remain synthetic."""
  36. raw = detail()
  37. raw.update(path="/ReservationDetail/Index/confirmed-synthetic", statusCode="2")
  38. raw["fields"].update({"Codice Prenotazione": "CONFIRMED-SYNTHETIC", "Data": "3 settembre 2026",
  39. "Ora": "09:26", "Stato": "Confermata"})
  40. return raw
  41. def confirmed_listing():
  42. raw = listing()
  43. table = raw["tables"][0]
  44. table["rows"].append({**table["rows"][0], "reference": "CONFIRMED-SYNTHETIC",
  45. "detailUrl": "https://prenotami.esteri.it/ReservationDetail/Index/confirmed-synthetic",
  46. "date": "3 settembre 2026", "time": "09:26", "status": "Confermata"})
  47. table["count"] = 2
  48. table["info"].update(recordsTotal=2, recordsDisplay=2, end=2)
  49. return raw
  50. class ReceiptParsingTests(unittest.TestCase):
  51. def test_observed_confirmed_pair_maps_active_without_inventing_slot_binding(self):
  52. parsed = parse_reservation_detail(confirmed_detail())
  53. self.assertIs(parsed.state, ReservationState.ACTIVE)
  54. self.assertEqual(parsed.day, date(2026, 9, 3))
  55. self.assertEqual(parsed.appointment_time, "09:26")
  56. self.assertFalse(hasattr(parsed, "service_id"))
  57. self.assertFalse(hasattr(parsed, "time_label"))
  58. def test_confirmed_label_with_wrong_code_or_code_without_label_stays_unknown(self):
  59. for label, code in (("Confermata", "3"), ("Confermata", "1"), ("Confermata", "02"),
  60. ("Annullata", "2"), ("Arbitrary", "2"), ("Confirmed", "2")):
  61. with self.subTest(label=label, code=code):
  62. raw = confirmed_detail()
  63. raw["fields"]["Stato"], raw["statusCode"] = label, code
  64. self.assertIs(parse_reservation_detail(raw).state, ReservationState.UNKNOWN)
  65. def test_cancelled_history_and_confirmed_list_candidate_remain_distinct(self):
  66. parsed = parse_reservation_listing(confirmed_listing())
  67. self.assertEqual([row.state for row in parsed.rows],
  68. [ReservationState.CANCELLED, ReservationState.ACTIVE])
  69. self.assertNotIn("CONFIRMED-SYNTHETIC", repr(parsed))
  70. def test_confirmed_mapping_is_not_hong_kong_or_date_specific(self):
  71. raw = confirmed_detail()
  72. raw["fields"].update({"Data": "12 febbraio 2027", "Servizio Erogato": "Different service",
  73. "Ora": "14:30"})
  74. self.assertIs(parse_reservation_detail(raw).state, ReservationState.ACTIVE)
  75. def test_cancelled_detail_has_assigned_time_not_invented_interval_or_service_id(self):
  76. result = parse_reservation_detail(detail())
  77. self.assertEqual(result.day, date(2026, 10, 13))
  78. self.assertEqual(result.appointment_time, "10:25")
  79. self.assertIs(result.state, ReservationState.CANCELLED)
  80. self.assertFalse(hasattr(result, "time_label"))
  81. self.assertFalse(hasattr(result, "service_id"))
  82. self.assertNotIn("SYNTHETIC-REFERENCE", repr(result))
  83. def test_unknown_and_conflicting_states_never_become_active(self):
  84. for text, code in (("Annullata", "2"), ("Confirmed", "3"), ("Confermata", "1"), ("Unknown", "99")):
  85. with self.subTest(code=code):
  86. raw = detail()
  87. raw["fields"]["Stato"], raw["statusCode"] = text, code
  88. self.assertIs(parse_reservation_detail(raw).state, ReservationState.UNKNOWN)
  89. def test_invalid_dates_and_times_fail_without_raw_values(self):
  90. for key, value in (("Data", "31 febbraio 2026"), ("Data", "10/13/2026"),
  91. ("Data", "13 SECRET 2026"), ("Ora", "10:05 - 11:05"),
  92. ("Ora", "24:00"), ("Ora", "10:25:00")):
  93. with self.subTest(key=key):
  94. raw = detail()
  95. raw["fields"][key] = value
  96. with self.assertRaises(ReservationReadError) as error:
  97. parse_reservation_detail(raw)
  98. self.assertNotIn(value, str(error.exception))
  99. def test_all_italian_months_parse_without_process_locale(self):
  100. from plugins.ita_reservation import MONTHS_IT
  101. for month, name in enumerate(MONTHS_IT, 1):
  102. raw = detail()
  103. raw["fields"]["Data"] = f"1 {name} 2027"
  104. self.assertEqual(parse_reservation_detail(raw).day, date(2027, month, 1))
  105. def test_missing_extra_empty_and_control_fields_are_rejected(self):
  106. for change in (lambda r: r["fields"].pop("Ora"),
  107. lambda r: r["fields"].update({"Email": "private@example.test"}),
  108. lambda r: r["fields"].update({"Stato": ""}),
  109. lambda r: r["fields"].update({"Ora": "10:25\n"}),
  110. lambda r: r.update(statusCode=3),
  111. lambda r: r.update(statusCode="SECRET"),
  112. lambda r: r.update(path="/UserArea")):
  113. raw = detail()
  114. change(raw)
  115. with self.assertRaises(ReservationReadError):
  116. parse_reservation_detail(raw)
  117. def test_listing_covered_with_structurally_empty_waitlist(self):
  118. result = parse_reservation_listing(listing())
  119. self.assertEqual(len(result.rows), 1)
  120. self.assertEqual(result.waitlist_count, 0)
  121. self.assertEqual(result.rows[0].appointment_time, "10:25")
  122. self.assertIs(result.rows[0].state, ReservationState.CANCELLED)
  123. self.assertNotIn("SYNTHETIC-REFERENCE", repr(result))
  124. self.assertNotIn("SYNTHETIC-REFERENCE", repr(result.rows[0]))
  125. self.assertFalse(hasattr(result, "complete")) # Not a bound Final snapshot.
  126. def test_loaded_multi_page_client_rows_are_supported_without_paging_actions(self):
  127. raw = listing()
  128. main = raw["tables"][0]
  129. main["count"] = 2
  130. main["rows"].append({**main["rows"][0], "reference": "SECOND", "detailUrl": "https://prenotami.esteri.it/ReservationDetail/Index/second"})
  131. main["info"].update(recordsTotal=2, recordsDisplay=2, pages=2, length=1)
  132. self.assertEqual(len(parse_reservation_listing(raw).rows), 2)
  133. def test_empty_initialized_tables_and_show_all_page(self):
  134. raw = listing()
  135. main = raw["tables"][0]
  136. main["info"]["length"] = -1
  137. self.assertEqual(parse_reservation_listing(raw).page_count, 1)
  138. main.update(count=0, rows=[])
  139. main["info"].update(recordsTotal=0, recordsDisplay=0, pages=0, start=0, end=0)
  140. raw["tables"][1] = {"id": "dataTableReservationsWL", "structuralEmpty": False,
  141. "count": 0, "info": copy.deepcopy(main["info"])}
  142. self.assertEqual(parse_reservation_listing(raw).rows, ())
  143. def test_coverage_and_pagination_changes_are_rejected(self):
  144. for changes in ({"serverSide": True}, {"serverSide": 0}, {"recordsTotal": 2},
  145. {"recordsDisplay": 0}, {"pages": 2}, {"page": True}, {"page": 1},
  146. {"start": 1}, {"end": 2}, {"length": 0}):
  147. with self.subTest(changes=changes):
  148. raw = listing()
  149. raw["tables"][0]["info"].update(changes)
  150. with self.assertRaises(ReservationReadError):
  151. parse_reservation_listing(raw)
  152. def test_bad_rows_headers_duplicate_links_and_missing_tables_rejected(self):
  153. for mode in ("missing_table", "wrong_order", "header", "missing_node", "count_bool", "duplicate_ref", "duplicate_link"):
  154. with self.subTest(mode=mode):
  155. raw = listing()
  156. main = raw["tables"][0]
  157. if mode == "missing_table":
  158. raw["tables"].pop()
  159. elif mode == "wrong_order":
  160. raw["tables"].reverse()
  161. elif mode == "header":
  162. main["headers"][7] = "Unexpected"
  163. elif mode == "missing_node":
  164. main["rows"] = []
  165. elif mode == "count_bool":
  166. main["count"] = True
  167. else:
  168. main["count"] = 2
  169. main["info"].update(recordsTotal=2, recordsDisplay=2, end=2)
  170. new = {**main["rows"][0]}
  171. new["detailUrl" if mode == "duplicate_ref" else "reference"] = (
  172. "https://prenotami.esteri.it/ReservationDetail/Index/second" if mode == "duplicate_ref" else "SECOND")
  173. main["rows"].append(new)
  174. with self.assertRaises(ReservationReadError):
  175. parse_reservation_listing(raw)
  176. def test_unsafe_detail_links_are_never_followed_or_accepted(self):
  177. for url in ("https://attacker.test/ReservationDetail/Index/x", "javascript:alert(1)",
  178. "https://prenotami.esteri.it/Reservation/Cancel/x", "https://prenotami.esteri.it/ReservationDetail/Index/x?token=SECRET",
  179. "https://prenotami.esteri.it/ReservationDetail/Index/../x", "https://prenotami.esteri.it/ReservationDetail/Index/%2fSECRET"):
  180. raw = listing()
  181. raw["tables"][0]["rows"][0]["detailUrl"] = url
  182. with self.assertRaises(ReservationReadError) as error:
  183. parse_reservation_listing(raw)
  184. self.assertNotIn("SECRET", str(error.exception))
  185. def test_nonempty_waitlist_remains_unsupported(self):
  186. raw = listing()
  187. raw["tables"][1] = {"id": "dataTableReservationsWL", "structuralEmpty": False,
  188. "count": 1, "info": copy.deepcopy(raw["tables"][0]["info"])}
  189. with self.assertRaisesRegex(ReservationReadError, "waitlist_schema_unverified"):
  190. parse_reservation_listing(raw)
  191. class BrowserReadTests(unittest.TestCase):
  192. def page(self, raw, *, detail_page=False):
  193. return SimpleNamespace(url="https://prenotami.esteri.it" + (detail()["path"] if detail_page else "/Reservation"),
  194. tab_id="synthetic-tab", run_js=Mock(return_value=raw))
  195. def test_detail_reads_current_page_once_without_navigation(self):
  196. page = self.page(detail(), detail_page=True)
  197. result = read_reservation_detail(page)
  198. self.assertIs(result.state, ReservationState.CANCELLED)
  199. page.run_js.assert_called_once_with(DETAIL_READ_SCRIPT)
  200. def test_list_requires_two_stable_reads(self):
  201. page = self.page(listing())
  202. with patch("plugins.ita_reservation.time.sleep"):
  203. self.assertEqual(len(read_reservation_listing(page).rows), 1)
  204. self.assertEqual(page.run_js.call_count, 2)
  205. self.assertTrue(all(call.args == (LIST_READ_SCRIPT,) for call in page.run_js.call_args_list))
  206. def test_not_ready_and_changed_records_reset_stable_read(self):
  207. original = listing()
  208. changed = listing()
  209. changed["tables"][0]["rows"][0]["time"] = "10:30"
  210. page = self.page(original)
  211. page.run_js.side_effect = [original, {"error": "not_ready"}, changed, changed]
  212. with patch("plugins.ita_reservation.time.sleep"):
  213. self.assertEqual(read_reservation_listing(page).rows[0].appointment_time, "10:30")
  214. self.assertEqual(page.run_js.call_count, 4)
  215. def test_wrong_origin_rejected_before_dom_read(self):
  216. page = self.page(detail(), detail_page=True)
  217. page.url = "https://attacker.test/ReservationDetail/Index/x"
  218. with self.assertRaises(ReservationReadError):
  219. read_reservation_detail(page)
  220. page.run_js.assert_not_called()
  221. def test_page_changes_during_read_rejected(self):
  222. page = self.page(detail(), detail_page=True)
  223. def change(_):
  224. page.tab_id = "another-tab"
  225. return detail()
  226. page.run_js.side_effect = change
  227. with self.assertRaisesRegex(ReservationReadError, "context_changed"):
  228. read_reservation_detail(page)
  229. def test_browser_error_message_from_page_is_not_echoed(self):
  230. page = self.page({"error": "PRIVATE-PAGE-MESSAGE"}, detail_page=True)
  231. with self.assertRaises(ReservationReadError) as error:
  232. read_reservation_detail(page)
  233. self.assertNotIn("PRIVATE", str(error.exception))
  234. def test_returned_path_must_match_current_detail(self):
  235. raw = detail()
  236. raw["path"] = "/ReservationDetail/Index/another"
  237. with self.assertRaises(ReservationReadError):
  238. read_reservation_detail(self.page(raw, detail_page=True))
  239. def test_tab_changes_between_stability_reads_rejected(self):
  240. page = self.page(listing())
  241. with patch("plugins.ita_reservation.time.sleep", side_effect=lambda _: setattr(page, "tab_id", "other")):
  242. with self.assertRaisesRegex(ReservationReadError, "context_changed"):
  243. read_reservation_listing(page)
  244. self.assertEqual(page.run_js.call_count, 1)
  245. def test_browser_errors_are_sanitized_without_exception_chain(self):
  246. page = self.page(detail(), detail_page=True)
  247. page.run_js.side_effect = RuntimeError("PRIVATE-TOKEN")
  248. with self.assertRaises(ReservationReadError) as caught:
  249. read_reservation_detail(page)
  250. self.assertNotIn("PRIVATE-TOKEN", "".join(traceback.format_exception(caught.exception)))
  251. def test_unready_read_is_bounded(self):
  252. page = self.page({"error": "not_ready"})
  253. with patch("plugins.ita_reservation.time.monotonic", side_effect=[0, 0, 0, 6]), patch("plugins.ita_reservation.time.sleep"):
  254. with self.assertRaisesRegex(ReservationReadError, "stability_timeout"):
  255. read_reservation_listing(page)
  256. self.assertEqual(page.run_js.call_count, 1)
  257. def test_invalid_wait_limits_fail_before_read(self):
  258. page = self.page(listing())
  259. for timeout, poll in ((True, .1), (0, .1), (61, .1), (float("inf"), .1), (1, 2)):
  260. with self.assertRaises(ReservationReadError):
  261. read_reservation_listing(page, timeout=timeout, poll_interval=poll)
  262. page.run_js.assert_not_called()
  263. def detail_html():
  264. rows = "".join(f'<div>\n<label>{name}:</label>\n{value}\n</div>'
  265. for name, value in detail()["fields"].items() if name != "Stato")
  266. return f'''<div class="app-menu"></div><div class="my-col-6">
  267. <h4>Dati Prenotazione</h4><div>{rows}
  268. <div>\n<label><b>* Stato:</b></label>\n<b>Annullata</b>\n</div></div></div>
  269. <div><h4>Dati Richiedente</h4><div data-private="true">PRIVATE-APPLICANT</div></div>
  270. <input type="hidden" id="IDTipoStatoPrenotazione" name="IDTipoStatoPrenotazione" value="3">
  271. <input type="hidden" name="DetailToPrint" value="PRIVATE-PRINT-PAYLOAD">
  272. <button id="btnStampa">Print</button>'''
  273. def list_html():
  274. heads = "".join(f"<th>{h}</th>" for h in [*HEADERS, ""])
  275. cells = '<td><a href="/ReservationDetail/Index/synthetic-link">SYNTHETIC-REFERENCE</a></td>'
  276. cells += ''.join(f'<td>{v}</td>' for v in ('Visti', 'Visti Nazionali e Schengen', '13 ottobre 2026', '10:25', 'Annullata'))
  277. cells += '<td data-private="true">PRIVATE-APPLICANT</td><td data-private="true">PRIVATE-ACTIONS</td>'
  278. return f'<div class="app-menu"></div><table id="dataTableReservations" class="dataTable"><thead><tr>{heads}</tr></thead><tbody><tr>{cells}</tr></tbody></table><table id="dataTableReservationsWL"></table>'
  279. def dom_tree(html):
  280. class FixtureParser(HTMLParser):
  281. def __init__(self):
  282. super().__init__()
  283. self.root = {"nodeType": 1, "tag": "document", "attrs": {}, "nodes": []}
  284. self.stack = [self.root]
  285. def handle_starttag(self, tag, attrs):
  286. attrs = dict(attrs)
  287. if "class" in attrs:
  288. attrs["class"] = attrs["class"].split()
  289. node = {"nodeType": 1, "tag": tag, "attrs": attrs, "nodes": []}
  290. self.stack[-1]["nodes"].append(node)
  291. if tag not in {"input", "br", "hr", "img", "meta", "link"}:
  292. self.stack.append(node)
  293. def handle_endtag(self, tag):
  294. assert self.stack[-1]["tag"] == tag, "invalid synthetic HTML fixture"
  295. self.stack.pop()
  296. def handle_data(self, data):
  297. self.stack[-1]["nodes"].append({"nodeType": 3, "text": data})
  298. def handle_comment(self, data):
  299. self.stack[-1]["nodes"].append({"nodeType": 8, "text": data})
  300. parser = FixtureParser()
  301. parser.feed(html)
  302. return parser.root
  303. JS_HARNESS = r"""
  304. const fs = require('fs'), vm = require('vm');
  305. const {script, tree, mode, detail} = JSON.parse(fs.readFileSync(0, 'utf8'));
  306. const location = {origin: mode === 'wrong_origin' ? 'https://attacker.test' : 'https://prenotami.esteri.it',
  307. pathname: detail ? '/ReservationDetail/Index/synthetic-link' : '/Reservation'};
  308. let apiGets = 0;
  309. class Node {
  310. constructor(data, parent=null) {
  311. this.nodeType=data.nodeType; this.tagName=(data.tag||'').toUpperCase(); this.attrs=data.attrs||{};
  312. this.own=data.text||''; this.parentElement=parent; this.childNodes=(data.nodes||[]).map(n=>new Node(n,this));
  313. this.classList={contains:s=>(this.attrs.class||[]).includes(s)};
  314. }
  315. get children(){return this.childNodes.filter(n=>n.nodeType===1);}
  316. get hidden(){return 'hidden' in this.attrs;}
  317. get style(){return Object.fromEntries((this.attrs.style||'').split(';').filter(Boolean)
  318. .map(s=>s.split(':').map(v=>v.trim())));}
  319. get textContent(){if(this.attrs['data-private']) throw Error('private cell read'); return this.own+this.childNodes.map(n=>n.textContent).join('');}
  320. get innerText(){if(this.attrs['data-private'])throw Error('private cell read');
  321. if((this.attrs.style||'').includes('display:none'))return '';
  322. return this.own+this.childNodes.map(n=>n.innerText).join('');}
  323. get value(){if(this.attrs.name==='DetailToPrint') throw Error('print payload read');return this.attrs.value||'';}
  324. get type(){return this.attrs.type||'';}
  325. get name(){return this.attrs.name||'';}
  326. get href(){return new URL(this.attrs.href,location.origin).href;}
  327. matches(selector){
  328. if(selector.includes(' ')){const [parent,child]=selector.split(' ');return this.matches(child)&&Boolean(this.parentElement?.closest(parent));}
  329. if(selector[0]==='.')return this.classList.contains(selector.slice(1));
  330. if(selector[0]==='#')return this.attrs.id===selector.slice(1);
  331. if(selector==='input#IDTipoStatoPrenotazione')return this.tagName==='INPUT'&&this.attrs.id==='IDTipoStatoPrenotazione';
  332. if(selector==='a[href]')return this.tagName==='A'&&'href' in this.attrs;
  333. return this.tagName.toLowerCase()===selector;
  334. }
  335. closest(s){return this.matches(s)?this:(this.parentElement?.closest(s)||null);}
  336. contains(n){return this===n||this.children.some(c=>c.contains(n));}
  337. querySelectorAll(s){return this.children.flatMap(c=>[...(c.matches(s)?[c]:[]),...c.querySelectorAll(s)]);}
  338. querySelector(s){return this.querySelectorAll(s)[0]||null;}
  339. }
  340. const document=new Node(tree);
  341. document.readyState=mode==='loading'?'loading':'complete';
  342. const isDataTable=t=>mode!=='uninitialized'&&t.attrs.id==='dataTableReservations';
  343. function $(t){return {DataTable(){
  344. apiGets++; if(!isDataTable(t))throw Error('initialized unexpectedly');
  345. const nodes=t.querySelectorAll('tbody tr'); const count=nodes.length;
  346. const info={page:0,pages:count?Math.ceil(count/10):0,start:0,end:Math.min(count,10),length:10,
  347. recordsTotal:count,recordsDisplay:count,serverSide:mode==='server_side'};
  348. if(mode==='partial')info.recordsTotal=count+1;
  349. return {page:{info:()=>info},search:()=>mode==='filter'?'PRIVATE-SEARCH':'',
  350. columns:()=>({search:()=>({toArray:()=>[mode==='column_filter'?'PRIVATE-SEARCH':'']})}),
  351. rows:()=>({count:()=>count,nodes:()=>({toArray:()=>mode==='deferred_nodes'?[]:nodes})})};
  352. }};}
  353. $.fn={dataTable:{isDataTable}};
  354. const window={location,jQuery:$,getComputedStyle:n=>({
  355. display:mode==='detached'?'':(n.style.display||'inline'),
  356. visibility:mode==='detached'?'':(n.style.visibility||'visible')})};
  357. const raw=vm.runInNewContext('(function(){'+script+'})()', {window,document}, {timeout:1000});
  358. process.stdout.write(JSON.stringify({raw,apiGets}));
  359. """
  360. @unittest.skipUnless(shutil.which("node"), "node required for actual readonly JS execution")
  361. class ActualJavaScriptTests(unittest.TestCase):
  362. def run_js(self, *, detail_page=False, mode="normal", html=None, script=None):
  363. proc = subprocess.run([shutil.which("node"), "-e", JS_HARNESS],
  364. input=json.dumps({"script": script or (DETAIL_READ_SCRIPT if detail_page else LIST_READ_SCRIPT),
  365. "tree": dom_tree(html or (detail_html() if detail_page else list_html())),
  366. "mode": mode, "detail": detail_page}),
  367. text=True, capture_output=True, timeout=10)
  368. self.assertEqual(proc.returncode, 0, proc.stderr)
  369. self.assertNotIn("PRIVATE-", proc.stdout)
  370. return json.loads(proc.stdout)
  371. def test_date_diagnostic_distinguishes_hidden_metadata_and_node_reader_excludes_it(self):
  372. # Hidden-element + text shape is observed; tag and contents are synthetic.
  373. html = list_html().replace('<td>13 ottobre 2026</td>',
  374. '<td><span style="display:none">20261013</span>13 ottobre 2026</td>')
  375. raw = self.run_js(html=html, script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
  376. page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
  377. run_js=Mock(return_value=raw))
  378. diagnostic = read_reservation_date_diagnostic(page)
  379. self.assertTrue(diagnostic["available"])
  380. item = diagnostic["samples"][0]
  381. self.assertFalse(item["textEqualsRendered"])
  382. self.assertFalse(item["text"]["acceptedByCurrentParser"])
  383. self.assertTrue(item["rendered"]["acceptedByCurrentParser"])
  384. self.assertEqual(item["text"]["italianMonths"], [10])
  385. self.assertTrue(item["parts"][0]["hidden"])
  386. self.assertNotIn("20261013", json.dumps(diagnostic))
  387. self.assertNotIn("13 ottobre 2026", json.dumps(diagnostic))
  388. parsed = parse_reservation_listing(self.run_js(html=html)["raw"])
  389. self.assertEqual(parsed.rows[0].day, date(2026, 10, 13))
  390. # Python's strict date grammar has NOT been loosened to extract substrings.
  391. mixed = listing()
  392. mixed["tables"][0]["rows"][0]["date"] = "2026101313 ottobre 2026"
  393. with self.assertRaisesRegex(ReservationReadError, "date_unrecognized"):
  394. parse_reservation_listing(mixed)
  395. def test_observed_hidden_other_element_then_text_shape_parses(self):
  396. html = list_html().replace('<td>13 ottobre 2026</td>',
  397. '<td><label style="display:none">20261013 09:35</label>13 ottobre 2026</td>')
  398. raw = self.run_js(html=html, script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
  399. page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
  400. run_js=Mock(return_value=raw))
  401. sample = read_reservation_date_diagnostic(page)["samples"][0]
  402. self.assertEqual(sample["parts"][0]["kind"], "other")
  403. self.assertEqual(sample["parts"][0]["shape"], "DDDDDDDD DD:DD")
  404. self.assertEqual(sample["text"]["shape"], "DDDDDDDD DD:DDDD LLLLLLL DDDD")
  405. result = parse_reservation_listing(self.run_js(html=html)["raw"])
  406. self.assertEqual(result.rows[0].day, date(2026, 10, 13))
  407. def test_hidden_date_metadata_is_not_read_or_assumed_to_be_a_sort_key(self):
  408. for marker in ('style="display: none"', 'hidden', 'style="visibility: hidden"'):
  409. with self.subTest(marker=marker):
  410. html = list_html().replace('<td>13 ottobre 2026</td>',
  411. f'<td><label {marker} data-private="true">PRIVATE-METADATA</label>13 ottobre 2026</td>')
  412. self.assertEqual(parse_reservation_listing(self.run_js(html=html)["raw"]).rows[0].day,
  413. date(2026, 10, 13))
  414. def test_unrendered_client_rows_use_text_nodes_not_cell_inner_text(self):
  415. html = list_html().replace('<td>13 ottobre 2026</td>',
  416. '<td data-private="true"><label style="display:none">20261013 09:35</label>13 ottobre 2026</td>')
  417. # Reading the parent td textContent/innerText would throw. Detached style
  418. # resolution is empty, but the child's own inline marker still exists.
  419. output = self.run_js(html=html, mode="detached")
  420. self.assertEqual(parse_reservation_listing(output["raw"]).rows[0].day, date(2026, 10, 13))
  421. def test_visible_elements_and_ambiguous_text_nodes_are_not_silently_dropped(self):
  422. for value in ('<label>20261013 09:35</label>13 ottobre 2026',
  423. '<span>13 ottobre 2026</span>',
  424. '13 ottobre 2026<!-- split -->14 ottobre 2026',
  425. '<label style="display:none">13 ottobre 2026</label>',
  426. '<label style="visibility:hidden"><span style="visibility:visible">Other</span></label>13 ottobre 2026',
  427. '<!-- comment -->' * 17 + '13 ottobre 2026'):
  428. with self.subTest(value=value):
  429. html = list_html().replace('<td>13 ottobre 2026</td>', f'<td>{value}</td>')
  430. self.assertEqual(self.run_js(html=html)["raw"], {"error": "date_cell_structure_changed"})
  431. def test_invalid_dates_and_visible_mixed_metadata_still_fail_strictly(self):
  432. for value in ('31 febbraio 2026', '20261013 09:3513 ottobre 2026', '2026-10-13'):
  433. with self.subTest(value=value):
  434. html = list_html().replace('<td>13 ottobre 2026</td>',
  435. f'<td><label style="display:none">SORT</label>{value}</td>')
  436. with self.assertRaisesRegex(ReservationReadError, "date_unrecognized"):
  437. parse_reservation_listing(self.run_js(html=html)["raw"])
  438. def test_hidden_metadata_is_removed_on_all_loaded_client_pages(self):
  439. html = list_html().replace('<td>13 ottobre 2026</td>',
  440. '<td><label style="display:none">20261013 09:35</label>13 ottobre 2026</td>')
  441. start, end = html.index('<tbody>') + len('<tbody>'), html.index('</tbody>')
  442. row = html[start:end]
  443. extra = ''.join(row.replace('SYNTHETIC-REFERENCE', f'REF-{i}').replace('synthetic-link', f'link-{i}')
  444. for i in range(10))
  445. html = html[:end] + extra + html[end:]
  446. result = parse_reservation_listing(self.run_js(html=html, mode="detached")["raw"])
  447. self.assertEqual(len(result.rows), 11)
  448. self.assertEqual(result.page_count, 2)
  449. self.assertTrue(all(r.day == date(2026, 10, 13) for r in result.rows))
  450. def test_date_diagnostic_does_not_initialize_or_read_wrong_headers(self):
  451. for mode in ("wrong_origin", "uninitialized", "loading"):
  452. output = self.run_js(mode=mode, script=DATE_DIAGNOSTIC_SCRIPT)
  453. self.assertIn("error", output["raw"])
  454. self.assertEqual(output["apiGets"], 0)
  455. output = self.run_js(html=list_html().replace('<th>Data</th>', '<th>Other</th>'),
  456. script=DATE_DIAGNOSTIC_SCRIPT)
  457. self.assertIn("error", output["raw"])
  458. self.assertEqual(output["apiGets"], 0)
  459. def test_date_diagnostic_masks_unknown_text_and_rejects_extra_payload(self):
  460. raw = self.run_js(script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
  461. raw["samples"][0]["text"] = "SECRET-USER@example.invalid"
  462. page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
  463. run_js=Mock(return_value=raw))
  464. diagnostic = read_reservation_date_diagnostic(page)
  465. self.assertTrue(diagnostic["available"])
  466. self.assertNotIn("SECRET", json.dumps(diagnostic))
  467. raw["samples"][0]["parts"][0]["kind"] = "SECRET-USER"
  468. self.assertEqual(read_reservation_date_diagnostic(page), {"available": False})
  469. def test_date_diagnostic_limits_output_and_context(self):
  470. raw = self.run_js(script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
  471. raw["samples"] *= 4
  472. page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
  473. run_js=Mock(return_value=raw))
  474. self.assertEqual(read_reservation_date_diagnostic(page), {"available": False})
  475. page.url = "https://prenotami.esteri.it/UserArea"
  476. page.run_js.reset_mock()
  477. self.assertEqual(read_reservation_date_diagnostic(page), {"available": False})
  478. page.run_js.assert_not_called()
  479. def test_actual_detail_script_parses_observed_text_siblings_and_b(self):
  480. output = self.run_js(detail_page=True)
  481. self.assertEqual(output["raw"], detail())
  482. self.assertIs(parse_reservation_detail(output["raw"]).state, ReservationState.CANCELLED)
  483. self.assertEqual(output["apiGets"], 0)
  484. def test_blank_text_nodes_and_comments_do_not_define_field_positions(self):
  485. html = detail_html().replace('<label>', '\n<!-- spacing -->\n<label>').replace('</label>', '</label>\n ')
  486. self.assertEqual(self.run_js(detail_page=True, html=html)["raw"], detail())
  487. def test_unsafe_detail_shapes_fail_closed(self):
  488. cases = {
  489. "missing": detail_html().replace('<label>Ora:</label>', '<label>Other:</label>'),
  490. "duplicate": detail_html().replace('<label>Ora:</label>', '<label>Ora:</label><label>Ora:</label>'),
  491. "wrapped": detail_html().replace('10:25', '<span>10:25</span>'),
  492. "code_duplicate": detail_html() + '<input id="IDTipoStatoPrenotazione" type="hidden">',
  493. "section_duplicate": detail_html() + '<h4>Dati Prenotazione</h4>',
  494. "no_auth": detail_html().replace('app-menu', 'no-menu'),
  495. "inline_conflict": detail_html().replace('<label>Ora:</label>', '<label>Ora: 14:00</label>'),
  496. "extra_value": detail_html().replace('10:25', '10:25<b>14:00</b>'),
  497. }
  498. for mode, html in cases.items():
  499. with self.subTest(mode=mode):
  500. self.assertIn("error", self.run_js(detail_page=True, html=html)["raw"])
  501. def test_actual_list_script_returns_no_applicant_or_unnamed_cell_values(self):
  502. output = self.run_js()
  503. parsed = parse_reservation_listing(output["raw"])
  504. self.assertEqual(parsed.rows[0].reference, "SYNTHETIC-REFERENCE")
  505. self.assertEqual(output["apiGets"], 1)
  506. self.assertEqual(parsed.waitlist_count, 0)
  507. def test_reading_metadata_never_initializes_tables(self):
  508. output = self.run_js(mode="uninitialized")
  509. self.assertEqual(output["raw"], {"error": "not_ready"})
  510. self.assertEqual(output["apiGets"], 0)
  511. def test_duplicate_table_or_missing_link_is_rejected(self):
  512. for html in (list_html() + '<table id="dataTableReservations"></table>',
  513. list_html().replace('href="/ReservationDetail/Index/synthetic-link"', 'data-no-link="true"')):
  514. self.assertIn("error", self.run_js(html=html)["raw"])
  515. def test_all_loaded_client_pages_are_read_without_page_draw(self):
  516. html = list_html()
  517. start, end = html.index('<tbody>') + len('<tbody>'), html.index('</tbody>')
  518. row = html[start:end]
  519. extra = ''.join(row.replace('SYNTHETIC-REFERENCE', f'REF-{i}').replace('synthetic-link', f'link-{i}') for i in range(10))
  520. html = html[:end] + extra + html[end:]
  521. result = parse_reservation_listing(self.run_js(html=html)["raw"])
  522. self.assertEqual(len(result.rows), 11)
  523. self.assertEqual(result.page_count, 2)
  524. def test_filters_server_paging_partial_rows_and_loading_fail_closed(self):
  525. for mode in ("server_side", "partial", "filter", "column_filter", "deferred_nodes", "loading", "wrong_origin"):
  526. with self.subTest(mode=mode):
  527. self.assertIn("error", self.run_js(mode=mode)["raw"])
  528. def test_waitlist_with_initialization_traces_not_counted_as_empty(self):
  529. for html in (list_html().replace('id="dataTableReservationsWL"', 'id="dataTableReservationsWL" class="dataTable"'),
  530. list_html().replace('<table id="dataTableReservationsWL"></table>', '<div class="dataTables_wrapper"><table id="dataTableReservationsWL"></table></div>')):
  531. self.assertEqual(self.run_js(html=html)["raw"], {"error": "not_ready"})
  532. if __name__ == "__main__":
  533. unittest.main()