#!/usr/bin/env python3
"""Private evidence mutation tests; never calls PHP/HTTP/database."""
from copy import deepcopy
import json
from pathlib import Path
import subprocess
import sys
import tempfile

if len(sys.argv)!=5:raise SystemExit('Expected B-old C-old B-green C-green report files')
original=[json.loads(Path(p).read_text()) for p in sys.argv[1:]]
script=Path(__file__).with_name('verify_results.py');passed=[]
with tempfile.TemporaryDirectory(prefix='cdx-upload-verify-') as tmp:
    paths=[Path(tmp)/f'{i}.json' for i in range(4)]
    def run(name,mutate=None,accept=False):
        reports=deepcopy(original)
        if mutate:mutate(reports)
        for path,report in zip(paths,reports):path.write_text(json.dumps(report))
        process=subprocess.run([sys.executable,str(script),*map(str,paths)],capture_output=True,text=True)
        if (process.returncode==0)!=accept:raise AssertionError('Verifier did not enforce '+name)
        passed.append(name)
    run('eight real ORM stage outcomes',accept=True)
    run('wrong old-C native error',lambda r:r[1]['cases'][0]['error'].__setitem__('native_code',0))
    run('missing green independent readback',lambda r:r[3]['cases'][0].__setitem__('source_equal',False))
    run('own metadata left behind',lambda r:r[3].__setitem__('independent_remaining',1))
    run('actual loaded Builder drift',lambda r:r[3].__setitem__('actual_loaded_builder_sha256','wrong'))
    run('forbidden storage invocation',lambda r:r[3].__setitem__('forbidden_storage_attempts',1))
    run('domain value unintentionally changed',lambda r:r[3]['source_fixture_rows']['store'].__setitem__('domain','CHANGED'))
    run('metadata size value changed',lambda r:r[3]['source_fixture_rows']['store'].__setitem__('file_size',18))
    run('natural table changed',lambda r:r[2].__setitem__('natural_unchanged',False))
    run('HTTP scope expansion',lambda r:r[2].__setitem__('http_requests',1))
print(json.dumps({'tests':len(passed),'passed':passed,'private_temporary_copies_cleaned':True},indent=2))
