from pathlib import Path
import json
from lupa.luajit21 import LuaRuntime
R=Path(__file__).resolve().parents[1]
api=json.loads((R/'tests/live-api-t1-failure.json').read_text())['api_functions']
assert 'elements' not in api['Vector3'] and 'to_elements' in api['Vector3']
source=(R/'src/spatial_runtime.lua').read_text()
L=LuaRuntime(unpack_returned_tuples=True)
sr=L.table();not_called=L.eval("function()error('unexpected invocation')end")
for ns in ['GameSession','UnitSynchronizer','Unit','Vector3']:
 sr[ns]=L.table_from({k:not_called for k in api[ns]})
sr['Application']=L.eval("{main_world=function()return 'w'end}")
sr['Network']=L.eval("{game_session=function()return 's'end,peer_id=function()return 1 end}")
sr['GameSession']['unit_synchronizer']=L.eval("function()error('REACHED_SYNCHRONIZER_GET',0)end")
policy=L.table_from({'max_mechs':8,'max_arms':16})
context=L.table_from({'session':'s','peer':1,'world':'w'})
meta=L.eval('{mech_descriptors={{}},candidate_descriptors={{}}}')
for legacy in [True,False]:
 text=source.replace("required(V,'to_elements')","required(V,'elements')") if legacy else source
 runtime=L.execute(text)(sr,L.table(),L.table(),policy)
 result=runtime['resolve'](L.table(),meta,context)
 assert result[0] is None and result[1]=='SPATIAL_UNAVAILABLE'
 detail=result[2]
 assert detail['error']==('SPATIAL_API_MISSING:elements' if legacy else 'REACHED_SYNCHRONIZER_GET'),detail['error']
report={'actual_inventory_fixture':True,'old_getter_error_reproduced':True,'corrected_getter_preflight_passed':True,'getter_contract':'Vector3.to_elements(vector) -> x, y, z','in_game_bridge_validated':False,'engine_calls':0}
(R/'validation/api-regression.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))
