test.ps1ā¢10.4 kB
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Test script for MCP implementation
.DESCRIPTION
This script runs tests and validation for the MCP server implementation.
.PARAMETER Language
Choose implementation language: 'node' or 'python' (default: 'node')
.PARAMETER TestType
Type of test to run: 'unit', 'integration', 'all' (default: 'all')
.PARAMETER VerboseOutput
Enable verbose test output
.EXAMPLE
.\scripts\test.ps1
.\scripts\test.ps1 -Language python
.\scripts\test.ps1 -TestType integration -Verbose
#>
param(
[Parameter(Mandatory = $false)]
[ValidateSet('node', 'python')]
[string]$Language = 'node',
[Parameter(Mandatory = $false)]
[ValidateSet('unit', 'integration', 'all')]
[string]$TestType = 'all',
[Parameter(Mandatory = $false)]
[switch]$VerboseOutput
)
# Set error action preference
$ErrorActionPreference = 'Stop'
# Colors for output
$colors = @{
Success = 'Green'
Warning = 'Yellow'
Error = 'Red'
Info = 'Cyan'
Header = 'Magenta'
Debug = 'Gray'
}
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = 'White'
)
$timestamp = Get-Date -Format "HH:mm:ss"
Write-Host "[$timestamp] $Message" -ForegroundColor $colors[$Color]
}
function Test-CommandExists {
param([string]$Command)
try {
Get-Command $Command -ErrorAction Stop | Out-Null
return $true
}
catch {
return $false
}
}
function Test-ServerHealth {
param([int]$Port = 3000)
Write-ColorOutput "Testing server health..." -Color Info
# Start server in background
$job = $null
try {
if ($Language -eq 'node') {
$job = Start-Job -ScriptBlock {
Set-Location $using:PWD
$env:PORT = $using:Port
node server.js
}
}
else {
$job = Start-Job -ScriptBlock {
Set-Location $using:PWD
& ".\venv\Scripts\Activate.ps1"
$env:PORT = $using:Port
python server.py --http
}
}
# Wait for server to start
Start-Sleep -Seconds 5
# Test health endpoint
$response = Invoke-RestMethod -Uri "http://localhost:$Port/health" -TimeoutSec 10
if ($response.status -eq 'healthy') {
Write-ColorOutput "Server health check passed" -Color Success
return $true
}
else {
Write-ColorOutput "Server health check failed" -Color Error
return $false
}
}
catch {
Write-ColorOutput "Server health check failed: $($_.Exception.Message)" -Color Error
return $false
}
finally {
if ($job) {
Stop-Job $job -ErrorAction SilentlyContinue
Remove-Job $job -ErrorAction SilentlyContinue
}
}
}
function Test-MCPTools {
param([int]$Port = 3000)
Write-ColorOutput "Testing MCP tools..." -Color Info
# Start server in background
$job = $null
try {
if ($Language -eq 'node') {
$job = Start-Job -ScriptBlock {
Set-Location $using:PWD
$env:PORT = $using:Port
node server.js
}
}
else {
$job = Start-Job -ScriptBlock {
Set-Location $using:PWD
& ".\venv\Scripts\Activate.ps1"
$env:PORT = $using:Port
python server.py --http
}
}
# Wait for server to start
Start-Sleep -Seconds 5
# Test basic functionality
$tests = @(
@{ name = "Health Check"; url = "http://localhost:$Port/health" }
)
$passedTests = 0
$totalTests = $tests.Count
foreach ($test in $tests) {
try {
$response = Invoke-RestMethod -Uri $test.url -TimeoutSec 5
Write-ColorOutput "$($test.name) passed" -Color Success
$passedTests++
}
catch {
Write-ColorOutput "$($test.name) failed: $($_.Exception.Message)" -Color Error
}
}
Write-ColorOutput "Test Results: $passedTests/$totalTests tests passed" -Color Info
return $passedTests -eq $totalTests
}
catch {
Write-ColorOutput "MCP tools test failed: $($_.Exception.Message)" -Color Error
return $false
}
finally {
if ($job) {
Stop-Job $job -ErrorAction SilentlyContinue
Remove-Job $job -ErrorAction SilentlyContinue
}
}
}
function Run-NodeTests {
Write-ColorOutput "Running Node.js tests..." -Color Header
if (-not (Test-CommandExists 'npm')) {
Write-ColorOutput "npm not found. Please run setup first." -Color Error
return $false
}
# Run npm test if available
if (Test-Path "package.json") {
$packageJson = Get-Content "package.json" | ConvertFrom-Json
if ($packageJson.scripts -and $packageJson.scripts.test) {
Write-ColorOutput "Running npm test..." -Color Info
npm test
$testResult = $LASTEXITCODE -eq 0
}
else {
Write-ColorOutput "No test script found in package.json" -Color Warning
$testResult = $true
}
}
else {
Write-ColorOutput "package.json not found" -Color Warning
$testResult = $true
}
# Run integration tests
$healthTest = Test-ServerHealth
$toolsTest = Test-MCPTools
return $testResult -and $healthTest -and $toolsTest
}
function Run-PythonTests {
Write-ColorOutput "Running Python tests..." -Color Header
if (-not (Test-CommandExists 'python')) {
Write-ColorOutput "Python not found. Please run setup first." -Color Error
return $false
}
# Activate virtual environment
if (Test-Path "venv\Scripts\Activate.ps1") {
& ".\venv\Scripts\Activate.ps1"
}
# Run pytest if available
if (Test-CommandExists 'pytest') {
Write-ColorOutput "Running pytest..." -Color Info
if ($VerboseOutput) {
pytest -v
}
else {
pytest
}
$testResult = $LASTEXITCODE -eq 0
}
else {
Write-ColorOutput "pytest not found, skipping unit tests" -Color Warning
$testResult = $true
}
# Run integration tests
$healthTest = Test-ServerHealth
$toolsTest = Test-MCPTools
return $testResult -and $healthTest -and $toolsTest
}
function Test-Prerequisites {
Write-ColorOutput "Checking prerequisites..." -Color Info
$allGood = $true
# Check language-specific prerequisites
if ($Language -eq 'node') {
if (-not (Test-CommandExists 'node')) {
Write-ColorOutput "Node.js not found" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "Node.js found" -Color Success
}
if (-not (Test-Path "package.json")) {
Write-ColorOutput "package.json not found" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "package.json found" -Color Success
}
if (-not (Test-Path "node_modules")) {
Write-ColorOutput "node_modules not found. Please run 'npm install'" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "node_modules found" -Color Success
}
}
else {
if (-not (Test-CommandExists 'python')) {
Write-ColorOutput "Python not found" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "Python found" -Color Success
}
if (-not (Test-Path "requirements.txt")) {
Write-ColorOutput "requirements.txt not found" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "requirements.txt found" -Color Success
}
if (-not (Test-Path "venv")) {
Write-ColorOutput "Virtual environment not found. Please run setup" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "Virtual environment found" -Color Success
}
}
# Check server file
$serverFile = if ($Language -eq 'node') { "server.js" } else { "server.py" }
if (-not (Test-Path $serverFile)) {
Write-ColorOutput "$serverFile not found" -Color Error
$allGood = $false
}
else {
Write-ColorOutput "$serverFile found" -Color Success
}
return $allGood
}
# Main execution
try {
Write-ColorOutput "MCP Implementation Tests" -Color Header
Write-ColorOutput "Language: $Language" -Color Info
Write-ColorOutput "Test Type: $TestType" -Color Info
Write-ColorOutput "Verbose: $VerboseOutput" -Color Info
Write-Host ""
# Check prerequisites
if (-not (Test-Prerequisites)) {
Write-ColorOutput "Prerequisites check failed. Please run setup first." -Color Error
exit 1
}
# Run tests based on language and type
$success = $false
switch ($TestType) {
'unit' {
if ($Language -eq 'node') {
npm test
$success = $LASTEXITCODE -eq 0
}
else {
& ".\venv\Scripts\Activate.ps1"
pytest
$success = $LASTEXITCODE -eq 0
}
}
'integration' {
$success = (Test-ServerHealth) -and (Test-MCPTools)
}
'all' {
if ($Language -eq 'node') {
$success = Run-NodeTests
}
else {
$success = Run-PythonTests
}
}
}
Write-Host ""
if ($success) {
Write-ColorOutput "All tests passed!" -Color Success
exit 0
}
else {
Write-ColorOutput "Some tests failed!" -Color Error
exit 1
}
}
catch {
Write-ColorOutput "Test execution failed: $($_.Exception.Message)" -Color Error
exit 1
}