#!/usr/bin/env bash
# Lint check: verify all AGOR_TEST references are documented.
# Run in CI to catch flag drift between code and documentation.

set -euo pipefail

echo "Checking AGOR_TEST flag references..."

# Known files that should reference AGOR_TEST (from test-mode-constants.ts)
KNOWN_FILES=(
  "agor-core/src/config.rs"
  "src-tauri/src/commands/misc.rs"
  "src-tauri/src/lib.rs"
  "src-tauri/src/watcher.rs"
  "src-tauri/src/fs_watcher.rs"
  "src-tauri/src/telemetry.rs"
  "src/App.svelte"
  "tests/e2e/wdio.conf.js"
  "tests/e2e/infra/fixtures.ts"
  "tests/e2e/infra/test-mode-constants.ts"
)

# Find all files referencing AGOR_TEST (excluding node_modules, target, .git)
FOUND=$(grep -rl 'AGOR_TEST' --include='*.rs' --include='*.ts' --include='*.js' --include='*.svelte' \
  --exclude-dir=node_modules --exclude-dir=target --exclude-dir=.git . 2>/dev/null | \
  sed 's|^\./||' | sort)

UNKNOWN=""
for f in $FOUND; do
  MATCH=0
  for k in "${KNOWN_FILES[@]}"; do
    if [[ "$f" == "$k" ]]; then
      MATCH=1
      break
    fi
  done
  if [[ $MATCH -eq 0 ]]; then
    UNKNOWN="$UNKNOWN  $f\n"
  fi
done

if [[ -n "$UNKNOWN" ]]; then
  echo ""
  echo "WARNING: AGOR_TEST referenced in files not in the known registry:"
  echo -e "$UNKNOWN"
  echo "Update tests/e2e/infra/test-mode-constants.ts and this script."
  exit 1
else
  echo "All AGOR_TEST references are documented. ✓"
fi
