#!/bin/bash
#
# MCP Direct Endpoint Test Script
# Version: 1.0
# Date: 2025-10-18
#
# This script tests the Direct HTTP endpoint with various scenarios
# including success cases and error cases.
#

ENDPOINT="http://localhost/OpenForum/AddOn/ClaudeMCP/Direct"
HEADERS=(-H "Content-Type: application/json" -H "Accept: application/json")

echo "======================================"
echo "MCP Direct Endpoint Test Suite"
echo "======================================"
echo ""

# Test 1: Initialize
echo "Test 1: Initialize MCP Connection"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" "${HEADERS[@]}" -d '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2024-11-05",
    "capabilities": {},
    "clientInfo": {"name": "test-client", "version": "1.0"}
  }
}' | python3 -m json.tool
echo -e "\n"

# Test 2: List Tools
echo "Test 2: List Available Tools"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" "${HEADERS[@]}" -d '{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/list",
  "params": {}
}' | python3 -m json.tool
echo -e "\n"

# Test 3: Call Tool (pageBuilder - read)
echo "Test 3: Call pageBuilder Tool"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" "${HEADERS[@]}" -d '{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "pageBuilder",
    "arguments": {
      "action": "read",
      "page_name": "OpenForum/AddOn/ClaudeMCP"
    }
  }
}' | python3 -m json.tool
echo -e "\n"

# Error Test 1: Missing Content-Type
echo "Error Test 1: Missing Content-Type Header"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" -H "Accept: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "tools/list",
  "params": {}
}' | python3 -m json.tool 2>/dev/null || echo "Error (as expected)"
echo -e "\n"

# Error Test 2: Wrong Content-Type
echo "Error Test 2: Wrong Content-Type"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" -H "Content-Type: text/plain" -H "Accept: application/json" -d '{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "tools/list",
  "params": {}
}' | python3 -m json.tool 2>/dev/null || echo "Error response"
echo -e "\n"

# Error Test 3: Empty Body
echo "Error Test 3: Empty Request Body"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" "${HEADERS[@]}" -d '' | python3 -m json.tool 2>/dev/null || echo "Error response"
echo -e "\n"

# Error Test 4: Invalid JSON
echo "Error Test 4: Invalid JSON Syntax"
echo "-----------------------------------"
curl -X POST "$ENDPOINT" "${HEADERS[@]}" -d '{invalid json}' | python3 -m json.tool 2>/dev/null || echo "Error response"
echo -e "\n"

# Error Test 5: Wrong HTTP Method (GET instead of POST)
echo "Error Test 5: Wrong HTTP Method (GET)"
echo "-----------------------------------"
curl -X GET "$ENDPOINT" "${HEADERS[@]}" | python3 -m json.tool 2>/dev/null || echo "Error response"
echo -e "\n"

echo "======================================"
echo "Test Suite Complete"
echo "======================================"
