import json
from pathlib import Path
p=Path(__file__).parent
load=lambda f:json.loads((p/f).read_text())
checks={}
for lane,n in [('B',24),('C',25)]:
    e=load(f'evidence-{lane}.json');a=load(f'final-audit-{lane}.json')
    checks[lane+'_all_actual_HTTP_captured']=e['actual_CLI_requests']==n and e['captured_wire_requests']==n
    checks[lane+'_5_real_POST_operations']=len(e['unique_method_paths'])==5 and all(s.startswith('POST ') for s in e['unique_method_paths'])
    checks[lane+'_source_fields_and_rejections']=e['all_independent_source_checks']
    checks[lane+'_all_consumed_relations']=e['all_consumed_reference_checks']
    checks[lane+'_formal_delete_own_zero']=a['own_tables']['feedingHistory']['owned_rows']==0
    checks[lane+'_original_table_metadata_unchanged']=a['own_tables']['feedingHistory']['natural_source_unchanged'] and a['own_tables']['feedingHistory']['metadata_unchanged']
    checks[lane+'_final_relations_unchanged']=len(a['references'])==3 and all(s['unchanged'] for s in a['references'].values())
    for phase in ('read-green','cleanup'):
        checks[lane+'_'+phase+'_CLI_green']=e['cli']['report-feedingHistory-'+phase]['stats']['assertions']['failed']==0
    final=[s for s in e['cases'] if s['label'] in ('feedingHistory.info_final','feedingHistory.list_final')][-2:]
    checks[lane+'_complete_final_nonempty_values']=len(final)==2 and all(s['source_rows']==1 and not s['source_api_differences'] for s in final)
checks['C_one_real_original_CREATE_BY_red']=sum(s['original_CREATE_BY_length_error'] for s in load('red-C.json')['cases'])==1
checks['C_time_red_only_create_time']={f for s in load('red-C.json')['cases'] for f in s['source_api_differences']}=={'create_time'}
checks.update(load('wire-comparison.json')['checks'])
print(json.dumps({'checks':checks,'check_count':len(checks),'passed':all(checks.values())},indent=2))
raise SystemExit(0 if all(checks.values()) else 1)
