#!/bin/bash
# Test script for mcpz browser MCP server

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}=== Testing mcpz browser MCP server ===${NC}"
echo ""

# Build the project first
echo -e "${YELLOW}Building mcpz...${NC}"
cargo build --release
echo -e "${GREEN}Build complete!${NC}"
echo ""

MCPZ="./target/release/mcpz"

# Function to send a JSON-RPC request and capture response
send_request() {
    local request="$1"
    echo "$request" | timeout 5 $MCPZ server browser --verbose 2>/dev/null || true
}

# Test 1: Test CLI help
echo -e "${YELLOW}Test 1: Checking browser server help...${NC}"
$MCPZ server browser --help
echo -e "${GREEN}Test 1 passed!${NC}"
echo ""

# Test 2: Test initialize request
echo -e "${YELLOW}Test 2: Testing MCP initialize...${NC}"
INIT_RESPONSE=$(echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | timeout 5 $MCPZ server browser 2>/dev/null | head -1)
if echo "$INIT_RESPONSE" | grep -q '"mcpz-browser"'; then
    echo -e "${GREEN}Test 2 passed! Server identifies as mcpz-browser${NC}"
else
    echo -e "${RED}Test 2 failed! Unexpected response: $INIT_RESPONSE${NC}"
    exit 1
fi
echo ""

# Test 3: Test tools/list request
echo -e "${YELLOW}Test 3: Testing tools/list...${NC}"
TOOLS_RESPONSE=$(echo -e '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}\n{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | timeout 5 $MCPZ server browser 2>/dev/null | tail -1)
if echo "$TOOLS_RESPONSE" | grep -q '"start_browser"'; then
    echo -e "${GREEN}Test 3 passed! Found start_browser tool${NC}"
else
    echo -e "${RED}Test 3 failed! Unexpected response: $TOOLS_RESPONSE${NC}"
    exit 1
fi

if echo "$TOOLS_RESPONSE" | grep -q '"open_url"'; then
    echo -e "${GREEN}Found open_url tool${NC}"
else
    echo -e "${RED}Missing open_url tool${NC}"
    exit 1
fi

if echo "$TOOLS_RESPONSE" | grep -q '"get_page"'; then
    echo -e "${GREEN}Found get_page tool${NC}"
else
    echo -e "${RED}Missing get_page tool${NC}"
    exit 1
fi

if echo "$TOOLS_RESPONSE" | grep -q '"get_page_as_markdown"'; then
    echo -e "${GREEN}Found get_page_as_markdown tool${NC}"
else
    echo -e "${RED}Missing get_page_as_markdown tool${NC}"
    exit 1
fi

if echo "$TOOLS_RESPONSE" | grep -q '"get_screenshot"'; then
    echo -e "${GREEN}Found get_screenshot tool${NC}"
else
    echo -e "${RED}Missing get_screenshot tool${NC}"
    exit 1
fi

if echo "$TOOLS_RESPONSE" | grep -q '"execute_script"'; then
    echo -e "${GREEN}Found execute_script tool${NC}"
else
    echo -e "${RED}Missing execute_script tool${NC}"
    exit 1
fi

if echo "$TOOLS_RESPONSE" | grep -q '"stop_browser"'; then
    echo -e "${GREEN}Found stop_browser tool${NC}"
else
    echo -e "${RED}Missing stop_browser tool${NC}"
    exit 1
fi
echo ""

# Test 4: Test server list includes browser
echo -e "${YELLOW}Test 4: Testing server list includes browser...${NC}"
if $MCPZ server --list 2>&1 | grep -q "browser"; then
    echo -e "${GREEN}Test 4 passed! Browser server listed${NC}"
else
    echo -e "${RED}Test 4 failed! Browser not in server list${NC}"
    exit 1
fi
echo ""

# Test 5: Test list command includes browser
echo -e "${YELLOW}Test 5: Testing list command includes browser...${NC}"
if $MCPZ list 2>&1 | grep -q "browser"; then
    echo -e "${GREEN}Test 5 passed! Browser in list output${NC}"
else
    echo -e "${RED}Test 5 failed! Browser not in list${NC}"
    exit 1
fi
echo ""

# Test 6: Run unit tests for browser module
echo -e "${YELLOW}Test 6: Running unit tests...${NC}"
cargo test browser --release
echo -e "${GREEN}Test 6 passed! Unit tests complete${NC}"
echo ""

echo -e "${GREEN}=== All tests passed! ===${NC}"
echo ""
echo "To test browser automation with actual Chrome, run:"
echo "  $MCPZ server browser --verbose"
echo ""
echo "Then send these JSON-RPC requests:"
echo '  {"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'
echo '  {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"start_browser","arguments":{}}}'
echo '  {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"open_url","arguments":{"url":"https://example.com"}}}'
echo '  {"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"get_page_as_markdown","arguments":{}}}'
echo '  {"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"stop_browser","arguments":{}}}'
