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
651 lines
21 KiB
TypeScript
651 lines
21 KiB
TypeScript
/**
|
|
* Tests for DeterministicToolGateway
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import {
|
|
DeterministicToolGateway,
|
|
createToolGateway,
|
|
} from '../src/gateway.js';
|
|
import type {
|
|
ToolSchema,
|
|
Budget,
|
|
GatewayDecision,
|
|
} from '../src/gateway.js';
|
|
|
|
// ============================================================================
|
|
// Test Helpers
|
|
// ============================================================================
|
|
|
|
function makeSchema(overrides: Partial<ToolSchema> = {}): ToolSchema {
|
|
return {
|
|
toolName: 'TestTool',
|
|
requiredParams: ['input'],
|
|
optionalParams: ['verbose'],
|
|
paramTypes: { input: 'string', verbose: 'boolean' },
|
|
maxParamSize: 1024,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function makeBudget(overrides: Partial<Budget> = {}): Budget {
|
|
return {
|
|
tokenBudget: { used: 0, limit: 1000 },
|
|
toolCallBudget: { used: 0, limit: 50 },
|
|
storageBudget: { usedBytes: 0, limitBytes: 1_000_000 },
|
|
timeBudget: { usedMs: 0, limitMs: 60_000 },
|
|
costBudget: { usedUsd: 0, limitUsd: 1.0 },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
// ============================================================================
|
|
// Idempotency Tests
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - Idempotency', () => {
|
|
let gateway: DeterministicToolGateway;
|
|
|
|
beforeEach(() => {
|
|
gateway = new DeterministicToolGateway({
|
|
idempotencyTtlMs: 5000,
|
|
});
|
|
});
|
|
|
|
it('should return cached result for identical tool calls', () => {
|
|
const params = { input: 'hello' };
|
|
|
|
// First call: evaluate + record
|
|
const firstDecision = gateway.evaluate('TestTool', params);
|
|
expect(firstDecision.allowed).toBe(true);
|
|
expect(firstDecision.idempotencyHit).toBe(false);
|
|
|
|
gateway.recordCall('TestTool', params, { output: 'world' }, 100);
|
|
|
|
// Second call: should hit cache
|
|
const secondDecision = gateway.evaluate('TestTool', params);
|
|
expect(secondDecision.allowed).toBe(true);
|
|
expect(secondDecision.idempotencyHit).toBe(true);
|
|
expect(secondDecision.cachedResult).toEqual({ output: 'world' });
|
|
});
|
|
|
|
it('should not return cached result for different params', () => {
|
|
gateway.recordCall('TestTool', { input: 'a' }, { result: 1 }, 50);
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'b' });
|
|
expect(decision.idempotencyHit).toBe(false);
|
|
expect(decision.cachedResult).toBeUndefined();
|
|
});
|
|
|
|
it('should not return cached result for different tool names', () => {
|
|
gateway.recordCall('ToolA', { input: 'x' }, { result: 1 }, 50);
|
|
|
|
const decision = gateway.evaluate('ToolB', { input: 'x' });
|
|
expect(decision.idempotencyHit).toBe(false);
|
|
});
|
|
|
|
it('should expire idempotency records after TTL', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
idempotencyTtlMs: 100,
|
|
});
|
|
|
|
gateway.recordCall('TestTool', { input: 'x' }, { result: 1 }, 10);
|
|
|
|
// Manually manipulate timestamp to simulate expiration
|
|
const history = gateway.getCallHistory();
|
|
expect(history.length).toBe(1);
|
|
|
|
// Alter timestamp to be in the past
|
|
history[0].timestamp = Date.now() - 200;
|
|
|
|
// Evaluate should not return cached (record is expired)
|
|
const decision = gateway.evaluate('TestTool', { input: 'x' });
|
|
expect(decision.idempotencyHit).toBe(false);
|
|
});
|
|
|
|
it('should list all call history', () => {
|
|
gateway.recordCall('Tool1', { a: 1 }, 'result1', 10);
|
|
gateway.recordCall('Tool2', { b: 2 }, 'result2', 20);
|
|
gateway.recordCall('Tool3', { c: 3 }, 'result3', 30);
|
|
|
|
const history = gateway.getCallHistory();
|
|
expect(history.length).toBe(3);
|
|
expect(history.map(h => h.toolName)).toEqual(['Tool1', 'Tool2', 'Tool3']);
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Idempotency Key Determinism Tests
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - Idempotency Key Determinism', () => {
|
|
let gateway: DeterministicToolGateway;
|
|
|
|
beforeEach(() => {
|
|
gateway = new DeterministicToolGateway();
|
|
});
|
|
|
|
it('should produce the same key for identical inputs', () => {
|
|
const key1 = gateway.getIdempotencyKey('Tool', { a: 1, b: 2 });
|
|
const key2 = gateway.getIdempotencyKey('Tool', { a: 1, b: 2 });
|
|
expect(key1).toBe(key2);
|
|
});
|
|
|
|
it('should produce the same key regardless of param order', () => {
|
|
const key1 = gateway.getIdempotencyKey('Tool', { b: 2, a: 1 });
|
|
const key2 = gateway.getIdempotencyKey('Tool', { a: 1, b: 2 });
|
|
expect(key1).toBe(key2);
|
|
});
|
|
|
|
it('should produce different keys for different tool names', () => {
|
|
const key1 = gateway.getIdempotencyKey('ToolA', { x: 1 });
|
|
const key2 = gateway.getIdempotencyKey('ToolB', { x: 1 });
|
|
expect(key1).not.toBe(key2);
|
|
});
|
|
|
|
it('should produce different keys for different param values', () => {
|
|
const key1 = gateway.getIdempotencyKey('Tool', { x: 1 });
|
|
const key2 = gateway.getIdempotencyKey('Tool', { x: 2 });
|
|
expect(key1).not.toBe(key2);
|
|
});
|
|
|
|
it('should produce a 64-character hex string (SHA-256)', () => {
|
|
const key = gateway.getIdempotencyKey('Tool', { foo: 'bar' });
|
|
expect(key).toMatch(/^[a-f0-9]{64}$/);
|
|
});
|
|
|
|
it('should handle nested objects deterministically', () => {
|
|
const key1 = gateway.getIdempotencyKey('Tool', {
|
|
nested: { z: 3, a: 1 },
|
|
top: 'value',
|
|
});
|
|
const key2 = gateway.getIdempotencyKey('Tool', {
|
|
top: 'value',
|
|
nested: { a: 1, z: 3 },
|
|
});
|
|
expect(key1).toBe(key2);
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Schema Validation Tests
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - Schema Validation', () => {
|
|
let gateway: DeterministicToolGateway;
|
|
|
|
beforeEach(() => {
|
|
gateway = new DeterministicToolGateway({
|
|
schemas: [
|
|
makeSchema(),
|
|
makeSchema({
|
|
toolName: 'TypedTool',
|
|
requiredParams: ['count', 'name', 'enabled'],
|
|
optionalParams: ['tags'],
|
|
paramTypes: {
|
|
count: 'number',
|
|
name: 'string',
|
|
enabled: 'boolean',
|
|
tags: 'array',
|
|
},
|
|
maxParamSize: 512,
|
|
allowedValues: {
|
|
name: ['alice', 'bob', 'charlie'],
|
|
},
|
|
}),
|
|
],
|
|
});
|
|
});
|
|
|
|
it('should accept valid params matching schema', () => {
|
|
const result = gateway.validateSchema('TestTool', {
|
|
input: 'hello',
|
|
verbose: true,
|
|
});
|
|
expect(result.valid).toBe(true);
|
|
expect(result.errors).toHaveLength(0);
|
|
});
|
|
|
|
it('should reject missing required params', () => {
|
|
const result = gateway.validateSchema('TestTool', { verbose: true });
|
|
expect(result.valid).toBe(false);
|
|
expect(result.errors).toContain('Missing required parameter: "input"');
|
|
});
|
|
|
|
it('should reject unknown params', () => {
|
|
const result = gateway.validateSchema('TestTool', {
|
|
input: 'hello',
|
|
unknown: 'bad',
|
|
});
|
|
expect(result.valid).toBe(false);
|
|
expect(result.errors).toContain('Unknown parameter: "unknown"');
|
|
});
|
|
|
|
it('should reject wrong param types', () => {
|
|
const result = gateway.validateSchema('TestTool', {
|
|
input: 42, // should be string
|
|
});
|
|
expect(result.valid).toBe(false);
|
|
expect(result.errors.some(e => e.includes('"input"') && e.includes('string'))).toBe(true);
|
|
});
|
|
|
|
it('should reject params exceeding max size', () => {
|
|
const result = gateway.validateSchema('TestTool', {
|
|
input: 'x'.repeat(2000),
|
|
});
|
|
expect(result.valid).toBe(false);
|
|
expect(result.errors.some(e => e.includes('exceeds limit'))).toBe(true);
|
|
});
|
|
|
|
it('should reject values not in allowed list', () => {
|
|
const result = gateway.validateSchema('TypedTool', {
|
|
count: 1,
|
|
name: 'eve', // not in ['alice', 'bob', 'charlie']
|
|
enabled: true,
|
|
});
|
|
expect(result.valid).toBe(false);
|
|
expect(result.errors.some(e => e.includes('"name"') && e.includes('allowed values'))).toBe(true);
|
|
});
|
|
|
|
it('should accept values in allowed list', () => {
|
|
const result = gateway.validateSchema('TypedTool', {
|
|
count: 1,
|
|
name: 'alice',
|
|
enabled: true,
|
|
});
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
|
|
it('should pass through tools without a registered schema', () => {
|
|
const result = gateway.validateSchema('UnknownTool', { anything: 'goes' });
|
|
expect(result.valid).toBe(true);
|
|
expect(result.errors).toHaveLength(0);
|
|
});
|
|
|
|
it('should detect array type correctly', () => {
|
|
const result = gateway.validateSchema('TypedTool', {
|
|
count: 1,
|
|
name: 'alice',
|
|
enabled: true,
|
|
tags: ['a', 'b'],
|
|
});
|
|
expect(result.valid).toBe(true);
|
|
});
|
|
|
|
it('should reject array when string is expected', () => {
|
|
const result = gateway.validateSchema('TestTool', {
|
|
input: ['not', 'a', 'string'],
|
|
});
|
|
expect(result.valid).toBe(false);
|
|
expect(result.errors.some(e => e.includes('"input"'))).toBe(true);
|
|
});
|
|
|
|
it('should block invalid params through evaluate pipeline', () => {
|
|
const decision = gateway.evaluate('TestTool', { verbose: true });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('schema-validation');
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Budget Enforcement Tests
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - Budget Enforcement', () => {
|
|
it('should allow calls within budget', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget(),
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'ok' });
|
|
expect(decision.allowed).toBe(true);
|
|
expect(decision.budgetRemaining).toBeDefined();
|
|
});
|
|
|
|
it('should block when tool call budget is exceeded', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
toolCallBudget: { used: 51, limit: 50 },
|
|
}),
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'blocked' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
expect(decision.reason).toContain('Budget exceeded');
|
|
});
|
|
|
|
it('should block when token budget is exceeded', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
tokenBudget: { used: 1001, limit: 1000 },
|
|
}),
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'blocked' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
});
|
|
|
|
it('should block when time budget is exceeded', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
timeBudget: { usedMs: 61_000, limitMs: 60_000 },
|
|
}),
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'blocked' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
expect(decision.reason).toContain('time');
|
|
});
|
|
|
|
it('should block when cost budget is exceeded', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
costBudget: { usedUsd: 1.5, limitUsd: 1.0 },
|
|
}),
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'blocked' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
expect(decision.reason).toContain('cost');
|
|
});
|
|
|
|
it('should block when storage budget is exceeded', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
storageBudget: { usedBytes: 2_000_000, limitBytes: 1_000_000 },
|
|
}),
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'blocked' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
expect(decision.reason).toContain('storage');
|
|
});
|
|
|
|
it('should use infinite limits when no budget is specified', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
|
|
const { withinBudget } = gateway.checkBudget();
|
|
expect(withinBudget).toBe(true);
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Budget Metering Tests
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - Budget Metering', () => {
|
|
let gateway: DeterministicToolGateway;
|
|
|
|
beforeEach(() => {
|
|
gateway = new DeterministicToolGateway({
|
|
budget: makeBudget(),
|
|
});
|
|
});
|
|
|
|
it('should increment tool call count on recordCall', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'result', 100);
|
|
const budget = gateway.getBudget();
|
|
expect(budget.toolCallBudget.used).toBe(1);
|
|
});
|
|
|
|
it('should accumulate time on recordCall', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'r1', 100);
|
|
gateway.recordCall('Tool', { a: 2 }, 'r2', 200);
|
|
const budget = gateway.getBudget();
|
|
expect(budget.timeBudget.usedMs).toBe(300);
|
|
});
|
|
|
|
it('should accumulate tokens when provided', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'r1', 50, 100);
|
|
gateway.recordCall('Tool', { a: 2 }, 'r2', 50, 200);
|
|
const budget = gateway.getBudget();
|
|
expect(budget.tokenBudget.used).toBe(300);
|
|
});
|
|
|
|
it('should not alter token budget when tokenCount is omitted', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'r1', 50);
|
|
const budget = gateway.getBudget();
|
|
expect(budget.tokenBudget.used).toBe(0);
|
|
});
|
|
|
|
it('should accumulate storage from result size', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, { big: 'x'.repeat(500) }, 10);
|
|
const budget = gateway.getBudget();
|
|
expect(budget.storageBudget.usedBytes).toBeGreaterThan(500);
|
|
});
|
|
|
|
it('should estimate cost based on tokens', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'result', 100, 1000);
|
|
const budget = gateway.getBudget();
|
|
// 1000 tokens * $0.003/1K = $0.003
|
|
expect(budget.costBudget.usedUsd).toBeCloseTo(0.003, 4);
|
|
});
|
|
|
|
it('should reset budget to zero', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'r', 100, 500);
|
|
gateway.resetBudget();
|
|
|
|
const budget = gateway.getBudget();
|
|
expect(budget.tokenBudget.used).toBe(0);
|
|
expect(budget.toolCallBudget.used).toBe(0);
|
|
expect(budget.storageBudget.usedBytes).toBe(0);
|
|
expect(budget.timeBudget.usedMs).toBe(0);
|
|
expect(budget.costBudget.usedUsd).toBe(0);
|
|
});
|
|
|
|
it('should preserve limits after reset', () => {
|
|
gateway.recordCall('Tool', { a: 1 }, 'r', 100, 500);
|
|
gateway.resetBudget();
|
|
|
|
const budget = gateway.getBudget();
|
|
expect(budget.tokenBudget.limit).toBe(1000);
|
|
expect(budget.toolCallBudget.limit).toBe(50);
|
|
});
|
|
|
|
it('should block after budget is exhausted through metering', () => {
|
|
const tightGateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
toolCallBudget: { used: 0, limit: 2 },
|
|
}),
|
|
});
|
|
|
|
tightGateway.recordCall('T', { a: 1 }, 'r1', 10);
|
|
tightGateway.recordCall('T', { a: 2 }, 'r2', 10);
|
|
tightGateway.recordCall('T', { a: 3 }, 'r3', 10);
|
|
|
|
const decision = tightGateway.evaluate('T', { a: 4 });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// EnforcementGates Integration Tests
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - EnforcementGates Integration', () => {
|
|
it('should block destructive commands through context', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
|
|
const decision = gateway.evaluate(
|
|
'Bash',
|
|
{ command: 'rm -rf /' },
|
|
{ command: 'rm -rf /' },
|
|
);
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toContain('enforcement');
|
|
});
|
|
|
|
it('should block tools not in allowlist', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
gateConfig: {
|
|
toolAllowlist: true,
|
|
allowedTools: ['Read', 'Write'],
|
|
},
|
|
});
|
|
|
|
const decision = gateway.evaluate('Bash', { command: 'ls' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toContain('enforcement:tool-allowlist');
|
|
});
|
|
|
|
it('should allow tools in the allowlist', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
gateConfig: {
|
|
toolAllowlist: true,
|
|
allowedTools: ['Read', 'Write'],
|
|
},
|
|
});
|
|
|
|
const decision = gateway.evaluate('Read', { path: '/tmp/file.txt' });
|
|
expect(decision.allowed).toBe(true);
|
|
});
|
|
|
|
it('should detect secrets in tool params', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
|
|
const decision = gateway.evaluate('Write', {
|
|
content: 'api_key = "sk-supersecretkey1234567890abcdef"',
|
|
});
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toContain('enforcement:secrets');
|
|
});
|
|
|
|
it('should allow clean tool params', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
|
|
const decision = gateway.evaluate('Write', {
|
|
content: 'const greeting = "hello world"',
|
|
});
|
|
expect(decision.allowed).toBe(true);
|
|
});
|
|
|
|
it('should run schema validation before gates', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
schemas: [makeSchema()],
|
|
});
|
|
|
|
// Missing required param should fail at schema, not gates
|
|
const decision = gateway.evaluate('TestTool', {});
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('schema-validation');
|
|
});
|
|
|
|
it('should run budget check before gates', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
toolCallBudget: { used: 100, limit: 50 },
|
|
}),
|
|
gateConfig: {
|
|
toolAllowlist: true,
|
|
allowedTools: ['Read'],
|
|
},
|
|
});
|
|
|
|
// Budget exceeded should fail before tool allowlist check
|
|
const decision = gateway.evaluate('Bash', { command: 'ls' });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.gate).toBe('budget');
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Factory Function Tests
|
|
// ============================================================================
|
|
|
|
describe('createToolGateway', () => {
|
|
it('should create a gateway with default config', () => {
|
|
const gateway = createToolGateway();
|
|
expect(gateway).toBeInstanceOf(DeterministicToolGateway);
|
|
});
|
|
|
|
it('should create a gateway with custom config', () => {
|
|
const gateway = createToolGateway({
|
|
schemas: [makeSchema()],
|
|
budget: makeBudget(),
|
|
idempotencyTtlMs: 10_000,
|
|
});
|
|
expect(gateway).toBeInstanceOf(DeterministicToolGateway);
|
|
|
|
const budget = gateway.getBudget();
|
|
expect(budget.tokenBudget.limit).toBe(1000);
|
|
});
|
|
|
|
it('should return a fully functional gateway', () => {
|
|
const gateway = createToolGateway({
|
|
schemas: [makeSchema()],
|
|
});
|
|
|
|
const decision = gateway.evaluate('TestTool', { input: 'valid' });
|
|
expect(decision.allowed).toBe(true);
|
|
expect(decision.idempotencyHit).toBe(false);
|
|
expect(decision.gate).toBe('none');
|
|
});
|
|
});
|
|
|
|
// ============================================================================
|
|
// Edge Cases
|
|
// ============================================================================
|
|
|
|
describe('DeterministicToolGateway - Edge Cases', () => {
|
|
it('should handle empty params', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
const decision = gateway.evaluate('Tool', {});
|
|
expect(decision.allowed).toBe(true);
|
|
});
|
|
|
|
it('should handle null-like param values', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
const decision = gateway.evaluate('Tool', { a: null, b: undefined });
|
|
expect(decision.allowed).toBe(true);
|
|
});
|
|
|
|
it('should produce consistent keys with undefined values', () => {
|
|
const gateway = new DeterministicToolGateway();
|
|
// JSON.stringify drops undefined values, so these should match
|
|
const key1 = gateway.getIdempotencyKey('Tool', { a: 1 });
|
|
const key2 = gateway.getIdempotencyKey('Tool', { a: 1, b: undefined });
|
|
expect(key1).toBe(key2);
|
|
});
|
|
|
|
it('should return budget remaining on allow', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget(),
|
|
});
|
|
|
|
const decision = gateway.evaluate('Tool', { x: 1 });
|
|
expect(decision.allowed).toBe(true);
|
|
expect(decision.budgetRemaining).toBeDefined();
|
|
expect(decision.budgetRemaining!.tokenBudget.limit).toBe(1000);
|
|
});
|
|
|
|
it('should return budget remaining on budget rejection', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget({
|
|
toolCallBudget: { used: 100, limit: 50 },
|
|
}),
|
|
});
|
|
|
|
const decision = gateway.evaluate('Tool', { x: 1 });
|
|
expect(decision.allowed).toBe(false);
|
|
expect(decision.budgetRemaining).toBeDefined();
|
|
expect(decision.budgetRemaining!.toolCallBudget.used).toBe(100);
|
|
});
|
|
|
|
it('should not mutate the budget through getBudget', () => {
|
|
const gateway = new DeterministicToolGateway({
|
|
budget: makeBudget(),
|
|
});
|
|
|
|
const budget1 = gateway.getBudget();
|
|
budget1.tokenBudget.used = 999;
|
|
|
|
const budget2 = gateway.getBudget();
|
|
expect(budget2.tokenBudget.used).toBe(0);
|
|
});
|
|
});
|