#!/bin/bash

# Test script to validate subscription ID handling fixes
# This tests the memory corruption fixes in subscription handling

echo "Testing subscription ID validation fixes..."

# Test malformed subscription IDs
echo "Testing malformed subscription IDs..."

# Test 1: Empty subscription ID
echo '["REQ","",{}]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null || echo "Empty ID test: Connection failed (expected)"

# Test 2: Very long subscription ID (over 64 chars)
echo '["REQ","verylongsubscriptionidthatshouldexceedthemaximumlengthlimitof64characters",{}]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null || echo "Long ID test: Connection failed (expected)"

# Test 3: Subscription ID with invalid characters
echo '["REQ","sub@123",{}]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null || echo "Invalid chars test: Connection failed (expected)"

# Test 4: NULL subscription ID (this should be caught by JSON parsing)
echo '["REQ",null,{}]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null || echo "NULL ID test: Connection failed (expected)"

# Test 5: Valid subscription ID (should work)
echo '["REQ","valid_sub_123",{}]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null && echo "Valid ID test: Success" || echo "Valid ID test: Failed"

echo "Testing CLOSE message validation..."

# Test 6: CLOSE with malformed subscription ID
echo '["CLOSE",""]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null || echo "CLOSE empty ID test: Connection failed (expected)"

# Test 7: CLOSE with valid subscription ID
echo '["CLOSE","valid_sub_123"]' | timeout 5 wscat -c ws://localhost:8888 2>/dev/null && echo "CLOSE valid ID test: Success" || echo "CLOSE valid ID test: Failed"

echo "Subscription validation tests completed."