import json, os, signal, subprocess, sys, tempfile, time
from pathlib import Path
ROOT=Path(__file__).parent
cases=json.loads((ROOT/'fixtures/cases.json').read_text(encoding='utf-8'))['cases']
required={'id','uid_gid_evidenced','groups_caps_evidenced','cwd_matches','env_matches','file_access','umask','created_mode','expected_mode','socket_scope','signal_sent','handler_observed','cleanup_observed','exit_reason','supervisor_action','running','health_ready','recovery_verified','decisive_evidence','transfer'}
assert len(cases)==8
for c in cases:
    missing=required-set(c); assert not missing,(c['id'],missing)
    assert c['decisive_evidence'] in {True,False}
    assert c['transfer'] in {True,False}
# One real local safety-bounded behavior check: own child process, temp dir, loopback only.
FIXTURE=ROOT/'runtime_fixture.py'
with tempfile.TemporaryDirectory(prefix='ops001_check_') as td:
    state=Path(td)/'state'; env=dict(os.environ); env['OPS001_STATE_DIR']=str(state)
    p=subprocess.Popen([sys.executable,str(FIXTURE),'--state-dir',str(state),'--umask','0077'],cwd=ROOT,env=env)
    end=time.time()+3
    while time.time()<end and not (state/'ready.json').exists(): time.sleep(0.02)
    assert (state/'ready.json').exists()
    startup=json.loads((state/'startup.json').read_text())
    assert startup['socket_host']=='127.0.0.1'
    assert startup['created_mode']=='0o600'
    assert startup['uid']==os.geteuid() and startup['gid']==os.getegid()
    os.kill(p.pid,signal.SIGTERM); assert p.wait(timeout=3)==0
    assert (state/'cleanup.json').exists()
print('PASS: 8 synthetic fixtures valid + local unprivileged loopback/SIGTERM/umask behavior check')
