#!/bin/bash

# Stress Test for VectorLite API
# This script tries to reproduce the HNSW panic by running multiple operations

BASE_URL="http://localhost:3001"

echo "🧪 VectorLite Stress Test"
echo "========================="

# Start server in background
echo "Starting server..."
cargo run --bin vectorlite &
SERVER_PID=$!

# Wait for server to start
sleep 3

echo "1. Health Check:"
curl -s "$BASE_URL/health" | jq .
echo

echo "2. Create Collection:"
curl -s -X POST "$BASE_URL/collections" \
  -H "Content-Type: application/json" \
  -d '{"name": "stress_test", "index_type": "hnsw"}' | jq .
echo

echo "3. Adding multiple texts rapidly:"
for i in {1..10}; do
    echo "Adding text $i..."
    curl -s -X POST "$BASE_URL/collections/stress_test/text" \
      -H "Content-Type: application/json" \
      -d "{\"text\": \"This is stress test text number $i with some additional content to make it longer and more varied\"}" | jq .
    sleep 0.1
done
echo

echo "4. Searching multiple times:"
for i in {1..5}; do
    echo "Search $i..."
    curl -s -X POST "$BASE_URL/collections/stress_test/search/text" \
      -H "Content-Type: application/json" \
      -d '{"query": "stress test", "k": 3}' | jq .
    sleep 0.1
done
echo

echo "5. Adding more text documents:"
for i in {11..15}; do
    echo "Adding text $i..."
    curl -s -X POST "$BASE_URL/collections/stress_test/text" \
      -H "Content-Type: application/json" \
      -d "{\"text\": \"Additional stress test text number $i with more content to test the system\"}" | jq .
    sleep 0.1
done
echo

echo "6. Final search:"
curl -s -X POST "$BASE_URL/collections/stress_test/search/text" \
  -H "Content-Type: application/json" \
  -d '{"query": "test", "k": 5}' | jq .
echo

echo "✅ Stress test completed!"

# Clean up
kill $SERVER_PID
