#!/bin/bash

# Run ONE failing test with maximum visibility

GATEWAY_URL="http://localhost:3000"
API_KEY="test-key-ce67dabb0d4e27e3d72877c921b89cae"

generate_uuid() {
    if command -v uuidgen > /dev/null 2>&1; then
        uuidgen
    else
        cat /proc/sys/kernel/random/uuid 2>/dev/null
    fi
}

echo "=== Running Idempotency Test with Full Visibility ==="
IDEMPOTENCY_KEY=$(generate_uuid)
echo "IDEMPOTENCY_KEY: $IDEMPOTENCY_KEY"

IDEMPOTENT_RESPONSE=$(curl -s --max-time 5 -X POST "$GATEWAY_URL/api/runs" \
    -H "x-api-key: $API_KEY" \
    -H "Idempotency-Key: $IDEMPOTENCY_KEY" \
    -H "Content-Type: application/json" \
    -d '{"name":"Idempotency Test","input":{"test":"idempotency"}}' 2>/dev/null)

echo ""
echo "Response captured:"
echo "  Length: ${#IDEMPOTENT_RESPONSE}"
echo "  First 200 chars: ${IDEMPOTENT_RESPONSE:0:200}"
echo ""

echo "Testing the exact condition from the test script:"
echo "  [ ! -z \"$IDEMPOTENT_RESPONSE\" ] = $([ ! -z "$IDEMPOTENT_RESPONSE" ] && echo "TRUE" || echo "FALSE")"
echo "  [ ${#IDEMPOTENT_RESPONSE} -gt 200 ] = $([ ${#IDEMPOTENT_RESPONSE} -gt 200 ] && echo "TRUE" || echo "FALSE")"
echo ""

echo "Combined condition:"
if [ ! -z "$IDEMPOTENT_RESPONSE" ] && [ ${#IDEMPOTENT_RESPONSE} -gt 200 ]; then
    echo "  ✓ WOULD PASS"
else
    echo "  ✗ WOULD FAIL"
fi
echo ""

echo "Now testing with eval (like run_test does):"
TEST_COMMAND="[ ! -z \"$IDEMPOTENT_RESPONSE\" ] && [ ${#IDEMPOTENT_RESPONSE} -gt 200 ]"
echo "  Command: $TEST_COMMAND"
if eval "$TEST_COMMAND"; then
    echo "  ✓ PASSED with eval"
else
    echo "  ✗ FAILED with eval"
fi

