"""Run from a current-main checkout with instrument.py and the scenario beside this file."""
import json
import os
from pathlib import Path
import shutil
import subprocess

assets = Path(__file__).resolve().parent
runner = Path("scripts/prove-scenario.ts")
scenario = Path("scripts/scenarios/force-stop-stranded-run.e2e.ts")
shutil.copyfile(assets / scenario.name, scenario)
original = runner.read_text()
needle = '() => run("vp", ["i"], { cwd: worktree, echo: true }).pipe(Effect.andThen(use(worktree))),'
assert needle in original
patch = '() => run("vp", ["i"], { cwd: worktree, echo: true }).pipe(Effect.andThen(run("python3", [' + json.dumps(str(assets / "instrument.py")) + ', worktree], { echo: true })), Effect.andThen(use(worktree))),'
try:
    runner.write_text(original.replace(needle, patch, 1))
    subprocess.run(["vp", "run", "scenarios:prove", str(scenario), "--base", "c198f73b3766090e02cace1a8b14f439f9fc63b6", "--head", "1054bb987e26029c0c45f31d31fabf2675f316bd", "--upload-command", "ghdrop upload"], env={**os.environ, "T3CODE_DEV_SEED": "0"}, check=False)
finally:
    runner.write_text(original)
