from pathlib import Path import csv,json,platform,datetime,sys HERE=Path(__file__).resolve().parent def clean(value):return str(value or '').strip() def read_csv(root,name): with (root/'input'/f'{name}.csv').open(encoding='utf-8-sig',newline='') as f:return list(csv.DictReader(f)) def hashfile(path): import hashlib return hashlib.sha256(path.read_bytes()).hexdigest() def known(value):return clean(value).lower() not in {"","미확인","unknown"} REPORT_HEADERS=['요청', '종결 조건', '검토 사유'] def compute(root=HERE): source=read_csv(root,'original-example');by_id={};unlinked=[] for r in source: rid=clean(r.get('접수번호')) if rid:by_id.setdefault(rid,[]).append(r) else:unlinked.append('source_request_missing') rejected=[];rows=[] for r in read_csv(root,'close_proposals'): rid=clean(r.get('request'));reasons=[] if not rid or rid not in by_id:reasons.append('request_unlinked') if clean(r.get('cause_confirmed'))!='yes':reasons.append('cause_unconfirmed') if clean(r.get('followup_verified'))!='yes':reasons.append('followup_unverified') if reasons:rejected.append(rid or '[missing_request]') rows.append([rid,'종결 근거 확인 필요' if reasons else '표의 조건 충족',' / '.join(reasons)]) first=by_id.get('Q-001',[]) actual={'inquiry_count':len(by_id),'source_row_count':len(source),'history_rows':{k:len(v) for k,v in by_id.items()},'Q001_first_quote':first[0]['문의자 원문'] if first else '', 'Q001_latest_quote':first[-1]['문의자 원문'] if first else '', 'unsupported_closure':rejected} return actual,rows,{'source_link_issues':unlinked,'scope':'Request identity and explicit flags only;not a cause diagnosis.'} def run(root=HERE): try:actual,rows,details=compute(root) except ValueError as exc: report={'lab_id':root.name,'status':'INPUT_ERROR','actual':None,'input_error':str(exc),'assertion_count':0} (root/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8') (root/'actual.json').write_text('null',encoding='utf-8') with (root/'results.csv').open('w',encoding='utf-8-sig',newline='') as f:csv.writer(f).writerows([['입력 검토 필요'],[str(exc)]]) print(json.dumps(report,ensure_ascii=False));return report expected=json.loads((root/'expected/report.json').read_text(encoding='utf-8')) checks=[{'field':k,'pass':actual.get(k)==v,'actual':actual.get(k),'expected':v} for k,v in expected.items()] passed=all(c['pass'] for c in checks) and set(actual)==set(expected) report={'lab_id':root.name,'synthetic_fixture':True,'environment':{'python':platform.python_version(),'platform':platform.system(),'processor':'Python standard library'},'run_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'status':'PASS' if passed else 'FAIL','assertion_count':len(checks),'actual':actual,'assertions':checks,'details':details,'limits':'Synthetic record checks only;not a real building inspection,safety/legal verdict,Excel app test,physical receipt or authenticity verification.'} (root/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8') (root/'actual.json').write_text(json.dumps(actual,ensure_ascii=False,indent=2),encoding='utf-8') with (root/'results.csv').open('w',encoding='utf-8-sig',newline='') as f: w=csv.writer(f);w.writerow(REPORT_HEADERS);w.writerows(rows) print(json.dumps(report,ensure_ascii=False,indent=2)) return report if __name__=='__main__': report=run() if report['status']!='PASS':raise SystemExit(1)