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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BIN="$ROOT_DIR/bin/harness-adapter"

PASS=0
FAIL=0

ok() {
  PASS=$((PASS + 1))
  printf 'ok %s\n' "$1"
}

bad() {
  FAIL=$((FAIL + 1))
  printf 'not ok %s -- %s\n' "$1" "$2"
}

run() {
  bash "$BIN" "$@"
}

state_file_for() {
  printf '%s/%s/adapters-state.json' "$1" "$2"
}

json_eq() {
  local file=$1
  local filter=$2
  local expected=$3
  local actual
  actual=$(jq -c "$filter" "$file")
  [[ "$actual" == "$expected" ]]
}

echo "adapter-state fixtures:"

tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT

root="$tmpdir/state"
machine="machine-a"
adapter="codex"
state_file=$(state_file_for "$root" "$machine")

if run enable --root "$root" --machine "$machine" --adapter "$adapter"; then
  if [[ -f "$state_file" ]] \
    && json_eq "$state_file" '.version' '"adapters-state/v1"' \
    && json_eq "$state_file" '.machine' '"machine-a"' \
    && json_eq "$state_file" '.adapters.codex.enabled' 'true' \
    && json_eq "$state_file" '.adapters.codex.config' '{}' \
    && json_eq "$state_file" '.adapters.codex.models' '[]'; then
    ok "enable creates default document"
  else
    bad "enable creates default document" "unexpected JSON content"
  fi
else
  bad "enable creates default document" "command failed"
fi

if run config --root "$root" --machine "$machine" --adapter "$adapter" --json '{"mode":"strict","timeout":30}'; then
  if json_eq "$state_file" '.adapters.codex.config' '{"mode":"strict","timeout":30}' \
    && json_eq "$state_file" '.adapters.codex.enabled' 'true'; then
    ok "config replaces adapter config only"
  else
    bad "config replaces adapter config only" "config mismatch"
  fi
else
  bad "config replaces adapter config only" "command failed"
fi

if run set-models --root "$root" --machine "$machine" --adapter "$adapter" gpt-5 gpt-5-mini; then
  if json_eq "$state_file" '.adapters.codex.models' '["gpt-5","gpt-5-mini"]'; then
    ok "set-models stores ordered model list"
  else
    bad "set-models stores ordered model list" "models mismatch"
  fi
else
  bad "set-models stores ordered model list" "command failed"
fi

if run disable --root "$root" --machine "$machine" --adapter "$adapter"; then
  if json_eq "$state_file" '.adapters.codex.enabled' 'false' \
    && json_eq "$state_file" '.adapters.codex.models' '["gpt-5","gpt-5-mini"]' \
    && json_eq "$state_file" '.adapters.codex.config' '{"mode":"strict","timeout":30}'; then
    ok "disable preserves config and models"
  else
    bad "disable preserves config and models" "disable mutated sibling fields"
  fi
else
  bad "disable preserves config and models" "command failed"
fi

if run enable --root "$root" --machine "$machine" --adapter "north-mini"; then
  if json_eq "$state_file" '.adapters | keys' '["codex","north-mini"]'; then
    ok "second adapter merges into same machine document"
  else
    bad "second adapter merges into same machine document" "adapter merge failed"
  fi
else
  bad "second adapter merges into same machine document" "command failed"
fi

if run set-models --root "$root" --machine "$machine" --adapter "$adapter"; then
  if json_eq "$state_file" '.adapters.codex.models' '[]'; then
    ok "set-models with no models clears list"
  else
    bad "set-models with no models clears list" "clear failed"
  fi
else
  bad "set-models with no models clears list" "command failed"
fi

if run config --root "$root" --machine "$machine" --adapter "$adapter" --json '[]' >/dev/null 2>&1; then
  bad "config rejects non-object JSON" "expected failure"
else
  rc=$?
  if [[ $rc -eq 2 ]]; then
    ok "config rejects non-object JSON"
  else
    bad "config rejects non-object JSON" "exit=$rc"
  fi
fi

if run enable --root "$root" --machine '../escape' --adapter "$adapter" >/dev/null 2>&1; then
  bad "invalid machine rejected" "expected failure"
else
  rc=$?
  if [[ $rc -eq 2 ]]; then
    ok "invalid machine rejected"
  else
    bad "invalid machine rejected" "exit=$rc"
  fi
fi

mkdir -p "$(dirname "$state_file")"
printf '{"version":"wrong/v1","machine":"%s","adapters":{}}' "$machine" > "$state_file"
if run enable --root "$root" --machine "$machine" --adapter "$adapter" >/dev/null 2>&1; then
  bad "version mismatch fails closed" "expected failure"
else
  rc=$?
  if [[ $rc -eq 2 ]]; then
    ok "version mismatch fails closed"
  else
    bad "version mismatch fails closed" "exit=$rc"
  fi
fi

printf 'not-json\n' > "$state_file"
if run enable --root "$root" --machine "$machine" --adapter "$adapter" >/dev/null 2>&1; then
  bad "invalid existing JSON fails closed" "expected failure"
else
  rc=$?
  if [[ $rc -eq 2 ]]; then
    ok "invalid existing JSON fails closed"
  else
    bad "invalid existing JSON fails closed" "exit=$rc"
  fi
fi

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