#!/usr/bin/env python3
"""Additional real-repository cache correctness captures; run after measure.py."""
import pathlib,sys,subprocess,os,re,datetime,json
root=pathlib.Path(sys.argv[1]).resolve();out=root/('invalidation-'+datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ'));out.mkdir()
env=dict(os.environ,NO_COLOR='1',FORCE_COLOR='0',TERM='dumb')
def run(phase):
    for side in ['base','head']:
        p=subprocess.run(['/usr/bin/time','-p','vp','run','knip:check'],cwd=root/side,env=env,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True)
        text=re.sub(r'\x1b\[[0-9;]*[A-Za-z]','',p.stdout).replace(str(root),'<CHECKOUTS>').replace(str(pathlib.Path.home()),'<HOME>')
        (out/f'{phase}-{side}.txt').write_text(f'{side} | {phase}\ncommand: /usr/bin/time -p vp run knip:check\nexit: {p.returncode}\n\n'+text)
        print(side,phase,p.returncode,flush=True)
originals={s:(root/s/'packages/shared/src/advertisedEndpoint.ts').read_text() for s in ['base','head']}
for side in ['base','head']:
    (root/side/'packages/shared/src/proofShape.ts').write_text('import * as S from "effect/Schema"; export type Shape = typeof S.String;\n')
    (root/side/'packages/shared/src/advertisedEndpoint.ts').write_text(originals[side]+'\nexport const proofImportedShape = null as unknown as import("./proofShape.ts").Shape;\n')
run('01-imported-schema');run('02-imported-schema-warm')
for side in ['base','head']:
    (root/side/'packages/shared/src/proofShape.ts').write_text('export type Shape = { id: number };\n')
run('03-imported-type-now-value');run('04-imported-type-now-value-warm')
for side in ['base','head']:
    (root/side/'packages/shared/src/proofShape.ts').write_text('import * as S from "effect/Schema"; export type Shape = typeof S.String;\n')
run('05-imported-type-schema-again')
for side in ['base','head']:
    (root/side/'packages/shared/src/advertisedEndpoint.ts').write_text(originals[side])
    (root/side/'packages/shared/src/proofShape.ts').unlink()
run('06-restored')
print('DONE '+out.name,flush=True)
