#!/bin/bash
# Boots the real server and exercises every guard that doesn't need OpenAI.
cd "$(dirname "$0")/.."

KEY="test-key-aaaaaaaaaaaaaaaaaaaaaaaaaaa"
export OPENAI_API_KEY="sk-dummy-not-used-by-these-tests"
RLKEY="ratelimit-key-ddddddddddddddddddddd"
export API_KEYS="$KEY,second-key-bbbbbbbbbbbbbbbbbbbbbbbb,$RLKEY"
export ALLOWED_ORIGINS="https://app.example.com"
export RATE_LIMIT_MAX=2
export PORT=3777

node src/server.js > /tmp/srv.log 2>&1 &
SRV=$!
trap "kill $SRV 2>/dev/null" EXIT

for i in $(seq 1 40); do curl -sf http://127.0.0.1:3777/health >/dev/null 2>&1 && break; sleep 0.25; done

pass=0; fail=0
check() { # name expected actual
  if [ "$2" = "$3" ]; then echo "  PASS  $1 ($3)"; pass=$((pass+1));
  else echo "  FAIL  $1 — want $2, got $3"; fail=$((fail+1)); fi
}
code() { curl -s -o /dev/null -w "%{http_code}" "$@"; }

J='Content-Type: application/json'
U=http://127.0.0.1:3777

check "health (no key needed)"        200 "$(code $U/health)"
check "no api key"                    401 "$(code -X POST -H "$J" -d '{"message":"hi"}' $U/api/chat)"
check "wrong api key"                 401 "$(code -X POST -H "$J" -H "x-api-key: wrong-key-cccccccccccccccccccccc" -d '{"message":"hi"}' $U/api/chat)"
check "empty message"                 400 "$(code -X POST -H "$J" -H "x-api-key: $KEY" -d '{"message":"   "}' $U/api/chat)"
check "oversized message"             400 "$(code -X POST -H "$J" -H "x-api-key: $KEY" -d "{\"message\":\"$(head -c 4100 < /dev/zero | tr '\0' 'x')\"}" $U/api/chat)"
check "path traversal conversationId" 400 "$(code -X POST -H "$J" -H "x-api-key: $KEY" -d '{"message":"hi","conversationId":"../../etc/passwd"}' $U/api/chat)"
check "unknown conversationId"        404 "$(code -X POST -H "$J" -H "x-api-key: $KEY" -d '{"message":"hi","conversationId":"aaaaaaaaaaaaaaaa"}' $U/api/chat)"
check "delete unknown id is ok"       200 "$(code -X DELETE -H "x-api-key: $KEY" $U/api/conversations/aaaaaaaaaaaaaaaa)"
check "delete too-short id"           400 "$(code -X DELETE -H "x-api-key: $KEY" $U/api/conversations/ab)"
check "unknown route, no key"         401 "$(code $U/api/nope)"
check "unknown route, valid key"       404 "$(code -H "x-api-key: $KEY" $U/api/nope)"
# /api/summarize — reachable without a real PDF: these all fail before any
# OpenAI call, so they cost nothing.
check "summarize, no api key"         401 "$(code -X POST -F "file=@$0" $U/api/summarize)"
check "summarize, no file"            400 "$(code -X POST -H "x-api-key: $KEY" $U/api/summarize)"
check "summarize, non-pdf upload"     400 "$(code -X POST -H "x-api-key: $KEY" -F "file=@$0" $U/api/summarize)"
check "summarize, forged pdf type"    400 "$(code -X POST -H "x-api-key: $KEY" -F "file=@$0;filename=fake.pdf;type=application/pdf" $U/api/summarize)"

check "second api key works"          400 "$(code -X POST -H "$J" -H "x-api-key: second-key-bbbbbbbbbbbbbbbbbbbbbbbb" -d '{"message":""}' $U/api/chat)"

# Rate limit: RATE_LIMIT_MAX=2. Valid input reaches OpenAI (dummy key -> 502),
# but quota is consumed first, so the third call must be refused.
r1=$(code -X POST -H "$J" -H "x-api-key: $RLKEY" -d '{"message":"one"}' $U/api/chat)
r2=$(code -X POST -H "$J" -H "x-api-key: $RLKEY" -d '{"message":"two"}' $U/api/chat)
r3=$(code -X POST -H "$J" -H "x-api-key: $RLKEY" -d '{"message":"three"}' $U/api/chat)
check "rate limit 3rd request"        429 "$r3"
check "rate limit allows first 2"     "502 502" "$r1 $r2"

acao_bad=$(curl -s -D- -o /dev/null -H "Origin: https://evil.example" $U/health | grep -ci "access-control-allow-origin")
acao_ok=$(curl -s -D- -o /dev/null -H "Origin: https://app.example.com" $U/health | grep -ci "access-control-allow-origin")
check "CORS denies unknown origin"    0 "$acao_bad"
check "CORS allows listed origin"     1 "$acao_ok"

echo
echo "  passed: $pass   failed: $fail"
exit $fail
