agent/php/tests/Feature/Mcp/Services/McpHealthServiceTest.php
Snider 91551dec9b feat(mcp): implement extended RFC services + transport (#842)
Additive-only — no existing files modified.

Services (php/Mcp/Services/):
- CircuitBreaker (3-state, Cache::add trial lock)
- DataRedactor (28 sensitive + 16 PII keys, partial-redact algorithm)
- McpHealthService (YAML registry + JSON-RPC stdio ping protocolVersion 2024-11-05)
- McpMetricsService (p50/p95/p99 linear interpolation)
- McpWebhookDispatcher (mcp.tool.executed → WebhookEndpoints)
- OpenApiGenerator (OpenAPI 3.0.3)
- ToolRateLimiter (Cache::put first, Cache::increment after — no reset)
- AgentSessionService (php/Mod/Mcp/Services/ namespace per spec)

Transport (php/Mcp/Transport/):
- McpContext (transport-agnostic callbacks)
- Contracts/McpToolHandler interface

Resources (php/Mcp/Resources/):
- AppConfig, ContentResource, DatabaseSchema

Config: php/resources/mcp/registry.yaml.
Pest Feature tests _Good/_Bad/_Ugly per AX-10 for each new class.

Co-authored-by: Codex <noreply@openai.com>
Closes tasks.lthn.sh/view.php?id=842
2026-04-25 05:50:16 +01:00

99 lines
3 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
require_once dirname(__DIR__).'/Support/bootstrap.php';
mcpRequire('Mcp/Services/McpHealthService.php');
use Core\Mcp\Services\McpHealthService;
use Illuminate\Support\Facades\Cache;
beforeEach(function (): void {
Cache::flush();
});
test('McpHealthService_check_Good_marks_a_stdio_server_online_when_initialize_returns_jsonrpc_result', function (): void {
$service = new class extends McpHealthService
{
protected function loadServerConfig(string $serverId): ?array
{
return [
'id' => $serverId,
'connection' => [
'type' => 'stdio',
'command' => 'php',
'args' => ['artisan', 'mcp:agent-server'],
'cwd' => '${APP_ROOT:-/tmp}',
],
];
}
protected function executeProcess(array $command, string $cwd, string $input): array
{
return [
'exit_code' => 0,
'output' => json_encode([
'jsonrpc' => '2.0',
'result' => [
'serverInfo' => ['name' => 'Host Hub'],
'protocolVersion' => '2024-11-05',
],
]),
'error' => '',
'response_time_ms' => 42,
];
}
};
$result = $service->check('host-hub', true);
expect($result['status'])->toBe(McpHealthService::STATUS_ONLINE)
->and($result['protocol_version'])->toBe('2024-11-05')
->and($result['response_time_ms'])->toBe(42);
});
test('McpHealthService_check_Bad_returns_unknown_for_missing_registry_entries', function (): void {
$service = new class extends McpHealthService
{
protected function loadServerConfig(string $serverId): ?array
{
return null;
}
};
expect($service->check('missing', true)['status'])->toBe(McpHealthService::STATUS_UNKNOWN);
});
test('McpHealthService_check_Ugly_marks_successful_but_malformed_stdio_output_as_degraded', function (): void {
$service = new class extends McpHealthService
{
protected function loadServerConfig(string $serverId): ?array
{
return [
'id' => $serverId,
'connection' => [
'type' => 'stdio',
'command' => 'php',
],
];
}
protected function executeProcess(array $command, string $cwd, string $input): array
{
return [
'exit_code' => 0,
'output' => "not-json\nstill-not-json",
'error' => '',
'response_time_ms' => 15,
];
}
};
$result = $service->check('marketing', true);
expect($result['status'])->toBe(McpHealthService::STATUS_DEGRADED)
->and($result['output'])->toContain('not-json');
});