mirror of
https://github.com/proffesor-for-testing/agentic-qe.git
synced 2026-09-19 08:45:47 +08:00
2b71ae4eb8
## 🔒 Security Fixes ### eval() Removal - Critical Security Vulnerability Fixed - **File**: `src/agents/TestDataArchitectAgent.ts` - **Impact**: Eliminated arbitrary code execution risk - **Solution**: Implemented safe expression evaluator with parser-based approach - **Supports**: - Comparison operators: `===`, `!==`, `==`, `!=`, `>`, `<`, `>=`, `<=` - Logical operators: `&&`, `||` - Boolean values: `true`, `false` - Number and string comparisons - **Lines**: 102 lines added (safeEvaluateExpression + parseValue methods) ## ✨ UX Enhancements ### CLAUDE.md Append Strategy - User-Requested Feature - **File**: `src/cli/commands/init.ts` - **User Feedback**: "AQE instructions should append by default, not prepend" - **Interactive Mode**: Prompts user to choose prepend or append - **--yes Mode**: Defaults to append (less disruptive to existing configs) - **Features**: - Auto-backup existing CLAUDE.md to CLAUDE.md.backup - Clear visual separator (---) between sections - User confirmation messages - **Lines**: 49 lines modified ### CLI Skills Count Fix - Accurate Display - **File**: `src/cli/commands/skills/index.ts` - **Before**: Showed "8/17" (hardcoded, incorrect) - **After**: Shows "34/34" (dynamic, accurate) - **Solution**: Added `getTotalQESkillsCount()` method using `QE_SKILLS.flatMap()` - **Future-proof**: Auto-updates when skills added - **Lines**: 11 lines modified ## 🎓 New Agent - Cherry-Picked from PR #22 ### CodeComplexityAnalyzerAgent Integration - **Author**: @mondweep (PR #22) - **Purpose**: Educational code complexity analysis and refactoring recommendations - **Files**: 7 files, 2,758 lines of code - **Capabilities**: - Cyclomatic complexity calculation - Cognitive complexity analysis - AI-powered refactoring suggestions - Quality scoring (0-100 scale) - Memory integration (`aqe/complexity/*`) - **Test Coverage**: 529 lines, 13+ test scenarios - **Documentation**: 754 lines (README + Claude agent definition) - **Examples**: 871 lines (3 interactive examples) ## 🔧 Code Quality Improvements ### Agent Enhancements - **BaseAgent.ts**: 136 lines modified - Improved property access patterns - Better encapsulation with public accessors - Enhanced lifecycle management - **TestGeneratorAgent.ts**: 377 lines added - Comprehensive null safety improvements - Enhanced error handling - Better context validation - **QualityGateAgent.ts**: 64 lines modified - Improved context handling - Better validation logic ### MCP Handler Improvements - **test-generate.ts**: 33 lines modified (better parameter handling) - **quality-analyze.ts**: 28 lines modified (context fix) - **regression-risk-analyze.ts**: 85 lines modified (parameter aliasing fix) ### Infrastructure Additions - **AgentLifecycleManager.ts**: 274 new lines (lifecycle coordination) - **SwarmMemoryManager.ts**: 3 lines modified (improved coordination) - **AgentRegistry.ts**: 20 lines added (better agent registration) ## 🧪 Testing Enhancements ### New Integration Tests (5 Test Suites) - **fleet-management.integration.test.ts**: 491 lines - **parameter-validation.integration.test.ts**: 606 lines - **quality-analyze.integration.test.ts**: 393 lines - **regression-risk.integration.test.ts**: 505 lines - **test-execution.integration.test.ts**: 536 lines - **test-harness.ts**: 289 lines (shared test utilities) - **Total**: 2,820 lines of integration test coverage ### Enhanced Unit Tests - **BaseAgent.enhanced.test.ts**: 909 lines (comprehensive BaseAgent testing) - **TestGeneratorAgent.null-safety.test.ts**: 1,511 lines (null safety validation) - **Total**: 2,420 lines of new unit test coverage ### Test Improvements - **PredictionTools.test.ts**: 118 lines modified - **Agent.test.ts**: 11 lines modified ## 📚 Documentation Expansion (27,439 insertions) ### Release Documentation (7 documents) - **RELEASE-1.3.6-PREPARATION.md**: 443 lines (complete release checklist) - **SECURITY-SCAN-RELEASE-1.3.6.md**: 422 lines (security validation) - **THREE-ISSUE-FIXES-SUMMARY.md**: 196 lines (user-reported fixes) - **CHERRY-PICK-PR22-SUMMARY.md**: 412 lines (integration details) - **FINAL-STATUS-REPORT-2025-10-30.md**: 403 lines (status summary) - **FINAL-MCP-VERIFICATION-SUMMARY.md**: 338 lines (MCP validation) - **MCP-PRODUCTION-READINESS-REPORT.md**: 318 lines (production readiness) ### Technical Documentation (7 documents) - **MCP-TOOLS-REFERENCE.md**: 1,555 lines (complete MCP API reference) - **MCP-TOOLS-USER-GUIDE.md**: 626 lines (user guide) - **MCP-TOOLS-AUTO-GENERATED.md**: 934 lines (auto-generated docs) - **MCP-TOOLS-MIGRATION.md**: 437 lines (migration guide) - **complexity-mitigation-plan.md**: 537 lines (code quality roadmap) - **swarm-execution-report.md**: 985 lines (execution analysis) - **HONEST-IMPLEMENTATION-ANALYSIS.md**: 62 lines (transparency report) ### Fix Documentation (7 documents) - **MCP-TOOLS-COMPLETE-FIX-REPORT.md**: 619 lines (comprehensive fix report) - **three-issue-fixes.md**: 345 lines (user-reported fixes) - **quality-analyze-context-fix.md**: 195 lines - **regression-risk-parameter-aliasing.md**: 303 lines - **test-generator-agent-null-safety-fixes.md**: 422 lines - **typescript-compilation-errors-fixed-2025-10-30.md**: 273 lines - **MCP-TOOLS-FIX-SUMMARY.md**: 272 lines ### Refactoring Plans (4 documents) - **swarm-memory-manager-refactoring-plan.md**: 796 lines - **base-agent-refactoring-plan.md**: 658 lines - **refactoring-summary.md**: 404 lines - **REFACTORING_STATUS.md**: 210 lines ### Testing Documentation (6 documents) - **mcp-tools-test-report.md**: 1,260 lines (comprehensive test report) - **testing-workflow.md**: 691 lines (MCP testing guide) - **TestGeneratorAgent-test-plan.md**: 318 lines (test plan) - **coverage-analysis-2025-10-30.md**: 661 lines (coverage report) - **regression-validation-report-2025-10-30.md**: 450 lines (regression validation) - **README.md** (tests/integration/mcp): 277 lines (integration test guide) ### CI/CD Documentation (3 documents) - **mcp-cicd-pipeline.md**: 402 lines (pipeline architecture) - **mcp-pipeline-setup-summary.md**: 343 lines (setup guide) - **mcp-cicd-implementation-notes.md**: 280 lines (implementation notes) ### MCP Verification (2 documents) - **MCP-VERIFICATION-SUMMARY.md**: 286 lines - **PR-22-ANALYSIS.md**: 373 lines ### Coverage Analysis (2 documents) - **coverage-analysis-2025-10-30.md**: 661 lines - **coverage-analysis-summary.json**: 417 lines (structured data) ## 🛠️ Scripts & Automation (5 Scripts, 2,117 Lines) ### New Utility Scripts - **validate-mcp-tools.js**: 280 lines (MCP tool validation) - **generate-mcp-report.js**: 252 lines (report generation) - **generate-mcp-docs.js**: 270 lines (documentation generation) - **verify-mcp-production-readiness.js**: 564 lines (production checks) - **test-issue-fixes.js**: 224 lines (fix verification) - **mcp-tools-summary.json**: 344 lines (structured summary) ## 📊 Examples & Guides ### MCP Integration Examples (2 Examples) - **basic-test-generation.js**: 200 lines (starter example) - **quality-analysis-pipeline.js**: 360 lines (advanced workflow) ## ⚙️ CI/CD Enhancements ### GitHub Actions Workflow - **.github/workflows/mcp-tools-test.yml**: 197 lines - Automated MCP tool testing - Multi-environment validation - Production readiness checks ### Git Hooks - **.husky/pre-commit**: 14 lines (pre-commit validation) ## 📦 Configuration Updates ### Version & Dependencies - **package.json**: Version bumped to 1.3.6 - **package-lock.json**: Dependency lock updated - **README.md**: Version references updated (27 lines modified) - **.claude/settings.json**: 4 lines modified ### CHANGELOG - **CHANGELOG.md**: 37 lines added - Complete v1.3.6 changelog entry - Security fixes documented - UX enhancements documented - Testing improvements documented ## 📈 Statistics Summary ### Code Changes - **78 files changed** - **27,439 insertions** (+) - **181 deletions** (-) - **Net change**: +27,258 lines ### Breakdown by Category - **Source Code**: ~1,200 lines (agents, handlers, infrastructure) - **Tests**: ~5,240 lines (integration + unit tests) - **Documentation**: ~18,000 lines (guides, reports, plans) - **Scripts**: ~2,117 lines (automation utilities) - **Examples**: ~560 lines (MCP examples) - **CI/CD**: ~211 lines (workflows + hooks) ### Quality Metrics - ✅ **TypeScript Compilation**: 0 errors (16 errors fixed) - ✅ **Security Vulnerabilities**: 0 critical (1 fixed) - ✅ **Integration Tests**: 5 new test suites, 2,820 lines - ✅ **Unit Tests**: 2,420 new lines - ✅ **Documentation**: 100% comprehensive coverage - ✅ **Backward Compatibility**: 100% maintained ## 🎯 User Impact ### Immediate Benefits 1. **Security**: Eliminates arbitrary code execution vulnerability 2. **UX**: Less disruptive CLAUDE.md initialization 3. **Accuracy**: Correct skills count display (34/34) 4. **Quality**: CodeComplexityAnalyzerAgent for refactoring guidance 5. **Reliability**: 16 TypeScript errors resolved ### Developer Experience 1. **Better Documentation**: 18,000+ lines of guides and references 2. **Better Testing**: 5,240 new lines of test coverage 3. **Better Tooling**: 2,117 lines of automation scripts 4. **Better Examples**: MCP integration examples 5. **Better CI/CD**: Automated testing and validation ## 🔄 Migration & Compatibility ### Breaking Changes - **NONE** - Fully backward compatible ### Deprecations - **NONE** ### New Features (Opt-in) - CodeComplexityAnalyzerAgent (available via Claude Code or MCP) - Interactive CLAUDE.md placement (interactive mode only) ## ✅ Verification ### Pre-Release Checks - ✅ TypeScript compilation: 0 errors - ✅ All three user-reported fixes verified - ✅ Integration tests passing - ✅ Documentation complete - ✅ CHANGELOG updated - ✅ Version numbers updated (package.json, README.md) - ✅ Security scan: No vulnerabilities - ✅ Backward compatibility verified ### Production Readiness - ✅ MCP tools validated (54 tools) - ✅ Agent registry verified (17 agents) - ✅ Skills library verified (34 skills) - ✅ CI/CD pipeline configured - ✅ Pre-commit hooks installed ## 🙏 Acknowledgments - **@mondweep**: CodeComplexityAnalyzerAgent contribution (PR #22) - **User Feedback**: CLAUDE.md append strategy request - **User Feedback**: CLI skills count display bug report - **User Feedback**: eval() security concern ## 📝 Release Notes **Version**: 1.3.6 **Date**: 2025-10-30 **Type**: Security & Quality Patch Release **Status**: Production Ready **Upgrade Recommendation**: 🔴 **HIGH PRIORITY** - Security vulnerability fixed (eval() removal) - User experience improvements - Enhanced stability (16 TypeScript errors fixed) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
499 lines
16 KiB
TypeScript
499 lines
16 KiB
TypeScript
/**
|
|
* Agent Unit Tests - Comprehensive TDD Implementation
|
|
* Testing the base Agent class with full coverage including edge cases and error scenarios
|
|
*/
|
|
|
|
import { Agent, AgentStatus, AgentCapability, AgentMetrics } from '@core/Agent';
|
|
import { Task, TaskStatus } from '@core/Task';
|
|
import { EventBus } from '@core/EventBus';
|
|
import { Logger } from '@utils/Logger';
|
|
|
|
// Mock implementations
|
|
jest.mock('@utils/Logger'); // Activate manual mock for Logger
|
|
jest.mock('@core/EventBus');
|
|
|
|
// Test Agent implementation
|
|
class TestAgent extends Agent {
|
|
private initializationError: Error | null = null;
|
|
private startError: Error | null = null;
|
|
private stopError: Error | null = null;
|
|
private taskExecutionError: Error | null = null;
|
|
private taskExecutionResult: any = { success: true };
|
|
|
|
setInitializationError(error: Error | null) {
|
|
this.initializationError = error;
|
|
}
|
|
|
|
setStartError(error: Error | null) {
|
|
this.startError = error;
|
|
}
|
|
|
|
setStopError(error: Error | null) {
|
|
this.stopError = error;
|
|
}
|
|
|
|
setTaskExecutionError(error: Error | null) {
|
|
this.taskExecutionError = error;
|
|
}
|
|
|
|
setTaskExecutionResult(result: any) {
|
|
this.taskExecutionResult = result;
|
|
}
|
|
|
|
protected async onInitialize(): Promise<void> {
|
|
if (this.initializationError) {
|
|
throw this.initializationError;
|
|
}
|
|
}
|
|
|
|
protected async onStart(): Promise<void> {
|
|
if (this.startError) {
|
|
throw this.startError;
|
|
}
|
|
}
|
|
|
|
protected async onStop(): Promise<void> {
|
|
if (this.stopError) {
|
|
throw this.stopError;
|
|
}
|
|
}
|
|
|
|
protected async executeTaskLogic(task: Task): Promise<any> {
|
|
// Add a small delay to ensure execution time > 0
|
|
await new Promise(resolve => setTimeout(resolve, 1));
|
|
|
|
if (this.taskExecutionError) {
|
|
throw this.taskExecutionError;
|
|
}
|
|
return this.taskExecutionResult;
|
|
}
|
|
|
|
protected async initializeCapabilities(): Promise<void> {
|
|
this.capabilities = [
|
|
{
|
|
name: 'test-capability',
|
|
version: '1.0.0',
|
|
description: 'Test capability for unit testing',
|
|
taskTypes: ['test-task', 'validation-task']
|
|
}
|
|
];
|
|
}
|
|
}
|
|
|
|
describe('Agent', () => {
|
|
let agent: TestAgent;
|
|
let mockEventBus: jest.Mocked<EventBus>;
|
|
let mockLogger: jest.Mocked<Logger>;
|
|
let mockTask: jest.Mocked<Task>;
|
|
|
|
beforeEach(() => {
|
|
// Reset Logger singleton to ensure clean state
|
|
if ((Logger as any).resetInstance) {
|
|
(Logger as any).resetInstance();
|
|
}
|
|
|
|
// Clear all mocks BEFORE getting logger instance
|
|
jest.clearAllMocks();
|
|
|
|
// Create mock EventBus
|
|
mockEventBus = {
|
|
emit: jest.fn(),
|
|
on: jest.fn(),
|
|
off: jest.fn(),
|
|
initialize: jest.fn().mockResolvedValue(undefined),
|
|
emitFleetEvent: jest.fn().mockResolvedValue('event-id-123'),
|
|
getEvent: jest.fn()
|
|
} as any;
|
|
|
|
// Get mocked Logger instance
|
|
mockLogger = Logger.getInstance() as jest.Mocked<Logger>;
|
|
|
|
// Create mock Task
|
|
mockTask = {
|
|
getId: jest.fn().mockReturnValue('task-123'),
|
|
getType: jest.fn().mockReturnValue('test-task'),
|
|
getStatus: jest.fn().mockReturnValue(TaskStatus.PENDING),
|
|
setStatus: jest.fn(),
|
|
setResult: jest.fn(),
|
|
setError: jest.fn(),
|
|
waitForCompletion: jest.fn().mockResolvedValue(undefined)
|
|
} as any;
|
|
|
|
// Create test agent
|
|
agent = new TestAgent('agent-123', 'test-agent', { testConfig: true }, mockEventBus);
|
|
});
|
|
|
|
describe('Initialization', () => {
|
|
it('should initialize agent successfully', async () => {
|
|
await agent.initialize();
|
|
|
|
expect(agent.getStatus()).toBe(AgentStatus.IDLE);
|
|
expect(agent.getId()).toBe('agent-123');
|
|
expect(agent.getType()).toBe('test-agent');
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Initializing agent test-agent (agent-123)');
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Agent test-agent (agent-123) initialized successfully');
|
|
});
|
|
|
|
it('should set agent status to ERROR on initialization failure', async () => {
|
|
const initError = new Error('Initialization failed');
|
|
agent.setInitializationError(initError);
|
|
|
|
await expect(agent.initialize()).rejects.toThrow('Initialization failed');
|
|
expect(agent.getStatus()).toBe(AgentStatus.ERROR);
|
|
expect(mockLogger.error).toHaveBeenCalledWith('Failed to initialize agent agent-123:', initError);
|
|
});
|
|
|
|
it('should emit initialization events', async () => {
|
|
const emitSpy = jest.spyOn(agent, 'emit');
|
|
|
|
await agent.initialize();
|
|
|
|
expect(emitSpy).toHaveBeenCalledWith('agent:initialized', {
|
|
agentId: 'agent-123',
|
|
type: 'test-agent'
|
|
});
|
|
});
|
|
|
|
it('should initialize capabilities correctly', async () => {
|
|
await agent.initialize();
|
|
|
|
const capabilities = agent.getCapabilities();
|
|
expect(capabilities).toHaveLength(1);
|
|
expect(capabilities[0]).toEqual({
|
|
name: 'test-capability',
|
|
version: '1.0.0',
|
|
description: 'Test capability for unit testing',
|
|
taskTypes: ['test-task', 'validation-task']
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Agent Lifecycle', () => {
|
|
beforeEach(async () => {
|
|
await agent.initialize();
|
|
});
|
|
|
|
it('should start agent successfully', async () => {
|
|
await agent.start();
|
|
|
|
expect(agent.getStatus()).toBe(AgentStatus.ACTIVE);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Agent test-agent (agent-123) started');
|
|
});
|
|
|
|
it('should reject start if not in IDLE status', async () => {
|
|
await agent.start();
|
|
|
|
await expect(agent.start()).rejects.toThrow('Agent agent-123 must be idle to start');
|
|
});
|
|
|
|
it('should handle start error', async () => {
|
|
const startError = new Error('Start failed');
|
|
agent.setStartError(startError);
|
|
|
|
await expect(agent.start()).rejects.toThrow('Start failed');
|
|
});
|
|
|
|
it('should stop agent successfully', async () => {
|
|
await agent.start();
|
|
await agent.stop();
|
|
|
|
expect(agent.getStatus()).toBe(AgentStatus.STOPPED);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Agent test-agent (agent-123) stopped');
|
|
});
|
|
|
|
it('should wait for current task completion before stopping', async () => {
|
|
await agent.start();
|
|
|
|
// Make task execution take longer so it's still running when stop() is called
|
|
let resolveTask: any;
|
|
const taskPromise = new Promise(resolve => {
|
|
resolveTask = resolve;
|
|
});
|
|
agent.setTaskExecutionResult(taskPromise);
|
|
|
|
// Assign a task (don't await so it's still running)
|
|
const assignPromise = agent.assignTask(mockTask);
|
|
|
|
// Give it a moment to start executing
|
|
await new Promise(resolve => setTimeout(resolve, 5));
|
|
|
|
// Mock task as running (which it should be)
|
|
mockTask.getStatus.mockReturnValue(TaskStatus.RUNNING);
|
|
|
|
// Now try to stop while task is running
|
|
const stopPromise = agent.stop();
|
|
|
|
// Should wait for completion
|
|
expect(mockTask.waitForCompletion).toHaveBeenCalled();
|
|
|
|
// Let the task complete
|
|
resolveTask({ success: true });
|
|
|
|
await Promise.all([stopPromise, assignPromise]);
|
|
});
|
|
|
|
it('should handle stop error gracefully', async () => {
|
|
await agent.start();
|
|
const stopError = new Error('Stop failed');
|
|
agent.setStopError(stopError);
|
|
|
|
await expect(agent.stop()).rejects.toThrow('Stop failed');
|
|
});
|
|
});
|
|
|
|
describe('Task Assignment and Execution', () => {
|
|
beforeEach(async () => {
|
|
await agent.initialize();
|
|
await agent.start();
|
|
});
|
|
|
|
it('should assign task successfully', async () => {
|
|
// Don't await - we want to check immediate status before async execution completes
|
|
const assignPromise = agent.assignTask(mockTask);
|
|
|
|
// Check status immediately (synchronously)
|
|
expect(agent.getStatus()).toBe(AgentStatus.BUSY);
|
|
expect(agent.getCurrentTask()).toBe(mockTask);
|
|
expect(mockLogger.info).toHaveBeenCalledWith('Task task-123 assigned to agent agent-123');
|
|
|
|
// Now wait for assignment to complete
|
|
await assignPromise;
|
|
});
|
|
|
|
it('should reject task assignment if agent not available', async () => {
|
|
await agent.stop();
|
|
|
|
await expect(agent.assignTask(mockTask)).rejects.toThrow(
|
|
'Agent agent-123 is not available for task assignment'
|
|
);
|
|
});
|
|
|
|
it('should reject task assignment if agent already has task', async () => {
|
|
// Assign first task (don't await so currentTask is still set)
|
|
const firstAssign = agent.assignTask(mockTask);
|
|
|
|
// Immediately try to assign another task
|
|
const anotherTask = { ...mockTask, getId: () => 'task-456' } as any;
|
|
await expect(agent.assignTask(anotherTask)).rejects.toThrow(
|
|
'Agent agent-123 already has an assigned task'
|
|
);
|
|
|
|
// Clean up first assignment
|
|
await firstAssign;
|
|
});
|
|
|
|
it('should reject unsupported task type', async () => {
|
|
mockTask.getType.mockReturnValue('unsupported-task');
|
|
|
|
await expect(agent.assignTask(mockTask)).rejects.toThrow(
|
|
'Agent agent-123 cannot handle task type unsupported-task'
|
|
);
|
|
});
|
|
|
|
it('should execute task successfully', async () => {
|
|
const taskResult = { data: 'test result', success: true };
|
|
agent.setTaskExecutionResult(taskResult);
|
|
|
|
await agent.assignTask(mockTask);
|
|
|
|
// Wait for task execution to complete
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
expect(mockTask.setStatus).toHaveBeenCalledWith(TaskStatus.RUNNING);
|
|
expect(mockTask.setResult).toHaveBeenCalledWith(taskResult);
|
|
expect(mockTask.setStatus).toHaveBeenCalledWith(TaskStatus.COMPLETED);
|
|
expect(agent.getStatus()).toBe(AgentStatus.ACTIVE);
|
|
expect(agent.getCurrentTask()).toBeNull();
|
|
});
|
|
|
|
it('should handle task execution failure', async () => {
|
|
const taskError = new Error('Task execution failed');
|
|
agent.setTaskExecutionError(taskError);
|
|
|
|
await agent.assignTask(mockTask);
|
|
|
|
// Wait for task execution to complete
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
expect(mockTask.setError).toHaveBeenCalledWith(taskError);
|
|
expect(mockTask.setStatus).toHaveBeenCalledWith(TaskStatus.FAILED);
|
|
expect(agent.getStatus()).toBe(AgentStatus.ACTIVE);
|
|
expect(agent.getCurrentTask()).toBeNull();
|
|
});
|
|
|
|
it('should emit task events during execution', async () => {
|
|
await agent.assignTask(mockTask);
|
|
|
|
// Wait for task execution to complete
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
expect(mockEventBus.emit).toHaveBeenCalledWith('task:started', {
|
|
agentId: 'agent-123',
|
|
taskId: 'task-123'
|
|
});
|
|
|
|
expect(mockEventBus.emit).toHaveBeenCalledWith('task:completed', expect.objectContaining({
|
|
agentId: 'agent-123',
|
|
taskId: 'task-123',
|
|
result: { success: true },
|
|
executionTime: expect.any(Number)
|
|
}));
|
|
});
|
|
});
|
|
|
|
describe('Capabilities and Task Type Handling', () => {
|
|
beforeEach(async () => {
|
|
await agent.initialize();
|
|
});
|
|
|
|
it('should correctly identify supported task types', () => {
|
|
expect(agent.canHandleTaskType('test-task')).toBe(true);
|
|
expect(agent.canHandleTaskType('validation-task')).toBe(true);
|
|
expect(agent.canHandleTaskType('unsupported-task')).toBe(false);
|
|
});
|
|
|
|
it('should return agent capabilities', () => {
|
|
const capabilities = agent.getCapabilities();
|
|
expect(capabilities).toHaveLength(1);
|
|
expect(capabilities[0].taskTypes).toContain('test-task');
|
|
expect(capabilities[0].taskTypes).toContain('validation-task');
|
|
});
|
|
});
|
|
|
|
describe('Metrics and Performance', () => {
|
|
beforeEach(async () => {
|
|
await agent.initialize();
|
|
await agent.start();
|
|
});
|
|
|
|
it('should track task completion metrics', async () => {
|
|
agent.setTaskExecutionResult({ success: true });
|
|
|
|
// Assign task and wait for it to start executing
|
|
const assignPromise = agent.assignTask(mockTask);
|
|
|
|
// Wait for task to complete execution (check that status returns to ACTIVE)
|
|
await assignPromise;
|
|
await new Promise(resolve => setTimeout(resolve, 50)); // Longer wait to ensure async completion
|
|
|
|
const metrics = agent.getMetrics();
|
|
expect(metrics.tasksCompleted).toBe(1);
|
|
expect(metrics.tasksFailured).toBe(0);
|
|
expect(metrics.averageExecutionTime).toBeGreaterThan(0);
|
|
expect(metrics.uptime).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('should track task failure metrics', async () => {
|
|
agent.setTaskExecutionError(new Error('Test failure'));
|
|
|
|
await agent.assignTask(mockTask);
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
const metrics = agent.getMetrics();
|
|
expect(metrics.tasksCompleted).toBe(0);
|
|
expect(metrics.tasksFailured).toBe(1);
|
|
});
|
|
|
|
it('should calculate average execution time correctly', async () => {
|
|
// Execute multiple tasks sequentially
|
|
for (let i = 0; i < 3; i++) {
|
|
const task = { ...mockTask, getId: () => `task-${i}`,
|
|
setStatus: jest.fn(),
|
|
setResult: jest.fn(),
|
|
setError: jest.fn(),
|
|
waitForCompletion: jest.fn().mockResolvedValue(undefined)
|
|
} as any;
|
|
await agent.assignTask(task);
|
|
// Wait longer to ensure each task completes before next
|
|
await new Promise(resolve => setTimeout(resolve, 50));
|
|
}
|
|
|
|
const metrics = agent.getMetrics();
|
|
expect(metrics.averageExecutionTime).toBeGreaterThan(0);
|
|
expect(metrics.tasksCompleted).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('Error Handling and Edge Cases', () => {
|
|
it('should handle agent errors and set ERROR status', async () => {
|
|
await agent.initialize();
|
|
|
|
const error = new Error('Agent malfunction');
|
|
agent.emit('error', error);
|
|
|
|
expect(agent.getStatus()).toBe(AgentStatus.ERROR);
|
|
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
'Agent agent-123 encountered an error:',
|
|
error
|
|
);
|
|
expect(mockEventBus.emit).toHaveBeenCalledWith('agent:error', {
|
|
agentId: 'agent-123',
|
|
error
|
|
});
|
|
});
|
|
|
|
it('should handle null task gracefully', async () => {
|
|
await agent.initialize();
|
|
await agent.start();
|
|
|
|
expect(() => agent.getCurrentTask()).not.toThrow();
|
|
expect(agent.getCurrentTask()).toBeNull();
|
|
});
|
|
|
|
it('should update last activity timestamp on task assignment', async () => {
|
|
await agent.initialize();
|
|
await agent.start();
|
|
|
|
const beforeTime = new Date();
|
|
await agent.assignTask(mockTask);
|
|
|
|
const metrics = agent.getMetrics();
|
|
expect(metrics.lastActivity).toBeInstanceOf(Date);
|
|
expect(metrics.lastActivity.getTime()).toBeGreaterThanOrEqual(beforeTime.getTime());
|
|
});
|
|
});
|
|
|
|
describe('Concurrent Task Handling', () => {
|
|
beforeEach(async () => {
|
|
await agent.initialize();
|
|
await agent.start();
|
|
});
|
|
|
|
it('should handle rapid task assignments correctly', async () => {
|
|
const tasks = Array.from({ length: 3 }, (_, i) => ({
|
|
...mockTask,
|
|
getId: () => `task-${i}`,
|
|
getType: () => 'test-task',
|
|
setStatus: jest.fn(),
|
|
setResult: jest.fn(),
|
|
setError: jest.fn(),
|
|
waitForCompletion: jest.fn().mockResolvedValue(undefined)
|
|
} as any));
|
|
|
|
// Try to assign multiple tasks rapidly (don't await first one)
|
|
const firstAssign = agent.assignTask(tasks[0]);
|
|
|
|
// Immediately try second assignment - should fail
|
|
await expect(agent.assignTask(tasks[1])).rejects.toThrow(
|
|
'Agent agent-123 already has an assigned task'
|
|
);
|
|
|
|
// Clean up
|
|
await firstAssign;
|
|
});
|
|
|
|
it('should be available for new tasks after completion', async () => {
|
|
await agent.assignTask(mockTask);
|
|
|
|
// Wait for task completion
|
|
await new Promise(resolve => setTimeout(resolve, 20));
|
|
|
|
expect(agent.getCurrentTask()).toBeNull();
|
|
expect(agent.getStatus()).toBe(AgentStatus.ACTIVE);
|
|
|
|
// Should be able to assign new task
|
|
const newTask = { ...mockTask, getId: () => 'task-456' } as any;
|
|
await expect(agent.assignTask(newTask)).resolves.not.toThrow();
|
|
});
|
|
});
|
|
}); |