Files
Profa 2e8642030b release: v1.4.3 - Test Suite Stabilization & TypeScript Fixes
🎯 Test Suite Stabilization - 94.2% Pass Rate Achieved!

This release represents a major quality milestone with systematic test
stabilization that increased the unit test pass rate from 71.1% (619/870)
to 94.2% (903/959), exceeding the 90% goal set in GitHub issue #33.

Major Changes:
- Fixed all TypeScript compilation errors blocking release
- Created batched integration test script to prevent OOM crashes
- Documented integration test memory leak root cause (issue #35)
- Enhanced mock infrastructure for better test reliability
- Updated release verification policies in CLAUDE.md

TypeScript Fixes:
- Fixed TestGeneratorAgent AgentCapability structure (4 errors)
- Rewrote Database mock with proper type casting (36 methods)
- Rewrote LearningEngine mock with proper type casting
- Excluded __mocks__ from TypeScript compilation

Test Infrastructure:
- Created scripts/test-integration-batched.sh for safe execution
- Batches 46 integration tests (5 files at a time)
- Prevents workspace crashes in constrained environments
- Updated package.json test:integration to use batched script

Memory Leak Investigation:
- Root cause: MemoryManager setInterval leak (138 timers)
- Missing cleanup in 31 of 46 integration tests
- Database connection pool exhaustion (23 instances)
- Documented in GitHub issue #35 for v1.4.4 fix

Release Verification:
- aqe init: 19 agents, 34 skills, 8 commands ✅
- Databases: memory.db (221KB), patterns.db (155KB) ✅
- MCP interface: 54 tools functional ✅
- Fleet startup: 5 agents running successfully ✅

Files Changed:
- Modified: 40 files (tests, mocks, core agents)
- New: 2 files (batched script, LearningEngine mock)
- Version: 1.4.2 → 1.4.3

Closes #33

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-06 08:10:28 +00:00

415 lines
13 KiB
TypeScript

import { jest } from '@jest/globals';
import { FleetManager } from '@core/FleetManager';
import { Agent, AgentStatus } from '@core/Agent';
import { Task, TaskStatus } from '@core/Task';
import { Logger } from '@utils/Logger';
import { Database } from '@utils/Database';
import { EventBus } from '@core/EventBus';
import { QEAgentType, FleetConfig } from '@types/index';
// Mock the Database module before importing FleetManager
jest.mock('@utils/Database');
// Mock the agents module before importing FleetManager
jest.mock('../../src/agents', () => ({
createAgent: jest.fn()
}));
// Define AgentType enum for tests (mirrors QEAgentType)
enum AgentType {
UNIT_TEST_GENERATOR = 'unit-test-generator',
INTEGRATION_TEST_GENERATOR = 'integration-test-generator',
TEST_EXECUTOR = 'test-executor',
COVERAGE_ANALYZER = 'coverage-analyzer'
}
// Topology type constants (FleetConfig uses string literals, not enum)
const TopologyType = {
MESH: 'mesh' as const,
HIERARCHICAL: 'hierarchical' as const,
RING: 'ring' as const,
ADAPTIVE: 'adaptive' as const,
STAR: 'ring' as const // Using 'ring' as fallback since 'star' isn't in FleetConfig
}
// London School TDD: Mock all dependencies
const mockLogger = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
getInstance: jest.fn()
} as unknown as jest.Mocked<Logger>;
// Logger is already mocked via manual mock in src/utils/__mocks__/Logger.ts
// No need to mock it again - the manual mock handles getInstance() automatically
const mockDatabase = {
initialize: jest.fn().mockResolvedValue(undefined),
close: jest.fn().mockResolvedValue(undefined),
run: jest.fn().mockReturnValue({ changes: 1, lastInsertRowid: 1 }),
get: jest.fn().mockReturnValue(undefined),
all: jest.fn().mockReturnValue([]),
prepare: jest.fn().mockReturnValue({
run: jest.fn().mockReturnValue({ changes: 1, lastInsertRowid: 1 }),
get: jest.fn().mockReturnValue(undefined),
all: jest.fn().mockReturnValue([])
}),
transaction: jest.fn((callback) => callback()),
upsertFleet: jest.fn().mockResolvedValue(undefined),
upsertAgent: jest.fn().mockResolvedValue(undefined),
upsertTask: jest.fn().mockResolvedValue(undefined)
} as any;
const mockEventBus = {
initialize: jest.fn().mockResolvedValue(undefined),
emit: jest.fn(),
on: jest.fn(),
off: jest.fn(),
once: jest.fn(),
removeAllListeners: jest.fn()
} as unknown as jest.Mocked<EventBus>;
const mockMetricsCollector = {
recordMetric: jest.fn().mockResolvedValue(undefined),
getMetrics: jest.fn().mockReturnValue({}),
incrementCounter: jest.fn(),
recordHistogram: jest.fn(),
recordGauge: jest.fn()
};
const mockAgentFactory = {
createAgent: jest.fn().mockResolvedValue({
id: 'test-agent-123',
type: QEAgentType.TEST_GENERATOR,
status: AgentStatus.IDLE,
start: jest.fn().mockResolvedValue(true),
stop: jest.fn().mockResolvedValue(true)
})
};
const mockAgent = {
id: 'test-agent-123',
type: QEAgentType.TEST_GENERATOR,
status: AgentStatus.IDLE,
capabilities: ['jest', 'typescript'],
start: jest.fn().mockResolvedValue(true),
stop: jest.fn().mockResolvedValue(true),
execute: jest.fn().mockResolvedValue({ success: true }),
getStatus: jest.fn().mockReturnValue(AgentStatus.IDLE),
getId: jest.fn().mockReturnValue('test-agent-123'),
initialize: jest.fn().mockResolvedValue(undefined),
assignTask: jest.fn().mockResolvedValue(undefined),
canHandleTaskType: jest.fn().mockReturnValue(true)
} as unknown as jest.Mocked<Agent>;
describe('FleetManager - London School TDD', () => {
let fleetManager: FleetManager;
let mockConfig: FleetConfig;
let createAgentMock: jest.Mock;
beforeEach(async () => {
jest.clearAllMocks();
// Reset the mocks
(mockDatabase.initialize as jest.Mock).mockResolvedValue(undefined);
(mockEventBus.initialize as jest.Mock).mockResolvedValue(undefined);
// Create a basic fleet config
mockConfig = {
agents: [
{ type: 'test-generator', count: 2, config: {} },
{ type: 'test-executor', count: 1, config: {} }
],
topology: 'hierarchical' as const,
maxAgents: 8
};
// Setup createAgent mock
const agentsModule = await import('../../src/agents');
createAgentMock = agentsModule.createAgent as jest.Mock;
createAgentMock.mockResolvedValue(mockAgent);
// Use dependency injection instead of manually replacing properties
fleetManager = new FleetManager(mockConfig, {
database: mockDatabase as any,
eventBus: mockEventBus,
logger: mockLogger
});
});
describe('Fleet Initialization', () => {
it('should initialize fleet with database and event bus', async () => {
await fleetManager.initialize();
// Verify database initialization
expect(mockDatabase.initialize).toHaveBeenCalled();
// Verify event bus initialization
expect(mockEventBus.initialize).toHaveBeenCalled();
// Verify logging
expect(mockLogger.info).toHaveBeenCalledWith(
expect.stringContaining('Initializing Fleet Manager')
);
expect(mockLogger.info).toHaveBeenCalledWith(
'Fleet Manager initialized successfully'
);
});
it('should handle initialization failure gracefully', async () => {
const initError = new Error('Database connection failed');
(mockDatabase.initialize as jest.Mock).mockRejectedValueOnce(initError);
await expect(fleetManager.initialize())
.rejects.toThrow('Database connection failed');
expect(mockLogger.error).toHaveBeenCalledWith(
'Failed to initialize Fleet Manager:',
initError
);
});
it('should create initial agent pool from configuration', async () => {
await fleetManager.initialize();
// Verify createAgent was called for each agent in config
// (2 test-generators + 1 test-executor = 3 agents)
expect(createAgentMock).toHaveBeenCalledTimes(3);
// Verify successful initialization
expect(mockLogger.info).toHaveBeenCalledWith(
'Fleet Manager initialized successfully'
);
});
});
describe('Agent Spawning', () => {
beforeEach(async () => {
await fleetManager.initialize({
topology: 'hierarchical' as const,
maxAgents: 8,
strategy: 'balanced'
});
});
it('should spawn unit test generator agent with jest specialization', async () => {
const agentConfig = {
type: QEAgentType.TEST_GENERATOR,
specialization: 'jest',
capabilities: ['typescript', 'mocking', 'coverage'],
resources: { cpu: 2, memory: 1024 }
};
const agentId = await fleetManager.spawnAgent(agentConfig);
// Verify agent creation collaboration
expect(mockAgentFactory.createAgent).toHaveBeenCalledWith(
expect.objectContaining({
type: QEAgentType.TEST_GENERATOR,
specialization: 'jest'
})
);
// Verify agent startup sequence
expect(mockAgent.start).toHaveBeenCalled();
// Verify logging interactions
expect(mockLogger.info).toHaveBeenCalledWith(
`Spawned agent ${agentId} of type unit-test-generator`
);
// Verify metrics recording
expect(mockMetricsCollector.recordMetric).toHaveBeenCalledWith(
'agent.spawned',
expect.objectContaining({ type: 'unit-test-generator' })
);
expect(agentId).toBe('test-agent-123');
});
it('should spawn integration test generator with api specialization', async () => {
const agentConfig = {
type: AgentType.INTEGRATION_TEST_GENERATOR,
specialization: 'api',
capabilities: ['rest', 'graphql', 'contract-testing']
};
const agentId = await fleetManager.spawnAgent(agentConfig);
expect(mockAgentFactory.createAgent).toHaveBeenCalledWith(
expect.objectContaining({
type: AgentType.INTEGRATION_TEST_GENERATOR,
specialization: 'api'
})
);
expect(agentId).toBeTruthy();
});
it('should reject spawning when fleet is at capacity', async () => {
// Fill fleet to capacity
const promises = [];
for (let i = 0; i < 8; i++) {
promises.push(fleetManager.spawnAgent({
type: AgentType.UNIT_TEST_GENERATOR,
specialization: 'jest'
}));
}
await Promise.all(promises);
// Attempt to spawn one more
await expect(fleetManager.spawnAgent({
type: AgentType.UNIT_TEST_GENERATOR,
specialization: 'jest'
})).rejects.toThrow('Fleet at maximum capacity');
expect(mockLogger.warn).toHaveBeenCalledWith(
'Cannot spawn agent: fleet at maximum capacity (8)'
);
});
it('should handle agent startup failure gracefully', async () => {
mockAgent.start.mockRejectedValueOnce(new Error('Startup failed'));
await expect(fleetManager.spawnAgent({
type: AgentType.UNIT_TEST_GENERATOR,
specialization: 'jest'
})).rejects.toThrow('Agent startup failed');
expect(mockLogger.error).toHaveBeenCalledWith(
'Agent startup failed: Startup failed'
);
});
});
describe('Fleet Coordination', () => {
beforeEach(async () => {
await fleetManager.initialize({
topology: TopologyType.MESH,
maxAgents: 6,
strategy: 'adaptive'
});
});
it('should coordinate task distribution across available agents', async () => {
// Spawn multiple agents
await fleetManager.spawnAgent({ type: AgentType.UNIT_TEST_GENERATOR, specialization: 'jest' });
await fleetManager.spawnAgent({ type: AgentType.INTEGRATION_TEST_GENERATOR, specialization: 'api' });
const task = {
id: 'task-123',
type: 'test-generation',
target: 'user-service',
requirements: { coverage: 90 }
};
await fleetManager.distributeTask(task);
// Verify task distribution behavior
expect(mockLogger.info).toHaveBeenCalledWith(
`Distributing task ${task.id} across mesh topology`
);
expect(mockMetricsCollector.recordMetric).toHaveBeenCalledWith(
'task.distributed',
expect.objectContaining({ taskId: 'task-123' })
);
});
it('should handle agent failure during task execution', async () => {
await fleetManager.spawnAgent({ type: AgentType.UNIT_TEST_GENERATOR, specialization: 'jest' });
// Mock agent failure
mockAgent.execute.mockRejectedValueOnce(new Error('Agent crashed'));
const task = { id: 'task-456', type: 'test-execution' };
const result = await fleetManager.distributeTask(task);
expect(result.success).toBe(false);
expect(mockLogger.error).toHaveBeenCalledWith(
'Task execution failed on agent test-agent-123: Agent crashed'
);
});
});
describe('Fleet Status and Metrics', () => {
it('should provide comprehensive fleet status', () => {
const status = fleetManager.getFleetStatus();
expect(status).toEqual({
topology: expect.any(String),
totalAgents: expect.any(Number),
activeAgents: expect.any(Number),
idleAgents: expect.any(Number),
health: expect.any(String)
});
expect(mockMetricsCollector.getMetrics).toHaveBeenCalled();
});
it('should calculate fleet efficiency metrics', () => {
const efficiency = fleetManager.calculateEfficiency();
expect(efficiency).toEqual({
resourceUtilization: expect.any(Number),
taskCompletionRate: expect.any(Number),
coordinationOverhead: expect.any(Number),
qualityScore: expect.any(Number)
});
});
});
describe('Fleet Shutdown', () => {
it('should gracefully shutdown all agents', async () => {
await fleetManager.initialize({ topology: TopologyType.RING, maxAgents: 4 });
await fleetManager.spawnAgent({ type: AgentType.UNIT_TEST_GENERATOR, specialization: 'jest' });
await fleetManager.shutdown();
// Verify shutdown sequence
expect(mockAgent.stop).toHaveBeenCalled();
expect(mockLogger.info).toHaveBeenCalledWith('Fleet shutdown completed');
expect(mockMetricsCollector.recordMetric).toHaveBeenCalledWith(
'fleet.shutdown',
expect.objectContaining({ graceful: true })
);
});
it('should handle agent shutdown failures', async () => {
await fleetManager.initialize({ topology: TopologyType.STAR, maxAgents: 2 });
await fleetManager.spawnAgent({ type: AgentType.UNIT_TEST_GENERATOR, specialization: 'jest' });
mockAgent.stop.mockRejectedValueOnce(new Error('Shutdown timeout'));
await fleetManager.shutdown();
expect(mockLogger.warn).toHaveBeenCalledWith(
'Agent test-agent-123 failed to shutdown gracefully: Shutdown timeout'
);
});
});
});
// Contract tests for fleet manager interfaces
describe('FleetManager Contracts', () => {
it('should satisfy IFleetManager interface contract', () => {
const testConfig: FleetConfig = {
agents: [],
topology: 'mesh' as const,
maxAgents: 4
};
const fleetManager = new FleetManager(testConfig, {
logger: mockLogger,
database: mockDatabase as any,
eventBus: mockEventBus
});
// Verify interface compliance
expect(typeof fleetManager.initialize).toBe('function');
expect(typeof fleetManager.spawnAgent).toBe('function');
expect(typeof fleetManager.getStatus).toBe('function');
expect(typeof fleetManager.stop).toBe('function');
});
});