#!/usr/bin/env bash
# S4 of docs/plans/2026-08-14-request-intake.md: od-worktree add ASSOCIATES with an existing
# open, unclaimed request row for this project — never creates one, never claims when the
# match is ambiguous or absent. 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 = the JSON array `list --json` should answer with
# $2 = "create" (default) | "fail" — how the fake `add` subcommand behaves
fake_requests() {
  local add_mode="${2:-create}"
  cat >"$TMP/bin/od-requests" <<STUB
#!/usr/bin/env bash
: >"$CLAIM_LOG.marker"
if [[ "\$1" == "list" ]]; then
  cat <<'JSON'
$1
JSON
  exit 0
fi
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" == "fail" ]]; 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"
}

LAST_ADD_RC=0
LAST_ADD_CREATED=0
run_add() {
  local name="$1"
  rm -f "$CLAIM_LOG" "$ADD_LOG"
  (cd "$REPO" && PATH="$TMP/bin:$PATH" 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 '[{"id":"req-only","worker":null}]'
run_add one-match
[[ -e "$CLAIM_LOG" && "$(cat "$CLAIM_LOG")" == "req-only" ]] \
  && ok "exactly one unclaimed row claims it" \
  || bad "exactly one unclaimed row claims it" "log=$(cat "$CLAIM_LOG" 2>/dev/null || echo MISSING) err=$(cat "$TMP/err")"

fake_requests '[]' create
run_add zero-match
[[ -e "$ADD_LOG" && "$(cat "$ADD_LOG")" == *"--origin agent-judgement"* ]] \
  && ok "no matching row: agent-judgement writes a new row instead of staying silent" \
  || bad "no matching row creates an agent-judgement row" "log=$(cat "$ADD_LOG" 2>/dev/null || echo MISSING)"
[[ -e "$CLAIM_LOG" && "$(cat "$CLAIM_LOG")" == "req-new" ]] \
  && ok "the newly created agent-judgement row is claimed for this worktree" \
  || bad "newly created row is claimed" "log=$(cat "$CLAIM_LOG" 2>/dev/null || echo MISSING)"

fake_requests '[]' fail
run_add zero-match-add-dedup
[[ ! -e "$CLAIM_LOG" ]] \
  && ok "a dedup 409 on the create attempt never claims a row it didn't just make" \
  || bad "dedup 409 on create never claims" "log=$(cat "$CLAIM_LOG" 2>/dev/null)"

fake_requests '[{"id":"req-a","worker":null},{"id":"req-b","worker":null}]'
run_add ambiguous-match
[[ ! -e "$CLAIM_LOG" ]] \
  && ok "an ambiguous (multiple) match never guesses which row to claim" \
  || bad "an ambiguous match never guesses" "log=$(cat "$CLAIM_LOG" 2>/dev/null)"

fake_requests '[{"id":"req-taken","worker":"lane-a@debian3"}]'
run_add already-claimed
[[ ! -e "$CLAIM_LOG" ]] \
  && ok "an already-claimed row is never re-claimed" \
  || bad "an already-claimed row is never re-claimed" "log=$(cat "$CLAIM_LOG" 2>/dev/null)"

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 ]]
