from pathlib import Path
import json
from gate_rules import evaluate,inspect_transfer
R=Path(__file__).parent
def load(p): return json.loads((R/p).read_text())
# 1 baseline
b=evaluate(load('fixtures/baseline.json')); assert b['state']=='CONTROL_TOWER_REVIEW_REQUIRED' and b['raw_task_count_progress']==60.0 and b['weighted_progress']==24.0 and 'TASK_COUNT_OVERSTATES_PROGRESS' in b['alerts']; print('PASS 1 baseline',b['state'],b['raw_task_count_progress'],b['weighted_progress'])
checks=[
('reweight_after_progress.json','HOLD_BASELINE_INTEGRITY'),
('missing_completion_evidence.json','HOLD_COMPLETION_EVIDENCE'),
('earned_exceeds_planned.json','HOLD_EARNED_WEIGHT'),
('missing_variance.json','HOLD_VARIANCE_EVIDENCE'),
('unapproved_scope_change.json','HOLD_CHANGE_CONTROL'),
('approved_change_without_revision.json','REBASELINE_REQUIRED'),
('missing_risk_forecast.json','HOLD_FORECAST_STATE'),
('missing_milestone_evidence.json','HOLD_MILESTONE_EVIDENCE'),
('missing_control_owner.json','HOLD_CONTROL_OWNER'),
('missing_provenance.json','HOLD_AUDIT_PROVENANCE')]
for i,(n,e) in enumerate(checks,2):
 out=evaluate(load('fixtures/'+n)); assert out['state']==e,(n,out,e); print('PASS',i,n,out['state'])
t=load('transfer/transfer_fixture.json'); tout=evaluate(t); assert tout['state']=='CONTROL_TOWER_REVIEW_REQUIRED'; assert tout['baseline_revision']==2 and t['history_prior_baseline']['weighted_progress']==24.0
bad=load('transfer/transfer_baseline_oracle_reuse.json'); assert evaluate(bad)['state']=='HOLD_BASELINE_ORACLE_REUSE'; assert 'BASELINE_ORACLE_REUSE_FORBIDDEN' in inspect_transfer(bad); print('PASS 12 transfer',tout['state'],'revision',tout['baseline_revision'])
print('12/12 PASS')
