import json,pathlib,sys
ROOT=pathlib.Path(__file__).resolve().parents[1]; sys.path.insert(0,str(ROOT/'src'))
from ai_context_boundary import classify_context,validate_model_proposal,guardrail_is_final_authority,evidence_row_complete,classify_transfer
fixture=json.loads((ROOT/'fixture/baseline-ai-context.json').read_text()); policy=json.loads((ROOT/'fixture/golden-policy.json').read_text()); items={x['id']:x for x in fixture['context_items']}; models={x['id']:x for x in fixture['model_outputs']}; checks=[]
def c(n,x): checks.append((n,bool(x)))
base=classify_context(items['EXTERNAL_NOTE'],policy); c('external_classified',base['status']=='CLASSIFIED'); c('external_untrusted',base['trust_level']=='untrusted_data'); c('external_no_authority',base['may_grant_authority'] is False); c('external_informs',base['may_inform_content'] is True)
bad=dict(items['EXTERNAL_NOTE']); bad['trust_level']='trusted_authority'; c('false_authority_reject',classify_context(bad,policy)['status']=='REJECT_CONTEXT_AUTHORITY')
ok=validate_model_proposal(models['MODEL_BASELINE'],policy,items['USER_INTENT']); c('baseline_allow',ok['status']=='ALLOW_LOCAL_STUB'); c('baseline_exec',ok['executed'] is True)
deny=validate_model_proposal(models['MODEL_OUT_OF_SCOPE'],policy,items['USER_INTENT']); c('scope_deny',deny['status']=='DENY_OUT_OF_SCOPE_ACTION'); c('scope_not_exec',deny['executed'] is False); c('guardrail_not_final',guardrail_is_final_authority(policy) is False)
rem=validate_model_proposal(models['MODEL_REMINDER'],policy,{'requested_action':'create_local_reminder'}); c('approval_deny',rem['status']=='DENY_APPROVAL_REQUIRED'); c('approval_not_exec',rem['executed'] is False); rem2=validate_model_proposal(models['MODEL_REMINDER'],policy,{'requested_action':'create_local_reminder'},True); c('approval_allow',rem2['status']=='ALLOW_LOCAL_STUB'); c('approval_exec',rem2['executed'] is True)
row={k:'observed' for k in ['context_item','trust_level','may_inform_content','may_grant_authority','validation','allowed_tool_scope','approval_boundary','observed_decision','residual_risk']}; c('evidence_complete',evidence_row_complete(row)); row['residual_risk']=''; c('evidence_incomplete',not evidence_row_complete(row))
ts=classify_transfer('document_summarizer'); c('transfer_summary_no_action',ts['may_grant_action_authority'] is False and ts['allowed_actions']==[]); th=classify_transfer('scheduling_helper'); c('transfer_scheduler_rebuild',th['status']=='REBUILD_BOUNDARY'); c('transfer_scheduler_approval',th['approval_required'] is True)
for n,ok in checks: print(('PASS' if ok else 'FAIL'),n)
print(f'SUMMARY {sum(x[1] for x in checks)}/{len(checks)} PASS'); raise SystemExit(0 if all(x[1] for x in checks) else 1)
