from pathlib import Path import hashlib out=Path('output') archive=out/'original-submission' archive.mkdir(exist_ok=True) names=['invoices.json','review-note.md','validate.py'] for name in names: source=out/name target=archive/name if target.exists(): raise RuntimeError('Refusing to overwrite preserved original: '+str(target)) target.write_bytes(source.read_bytes()) assert target.read_bytes()==source.read_bytes() print('Preserved byte-identical originals: '+', '.join(names)) p=out/'validate.py' s=p.read_text() s=s.replace('import json,csv,re','import json,csv,re\nfrom datetime import date') marker="words=len(Path('output/review-note.md').read_text().split())" extra='''# Expected image fields below are manually transcribed from the supplied PNGs. # This checks JSON against that transcription, not automated image recognition. expected=[ ('inputs/invoice-01.png','Cedar Print','000742','2026-10-18','PO-1042','CAD', [('Workshop handbooks',80,'7.50','600.00')], ('600.00','72.00','20.00','692.00','0412'), {'SHIPPING_AUTHORIZATION_NOT_EVIDENCED'}), ('inputs/invoice-02.png','Harbour AV','001083','2026-10-19','PO-1043','CAD', [('Microphone rental',3,'85.00','255.00'),('Stand rental',2,'15.00','30.00')], ('285.00','34.20','0.00','329.20','8891'), {'QUANTITY_EXCEEDS_PO','TOTAL_ARITHMETIC_ERROR','BANK_ACCOUNT_SUFFIX_CHANGED'}) ] for inv,exp in zip(data,expected): assert tuple(inv[k] for k in ['source_image','supplier','invoice_number','invoice_date','po','currency'])==exp[:6] assert date.fromisoformat(inv['invoice_date']).isoformat()==inv['invoice_date'] assert [(l['description'],l['quantity'],l['unit_price'],l['amount']) for l in inv['line_items']]==exp[6] assert tuple(inv[k] for k in ['subtotal','tax','shipping','total','bank_account_suffix'])==exp[7] assert {e['code'] for e in inv['exceptions']}==exp[8] assert inv['uncertain_fields']==[] assert Path(inv['source_image']).is_file() matching=[r for r in po if r['po']==inv['po']] assert {r['item'] for r in matching}=={l['description'] for l in inv['line_items']} for field in ['subtotal','tax','shipping','total']: assert re.fullmatch(r'\\d+\\.\\d{2}',inv[field]) for exception in inv['exceptions']: assert exception['detail'] and exception['sources'] for citation in exception['sources']: assert Path(citation.split(':',1)[0]).is_file() print('PASS: JSON matches manual image transcription, dates and amounts have valid formats, PO items are complete, exception codes and citation file existence checked.') ''' assert marker in s p.write_text(s.replace(marker,extra+marker)) for name in ['invoices.json','review-note.md']: assert (out/name).read_bytes()==(archive/name).read_bytes() print('No changes to invoice data or review-note conclusions; validator extended.')