#!/usr/bin/env python3
import difflib
import hashlib
import json
import pathlib
import subprocess
here=pathlib.Path(__file__).resolve().parent
business=here.parents[4]/'zhctproject'/'store'
file='application/p/logic/DateMenu.php'
old=(business/file).read_bytes();new=(here/'source'/file).read_bytes()
assert hashlib.sha256(old).hexdigest()=='4749b69e8e4de3cfd0c05eb4be1a9cf182a7a22182d7c020a9cd48c60f7e44e3'
(here/'dining-time-proposed.patch').write_text(''.join(difflib.unified_diff(old.decode().splitlines(True),new.decode().splitlines(True),fromfile='a/'+file,tofile='b/'+file)))
tests={}
for name,root,code in [('original',business,1),('candidate',here/'source',0)]:
    p=subprocess.run(['/opt/homebrew/Cellar/php@7.4/7.4.33_13/bin/php',str(here/'StaffDiningTimestampContractTest.php'),str(root)],capture_output=True,text=True)
    assert p.returncode==code
    tests[name]=json.loads(p.stdout)
(here/'DINING_TDD.json').write_text(json.dumps({'source':file,'original_sha256':hashlib.sha256(old).hexdigest(),'candidate_sha256':hashlib.sha256(new).hexdigest(),'tests':tests,'group_by_changed':False,'selected_representative_changed':False,'main_business_writes':0},indent=2)+'\n')
print(json.dumps({'candidate_assertions':tests['candidate']['assertions'],'candidate_sha256':hashlib.sha256(new).hexdigest()}))
