23f7624596
ADR-166 MCP Bridge Security Lock / Static-source security lock (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / Compose default binds loopback + Mongo has auth (push) Failing after 2s
CodeQL Advanced / Analyze (rust) (push) Failing after 0s
ADR-166 MCP Bridge Security Lock / plugin-agent-federation bindHost default (push) Failing after 1s
ADR-166 MCP Bridge Security Lock / Runtime behavior — 401 + terminal gate + fail-closed (push) Failing after 4s
business-pods-smoke / smoke (push) Failing after 1s
all-plugins-smoke / smoke-all (push) Failing after 2s
CI/CD Pipeline / Security & Code Quality (push) Failing after 1s
CI/CD Pipeline / Test Suite (ubuntu-latest) (push) Failing after 1s
CI/CD Pipeline / Build & Package (macos-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (ubuntu-latest) (push) Has been skipped
CI/CD Pipeline / Build & Package (windows-latest) (push) Has been skipped
CI/CD Pipeline / Documentation & Examples (push) Failing after 1s
Clone Tracker (14-day rolling) / Snapshot clones for ruflo ecosystem (push) Failing after 1s
CodeQL Advanced / Analyze (actions) (push) Failing after 1s
CodeQL Advanced / Analyze (javascript-typescript) (push) Failing after 1s
federation-peer-rust / stable-noop (push) Failing after 1s
metaharness-ci / score (push) Failing after 1s
metaharness-ci / router-compat (push) Failing after 0s
metaharness-ci / similarity-tests (push) Failing after 0s
no-agentbbs-smoke / smoke-without-agentbbs (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (windows-latest) (push) Has been skipped
codex-integration-audit / Codex integration audit (push) Failing after 1s
helpers-manifest-guard / guard (push) Failing after 1s
🔗 Cross-Agent Integration Tests / 🤝 Agent Coordination Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🧠 Memory Sharing Integration (push) Has been skipped
🔗 Cross-Agent Integration Tests / 🛡️ Fault Tolerance Tests (push) Has been skipped
🔗 Cross-Agent Integration Tests / ⚡ Performance Integration Tests (push) Has been skipped
metaharness-ci / mcp-scan (push) Failing after 1s
metaharness-ci / eject-dryrun (push) Failing after 1s
metaharness-ci / metaharness-real-data (push) Failing after 0s
no-cli-optdep-bloat-2561 / guard (push) Failing after 1s
no-metaharness-smoke / smoke-without-metaharness (push) Failing after 1s
no-phantom-agentic-flow-subpath / guard (push) Failing after 1s
🔄 Automated Rollback Manager / 🚨 Failure Detection (push) Failing after 1s
V3 CI/CD Pipeline / Plugin hooks smoke / ubuntu-latest / Node 22 (push) Failing after 1s
V3 CI/CD Pipeline / ruflo-graph-intelligence build + test smoke (#2044, ADR-123) (push) Failing after 1s
CVE Audit Gate / Audit root (critical-blocking) (push) Failing after 2s
cost-tracker-smoke / smoke (push) Failing after 3s
oia-audit-weekly / audit (push) Failing after 2s
ruflo-agent-smoke / ruflo-agent structural smoke (push) Failing after 1s
📊 Status Badges Update / 📊 Update Status Badges (push) Failing after 1s
V3 CI/CD Pipeline / Static regression guards (#2267 YAML + (push) Failing after 1s
V3 CI/CD Pipeline / Test V3 Packages (push) Failing after 0s
V3 CI/CD Pipeline / agent_execute provider routing smoke (#2042) (push) Failing after 0s
CVE Audit Gate / Audit v3 (critical-blocking) (push) Failing after 1s
federation-peer-rust / stable-native (push) Failing after 2s
🔗 Cross-Agent Integration Tests / 🚀 Integration Test Setup (push) Failing after 2s
neural-trader-smoke / runtime-smoke (push) Failing after 1s
V3 CI/CD Pipeline / Build V3 (macos-latest) (push) Has been skipped
V3 CI/CD Pipeline / Build V3 (ubuntu-latest) (push) Has been skipped
V3 CI/CD Pipeline / Type Check V3 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 24 (push) Failing after 1s
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / ubuntu-latest / Node 22 (push) Failing after 2s
V3 CI/CD Pipeline / browser rvf create flag smoke (#2015) (push) Failing after 0s
V3 CI/CD Pipeline / Dependency review (#2046) (push) Has been skipped
V3 CI/CD Pipeline / Supply-chain audit (#2046) (push) Failing after 0s
V3 CI/CD Pipeline / witness marker drift smoke (#2021) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader portfolio CG smoke (#2068, ADR-126 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / neural-trader backtest signing smoke (#2068, ADR-126 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / kg-extract type-import classification smoke (#2049) (push) Failing after 0s
V3 CI/CD Pipeline / witness verify precondition smoke (#1880) (push) Failing after 2s
V3 CI/CD Pipeline / neural-trader pipeline risk-gate smoke (#2068, ADR-126 Phase 5) (push) Failing after 0s
V3 CI/CD Pipeline / neural-trader feature attribution smoke (#2068, ADR-126 Phase 6) (push) Failing after 0s
V3 CI/CD Pipeline / plugin-registry signature verification smoke (#1922, CWE-347) (push) Failing after 4s
V3 CI/CD Pipeline / memory stats legacy-DB smoke (#2120) (push) Failing after 4s
V3 CI/CD Pipeline / github deprecated actions smoke (#2089, ADR-127 Phase 3) (push) Failing after 1s
V3 CI/CD Pipeline / graph query + pathfinder smoke (ADR-130 P2+P5) (push) Has been skipped
V3 CI/CD Pipeline / graph trajectory hooks smoke (ADR-130 P3) (push) Has been skipped
V3 CI/CD Pipeline / graph plugin adapter smoke (ADR-130 P4) (push) Has been skipped
V3 CI/CD Pipeline / graph benchmark (ADR-130 P6) (push) Has been skipped
V3 CI/CD Pipeline / statusline generator delegation smoke (#2195) (push) Failing after 1s
V3 CI/CD Pipeline / wizard init regression guard (#2206 (push) Failing after 1s
V3 CI/CD Pipeline / memory no-stray-db smoke (ADR-125 P7) (push) Failing after 1s
V3 CI/CD Pipeline / github-safe injection smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github actions pin smoke (#2089, ADR-127 Phase 1) (push) Failing after 1s
V3 CI/CD Pipeline / github attribution opt-in smoke (#2089, ADR-127 Phase 4) (push) Failing after 1s
V3 CI/CD Pipeline / pre-bash hook safety smoke (#2017) (push) Failing after 1s
V3 CI/CD Pipeline / Memory import smoke / ubuntu-latest (push) Failing after 0s
V3 CI/CD Pipeline / MCP protocol smoke / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / ruvllm WASM auto-init smoke (#2086) (push) Failing after 4s
V3 CI/CD Pipeline / MCP paired-tool round-trip smoke (#1889) (push) Failing after 1s
V3 CI/CD Pipeline / Plugin package install-safety (#1902/#1903/#1904) (push) Failing after 1s
V3 CI/CD Pipeline / Tool description discoverability (ADR-112) (push) Failing after 3s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (22) (push) Failing after 1s
V3 CI/CD Pipeline / CLI npx-install smoke (#1147 / (24) (push) Failing after 1s
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / ubuntu-latest (push) Failing after 2s
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / ubuntu-latest (push) Failing after 1s
V3 CI/CD Pipeline / Vector-index dimension audit (#1947) (push) Failing after 0s
V3 CI/CD Pipeline / Hook-command install safety (#1921) (push) Failing after 1s
V3 CI/CD Pipeline / ToolOutputGuardrail smoke (ADR-131, (push) Failing after 1s
V3 CI/CD Pipeline / init-bundle invariants smoke (#2095, ADR-128 Phase 5) (push) Failing after 1s
V3 CI/CD Pipeline / wasm provider bridge smoke (ADR-129 P1) (push) Failing after 2s
V3 CI/CD Pipeline / wasm gallery CRUD smoke (ADR-129 P3) (push) Failing after 1s
V3 CI/CD Pipeline / wasm plugin bridge smoke (ADR-129 P4) (push) Failing after 0s
V3 CI/CD Pipeline / wasm compose smoke (ADR-129 P2) (push) Failing after 4s
V3 CI/CD Pipeline / graph schema smoke (ADR-130 P1) (push) Failing after 0s
Validate Marketplace / validate (push) Failing after 1s
🔍 Verification Pipeline / 🚀 Setup Verification (push) Failing after 1s
🔍 Verification Pipeline / 🛡️ Security Verification (push) Has been skipped
🔍 Verification Pipeline / 📝 Code Quality (push) Has been skipped
🔍 Verification Pipeline / 🧪 Test Verification (${{ matrix.os }}, Node ${{ matrix.node }}) (push) Has been skipped
🔍 Verification Pipeline / 🏗️ Build Verification (push) Has been skipped
🔍 Verification Pipeline / 📚 Documentation Verification (push) Has been skipped
CVE Audit Gate / High-severity report (warn only) (push) Has been cancelled
🔄 Automated Rollback Manager / 🔄 Execute Rollback (push) Has been cancelled
🔄 Automated Rollback Manager / ✅ Post-Rollback Verification (push) Has been cancelled
🔄 Automated Rollback Manager / 📊 Rollback Monitoring (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook execution smoke (#2132) / windows-latest (push) Has been cancelled
🔄 Automated Rollback Manager / ⏳ Manual Rollback Approval (push) Has been cancelled
V3 CI/CD Pipeline / MCP protocol smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Memory import smoke / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows hook shim smoke (#2132) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Windows init hooks smoke (#2132) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / macos-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / ubuntu-latest (push) Has been cancelled
V3 CI/CD Pipeline / Witness verify (signed manifest) / windows-latest (push) Has been cancelled
V3 CI/CD Pipeline / Publish to npm (alpha) (push) Has been cancelled
V3 CI/CD Pipeline / Smoke (no better-sqlite3) / macos-latest / Node 22 (push) Has been cancelled
V3 CI/CD Pipeline / Plugin hooks smoke / macos-latest / Node 22 (push) Has been cancelled
CI/CD Pipeline / Deploy & Release (push) Has been cancelled
CI/CD Pipeline / CI Status (push) Has been cancelled
🔗 Cross-Agent Integration Tests / 📊 Integration Test Report (push) Has been cancelled
🔄 Automated Rollback Manager / 🔍 Pre-Rollback Validation (push) Has been cancelled
🔍 Verification Pipeline / ⚡ Performance Verification (push) Has been cancelled
🔍 Verification Pipeline / 📊 Verification Report (push) Has been cancelled
778 lines
23 KiB
TypeScript
778 lines
23 KiB
TypeScript
/**
|
|
* TDD Tests for Utility Classes
|
|
*
|
|
* Tests for RateLimiter, CircuitBreaker, MetricsCollector,
|
|
* HealthChecker, and retry utilities.
|
|
*
|
|
* @module @claude-flow/teammate-plugin/tests/utils
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
|
|
import { RateLimiter } from '../src/utils/rate-limiter.js';
|
|
import { CircuitBreaker, CircuitBreakerOpenError } from '../src/utils/circuit-breaker.js';
|
|
import { MetricsCollector } from '../src/utils/metrics-collector.js';
|
|
import { HealthChecker } from '../src/utils/health-checker.js';
|
|
import {
|
|
withRetry,
|
|
createRetryState,
|
|
calculateBackoffDelay,
|
|
sleep,
|
|
withTimeout,
|
|
} from '../src/utils/retry.js';
|
|
|
|
import {
|
|
DEFAULT_RATE_LIMIT_CONFIG,
|
|
DEFAULT_CIRCUIT_BREAKER_CONFIG,
|
|
DEFAULT_HEALTH_CHECK_CONFIG,
|
|
DEFAULT_RETRY_CONFIG,
|
|
TeammateErrorCode,
|
|
} from '../src/types.js';
|
|
|
|
// ============================================================================
|
|
// RateLimiter Tests
|
|
// ============================================================================
|
|
|
|
describe('RateLimiter', () => {
|
|
let rateLimiter: RateLimiter;
|
|
|
|
beforeEach(() => {
|
|
rateLimiter = new RateLimiter(DEFAULT_RATE_LIMIT_CONFIG);
|
|
});
|
|
|
|
describe('checkLimit', () => {
|
|
it('should allow operations within limit', () => {
|
|
// Default spawnPerMinute is 10
|
|
for (let i = 0; i < 10; i++) {
|
|
expect(rateLimiter.checkLimit('spawnPerMinute')).toBe(true);
|
|
}
|
|
});
|
|
|
|
it('should block operations exceeding limit', () => {
|
|
// Exhaust the limit
|
|
for (let i = 0; i < 10; i++) {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
}
|
|
|
|
// Next call should be blocked
|
|
expect(rateLimiter.checkLimit('spawnPerMinute')).toBe(false);
|
|
});
|
|
|
|
it('should track different operations independently', () => {
|
|
// Use up spawn limit
|
|
for (let i = 0; i < 10; i++) {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
}
|
|
|
|
// Messages should still be allowed (limit is 100)
|
|
expect(rateLimiter.checkLimit('messagesPerMinute')).toBe(true);
|
|
});
|
|
|
|
it('should reset after window expires', async () => {
|
|
// Create a rate limiter with a very short window for testing
|
|
const testConfig = { ...DEFAULT_RATE_LIMIT_CONFIG, spawnPerMinute: 1 };
|
|
const shortLimiter = new RateLimiter(testConfig);
|
|
|
|
// Use the limit
|
|
shortLimiter.checkLimit('spawnPerMinute');
|
|
expect(shortLimiter.checkLimit('spawnPerMinute')).toBe(false);
|
|
|
|
// Manual reset simulates window expiry
|
|
shortLimiter.reset('spawnPerMinute');
|
|
expect(shortLimiter.checkLimit('spawnPerMinute')).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('getState', () => {
|
|
it('should return undefined for unused operations', () => {
|
|
expect(rateLimiter.getState('spawnPerMinute')).toBeUndefined();
|
|
});
|
|
|
|
it('should return state after operation is used', () => {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
|
|
const state = rateLimiter.getState('spawnPerMinute');
|
|
expect(state).toBeDefined();
|
|
expect(state?.count).toBe(1);
|
|
expect(state?.blocked).toBe(false);
|
|
});
|
|
|
|
it('should mark state as blocked when limit exceeded', () => {
|
|
for (let i = 0; i < 11; i++) {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
}
|
|
|
|
const state = rateLimiter.getState('spawnPerMinute');
|
|
expect(state?.blocked).toBe(true);
|
|
expect(state?.nextAllowedAt).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('getRemaining', () => {
|
|
it('should return full limit when no operations used', () => {
|
|
expect(rateLimiter.getRemaining('spawnPerMinute')).toBe(10);
|
|
});
|
|
|
|
it('should decrease as operations are used', () => {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
|
|
expect(rateLimiter.getRemaining('spawnPerMinute')).toBe(7);
|
|
});
|
|
|
|
it('should return 0 when limit exhausted', () => {
|
|
for (let i = 0; i < 10; i++) {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
}
|
|
|
|
expect(rateLimiter.getRemaining('spawnPerMinute')).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('reset', () => {
|
|
it('should reset specific operation', () => {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
rateLimiter.checkLimit('messagesPerMinute');
|
|
|
|
rateLimiter.reset('spawnPerMinute');
|
|
|
|
expect(rateLimiter.getState('spawnPerMinute')).toBeUndefined();
|
|
expect(rateLimiter.getState('messagesPerMinute')).toBeDefined();
|
|
});
|
|
|
|
it('should reset all operations when no argument provided', () => {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
rateLimiter.checkLimit('messagesPerMinute');
|
|
|
|
rateLimiter.reset();
|
|
|
|
expect(rateLimiter.getState('spawnPerMinute')).toBeUndefined();
|
|
expect(rateLimiter.getState('messagesPerMinute')).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('isBlocked', () => {
|
|
it('should return false when not blocked', () => {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
expect(rateLimiter.isBlocked('spawnPerMinute')).toBe(false);
|
|
});
|
|
|
|
it('should return true when blocked', () => {
|
|
for (let i = 0; i < 11; i++) {
|
|
rateLimiter.checkLimit('spawnPerMinute');
|
|
}
|
|
expect(rateLimiter.isBlocked('spawnPerMinute')).toBe(true);
|
|
});
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// CircuitBreaker Tests
|
|
// ============================================================================
|
|
|
|
describe('CircuitBreaker', () => {
|
|
let circuitBreaker: CircuitBreaker;
|
|
|
|
beforeEach(() => {
|
|
circuitBreaker = new CircuitBreaker(DEFAULT_CIRCUIT_BREAKER_CONFIG);
|
|
});
|
|
|
|
describe('initial state', () => {
|
|
it('should start in closed state', () => {
|
|
expect(circuitBreaker.isClosed()).toBe(true);
|
|
expect(circuitBreaker.isOpen()).toBe(false);
|
|
expect(circuitBreaker.isHalfOpen()).toBe(false);
|
|
});
|
|
|
|
it('should have zero failures and successes', () => {
|
|
const state = circuitBreaker.getState();
|
|
expect(state.failures).toBe(0);
|
|
expect(state.successes).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('execute', () => {
|
|
it('should execute function and return result when closed', async () => {
|
|
const result = await circuitBreaker.execute(async () => 'success');
|
|
expect(result).toBe('success');
|
|
});
|
|
|
|
it('should record success after successful execution', async () => {
|
|
await circuitBreaker.execute(async () => 'ok');
|
|
|
|
const state = circuitBreaker.getState();
|
|
expect(state.successes).toBe(1);
|
|
expect(state.lastSuccess).not.toBeNull();
|
|
});
|
|
|
|
it('should record failure after failed execution', async () => {
|
|
try {
|
|
await circuitBreaker.execute(async () => {
|
|
throw new Error('test error');
|
|
});
|
|
} catch {
|
|
// Expected
|
|
}
|
|
|
|
const state = circuitBreaker.getState();
|
|
expect(state.failures).toBe(1);
|
|
expect(state.lastFailure).not.toBeNull();
|
|
});
|
|
|
|
it('should open circuit after failure threshold reached', async () => {
|
|
// Default threshold is 5
|
|
for (let i = 0; i < 5; i++) {
|
|
try {
|
|
await circuitBreaker.execute(async () => {
|
|
throw new Error('failure');
|
|
});
|
|
} catch {
|
|
// Expected
|
|
}
|
|
}
|
|
|
|
expect(circuitBreaker.isOpen()).toBe(true);
|
|
});
|
|
|
|
it('should reject immediately when open', async () => {
|
|
circuitBreaker.forceOpen();
|
|
|
|
await expect(
|
|
circuitBreaker.execute(async () => 'should not run')
|
|
).rejects.toThrow(CircuitBreakerOpenError);
|
|
});
|
|
|
|
it('should include error code in CircuitBreakerOpenError', async () => {
|
|
circuitBreaker.forceOpen();
|
|
|
|
try {
|
|
await circuitBreaker.execute(async () => 'test');
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(CircuitBreakerOpenError);
|
|
expect((error as CircuitBreakerOpenError).code).toBe(TeammateErrorCode.BACKEND_UNAVAILABLE);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('state transitions', () => {
|
|
it('should transition from closed to open after failures', async () => {
|
|
expect(circuitBreaker.isClosed()).toBe(true);
|
|
|
|
// Trigger failures up to threshold (5)
|
|
for (let i = 0; i < 5; i++) {
|
|
try {
|
|
await circuitBreaker.execute(async () => {
|
|
throw new Error('fail');
|
|
});
|
|
} catch {
|
|
// Expected
|
|
}
|
|
}
|
|
|
|
expect(circuitBreaker.isOpen()).toBe(true);
|
|
});
|
|
|
|
it('should transition from open to half-open after reset time', async () => {
|
|
const quickConfig = {
|
|
...DEFAULT_CIRCUIT_BREAKER_CONFIG,
|
|
resetTimeMs: 10, // Very short for testing
|
|
};
|
|
const quickBreaker = new CircuitBreaker(quickConfig);
|
|
quickBreaker.forceOpen();
|
|
|
|
// Wait for reset time
|
|
await sleep(20);
|
|
|
|
// Try to execute - should transition to half-open
|
|
try {
|
|
await quickBreaker.execute(async () => 'test');
|
|
} catch {
|
|
// May fail, but state should change
|
|
}
|
|
|
|
expect(quickBreaker.isHalfOpen()).toBe(true);
|
|
});
|
|
|
|
it('should transition from half-open to closed after successes', async () => {
|
|
circuitBreaker.forceHalfOpen();
|
|
|
|
// Success threshold is 2
|
|
await circuitBreaker.execute(async () => 'success1');
|
|
await circuitBreaker.execute(async () => 'success2');
|
|
|
|
expect(circuitBreaker.isClosed()).toBe(true);
|
|
});
|
|
|
|
it('should transition from half-open to open on failure', async () => {
|
|
circuitBreaker.forceHalfOpen();
|
|
|
|
// Need to accumulate failures
|
|
for (let i = 0; i < 5; i++) {
|
|
try {
|
|
await circuitBreaker.execute(async () => {
|
|
throw new Error('fail');
|
|
});
|
|
} catch {
|
|
// Expected
|
|
}
|
|
}
|
|
|
|
expect(circuitBreaker.isOpen()).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('reset', () => {
|
|
it('should reset to initial closed state', async () => {
|
|
// Open the circuit
|
|
circuitBreaker.forceOpen();
|
|
expect(circuitBreaker.isOpen()).toBe(true);
|
|
|
|
// Reset
|
|
circuitBreaker.reset();
|
|
|
|
expect(circuitBreaker.isClosed()).toBe(true);
|
|
const state = circuitBreaker.getState();
|
|
expect(state.failures).toBe(0);
|
|
expect(state.successes).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('disabled circuit breaker', () => {
|
|
it('should pass through when disabled', async () => {
|
|
const disabledBreaker = new CircuitBreaker({
|
|
...DEFAULT_CIRCUIT_BREAKER_CONFIG,
|
|
enabled: false,
|
|
});
|
|
|
|
disabledBreaker.forceOpen();
|
|
|
|
// Should still execute even though "open"
|
|
const result = await disabledBreaker.execute(async () => 'passed');
|
|
expect(result).toBe('passed');
|
|
});
|
|
});
|
|
|
|
describe('timeout', () => {
|
|
it('should timeout slow operations', async () => {
|
|
const quickTimeoutConfig = {
|
|
...DEFAULT_CIRCUIT_BREAKER_CONFIG,
|
|
timeoutMs: 10,
|
|
};
|
|
const quickBreaker = new CircuitBreaker(quickTimeoutConfig);
|
|
|
|
await expect(
|
|
quickBreaker.execute(async () => {
|
|
await sleep(100);
|
|
return 'too slow';
|
|
})
|
|
).rejects.toThrow('timeout');
|
|
});
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// MetricsCollector Tests
|
|
// ============================================================================
|
|
|
|
describe('MetricsCollector', () => {
|
|
let metrics: MetricsCollector;
|
|
|
|
beforeEach(() => {
|
|
metrics = new MetricsCollector();
|
|
});
|
|
|
|
describe('increment', () => {
|
|
it('should increment counter metrics', () => {
|
|
metrics.increment('teamsCreated');
|
|
metrics.increment('teamsCreated');
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.teamsCreated).toBe(2);
|
|
});
|
|
|
|
it('should increment by custom amount', () => {
|
|
metrics.increment('messagesSent', 5);
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.messagesSent).toBe(5);
|
|
});
|
|
|
|
it('should update lastActivityAt', () => {
|
|
const before = new Date();
|
|
metrics.increment('teamsCreated');
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.lastActivityAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
});
|
|
});
|
|
|
|
describe('decrement', () => {
|
|
it('should decrement counter metrics', () => {
|
|
metrics.increment('activeTeams', 5);
|
|
metrics.decrement('activeTeams', 2);
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.activeTeams).toBe(3);
|
|
});
|
|
|
|
it('should not go below zero', () => {
|
|
metrics.increment('activeTeams', 2);
|
|
metrics.decrement('activeTeams', 10);
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.activeTeams).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('set', () => {
|
|
it('should set gauge metrics to specific value', () => {
|
|
metrics.set('activeTeammates', 42);
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.activeTeammates).toBe(42);
|
|
});
|
|
});
|
|
|
|
describe('recordLatency', () => {
|
|
it('should record latency measurements', () => {
|
|
metrics.recordLatency('spawnLatency', 100);
|
|
metrics.recordLatency('spawnLatency', 200);
|
|
metrics.recordLatency('spawnLatency', 150);
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.spawnLatency).toHaveLength(3);
|
|
});
|
|
});
|
|
|
|
describe('getPercentile', () => {
|
|
it('should calculate percentiles correctly', () => {
|
|
// Add values 1-100
|
|
for (let i = 1; i <= 100; i++) {
|
|
metrics.recordLatency('messageLatency', i);
|
|
}
|
|
|
|
// P50 should be ~50
|
|
const p50 = metrics.getPercentile('messageLatency', 50);
|
|
expect(p50).toBeGreaterThanOrEqual(49);
|
|
expect(p50).toBeLessThanOrEqual(51);
|
|
|
|
// P99 should be ~99
|
|
const p99 = metrics.getPercentile('messageLatency', 99);
|
|
expect(p99).toBeGreaterThanOrEqual(98);
|
|
});
|
|
|
|
it('should return 0 for empty histogram', () => {
|
|
expect(metrics.getPercentile('spawnLatency', 50)).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('getAverageLatency', () => {
|
|
it('should calculate average correctly', () => {
|
|
metrics.recordLatency('spawnLatency', 100);
|
|
metrics.recordLatency('spawnLatency', 200);
|
|
metrics.recordLatency('spawnLatency', 300);
|
|
|
|
expect(metrics.getAverageLatency('spawnLatency')).toBe(200);
|
|
});
|
|
|
|
it('should return 0 for empty histogram', () => {
|
|
expect(metrics.getAverageLatency('spawnLatency')).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('getSnapshot', () => {
|
|
it('should include calculated rates', () => {
|
|
metrics.increment('messagesSent', 10);
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.rates).toBeDefined();
|
|
expect(snapshot.rates.messagesPerSecond).toBeGreaterThanOrEqual(0);
|
|
});
|
|
|
|
it('should include timestamp', () => {
|
|
const before = new Date();
|
|
const snapshot = metrics.getSnapshot();
|
|
|
|
expect(snapshot.timestamp.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
});
|
|
});
|
|
|
|
describe('reset', () => {
|
|
it('should reset all metrics to initial values', () => {
|
|
metrics.increment('teamsCreated', 5);
|
|
metrics.recordLatency('spawnLatency', 100);
|
|
|
|
metrics.reset();
|
|
|
|
const snapshot = metrics.getSnapshot();
|
|
expect(snapshot.metrics.teamsCreated).toBe(0);
|
|
expect(snapshot.metrics.spawnLatency).toHaveLength(0);
|
|
});
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// HealthChecker Tests
|
|
// ============================================================================
|
|
|
|
describe('HealthChecker', () => {
|
|
let healthChecker: HealthChecker;
|
|
let statusChangeSpy: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
statusChangeSpy = vi.fn();
|
|
healthChecker = new HealthChecker(
|
|
{
|
|
...DEFAULT_HEALTH_CHECK_CONFIG,
|
|
intervalMs: 100, // Short interval for testing
|
|
},
|
|
statusChangeSpy
|
|
);
|
|
});
|
|
|
|
afterEach(() => {
|
|
healthChecker.stopAll();
|
|
});
|
|
|
|
describe('startChecking', () => {
|
|
it('should add teammate to checks', () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => true);
|
|
|
|
const check = healthChecker.getCheck('teammate-1');
|
|
expect(check).toBeDefined();
|
|
expect(check?.teammateId).toBe('teammate-1');
|
|
expect(check?.teamName).toBe('team-1');
|
|
});
|
|
|
|
it('should perform initial check', async () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => true);
|
|
|
|
// Wait for initial check - async checks need adequate time
|
|
await sleep(150);
|
|
|
|
const check = healthChecker.getCheck('teammate-1');
|
|
// After initial check, status should transition from 'unknown'
|
|
// to either 'healthy' or 'degraded' depending on threshold
|
|
expect(['healthy', 'degraded']).toContain(check?.status);
|
|
});
|
|
});
|
|
|
|
describe('stopChecking', () => {
|
|
it('should remove teammate from checks', () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => true);
|
|
healthChecker.stopChecking('teammate-1');
|
|
|
|
expect(healthChecker.getCheck('teammate-1')).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('health status transitions', () => {
|
|
it('should become healthy after successful checks', async () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => true);
|
|
|
|
// Wait for checks to accumulate
|
|
await sleep(300);
|
|
|
|
const check = healthChecker.getCheck('teammate-1');
|
|
expect(check?.status).toBe('healthy');
|
|
});
|
|
|
|
it('should become unhealthy after failed checks', async () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => false);
|
|
|
|
// Wait for checks to fail
|
|
await sleep(400);
|
|
|
|
const check = healthChecker.getCheck('teammate-1');
|
|
expect(check?.status).toBe('unhealthy');
|
|
});
|
|
|
|
it('should call onStatusChange when status changes', async () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => true);
|
|
|
|
// Wait for status change
|
|
await sleep(300);
|
|
|
|
expect(statusChangeSpy).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('getTeamReport', () => {
|
|
it('should aggregate health by team', async () => {
|
|
healthChecker.startChecking('t1', 'team-1', async () => true);
|
|
healthChecker.startChecking('t2', 'team-1', async () => true);
|
|
healthChecker.startChecking('t3', 'team-2', async () => true);
|
|
|
|
// Wait for checks
|
|
await sleep(300);
|
|
|
|
const report = healthChecker.getTeamReport('team-1');
|
|
expect(report.teamName).toBe('team-1');
|
|
expect(report.teammates).toHaveLength(2);
|
|
});
|
|
|
|
it('should calculate overall status', async () => {
|
|
healthChecker.startChecking('t1', 'team-1', async () => true);
|
|
healthChecker.startChecking('t2', 'team-1', async () => true);
|
|
|
|
await sleep(300);
|
|
|
|
const report = healthChecker.getTeamReport('team-1');
|
|
expect(report.overallStatus).toBe('healthy');
|
|
});
|
|
});
|
|
|
|
describe('forceCheck', () => {
|
|
it('should perform check immediately', async () => {
|
|
healthChecker.startChecking('teammate-1', 'team-1', async () => true);
|
|
|
|
await healthChecker.forceCheck('teammate-1', async () => false);
|
|
|
|
const check = healthChecker.getCheck('teammate-1');
|
|
expect(check?.consecutiveFailures).toBe(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Retry Utilities Tests
|
|
// ============================================================================
|
|
|
|
describe('Retry Utilities', () => {
|
|
describe('withRetry', () => {
|
|
it('should return result on first success', async () => {
|
|
const result = await withRetry(
|
|
async () => 'success',
|
|
DEFAULT_RETRY_CONFIG
|
|
);
|
|
|
|
expect(result).toBe('success');
|
|
});
|
|
|
|
it('should retry on failure and eventually succeed', async () => {
|
|
let attempts = 0;
|
|
const result = await withRetry(
|
|
async () => {
|
|
attempts++;
|
|
if (attempts < 3) throw new Error('not yet');
|
|
return 'success';
|
|
},
|
|
{ ...DEFAULT_RETRY_CONFIG, initialDelayMs: 10 }
|
|
);
|
|
|
|
expect(result).toBe('success');
|
|
expect(attempts).toBe(3);
|
|
});
|
|
|
|
it('should throw after max retries exhausted', async () => {
|
|
await expect(
|
|
withRetry(
|
|
async () => {
|
|
throw new Error('always fails');
|
|
},
|
|
{ ...DEFAULT_RETRY_CONFIG, maxRetries: 2, initialDelayMs: 10 }
|
|
)
|
|
).rejects.toThrow('always fails');
|
|
});
|
|
|
|
it('should respect isRetryable filter', async () => {
|
|
let attempts = 0;
|
|
const nonRetryableError = new Error('non-retryable');
|
|
|
|
await expect(
|
|
withRetry(
|
|
async () => {
|
|
attempts++;
|
|
throw nonRetryableError;
|
|
},
|
|
{ ...DEFAULT_RETRY_CONFIG, initialDelayMs: 10 },
|
|
(error) => error.message !== 'non-retryable'
|
|
)
|
|
).rejects.toThrow('non-retryable');
|
|
|
|
// Should not have retried
|
|
expect(attempts).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('calculateBackoffDelay', () => {
|
|
it('should increase exponentially', () => {
|
|
const config = { ...DEFAULT_RETRY_CONFIG, initialDelayMs: 100, backoffMultiplier: 2 };
|
|
|
|
expect(calculateBackoffDelay(1, config)).toBe(100);
|
|
expect(calculateBackoffDelay(2, config)).toBe(200);
|
|
expect(calculateBackoffDelay(3, config)).toBe(400);
|
|
});
|
|
|
|
it('should cap at maxDelayMs', () => {
|
|
const config = {
|
|
...DEFAULT_RETRY_CONFIG,
|
|
initialDelayMs: 100,
|
|
backoffMultiplier: 2,
|
|
maxDelayMs: 500,
|
|
};
|
|
|
|
expect(calculateBackoffDelay(10, config)).toBe(500);
|
|
});
|
|
});
|
|
|
|
describe('sleep', () => {
|
|
it('should delay for specified duration', async () => {
|
|
const start = Date.now();
|
|
await sleep(50);
|
|
const elapsed = Date.now() - start;
|
|
|
|
expect(elapsed).toBeGreaterThanOrEqual(40); // Allow some variance
|
|
});
|
|
});
|
|
|
|
describe('withTimeout', () => {
|
|
it('should return result if within timeout', async () => {
|
|
const result = await withTimeout(
|
|
async () => 'fast',
|
|
100
|
|
);
|
|
|
|
expect(result).toBe('fast');
|
|
});
|
|
|
|
it('should throw on timeout', async () => {
|
|
await expect(
|
|
withTimeout(
|
|
async () => {
|
|
await sleep(200);
|
|
return 'slow';
|
|
},
|
|
50
|
|
)
|
|
).rejects.toThrow('timed out');
|
|
});
|
|
|
|
it('should use custom timeout error', async () => {
|
|
const customError = new Error('custom timeout');
|
|
|
|
await expect(
|
|
withTimeout(
|
|
async () => {
|
|
await sleep(100);
|
|
},
|
|
10,
|
|
customError
|
|
)
|
|
).rejects.toThrow('custom timeout');
|
|
});
|
|
});
|
|
|
|
describe('createRetryState', () => {
|
|
it('should create initial retry state', () => {
|
|
const state = createRetryState();
|
|
|
|
expect(state.attempt).toBe(0);
|
|
expect(state.lastError).toBeNull();
|
|
expect(state.nextRetryAt).toBeNull();
|
|
expect(state.totalDelayMs).toBe(0);
|
|
});
|
|
});
|
|
});
|