#!/usr/bin/env bash
# S4 of docs/plans/2026-08-14-request-intake.md: od-worktree add associates with — or creates —
# a request row keyed on the worktree slug (`--work-key`), server-side exact-key dedup (409) is
# the ONLY convergence mechanism, never a client-side row list/count. Uses a fake od-requests on
# PATH so no real collector is hit.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ODWT="$ROOT/bin/od-worktree"
PASS=0; FAIL=0
ok()  { PASS=$((PASS+1)); printf 'PASS %s\n' "$1"; }
bad() { FAIL=$((FAIL+1)); printf 'FAIL %s\n     %s\n' "$1" "$2"; }

TMP=$(mktemp -d "${TMPDIR:-/tmp}/odwt-assoc-XXXX")
trap 'rm -rf "$TMP"' EXIT

REPO="$TMP/repo"
mkdir -p "$REPO"
git -C "$REPO" init -q
git -C "$REPO" config user.email t@t; git -C "$REPO" config user.name t; git -C "$REPO" config commit.gpgsign false
: >"$REPO/f"; git -C "$REPO" add -A; git -C "$REPO" commit -qm init

mkdir -p "$TMP/bin"
cat >"$TMP/bin/df" <<'STUB'
#!/bin/sh
printf 'Filesystem 1024-blocks Used Available Capacity Mounted on\nmock 40000000 10000000 30000000 25%% /\n'
STUB
chmod +x "$TMP/bin/df"

CLAIM_LOG="$TMP/claims.log"
ADD_LOG="$TMP/adds.log"

# $1 = "create" (default) | "dup" — whether the fake `add` 409s (simulating an exact --work-key
# match already on the board) or creates a fresh row.
fake_requests() {
  local add_mode="${1:-create}"
  cat >"$TMP/bin/od-requests" <<STUB
#!/usr/bin/env bash
if [[ "\$1" == "claim" ]]; then
  printf '%s\n' "\$2" >> "$CLAIM_LOG"
  exit 0
fi
if [[ "\$1" == "add" ]]; then
  printf '%s\n' "\$*" >> "$ADD_LOG"
  if [[ "$add_mode" == "dup" ]]; then
    echo "already tracked: #req-existing"
    exit 1
  fi
  echo "created #req-new"
  exit 0
fi
exit 1
STUB
  chmod +x "$TMP/bin/od-requests"
}

# A curated, real-binary-only PATH — deliberately excludes ~/.claude/bin (and anywhere else on
# the ambient $PATH) so the REAL, deployed od-requests can never be found and silently write a
# live board row when a test wants "no od-requests on PATH" or exercises the fake shim; git/bun
# still resolve from their real system locations.
SAFE_PATH="$TMP/bin:/usr/bin:/bin"

LAST_ADD_RC=0
LAST_ADD_CREATED=0
run_add() {
  local name="$1"
  rm -f "$CLAIM_LOG" "$ADD_LOG"
  (cd "$REPO" && PATH="$SAFE_PATH" CLAUDE_SESSION_ID="worktree-session-id" OD_SHIM_REPOS_FILE="$TMP/state/shim-repos" bash "$ODWT" add "$name" HEAD) >"$TMP/out" 2>"$TMP/err"
  LAST_ADD_RC=$?
  [[ -e "$REPO/.worktrees/$name" ]] && LAST_ADD_CREATED=1 || LAST_ADD_CREATED=0
  git -C "$REPO" worktree remove "$REPO/.worktrees/$name" --force >/dev/null 2>&1 || true
}

fake_requests create
run_add fresh-slug
[[ -e "$ADD_LOG" && "$(cat "$ADD_LOG")" == *"--work-key fresh-slug"* && "$(cat "$ADD_LOG")" == *"--origin agent-judgement"* ]] \
  && ok "add is called with the worktree slug as --work-key, never a title-only guess" \
  || bad "add called with --work-key" "log=$(cat "$ADD_LOG" 2>/dev/null || echo MISSING)"
[[ "$(cat "$ADD_LOG")" == *"--session-id worktree-session-id"* ]] \
  && ok "worktree passes its available CLI session id to add" \
  || bad "worktree passes session id to add" "log=$(cat "$ADD_LOG" 2>/dev/null || echo MISSING)"
[[ -e "$CLAIM_LOG" && "$(cat "$CLAIM_LOG")" == "req-new" ]] \
  && ok "a fresh key creates and claims a new row" \
  || bad "a fresh key creates and claims a new row" "log=$(cat "$CLAIM_LOG" 2>/dev/null || echo MISSING)"

fake_requests dup
run_add repeat-slug
[[ -e "$ADD_LOG" ]] \
  && ok "the write is still attempted even when the key is expected to already exist" \
  || bad "add is attempted" "log=MISSING"
[[ -e "$CLAIM_LOG" && "$(cat "$CLAIM_LOG")" == "req-existing" ]] \
  && ok "an exact --work-key 409 associates (claims) the EXISTING matched row, never creates a duplicate" \
  || bad "a 409 exact-key match associates the existing row" "log=$(cat "$CLAIM_LOG" 2>/dev/null || echo MISSING)"

rm -f "$TMP/bin/od-requests"
run_add no-od-requests
[[ "$LAST_ADD_RC" -eq 0 && "$LAST_ADD_CREATED" -eq 1 ]] \
  && ok "missing od-requests on PATH never blocks worktree creation" \
  || bad "missing od-requests on PATH never blocks worktree creation" "rc=$LAST_ADD_RC created=$LAST_ADD_CREATED err=$(cat "$TMP/err")"

echo
echo "PASS=$PASS FAIL=$FAIL"
[[ "$FAIL" -eq 0 ]]
