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 ['shop_domain_or_tenant_ref','api_surface','api_version','observed_api_version','access_scope_set','external_gid','schema_mapping_version','event_id_or_dedupe_key','authenticity_check','idempotency_key','retry_state','reconciliation_cursor','evidence_ref']),None,None))
    checks.append((c['id'],'version_gate',not ((c.get('api_version_status')!='STABLE_SUPPORTED' or c.get('api_version')!=c.get('observed_api_version')) and got=='ACCEPT_CONNECTOR'),None,None))
    checks.append((c['id'],'scope_auth_gate',not ((not c.get('required_scopes_satisfied') or c.get('authenticity_check')!='VERIFIED') and got=='ACCEPT_CONNECTOR'),None,None))
    checks.append((c['id'],'event_gate',not ((c.get('event_duplicate') and c.get('duplicate_effect_applied') or c.get('out_of_order_event') and c.get('canonical_regression_attempted')) and got=='ACCEPT_CONNECTOR'),None,None))
    checks.append((c['id'],'retry_mapping_gate',not ((not c.get('mapping_version_current') or c.get('retry_same_intent') and not c.get('retry_reuses_key')) and got=='ACCEPT_CONNECTOR'),None,None))
failed=[x for x in checks if not x[2]]
print(f'COM-027 LAB: {len(checks)-len(failed)}/{len(checks)} PASS')
for x in failed: print('FAIL',x)
sys.exit(1 if failed else 0)
