agent/php/tests/Feature/Mcp/Services/CircuitBreakerTest.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

69 lines
2.2 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
require_once dirname(__DIR__).'/Support/bootstrap.php';
mcpRequire('Mcp/Services/CircuitBreaker.php');
use Core\Mcp\Exceptions\CircuitOpenException;
use Core\Mcp\Services\CircuitBreaker;
use Illuminate\Support\Facades\Cache;
beforeEach(function (): void {
Cache::flush();
config()->set('mcp.circuit_breaker.default_threshold', 2);
config()->set('mcp.circuit_breaker.default_reset_timeout', 60);
config()->set('mcp.circuit_breaker.default_failure_window', 120);
});
test('CircuitBreaker_call_Good_allows_closed_operations_and_records_success', function (): void {
$breaker = new CircuitBreaker;
$result = $breaker->call('agentic', static fn (): string => 'ok');
expect($result)->toBe('ok')
->and($breaker->getState('agentic'))->toBe(CircuitBreaker::STATE_CLOSED)
->and($breaker->getStats('agentic')['successes'])->toBe(1);
});
test('CircuitBreaker_call_Bad_trips_open_and_fails_fast_without_a_fallback', function (): void {
$breaker = new CircuitBreaker;
try {
$breaker->call('agentic', static function (): never {
throw new RuntimeException('Connection refused');
});
} catch (RuntimeException) {
}
try {
$breaker->call('agentic', static function (): never {
throw new RuntimeException('Connection refused');
});
} catch (RuntimeException) {
}
expect($breaker->getState('agentic'))->toBe(CircuitBreaker::STATE_OPEN);
$breaker->call('agentic', static fn (): string => 'ignored');
})->throws(CircuitOpenException::class);
test('CircuitBreaker_call_Ugly_uses_fallback_when_half_open_trial_is_already_locked', function (): void {
$breaker = new CircuitBreaker;
Cache::put('circuit_breaker:content:state', CircuitBreaker::STATE_OPEN, 86400);
Cache::put('circuit_breaker:content:opened_at', time() - 120, 86400);
Cache::put('circuit_breaker:content:trial_lock', true, 30);
$result = $breaker->call(
'content',
static fn (): string => 'never',
static fn (): string => 'fallback',
);
expect($result)->toBe('fallback')
->and($breaker->getState('content'))->toBe(CircuitBreaker::STATE_HALF_OPEN);
});