#!/usr/bin/env bash
# Run from a current-main checkout with dependencies installed and /usr/local/bin/opencode available.
# Put the companion scenario at scripts/scenarios/acp-cache-recovery.e2e.ts and apply acp-fixture.patch.
set -eu
proof_out=$(mktemp -d)
base_sha=506a0438a31733a16a7440919c16db54acd0c7ac
head_sha=cf589e67940ad8496c24fa81fae13e4869db5530
export T3CODE_DEV_SEED=0
export T3_SCENARIO_INHERIT_ENV=ACP_RECOVERY_RESTART,ACP_RECOVERY_OFFLINE
prove() {
  vp run scenarios:prove scripts/scenarios/acp-cache-recovery.e2e.ts --base "$base_sha" --head "$head_sha"
  cp -a scripts/.e2e/prove "$proof_out/$1"
}
prove missing
export ACP_RECOVERY_RESTART=1
prove restart
python3 - <<'PY'
from pathlib import Path
for sha in ('506a0438a3', 'cf589e6794'):
    homes=list(Path('.t3').glob('scenarios-*-scenario-'+sha))
    assert len(homes)==1
    cache=homes[0]/'caches/acp-registry/registry.json'
    cache.parent.mkdir(parents=True,exist_ok=True)
    cache.write_text('{corrupt registry metadata')
PY
prove corrupt
export ACP_RECOVERY_OFFLINE=1
prove offline
printf 'Evidence retained in %s\n' "$proof_out"
