from pathlib import Path
import json, sys
sys.path.insert(0,str(Path(__file__).parent))
from evaluator import evaluate
cs=json.loads((Path(__file__).parent/'fixtures/cases.json').read_text())['fixtures']
checks=[]
for c in cs:
    got=evaluate(c)
    checks.append((c['id'],'decision',got==c['expected'],got,c['expected']))
    checks.append((c['id'],'required_fields',all(k in c for k in ['contract_version','auth_scheme','credential_scope','request_schema_version','response_schema_version','mapping_version']),None,None))
    checks.append((c['id'],'provider_boundary',not (c.get('provider_leak') and got=='ACCEPT_CONTRACT'),None,None))
    checks.append((c['id'],'retry_safety',not (c.get('idempotency_strategy')=='NONE' and c.get('reconciliation_strategy')=='NONE' and got=='ACCEPT_CONTRACT'),None,None))
    checks.append((c['id'],'event_identity',not (c.get('webhook_contract') not in (None,'none') and c.get('event_identity') in (None,'NONE','UNKNOWN') and got=='ACCEPT_CONTRACT'),None,None))
    checks.append((c['id'],'transfer_boundary',not (c.get('transfer') and c.get('canonical_interface_changed') is True),None,None))
failed=[x for x in checks if not x[2]]
print(f'COM-029 LAB: {len(checks)-len(failed)}/{len(checks)} PASS')
for x in failed: print('FAIL',x)
sys.exit(1 if failed else 0)
