#!/usr/bin/env python3
"""Real shot/goal/replay HUD lifetime, size, native glyph and controller checks.

The explicit native four-pad fixture supplies game/hardware state; only private
copies run. A normal controller shot crosses the goal line in goal cases. The
timeout/pause fixtures hold the released puck away from the net to isolate UI.
"""
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import argparse,json,os,subprocess
ap=argparse.ArgumentParser(description=__doc__);ap.add_argument('rom',type=Path);ap.add_argument('output',type=Path);ap.add_argument('--state',type=Path,required=True);a=ap.parse_args();out=a.output.resolve();out.mkdir(parents=True,exist_ok=True);here=Path(__file__).resolve().parent
cases=[dict(kind='critical',pad=1),dict(kind='skip',pad=1),dict(kind='short',pad=1,duration=5),dict(kind='goal',pad=2),dict(kind='goal',pad=3),dict(kind='goal',pad=4),dict(kind='timeout',pad=1),dict(kind='pause',pad=1)]
def run(spec):
 p=out/f"{spec['kind']}-pad{spec['pad']}";p.mkdir(exist_ok=True);cmd=p/'commands.txt';cmd.write_text(f'100 load {a.state.resolve()}\n1801 exit\n')
 env={k:v for k,v in os.environ.items() if not k.startswith('GPGX_')};env.update(GPGX_DEV='1281',GPGX_DEV2='1281',GPGX_HOOK=str(here/'shot_panel_hook.py'),SHOT_PANEL_CASE=json.dumps(spec))
 with (p/'log.txt').open('w') as log:subprocess.run(['python3',str(here/'test_core.py'),str(a.rom.resolve()),str(cmd),str(p)],env=env,stdout=log,stderr=log,check=True,timeout=25)
 return json.loads((p/'result.json').read_text())
with ThreadPoolExecutor(max_workers=3) as pool:results=list(pool.map(run,cases))
(out/'results.json').write_text(json.dumps(results,indent=2)+'\n');print('PASS',len(results),'native shot-panel cases,',sum(r['checks'] for r in results),'VRAM checks: crit/short/all pads/replay/skip/timeout/pause')
