from pathlib import Path
import json
from lupa.luajit21 import LuaRuntime
R=Path(__file__).resolve().parents[1]
L=LuaRuntime(unpack_returned_tuples=True);L.globals().SRC=str(R/'src')
result=L.execute(r'''
local checks=0;local function C(x,m)assert(x,m);checks=checks+1 end
local ART=dofile(SRC..'/art.lua');local factory=dofile(SRC..'/render_mech.lua')
for _,scale in ipairs({1,1.5,2})do
 local ops={};local serial=0;local fail=false;local fallback
 local sr={World={},Gui={},Vector2=function(x,y)return {x,y}end,Vector3=function(x,y,z)return {x,y,z}end,Color=function(a,r,g,b)return {a,r,g,b}end}
 local function amber(c)return c[2]==236 and c[3]==190 and c[4]==72 end
 sr.Gui.rect=function(g,p,size,c)
  if fail and amber(c)then return nil end
  serial=serial+1;ops[serial]={p=p,size=size,c=c};return serial
 end
 sr.Gui.destroy_rect=function(g,id)ops[id]=nil end
 local fb={draw=function(w,h,v)fallback=v.title end}
 local config=dofile(SRC..'/config.lua')(nil);config.scale=scale
 local D=factory(sr,config,ART,fb);D.gui='G'
 local function value(mark)return {title={code='EXO-51',cn='伐木者'},hull=1800,hull_max=1800,rows={},zones={},arms={{side='L',hp=800,max=800},{side='R',hp=800,max=800}},arm_association=mark}end
 local function count()local n=0;for _,op in pairs(ops)do if amber(op.c)then n=n+1 end end;return n end
 D.draw(1920,1080,value(nil));C(D.mode=='MECH'and count()==0,'native path must not gain a heuristic marker')
 D.draw(1920,1080,value('SPATIAL_HEURISTIC'));C(count()==8,'marker lost to static cache or missing glyph')
 D.draw(1920,1080,value(nil));C(count()==0,'stale marker not removed')
 fail=true;D.draw(1920,1080,value('SPATIAL_HEURISTIC'));C(D.mode=='TEXT'and fallback=='EXO-51 [~ARMS]','failed marker must retain a text warning')
end
return {checks=checks,scales=3,cache_toggle=true,failed_marker_falls_back=true,in_game_verified=false}
''')
report=dict(result.items());(R/'validation/spatial-render-tests.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))
