#!/bin/bash
# Pre-flight: verify Mooncake kv_consumer + dummy bootstrap can start
# and answer at least a trivial request.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUN_DIR="${RUN_DIR:-$HERE/../results/preflight/kv_consumer}"
mkdir -p "$RUN_DIR"

DUMMY_BOOTSTRAP_PORT="${DUMMY_BOOTSTRAP_PORT:-8997}"
PORT="${PORT:-8000}"

bash "$HERE/../launch/launch_mooncake_consumer.sh" || {
    echo "SKIP: kv_consumer launch failed (likely Mooncake bootstrap incompatible with dummy)" >&2
    exit 42
}

# kv_consumer can be sent a regular (non-PD-sep) request — it will just
# do local prefill+decode. It should succeed.
MODEL="$HOME/models/Qwen/$(ls $HOME/models/Qwen | grep Qwen3-Coder-30B | head -1)"

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\":\"$MODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"hi $i\"}],\"max_tokens\":4,\"temperature\":0}" \
        --max-time 30 || echo "000")
    if [[ "$code" != "200" ]]; then
        echo "FAIL: req $i status=$code" >&2
        exit 1
    fi
done

echo "OK: kv_consumer reachable, all 5 requests succeeded"
