import os
from pathlib import Path
import subprocess
import tempfile

# Run from a current-main checkout with dependencies installed.
with tempfile.TemporaryDirectory(prefix="posix-path-proof-", dir=Path(__file__).parent) as scratch:
    quoted = Path(scratch) / 'demo' / 'bin"'
    quoted.mkdir(parents=True)
    stub = quoted / "agy"
    stub.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "$(dirname "$0")/invocations.log"\nprintf "1.0.0\\n"\n')
    stub.chmod(0o755)
    env = os.environ.copy()
    env["PATH"] = str(quoted) + os.pathsep + env["PATH"]
    env["T3CODE_DEV_SEED"] = "0"
    env["T3_SCENARIO_INHERIT_ENV"] = "T3CODE_DEV_SEED"
    subprocess.run(["sh", "-c", 'test "$(command -v agy)" = "$1" && test "$(agy --version)" = "1.0.0"', "sh", str(stub)], env=env, check=True)
    base = subprocess.check_output(["git", "merge-base", "origin/main", "e79dc81596cc110c904663fc1820f7308907879c"], text=True).strip()
    result = subprocess.run(["vp", "run", "scenarios:prove", "scripts/scenarios/posix-path-quotes.e2e.ts", "--base", base, "--head", "e79dc81596cc110c904663fc1820f7308907879c", "--upload-command", "ghdrop upload"], env=env)
    raise SystemExit(result.returncode)
