#!/bin/bash
# Final comprehensive chunking test

API_KEY="vn3_cdd6d45f2045d03d5adac56eda6af9a9b781211038972807f35d52dfb6400144"
API_URL="http://localhost:8000/v1/tts/generate"

echo "================================================================"
echo "FINAL COMPREHENSIVE CHUNKING TESTS"
echo "================================================================"
echo ""

# Test 1: Short text
echo "TEST 1: Short text (no chunking expected)"
SHORT="Hello world. This is short."
echo "Length: ${#SHORT} chars"
curl -s -X POST "$API_URL" -H "Content-Type: application/json" -H "X-API-Key: $API_KEY" \
  -d "{\"text\": \"$SHORT\", \"speaker\": \"lipakshi\", \"seed\": 42}" \
  -D /tmp/final_short_h.txt -o /tmp/final_short.wav
DUR=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 /tmp/final_short.wav 2>&1)
CHUNKED=$(grep -i "x-text-chunked" /tmp/final_short_h.txt | cut -d: -f2 | tr -d " \r")
echo "  Result: Chunked=$CHUNKED, Duration=${DUR}s"
echo ""

# Test 2: Medium text (chunking should trigger)
echo "TEST 2: Medium text (chunking should trigger at 800+ chars)"
MEDIUM="This is a medium length text. It has multiple sentences. Each sentence adds content. We need several sentences here. The chunking algorithm should handle this well. Natural boundaries should be respected. The audio should flow smoothly. This is enough to test medium-length processing. We continue with more sentences to ensure we exceed the 800 character threshold that triggers chunking. Here we add even more content to make absolutely sure the chunking is triggered. The algorithm will intelligently split this text at natural boundaries. Each chunk will be processed with appropriate max_tokens to avoid excessive silence."
echo "Length: ${#MEDIUM} chars"
time curl -s -X POST "$API_URL" -H "Content-Type: application/json" -H "X-API-Key: $API_KEY" \
  -d "{\"text\": \"$MEDIUM\", \"speaker\": \"reet\", \"seed\": 42}" \
  -D /tmp/final_medium_h.txt -o /tmp/final_medium.wav
DUR=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 /tmp/final_medium.wav 2>&1)
CHUNKED=$(grep -i "x-text-chunked" /tmp/final_medium_h.txt | cut -d: -f2 | tr -d " \r")
BYTES=$(grep -i "x-audio-bytes" /tmp/final_medium_h.txt | cut -d: -f2 | tr -d " \r" | numfmt --to=iec 2>/dev/null || echo $BYTES)
echo "  Result: Chunked=$CHUNKED, Duration=${DUR}s, Size=$BYTES"
echo ""

echo "================================================================"
echo "SUMMARY"
echo "================================================================"
ls -lh /tmp/final_*.wav
echo ""
echo "✅ Tests complete! Ready for manual validation."

