summaryrefslogtreecommitdiff
path: root/mcp-servers/simple-mcp-server/.claude/hooks/test-runner.sh
blob: 964f80c85361742663e9a9140650e3210985960a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash

# Test Runner Hook for Simple MCP Server
# Enhanced test execution with coverage and protocol validation

set -e

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

echo -e "${BLUE}๐Ÿงช Test Runner Hook${NC}"
echo "======================================"

# Parse test type from arguments
TEST_TYPE="${1:-all}"
WATCH_MODE="${2:-false}"

# Function to run tests
run_tests() {
    local type=$1
    local title=$2
    
    echo -e "\n${YELLOW}Running $title...${NC}"
    
    if [ "$type" = "unit" ]; then
        TEST_CMD="npx vitest run tests/unit"
    elif [ "$type" = "integration" ]; then
        TEST_CMD="npx vitest run tests/integration"
    else
        TEST_CMD="npx vitest run"
    fi
    
    if [ "$WATCH_MODE" = "true" ]; then
        TEST_CMD="${TEST_CMD/run/}"
    fi
    
    if $TEST_CMD; then
        echo -e "${GREEN}  โœ“ $title passed${NC}"
        return 0
    else
        echo -e "${RED}  โœ— $title failed${NC}"
        return 1
    fi
}

# Function to check MCP protocol compliance
check_protocol_compliance() {
    echo -e "\n${YELLOW}๐Ÿ”Œ Checking MCP Protocol Compliance...${NC}"
    
    # Check server initialization
    echo "  Checking server initialization..."
    if node -e "require('./dist/index.js')" 2>/dev/null; then
        echo -e "${GREEN}    โœ“ Server module loads${NC}"
    else
        echo -e "${YELLOW}    โš  Server not built (run 'npm run build')${NC}"
    fi
    
    # Check for required handlers
    echo "  Checking protocol handlers..."
    
    # This would normally check the actual implementation
    # For now, we'll check if the files exist
    if [ -f "src/index.ts" ]; then
        if grep -q "ListToolsRequestSchema\|ListResourcesRequestSchema\|ListPromptsRequestSchema" src/index.ts 2>/dev/null; then
            echo -e "${GREEN}    โœ“ Protocol handlers found${NC}"
        else
            echo -e "${YELLOW}    โš  Some protocol handlers may be missing${NC}"
        fi
    fi
    
    # Check capabilities
    echo "  Checking capabilities..."
    local has_capability=false
    
    if [ -d "src/tools" ] && [ "$(ls -A src/tools 2>/dev/null)" ]; then
        echo -e "${GREEN}    โœ“ Tools capability${NC}"
        has_capability=true
    fi
    
    if [ -d "src/resources" ] && [ "$(ls -A src/resources 2>/dev/null)" ]; then
        echo -e "${GREEN}    โœ“ Resources capability${NC}"
        has_capability=true
    fi
    
    if [ -d "src/prompts" ] && [ "$(ls -A src/prompts 2>/dev/null)" ]; then
        echo -e "${GREEN}    โœ“ Prompts capability${NC}"
        has_capability=true
    fi
    
    if [ "$has_capability" = false ]; then
        echo -e "${YELLOW}    โš  No capabilities implemented yet${NC}"
    fi
}

# Function to generate coverage report
generate_coverage() {
    echo -e "\n${YELLOW}๐Ÿ“Š Generating Coverage Report...${NC}"
    
    if npx vitest run --coverage 2>/dev/null; then
        echo -e "${GREEN}  โœ“ Coverage report generated${NC}"
        
        # Parse coverage summary if available
        if [ -f "coverage/coverage-summary.json" ]; then
            echo "  Coverage Summary:"
            node -e "
                const coverage = require('./coverage/coverage-summary.json');
                const total = coverage.total;
                const metrics = ['statements', 'branches', 'functions', 'lines'];
                metrics.forEach(metric => {
                    const pct = total[metric].pct;
                    const color = pct >= 80 ? '\\033[0;32m' : pct >= 60 ? '\\033[1;33m' : '\\033[0;31m';
                    console.log('    ' + metric + ': ' + color + pct.toFixed(1) + '%\\033[0m');
                });
            " 2>/dev/null || echo "    (Could not parse coverage summary)"
        fi
        
        echo "  View detailed report: coverage/index.html"
    else
        echo -e "${YELLOW}  โš  Coverage generation failed${NC}"
    fi
}

# Main execution
echo "Test configuration:"
echo "  Type: $TEST_TYPE"
echo "  Watch: $WATCH_MODE"

# Pre-test checks
echo -e "\n${YELLOW}๐Ÿ“‹ Pre-test checks...${NC}"

# Check if test framework is installed
if ! command -v vitest &> /dev/null && ! npx vitest --version &> /dev/null; then
    echo -e "${RED}  โœ— Vitest not installed${NC}"
    echo "  Install with: npm install -D vitest"
    exit 1
fi

# Check if test directory exists
if [ ! -d "tests" ] && [ ! -d "src/__tests__" ]; then
    echo -e "${YELLOW}  โš  No test directory found${NC}"
    echo "  Create tests in 'tests/' directory"
fi

# Run appropriate tests
case $TEST_TYPE in
    unit)
        run_tests "unit" "Unit Tests"
        ;;
    integration)
        run_tests "integration" "Integration Tests"
        ;;
    coverage)
        generate_coverage
        ;;
    protocol)
        check_protocol_compliance
        ;;
    all)
        FAILED=false
        
        run_tests "unit" "Unit Tests" || FAILED=true
        run_tests "integration" "Integration Tests" || FAILED=true
        check_protocol_compliance
        
        if [ "$FAILED" = true ]; then
            echo -e "\n${RED}โŒ Some tests failed${NC}"
            exit 1
        fi
        ;;
    *)
        echo -e "${RED}Unknown test type: $TEST_TYPE${NC}"
        echo "Valid options: unit, integration, coverage, protocol, all"
        exit 1
        ;;
esac

# Test summary
echo ""
echo "======================================"

if [ "$WATCH_MODE" = "true" ]; then
    echo -e "${BLUE}๐Ÿ‘๏ธ Watching for changes...${NC}"
    echo "Press Ctrl+C to stop"
else
    echo -e "${GREEN}โœ… Test run complete${NC}"
    
    # Provide helpful next steps
    echo ""
    echo "Next steps:"
    echo "  โ€ข Fix any failing tests"
    echo "  โ€ข Run with coverage: npm test -- coverage"
    echo "  โ€ข Test with MCP Inspector: npx @modelcontextprotocol/inspector"
fi