#!/usr/bin/env bash
set -euo pipefail

ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../.." && pwd)
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

DEPLOY_ROOT="$TMP/deploy"
SCRIPT_DIR="$DEPLOY_ROOT/modules/workstation/claude/bin"
mkdir -p "$SCRIPT_DIR" "$DEPLOY_ROOT/collector/scripts" "$TMP/bin"
cp "$ROOT/modules/workstation/claude/bin/od-live-report" "$SCRIPT_DIR/od-live-report"
touch "$DEPLOY_ROOT/collector/scripts/generate-live-report.ts"

cat >"$TMP/bin/bun" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
printf '%s\n' "$@" >"$OD_LIVE_REPORT_TEST_LOG"
SH
chmod +x "$TMP/bin/bun"

assert_generator_path() {
  local invocation=$1
  local log=$2

  PATH="$TMP/bin:$PATH" OD_LIVE_REPORT_TEST_LOG="$log" bash "$invocation" --deterministic
  [[ $(<"$log") == $'run\n'"$DEPLOY_ROOT"$'/collector/scripts/generate-live-report.ts\n--deterministic' ]]
}

assert_generator_path "$SCRIPT_DIR/od-live-report" "$TMP/direct.log"
ln -s "$SCRIPT_DIR/od-live-report" "$TMP/od-live-report"
assert_generator_path "$TMP/od-live-report" "$TMP/symlink.log"

printf 'live-report physical path resolution: ok\n'
