#!/bin/bash
# Pre-flight: verify NoOpConnector loads and serves requests.
# Exits 0 = OK, 42 = SKIP (dependency missing), nonzero = fail.

set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUN_DIR="${RUN_DIR:-$HERE/../results/preflight/noop}"
mkdir -p "$RUN_DIR"

bash "$HERE/../launch/launch_noop_connector.sh"
PORT="${PORT:-8000}"

# Issue 5 short requests
for i in 1 2 3 4 5; do
    code=$(curl -s -o "$RUN_DIR/req_$i.json" -w "%{http_code}" \
        -X POST "http://127.0.0.1:$PORT/v1/chat/completions" \
        -H 'Content-Type: application/json' \
        -d "{\"model\":\"$(ls $HOME/models/Qwen | grep Qwen3-Coder-30B | head -1 | xargs -I{} echo $HOME/models/Qwen/{})\",\"messages\":[{\"role\":\"user\",\"content\":\"hello $i\"}],\"max_tokens\":4,\"temperature\":0}" \
        --max-time 60 || echo "000")
    if [[ "$code" != "200" ]]; then
        echo "FAIL: req $i status=$code" >&2
        exit 1
    fi
done

echo "OK: all 5 requests succeeded"
