import json, subprocess, sys

def run(p):
    x=subprocess.run([sys.executable,'checkpoint_gate.py'],input=json.dumps(p),text=True,capture_output=True)
    return x.returncode, json.loads(x.stdout)

def base(**kw):
    p={'capture_at':'2026-09-14T03:00:00Z','contract_version':'checkpoint/v1','tree_size':10,'sealed_entries':10,'basis':'same_capture','raw_values':{'tree_size':10,'sealed_entries':10}}
    p.update(kw); return p

cases=[
 (base(),(0,'CONSISTENT_AT_CAPTURE')),
 (base(tree_size=11),(0,'INCONSISTENT')),
 (base(basis='later_linked'),(0,'UNWITNESSED_JOIN')),
 (base(basis='later_linked',witness={'link':'explicit','prior_capture_at':'2026-09-14T03:00:00Z'}),(0,'CONSISTENT_AT_CAPTURE')),
 (base(contract_version='checkpoint/v0'),(2,'REFUSE')),
 (base(tree_size='10'),(2,'REFUSE')),
 (base(basis='later_linked',tree_size=11,witness={'link':'explicit','prior_capture_at':'2026-09-14T03:00:00Z'}),(0,'INCONSISTENT')),
]
for p,(code,label) in cases:
    got=run(p)
    assert got[0]==code and got[1]['classification']==label,(p,got,(code,label))
print(f'{len(cases)} cases passed; positive and negative controls exercised')
