| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- """Synthetic fixtures reproducing observed markup; never calls the website."""
- import copy
- import json
- import shutil
- import subprocess
- import traceback
- import unittest
- from datetime import date
- from html.parser import HTMLParser
- from types import SimpleNamespace
- from unittest.mock import Mock, patch
- from plugins.ita_booking_final import ReservationState
- from plugins.ita_reservation import (
- DETAIL_READ_SCRIPT, LIST_READ_SCRIPT, DATE_DIAGNOSTIC_SCRIPT, HEADERS, ReservationReadError,
- parse_reservation_detail, parse_reservation_listing,
- read_reservation_detail, read_reservation_listing, read_reservation_date_diagnostic,
- )
- def detail():
- return {"path": "/ReservationDetail/Index/synthetic-link", "statusCode": "3", "fields": {
- "Servizio Consolare": "Visti", "Servizio Erogato": "Visti Nazionali e Schengen",
- "Codice Prenotazione": "SYNTHETIC-REFERENCE", "Tipo Prenotazione": "Prenotazione Singola",
- "Data": "13 ottobre 2026", "Ora": "10:25", "Stato": "Annullata",
- }}
- def listing():
- return {"path": "/Reservation", "tables": [
- {"id": "dataTableReservations", "structuralEmpty": False, "headers": [*HEADERS, ""], "count": 1,
- "info": {"page": 0, "pages": 1, "start": 0, "end": 1, "length": 10,
- "recordsTotal": 1, "recordsDisplay": 1, "serverSide": False},
- "rows": [{"reference": "SYNTHETIC-REFERENCE", "detailUrl": "https://prenotami.esteri.it/ReservationDetail/Index/synthetic-link",
- "office": "Visti", "service": "Visti Nazionali e Schengen", "date": "13 ottobre 2026",
- "time": "10:25", "status": "Annullata"}]},
- {"id": "dataTableReservationsWL", "structuralEmpty": True},
- ]}
- def confirmed_detail():
- """Observed 2026-08-31 business shape; identifiers remain synthetic."""
- raw = detail()
- raw.update(path="/ReservationDetail/Index/confirmed-synthetic", statusCode="2")
- raw["fields"].update({"Codice Prenotazione": "CONFIRMED-SYNTHETIC", "Data": "3 settembre 2026",
- "Ora": "09:26", "Stato": "Confermata"})
- return raw
- def confirmed_listing():
- raw = listing()
- table = raw["tables"][0]
- table["rows"].append({**table["rows"][0], "reference": "CONFIRMED-SYNTHETIC",
- "detailUrl": "https://prenotami.esteri.it/ReservationDetail/Index/confirmed-synthetic",
- "date": "3 settembre 2026", "time": "09:26", "status": "Confermata"})
- table["count"] = 2
- table["info"].update(recordsTotal=2, recordsDisplay=2, end=2)
- return raw
- class ReceiptParsingTests(unittest.TestCase):
- def test_observed_confirmed_pair_maps_active_without_inventing_slot_binding(self):
- parsed = parse_reservation_detail(confirmed_detail())
- self.assertIs(parsed.state, ReservationState.ACTIVE)
- self.assertEqual(parsed.day, date(2026, 9, 3))
- self.assertEqual(parsed.appointment_time, "09:26")
- self.assertFalse(hasattr(parsed, "service_id"))
- self.assertFalse(hasattr(parsed, "time_label"))
- def test_confirmed_label_with_wrong_code_or_code_without_label_stays_unknown(self):
- for label, code in (("Confermata", "3"), ("Confermata", "1"), ("Confermata", "02"),
- ("Annullata", "2"), ("Arbitrary", "2"), ("Confirmed", "2")):
- with self.subTest(label=label, code=code):
- raw = confirmed_detail()
- raw["fields"]["Stato"], raw["statusCode"] = label, code
- self.assertIs(parse_reservation_detail(raw).state, ReservationState.UNKNOWN)
- def test_cancelled_history_and_confirmed_list_candidate_remain_distinct(self):
- parsed = parse_reservation_listing(confirmed_listing())
- self.assertEqual([row.state for row in parsed.rows],
- [ReservationState.CANCELLED, ReservationState.ACTIVE])
- self.assertNotIn("CONFIRMED-SYNTHETIC", repr(parsed))
- def test_confirmed_mapping_is_not_hong_kong_or_date_specific(self):
- raw = confirmed_detail()
- raw["fields"].update({"Data": "12 febbraio 2027", "Servizio Erogato": "Different service",
- "Ora": "14:30"})
- self.assertIs(parse_reservation_detail(raw).state, ReservationState.ACTIVE)
- def test_cancelled_detail_has_assigned_time_not_invented_interval_or_service_id(self):
- result = parse_reservation_detail(detail())
- self.assertEqual(result.day, date(2026, 10, 13))
- self.assertEqual(result.appointment_time, "10:25")
- self.assertIs(result.state, ReservationState.CANCELLED)
- self.assertFalse(hasattr(result, "time_label"))
- self.assertFalse(hasattr(result, "service_id"))
- self.assertNotIn("SYNTHETIC-REFERENCE", repr(result))
- def test_unknown_and_conflicting_states_never_become_active(self):
- for text, code in (("Annullata", "2"), ("Confirmed", "3"), ("Confermata", "1"), ("Unknown", "99")):
- with self.subTest(code=code):
- raw = detail()
- raw["fields"]["Stato"], raw["statusCode"] = text, code
- self.assertIs(parse_reservation_detail(raw).state, ReservationState.UNKNOWN)
- def test_invalid_dates_and_times_fail_without_raw_values(self):
- for key, value in (("Data", "31 febbraio 2026"), ("Data", "10/13/2026"),
- ("Data", "13 SECRET 2026"), ("Ora", "10:05 - 11:05"),
- ("Ora", "24:00"), ("Ora", "10:25:00")):
- with self.subTest(key=key):
- raw = detail()
- raw["fields"][key] = value
- with self.assertRaises(ReservationReadError) as error:
- parse_reservation_detail(raw)
- self.assertNotIn(value, str(error.exception))
- def test_all_italian_months_parse_without_process_locale(self):
- from plugins.ita_reservation import MONTHS_IT
- for month, name in enumerate(MONTHS_IT, 1):
- raw = detail()
- raw["fields"]["Data"] = f"1 {name} 2027"
- self.assertEqual(parse_reservation_detail(raw).day, date(2027, month, 1))
- def test_missing_extra_empty_and_control_fields_are_rejected(self):
- for change in (lambda r: r["fields"].pop("Ora"),
- lambda r: r["fields"].update({"Email": "private@example.test"}),
- lambda r: r["fields"].update({"Stato": ""}),
- lambda r: r["fields"].update({"Ora": "10:25\n"}),
- lambda r: r.update(statusCode=3),
- lambda r: r.update(statusCode="SECRET"),
- lambda r: r.update(path="/UserArea")):
- raw = detail()
- change(raw)
- with self.assertRaises(ReservationReadError):
- parse_reservation_detail(raw)
- def test_listing_covered_with_structurally_empty_waitlist(self):
- result = parse_reservation_listing(listing())
- self.assertEqual(len(result.rows), 1)
- self.assertEqual(result.waitlist_count, 0)
- self.assertEqual(result.rows[0].appointment_time, "10:25")
- self.assertIs(result.rows[0].state, ReservationState.CANCELLED)
- self.assertNotIn("SYNTHETIC-REFERENCE", repr(result))
- self.assertNotIn("SYNTHETIC-REFERENCE", repr(result.rows[0]))
- self.assertFalse(hasattr(result, "complete")) # Not a bound Final snapshot.
- def test_loaded_multi_page_client_rows_are_supported_without_paging_actions(self):
- raw = listing()
- main = raw["tables"][0]
- main["count"] = 2
- main["rows"].append({**main["rows"][0], "reference": "SECOND", "detailUrl": "https://prenotami.esteri.it/ReservationDetail/Index/second"})
- main["info"].update(recordsTotal=2, recordsDisplay=2, pages=2, length=1)
- self.assertEqual(len(parse_reservation_listing(raw).rows), 2)
- def test_empty_initialized_tables_and_show_all_page(self):
- raw = listing()
- main = raw["tables"][0]
- main["info"]["length"] = -1
- self.assertEqual(parse_reservation_listing(raw).page_count, 1)
- main.update(count=0, rows=[])
- main["info"].update(recordsTotal=0, recordsDisplay=0, pages=0, start=0, end=0)
- raw["tables"][1] = {"id": "dataTableReservationsWL", "structuralEmpty": False,
- "count": 0, "info": copy.deepcopy(main["info"])}
- self.assertEqual(parse_reservation_listing(raw).rows, ())
- def test_coverage_and_pagination_changes_are_rejected(self):
- for changes in ({"serverSide": True}, {"serverSide": 0}, {"recordsTotal": 2},
- {"recordsDisplay": 0}, {"pages": 2}, {"page": True}, {"page": 1},
- {"start": 1}, {"end": 2}, {"length": 0}):
- with self.subTest(changes=changes):
- raw = listing()
- raw["tables"][0]["info"].update(changes)
- with self.assertRaises(ReservationReadError):
- parse_reservation_listing(raw)
- def test_bad_rows_headers_duplicate_links_and_missing_tables_rejected(self):
- for mode in ("missing_table", "wrong_order", "header", "missing_node", "count_bool", "duplicate_ref", "duplicate_link"):
- with self.subTest(mode=mode):
- raw = listing()
- main = raw["tables"][0]
- if mode == "missing_table":
- raw["tables"].pop()
- elif mode == "wrong_order":
- raw["tables"].reverse()
- elif mode == "header":
- main["headers"][7] = "Unexpected"
- elif mode == "missing_node":
- main["rows"] = []
- elif mode == "count_bool":
- main["count"] = True
- else:
- main["count"] = 2
- main["info"].update(recordsTotal=2, recordsDisplay=2, end=2)
- new = {**main["rows"][0]}
- new["detailUrl" if mode == "duplicate_ref" else "reference"] = (
- "https://prenotami.esteri.it/ReservationDetail/Index/second" if mode == "duplicate_ref" else "SECOND")
- main["rows"].append(new)
- with self.assertRaises(ReservationReadError):
- parse_reservation_listing(raw)
- def test_unsafe_detail_links_are_never_followed_or_accepted(self):
- for url in ("https://attacker.test/ReservationDetail/Index/x", "javascript:alert(1)",
- "https://prenotami.esteri.it/Reservation/Cancel/x", "https://prenotami.esteri.it/ReservationDetail/Index/x?token=SECRET",
- "https://prenotami.esteri.it/ReservationDetail/Index/../x", "https://prenotami.esteri.it/ReservationDetail/Index/%2fSECRET"):
- raw = listing()
- raw["tables"][0]["rows"][0]["detailUrl"] = url
- with self.assertRaises(ReservationReadError) as error:
- parse_reservation_listing(raw)
- self.assertNotIn("SECRET", str(error.exception))
- def test_nonempty_waitlist_remains_unsupported(self):
- raw = listing()
- raw["tables"][1] = {"id": "dataTableReservationsWL", "structuralEmpty": False,
- "count": 1, "info": copy.deepcopy(raw["tables"][0]["info"])}
- with self.assertRaisesRegex(ReservationReadError, "waitlist_schema_unverified"):
- parse_reservation_listing(raw)
- class BrowserReadTests(unittest.TestCase):
- def page(self, raw, *, detail_page=False):
- return SimpleNamespace(url="https://prenotami.esteri.it" + (detail()["path"] if detail_page else "/Reservation"),
- tab_id="synthetic-tab", run_js=Mock(return_value=raw))
- def test_detail_reads_current_page_once_without_navigation(self):
- page = self.page(detail(), detail_page=True)
- result = read_reservation_detail(page)
- self.assertIs(result.state, ReservationState.CANCELLED)
- page.run_js.assert_called_once_with(DETAIL_READ_SCRIPT)
- def test_list_requires_two_stable_reads(self):
- page = self.page(listing())
- with patch("plugins.ita_reservation.time.sleep"):
- self.assertEqual(len(read_reservation_listing(page).rows), 1)
- self.assertEqual(page.run_js.call_count, 2)
- self.assertTrue(all(call.args == (LIST_READ_SCRIPT,) for call in page.run_js.call_args_list))
- def test_not_ready_and_changed_records_reset_stable_read(self):
- original = listing()
- changed = listing()
- changed["tables"][0]["rows"][0]["time"] = "10:30"
- page = self.page(original)
- page.run_js.side_effect = [original, {"error": "not_ready"}, changed, changed]
- with patch("plugins.ita_reservation.time.sleep"):
- self.assertEqual(read_reservation_listing(page).rows[0].appointment_time, "10:30")
- self.assertEqual(page.run_js.call_count, 4)
- def test_wrong_origin_rejected_before_dom_read(self):
- page = self.page(detail(), detail_page=True)
- page.url = "https://attacker.test/ReservationDetail/Index/x"
- with self.assertRaises(ReservationReadError):
- read_reservation_detail(page)
- page.run_js.assert_not_called()
- def test_page_changes_during_read_rejected(self):
- page = self.page(detail(), detail_page=True)
- def change(_):
- page.tab_id = "another-tab"
- return detail()
- page.run_js.side_effect = change
- with self.assertRaisesRegex(ReservationReadError, "context_changed"):
- read_reservation_detail(page)
- def test_browser_error_message_from_page_is_not_echoed(self):
- page = self.page({"error": "PRIVATE-PAGE-MESSAGE"}, detail_page=True)
- with self.assertRaises(ReservationReadError) as error:
- read_reservation_detail(page)
- self.assertNotIn("PRIVATE", str(error.exception))
- def test_returned_path_must_match_current_detail(self):
- raw = detail()
- raw["path"] = "/ReservationDetail/Index/another"
- with self.assertRaises(ReservationReadError):
- read_reservation_detail(self.page(raw, detail_page=True))
- def test_tab_changes_between_stability_reads_rejected(self):
- page = self.page(listing())
- with patch("plugins.ita_reservation.time.sleep", side_effect=lambda _: setattr(page, "tab_id", "other")):
- with self.assertRaisesRegex(ReservationReadError, "context_changed"):
- read_reservation_listing(page)
- self.assertEqual(page.run_js.call_count, 1)
- def test_browser_errors_are_sanitized_without_exception_chain(self):
- page = self.page(detail(), detail_page=True)
- page.run_js.side_effect = RuntimeError("PRIVATE-TOKEN")
- with self.assertRaises(ReservationReadError) as caught:
- read_reservation_detail(page)
- self.assertNotIn("PRIVATE-TOKEN", "".join(traceback.format_exception(caught.exception)))
- def test_unready_read_is_bounded(self):
- page = self.page({"error": "not_ready"})
- with patch("plugins.ita_reservation.time.monotonic", side_effect=[0, 0, 0, 6]), patch("plugins.ita_reservation.time.sleep"):
- with self.assertRaisesRegex(ReservationReadError, "stability_timeout"):
- read_reservation_listing(page)
- self.assertEqual(page.run_js.call_count, 1)
- def test_invalid_wait_limits_fail_before_read(self):
- page = self.page(listing())
- for timeout, poll in ((True, .1), (0, .1), (61, .1), (float("inf"), .1), (1, 2)):
- with self.assertRaises(ReservationReadError):
- read_reservation_listing(page, timeout=timeout, poll_interval=poll)
- page.run_js.assert_not_called()
- def detail_html():
- rows = "".join(f'<div>\n<label>{name}:</label>\n{value}\n</div>'
- for name, value in detail()["fields"].items() if name != "Stato")
- return f'''<div class="app-menu"></div><div class="my-col-6">
- <h4>Dati Prenotazione</h4><div>{rows}
- <div>\n<label><b>* Stato:</b></label>\n<b>Annullata</b>\n</div></div></div>
- <div><h4>Dati Richiedente</h4><div data-private="true">PRIVATE-APPLICANT</div></div>
- <input type="hidden" id="IDTipoStatoPrenotazione" name="IDTipoStatoPrenotazione" value="3">
- <input type="hidden" name="DetailToPrint" value="PRIVATE-PRINT-PAYLOAD">
- <button id="btnStampa">Print</button>'''
- def list_html():
- heads = "".join(f"<th>{h}</th>" for h in [*HEADERS, ""])
- cells = '<td><a href="/ReservationDetail/Index/synthetic-link">SYNTHETIC-REFERENCE</a></td>'
- cells += ''.join(f'<td>{v}</td>' for v in ('Visti', 'Visti Nazionali e Schengen', '13 ottobre 2026', '10:25', 'Annullata'))
- cells += '<td data-private="true">PRIVATE-APPLICANT</td><td data-private="true">PRIVATE-ACTIONS</td>'
- 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>'
- def dom_tree(html):
- class FixtureParser(HTMLParser):
- def __init__(self):
- super().__init__()
- self.root = {"nodeType": 1, "tag": "document", "attrs": {}, "nodes": []}
- self.stack = [self.root]
- def handle_starttag(self, tag, attrs):
- attrs = dict(attrs)
- if "class" in attrs:
- attrs["class"] = attrs["class"].split()
- node = {"nodeType": 1, "tag": tag, "attrs": attrs, "nodes": []}
- self.stack[-1]["nodes"].append(node)
- if tag not in {"input", "br", "hr", "img", "meta", "link"}:
- self.stack.append(node)
- def handle_endtag(self, tag):
- assert self.stack[-1]["tag"] == tag, "invalid synthetic HTML fixture"
- self.stack.pop()
- def handle_data(self, data):
- self.stack[-1]["nodes"].append({"nodeType": 3, "text": data})
- def handle_comment(self, data):
- self.stack[-1]["nodes"].append({"nodeType": 8, "text": data})
- parser = FixtureParser()
- parser.feed(html)
- return parser.root
- JS_HARNESS = r"""
- const fs = require('fs'), vm = require('vm');
- const {script, tree, mode, detail} = JSON.parse(fs.readFileSync(0, 'utf8'));
- const location = {origin: mode === 'wrong_origin' ? 'https://attacker.test' : 'https://prenotami.esteri.it',
- pathname: detail ? '/ReservationDetail/Index/synthetic-link' : '/Reservation'};
- let apiGets = 0;
- class Node {
- constructor(data, parent=null) {
- this.nodeType=data.nodeType; this.tagName=(data.tag||'').toUpperCase(); this.attrs=data.attrs||{};
- this.own=data.text||''; this.parentElement=parent; this.childNodes=(data.nodes||[]).map(n=>new Node(n,this));
- this.classList={contains:s=>(this.attrs.class||[]).includes(s)};
- }
- get children(){return this.childNodes.filter(n=>n.nodeType===1);}
- get hidden(){return 'hidden' in this.attrs;}
- get style(){return Object.fromEntries((this.attrs.style||'').split(';').filter(Boolean)
- .map(s=>s.split(':').map(v=>v.trim())));}
- get textContent(){if(this.attrs['data-private']) throw Error('private cell read'); return this.own+this.childNodes.map(n=>n.textContent).join('');}
- get innerText(){if(this.attrs['data-private'])throw Error('private cell read');
- if((this.attrs.style||'').includes('display:none'))return '';
- return this.own+this.childNodes.map(n=>n.innerText).join('');}
- get value(){if(this.attrs.name==='DetailToPrint') throw Error('print payload read');return this.attrs.value||'';}
- get type(){return this.attrs.type||'';}
- get name(){return this.attrs.name||'';}
- get href(){return new URL(this.attrs.href,location.origin).href;}
- matches(selector){
- if(selector.includes(' ')){const [parent,child]=selector.split(' ');return this.matches(child)&&Boolean(this.parentElement?.closest(parent));}
- if(selector[0]==='.')return this.classList.contains(selector.slice(1));
- if(selector[0]==='#')return this.attrs.id===selector.slice(1);
- if(selector==='input#IDTipoStatoPrenotazione')return this.tagName==='INPUT'&&this.attrs.id==='IDTipoStatoPrenotazione';
- if(selector==='a[href]')return this.tagName==='A'&&'href' in this.attrs;
- return this.tagName.toLowerCase()===selector;
- }
- closest(s){return this.matches(s)?this:(this.parentElement?.closest(s)||null);}
- contains(n){return this===n||this.children.some(c=>c.contains(n));}
- querySelectorAll(s){return this.children.flatMap(c=>[...(c.matches(s)?[c]:[]),...c.querySelectorAll(s)]);}
- querySelector(s){return this.querySelectorAll(s)[0]||null;}
- }
- const document=new Node(tree);
- document.readyState=mode==='loading'?'loading':'complete';
- const isDataTable=t=>mode!=='uninitialized'&&t.attrs.id==='dataTableReservations';
- function $(t){return {DataTable(){
- apiGets++; if(!isDataTable(t))throw Error('initialized unexpectedly');
- const nodes=t.querySelectorAll('tbody tr'); const count=nodes.length;
- const info={page:0,pages:count?Math.ceil(count/10):0,start:0,end:Math.min(count,10),length:10,
- recordsTotal:count,recordsDisplay:count,serverSide:mode==='server_side'};
- if(mode==='partial')info.recordsTotal=count+1;
- return {page:{info:()=>info},search:()=>mode==='filter'?'PRIVATE-SEARCH':'',
- columns:()=>({search:()=>({toArray:()=>[mode==='column_filter'?'PRIVATE-SEARCH':'']})}),
- rows:()=>({count:()=>count,nodes:()=>({toArray:()=>mode==='deferred_nodes'?[]:nodes})})};
- }};}
- $.fn={dataTable:{isDataTable}};
- const window={location,jQuery:$,getComputedStyle:n=>({
- display:mode==='detached'?'':(n.style.display||'inline'),
- visibility:mode==='detached'?'':(n.style.visibility||'visible')})};
- const raw=vm.runInNewContext('(function(){'+script+'})()', {window,document}, {timeout:1000});
- process.stdout.write(JSON.stringify({raw,apiGets}));
- """
- @unittest.skipUnless(shutil.which("node"), "node required for actual readonly JS execution")
- class ActualJavaScriptTests(unittest.TestCase):
- def run_js(self, *, detail_page=False, mode="normal", html=None, script=None):
- proc = subprocess.run([shutil.which("node"), "-e", JS_HARNESS],
- input=json.dumps({"script": script or (DETAIL_READ_SCRIPT if detail_page else LIST_READ_SCRIPT),
- "tree": dom_tree(html or (detail_html() if detail_page else list_html())),
- "mode": mode, "detail": detail_page}),
- text=True, capture_output=True, timeout=10)
- self.assertEqual(proc.returncode, 0, proc.stderr)
- self.assertNotIn("PRIVATE-", proc.stdout)
- return json.loads(proc.stdout)
- def test_date_diagnostic_distinguishes_hidden_metadata_and_node_reader_excludes_it(self):
- # Hidden-element + text shape is observed; tag and contents are synthetic.
- html = list_html().replace('<td>13 ottobre 2026</td>',
- '<td><span style="display:none">20261013</span>13 ottobre 2026</td>')
- raw = self.run_js(html=html, script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
- page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
- run_js=Mock(return_value=raw))
- diagnostic = read_reservation_date_diagnostic(page)
- self.assertTrue(diagnostic["available"])
- item = diagnostic["samples"][0]
- self.assertFalse(item["textEqualsRendered"])
- self.assertFalse(item["text"]["acceptedByCurrentParser"])
- self.assertTrue(item["rendered"]["acceptedByCurrentParser"])
- self.assertEqual(item["text"]["italianMonths"], [10])
- self.assertTrue(item["parts"][0]["hidden"])
- self.assertNotIn("20261013", json.dumps(diagnostic))
- self.assertNotIn("13 ottobre 2026", json.dumps(diagnostic))
- parsed = parse_reservation_listing(self.run_js(html=html)["raw"])
- self.assertEqual(parsed.rows[0].day, date(2026, 10, 13))
- # Python's strict date grammar has NOT been loosened to extract substrings.
- mixed = listing()
- mixed["tables"][0]["rows"][0]["date"] = "2026101313 ottobre 2026"
- with self.assertRaisesRegex(ReservationReadError, "date_unrecognized"):
- parse_reservation_listing(mixed)
- def test_observed_hidden_other_element_then_text_shape_parses(self):
- html = list_html().replace('<td>13 ottobre 2026</td>',
- '<td><label style="display:none">20261013 09:35</label>13 ottobre 2026</td>')
- raw = self.run_js(html=html, script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
- page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
- run_js=Mock(return_value=raw))
- sample = read_reservation_date_diagnostic(page)["samples"][0]
- self.assertEqual(sample["parts"][0]["kind"], "other")
- self.assertEqual(sample["parts"][0]["shape"], "DDDDDDDD DD:DD")
- self.assertEqual(sample["text"]["shape"], "DDDDDDDD DD:DDDD LLLLLLL DDDD")
- result = parse_reservation_listing(self.run_js(html=html)["raw"])
- self.assertEqual(result.rows[0].day, date(2026, 10, 13))
- def test_hidden_date_metadata_is_not_read_or_assumed_to_be_a_sort_key(self):
- for marker in ('style="display: none"', 'hidden', 'style="visibility: hidden"'):
- with self.subTest(marker=marker):
- html = list_html().replace('<td>13 ottobre 2026</td>',
- f'<td><label {marker} data-private="true">PRIVATE-METADATA</label>13 ottobre 2026</td>')
- self.assertEqual(parse_reservation_listing(self.run_js(html=html)["raw"]).rows[0].day,
- date(2026, 10, 13))
- def test_unrendered_client_rows_use_text_nodes_not_cell_inner_text(self):
- html = list_html().replace('<td>13 ottobre 2026</td>',
- '<td data-private="true"><label style="display:none">20261013 09:35</label>13 ottobre 2026</td>')
- # Reading the parent td textContent/innerText would throw. Detached style
- # resolution is empty, but the child's own inline marker still exists.
- output = self.run_js(html=html, mode="detached")
- self.assertEqual(parse_reservation_listing(output["raw"]).rows[0].day, date(2026, 10, 13))
- def test_visible_elements_and_ambiguous_text_nodes_are_not_silently_dropped(self):
- for value in ('<label>20261013 09:35</label>13 ottobre 2026',
- '<span>13 ottobre 2026</span>',
- '13 ottobre 2026<!-- split -->14 ottobre 2026',
- '<label style="display:none">13 ottobre 2026</label>',
- '<label style="visibility:hidden"><span style="visibility:visible">Other</span></label>13 ottobre 2026',
- '<!-- comment -->' * 17 + '13 ottobre 2026'):
- with self.subTest(value=value):
- html = list_html().replace('<td>13 ottobre 2026</td>', f'<td>{value}</td>')
- self.assertEqual(self.run_js(html=html)["raw"], {"error": "date_cell_structure_changed"})
- def test_invalid_dates_and_visible_mixed_metadata_still_fail_strictly(self):
- for value in ('31 febbraio 2026', '20261013 09:3513 ottobre 2026', '2026-10-13'):
- with self.subTest(value=value):
- html = list_html().replace('<td>13 ottobre 2026</td>',
- f'<td><label style="display:none">SORT</label>{value}</td>')
- with self.assertRaisesRegex(ReservationReadError, "date_unrecognized"):
- parse_reservation_listing(self.run_js(html=html)["raw"])
- def test_hidden_metadata_is_removed_on_all_loaded_client_pages(self):
- html = list_html().replace('<td>13 ottobre 2026</td>',
- '<td><label style="display:none">20261013 09:35</label>13 ottobre 2026</td>')
- start, end = html.index('<tbody>') + len('<tbody>'), html.index('</tbody>')
- row = html[start:end]
- extra = ''.join(row.replace('SYNTHETIC-REFERENCE', f'REF-{i}').replace('synthetic-link', f'link-{i}')
- for i in range(10))
- html = html[:end] + extra + html[end:]
- result = parse_reservation_listing(self.run_js(html=html, mode="detached")["raw"])
- self.assertEqual(len(result.rows), 11)
- self.assertEqual(result.page_count, 2)
- self.assertTrue(all(r.day == date(2026, 10, 13) for r in result.rows))
- def test_date_diagnostic_does_not_initialize_or_read_wrong_headers(self):
- for mode in ("wrong_origin", "uninitialized", "loading"):
- output = self.run_js(mode=mode, script=DATE_DIAGNOSTIC_SCRIPT)
- self.assertIn("error", output["raw"])
- self.assertEqual(output["apiGets"], 0)
- output = self.run_js(html=list_html().replace('<th>Data</th>', '<th>Other</th>'),
- script=DATE_DIAGNOSTIC_SCRIPT)
- self.assertIn("error", output["raw"])
- self.assertEqual(output["apiGets"], 0)
- def test_date_diagnostic_masks_unknown_text_and_rejects_extra_payload(self):
- raw = self.run_js(script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
- raw["samples"][0]["text"] = "SECRET-USER@example.invalid"
- page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
- run_js=Mock(return_value=raw))
- diagnostic = read_reservation_date_diagnostic(page)
- self.assertTrue(diagnostic["available"])
- self.assertNotIn("SECRET", json.dumps(diagnostic))
- raw["samples"][0]["parts"][0]["kind"] = "SECRET-USER"
- self.assertEqual(read_reservation_date_diagnostic(page), {"available": False})
- def test_date_diagnostic_limits_output_and_context(self):
- raw = self.run_js(script=DATE_DIAGNOSTIC_SCRIPT)["raw"]
- raw["samples"] *= 4
- page = SimpleNamespace(url="https://prenotami.esteri.it/Reservation", tab_id="synthetic-tab",
- run_js=Mock(return_value=raw))
- self.assertEqual(read_reservation_date_diagnostic(page), {"available": False})
- page.url = "https://prenotami.esteri.it/UserArea"
- page.run_js.reset_mock()
- self.assertEqual(read_reservation_date_diagnostic(page), {"available": False})
- page.run_js.assert_not_called()
- def test_actual_detail_script_parses_observed_text_siblings_and_b(self):
- output = self.run_js(detail_page=True)
- self.assertEqual(output["raw"], detail())
- self.assertIs(parse_reservation_detail(output["raw"]).state, ReservationState.CANCELLED)
- self.assertEqual(output["apiGets"], 0)
- def test_blank_text_nodes_and_comments_do_not_define_field_positions(self):
- html = detail_html().replace('<label>', '\n<!-- spacing -->\n<label>').replace('</label>', '</label>\n ')
- self.assertEqual(self.run_js(detail_page=True, html=html)["raw"], detail())
- def test_unsafe_detail_shapes_fail_closed(self):
- cases = {
- "missing": detail_html().replace('<label>Ora:</label>', '<label>Other:</label>'),
- "duplicate": detail_html().replace('<label>Ora:</label>', '<label>Ora:</label><label>Ora:</label>'),
- "wrapped": detail_html().replace('10:25', '<span>10:25</span>'),
- "code_duplicate": detail_html() + '<input id="IDTipoStatoPrenotazione" type="hidden">',
- "section_duplicate": detail_html() + '<h4>Dati Prenotazione</h4>',
- "no_auth": detail_html().replace('app-menu', 'no-menu'),
- "inline_conflict": detail_html().replace('<label>Ora:</label>', '<label>Ora: 14:00</label>'),
- "extra_value": detail_html().replace('10:25', '10:25<b>14:00</b>'),
- }
- for mode, html in cases.items():
- with self.subTest(mode=mode):
- self.assertIn("error", self.run_js(detail_page=True, html=html)["raw"])
- def test_actual_list_script_returns_no_applicant_or_unnamed_cell_values(self):
- output = self.run_js()
- parsed = parse_reservation_listing(output["raw"])
- self.assertEqual(parsed.rows[0].reference, "SYNTHETIC-REFERENCE")
- self.assertEqual(output["apiGets"], 1)
- self.assertEqual(parsed.waitlist_count, 0)
- def test_reading_metadata_never_initializes_tables(self):
- output = self.run_js(mode="uninitialized")
- self.assertEqual(output["raw"], {"error": "not_ready"})
- self.assertEqual(output["apiGets"], 0)
- def test_duplicate_table_or_missing_link_is_rejected(self):
- for html in (list_html() + '<table id="dataTableReservations"></table>',
- list_html().replace('href="/ReservationDetail/Index/synthetic-link"', 'data-no-link="true"')):
- self.assertIn("error", self.run_js(html=html)["raw"])
- def test_all_loaded_client_pages_are_read_without_page_draw(self):
- html = list_html()
- start, end = html.index('<tbody>') + len('<tbody>'), html.index('</tbody>')
- row = html[start:end]
- extra = ''.join(row.replace('SYNTHETIC-REFERENCE', f'REF-{i}').replace('synthetic-link', f'link-{i}') for i in range(10))
- html = html[:end] + extra + html[end:]
- result = parse_reservation_listing(self.run_js(html=html)["raw"])
- self.assertEqual(len(result.rows), 11)
- self.assertEqual(result.page_count, 2)
- def test_filters_server_paging_partial_rows_and_loading_fail_closed(self):
- for mode in ("server_side", "partial", "filter", "column_filter", "deferred_nodes", "loading", "wrong_origin"):
- with self.subTest(mode=mode):
- self.assertIn("error", self.run_js(mode=mode)["raw"])
- def test_waitlist_with_initialization_traces_not_counted_as_empty(self):
- for html in (list_html().replace('id="dataTableReservationsWL"', 'id="dataTableReservationsWL" class="dataTable"'),
- list_html().replace('<table id="dataTableReservationsWL"></table>', '<div class="dataTables_wrapper"><table id="dataTableReservationsWL"></table></div>')):
- self.assertEqual(self.run_js(html=html)["raw"], {"error": "not_ready"})
- if __name__ == "__main__":
- unittest.main()
|