' for v in ('Visti', 'Visti Nazionali e Schengen', '13 ottobre 2026', '10:25', 'Annullata'))
cells += '
PRIVATE-APPLICANT
PRIVATE-ACTIONS
'
return f'
{heads}
{cells}
'
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('
13 ottobre 2026
',
'
2026101313 ottobre 2026
')
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('
13 ottobre 2026
',
'
13 ottobre 2026
')
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('
13 ottobre 2026
',
f'
13 ottobre 2026
')
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('
13 ottobre 2026
',
'
13 ottobre 2026
')
# 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 ('13 ottobre 2026',
'13 ottobre 2026',
'13 ottobre 202614 ottobre 2026',
'',
'13 ottobre 2026',
'' * 17 + '13 ottobre 2026'):
with self.subTest(value=value):
html = list_html().replace('
13 ottobre 2026
', f'
{value}
')
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('
13 ottobre 2026
',
f'
{value}
')
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('
13 ottobre 2026
',
'
13 ottobre 2026
')
start, end = html.index('') + len(''), html.index('')
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('
',
"no_auth": detail_html().replace('app-menu', 'no-menu'),
"inline_conflict": detail_html().replace('Ora:', 'Ora: 14:00'),
"extra_value": detail_html().replace('10:25', '10:2514:00'),
}
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() + '
',
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('') + len(''), html.index('')
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('
', '
')):
self.assertEqual(self.run_js(html=html)["raw"], {"error": "not_ready"})
if __name__ == "__main__":
unittest.main()