from pathlib import Path import json,subprocess,tempfile,re,hashlib p=Path(__file__).parent.resolve();sha=lambda b:hashlib.sha256(b).hexdigest() def run(script,f,default=True): args=['node',str(p/script),str(p/'research/maxgap-law.js'),str(p/'out-mg-big.txt')] if default:args.append(str(f)) return subprocess.run(args,capture_output=True,check=True) orig=run('compare-mg.js',p/'out-mg.txt');fixed=run('compare-mg-fixed.js',p/'out-mg.txt') # All preexisting lines and stderr are preserved; the only addition is one digest per comparison. strip=lambda b:b'\n'.join(l for l in b.split(b'\n') if b': normalized SHA256 embedded ' not in l) assert strip(fixed.stdout)==orig.stdout and fixed.stderr==orig.stderr (p/'original.out').write_bytes(orig.stdout);(p/'fixed.out').write_bytes(fixed.stdout) rows=(p/'out-mg.txt').read_text().splitlines();progress=re.compile(r'^\s+\.\.\. x=\d+ mode=\d+ \d+%') trials=[] with tempfile.TemporaryDirectory() as td: f=Path(td)/'input.txt' for i,line in enumerate(rows): if progress.match(line):continue rr=rows.copy();rr[i]=line+' [DATA_CHANGED]';f.write_text('\n'.join(rr)+'\n') old=run('compare-mg.js',f);new=run('compare-mg-fixed.js',f) assert new.stdout!=fixed.stdout trials.append({'raw_line':i+1,'original_stdout_unchanged':old.stdout==orig.stdout,'fixed_stdout_changed':True}) for r in json.loads((p/'probe.json').read_text()): rr=rows.copy();rr[r['raw_line']-1]=r['new'];f.write_text('\n'.join(rr)+'\n') assert run('compare-mg.js',f).stdout==orig.stdout assert run('compare-mg-fixed.js',f).stdout!=fixed.stdout # The accepted verifier is rerun unchanged with the fixed comparator installed under its expected name. q=Path(td)/'verification';q.mkdir();(q/'research').mkdir() for n in ['out-mg.txt','out-mg-big.txt','verify-compare-mg.js','research/maxgap-law.js']:(q/n).write_bytes((p/n).read_bytes()) (q/'compare-mg.js').write_bytes((p/'compare-mg-fixed.js').read_bytes()) v=subprocess.run(['node','verify-compare-mg.js'],cwd=q,capture_output=True,check=True) (p/'verification.out').write_bytes(v.stdout) # Both invocation forms include a digest and retain the original display prefix/contents. o=run('compare-mg.js',None,False);n=run('compare-mg-fixed.js',None,False) assert strip(n.stdout)==o.stdout and n.stderr==o.stderr result={'rows_tested':len(trials),'original_invisible_rows':sum(r['original_stdout_unchanged'] for r in trials),'fixed_visible_rows':len(trials),'numeric_witnesses':3,'accepted_verifier_passed':True,'cases':trials} (p/'coverage-checks.json').write_text(json.dumps(result,indent=2)+'\n');print(json.dumps({k:v for k,v in result.items() if k!='cases'}))