task-master-neo-sdlc
Version:
Enhanced task management system with Neo SDLC agents and MCP tools for comprehensive, AI-driven software development lifecycle management.
169 lines (141 loc) • 6.78 kB
JavaScript
import { DatabaseManagerAgent } from '../database-manager';
import { KnowledgeGraph } from '../../knowledge-graph'; // Adjust path
import { AgentWorkflowSystem } from '../../agent-workflow'; // Adjust path
// Mocks
const mockKnowledgeGraph = {
addNode: jest.fn(),
findNodes: jest.fn(),
updateContext: jest.fn()
};
const mockWorkflow = {
triggerEvent: jest.fn()
};
describe('DatabaseManagerAgent', () => {
let agent;
beforeEach(() => {
jest.clearAllMocks();
agent = new DatabaseManagerAgent(mockKnowledgeGraph, mockWorkflow);
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
console.log.mockRestore();
console.warn.mockRestore();
});
it('should plan a schema migration', async () => {
const currentSchemaId = 'schema:v1';
const targetSchemaId = 'schema:v2';
const mockNodeCurrent = { id: currentSchemaId, type: 'db_schema', data: { version: 1 } };
const mockNodeTarget = { id: targetSchemaId, type: 'db_schema', data: { version: 2 } };
mockKnowledgeGraph.findNodes
.mockResolvedValueOnce([mockNodeCurrent]) // Finding current schema
.mockResolvedValueOnce([mockNodeTarget]); // Finding target schema
mockKnowledgeGraph.addNode.mockResolvedValue(undefined);
const plan = await agent.planSchemaMigration(currentSchemaId, targetSchemaId);
expect(plan).toBeDefined();
expect(plan.id).toMatch(/^migrationPlan_/);
expect(plan.fromSchema).toBe(currentSchemaId);
expect(plan.toSchema).toBe(targetSchemaId);
expect(plan.steps.length).toBeGreaterThan(0);
expect(plan.status).toBe('pending');
expect(mockKnowledgeGraph.findNodes).toHaveBeenCalledTimes(2);
expect(mockKnowledgeGraph.addNode).toHaveBeenCalledWith({
id: expect.stringMatching(/^dbMigrationPlan:/),
type: 'db_migration_plan',
data: plan
});
});
it('should throw error if schema not found during planning', async () => {
mockKnowledgeGraph.findNodes.mockResolvedValue([]); // Simulate not found
await expect(agent.planSchemaMigration('schema:nonexistent', 'schema:v2')).rejects.toThrow(
'Could not find current or target schema definition.'
);
});
it('should execute a migration plan', async () => {
const planId = 'migrationPlan_123';
const mockPlanNode = {
id: `dbMigrationPlan:${planId}`,
type: 'db_migration_plan',
data: { id: planId, status: 'pending' }
};
mockKnowledgeGraph.findNodes.mockResolvedValue([mockPlanNode]);
mockKnowledgeGraph.updateContext.mockResolvedValue(undefined);
await agent.executeMigration(planId);
expect(mockKnowledgeGraph.findNodes).toHaveBeenCalledWith({ id: `dbMigrationPlan:${planId}` });
expect(mockKnowledgeGraph.updateContext).toHaveBeenCalledTimes(2);
// Check status updates
expect(mockKnowledgeGraph.updateContext).toHaveBeenNthCalledWith(1, {
id: mockPlanNode.id,
data: expect.objectContaining({ status: 'executing' })
});
expect(mockKnowledgeGraph.updateContext).toHaveBeenNthCalledWith(2, {
id: mockPlanNode.id,
data: expect.objectContaining({ status: 'completed' })
});
// Optionally check workflow event
// expect(mockWorkflow.triggerEvent).toHaveBeenCalledWith('dbMigrationCompleted', { planId });
});
it('should throw error if plan not found during execution', async () => {
mockKnowledgeGraph.findNodes.mockResolvedValue([]); // Simulate not found
await expect(agent.executeMigration('nonexistent_plan')).rejects.toThrow(
'Migration plan nonexistent_plan not found.'
);
});
it('should generate seeding instructions', async () => {
const schemaId = 'schema:users_v1';
const options = { environment: 'test', count: 10 };
const mockSchemaNode = { id: schemaId, type: 'db_schema', data: {} };
mockKnowledgeGraph.findNodes.mockResolvedValue([mockSchemaNode]);
mockKnowledgeGraph.addNode.mockResolvedValue(undefined);
const instructions = await agent.generateSeedingInstructions(schemaId, options);
expect(instructions).toBeDefined();
expect(instructions.id).toMatch(/^seedInstructions_/);
expect(instructions.schemaId).toBe(schemaId);
expect(instructions.options).toEqual(options);
expect(instructions.script.length).toBeGreaterThan(0);
expect(instructions.status).toBe('generated');
expect(mockKnowledgeGraph.findNodes).toHaveBeenCalledWith({ id: schemaId, type: 'db_schema' });
expect(mockKnowledgeGraph.addNode).toHaveBeenCalledWith({
id: expect.stringMatching(/^dbSeedInstructions:/),
type: 'db_seed_instructions',
data: instructions
});
});
it('should throw error if schema not found during seeding generation', async () => {
mockKnowledgeGraph.findNodes.mockResolvedValue([]); // Simulate not found
await expect(agent.generateSeedingInstructions('nonexistent_schema')).rejects.toThrow(
'Schema nonexistent_schema not found.'
);
});
it('should analyze query performance', async () => {
const query = 'SELECT * FROM products';
const schemaId = 'schema:products_v3';
const mockSchemaNode = { id: schemaId, type: 'db_schema', data: {} };
mockKnowledgeGraph.findNodes.mockResolvedValue([mockSchemaNode]);
mockKnowledgeGraph.addNode.mockResolvedValue(undefined);
const analysis = await agent.analyzeQueryPerformance(query, schemaId);
expect(analysis).toBeDefined();
expect(analysis.id).toMatch(/^queryAnalysis_/);
expect(analysis.query).toBe(query);
expect(analysis.schemaId).toBe(schemaId);
expect(analysis.potentialIssues.length).toBeGreaterThan(0);
expect(analysis.recommendations.length).toBeGreaterThan(0);
expect(mockKnowledgeGraph.findNodes).toHaveBeenCalledWith({ id: schemaId, type: 'db_schema' });
expect(mockKnowledgeGraph.addNode).toHaveBeenCalledWith({
id: expect.stringMatching(/^dbQueryAnalysis:/),
type: 'db_query_analysis',
data: analysis
});
});
it('should analyze query performance even if schema not found (with warning)', async () => {
const query = 'SELECT id FROM orders WHERE status = \'pending\'';
const schemaId = 'schema:orders_nonexistent';
mockKnowledgeGraph.findNodes.mockResolvedValue([]); // Schema not found
mockKnowledgeGraph.addNode.mockResolvedValue(undefined);
const analysis = await agent.analyzeQueryPerformance(query, schemaId);
expect(console.warn).toHaveBeenCalledWith(`Schema ${schemaId} not found for query analysis context.`);
expect(analysis).toBeDefined();
expect(analysis.potentialIssues).toEqual([]); // No issues found by basic checks
expect(mockKnowledgeGraph.addNode).toHaveBeenCalled();
});
});