task-master-neo-sdlc
Version:
Enhanced task management system with Neo SDLC agents and MCP tools for comprehensive, AI-driven software development lifecycle management.
148 lines (122 loc) • 6.2 kB
JavaScript
import { FrontendDevelopmentAgent } from '../frontend-developer';
import { KnowledgeGraph } from '../../knowledge-graph'; // Adjust path
import { AgentWorkflowSystem } from '../../agent-workflow'; // Adjust path
import { DesignSystemManager } from '../../design-system-manager'; // Adjust path
// Mocks
const mockKnowledgeGraph = {
addNode: jest.fn(),
findNodes: jest.fn(),
updateContext: jest.fn(),
addEdge: jest.fn()
};
const mockWorkflow = {};
// Mock DesignSystemManager methods used by the agent
const mockDesignSystemManager = {
addComponent: jest.fn(),
getComponent: jest.fn(),
updateComponent: jest.fn()
};
describe('FrontendDevelopmentAgent', () => {
let agent;
beforeEach(() => {
jest.clearAllMocks();
agent = new FrontendDevelopmentAgent(mockKnowledgeGraph, mockWorkflow, mockDesignSystemManager);
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
console.log.mockRestore();
console.error.mockRestore();
});
it('should create a component using DesignSystemManager', async () => {
const name = 'Test Button';
const description = 'A simple button';
const type = 'atom';
const props = [{ name: 'label', type: 'string', required: true, description: 'Button text' }];
const expectedComponentId = 'atom:test-button';
const mockReturnedComponent = { id: expectedComponentId, name, type, /*...*/ };
mockDesignSystemManager.addComponent.mockResolvedValue(mockReturnedComponent);
const component = await agent.createComponent(name, description, type, props);
expect(component).toBe(mockReturnedComponent);
expect(mockDesignSystemManager.addComponent).toHaveBeenCalledWith(expect.objectContaining({
id: expectedComponentId,
name,
description,
type,
props,
template: expect.any(String),
documentation: expect.any(String),
tests: expect.any(Array)
}));
});
it('should handle error during component creation', async () => {
const name = 'Error Button';
const error = new Error('Validation failed');
mockDesignSystemManager.addComponent.mockRejectedValue(error);
await expect(agent.createComponent(name, '', 'atom', [])).rejects.toThrow('Validation failed');
expect(console.error).toHaveBeenCalled();
});
it('should implement UI logic by updating component via DesignSystemManager', async () => {
const componentId = 'molecule:login-form';
const logicDescription = 'Handle form submission';
const initialComponent = { id: componentId, name: 'Login Form', template: '<form></form>' };
const mockUpdatedComponent = { ...initialComponent, template: initialComponent.template + '\n<script>...</script>' };
mockDesignSystemManager.getComponent.mockResolvedValue(initialComponent);
mockDesignSystemManager.updateComponent.mockResolvedValue(mockUpdatedComponent);
const updatedComponent = await agent.implementUILogic(componentId, logicDescription);
expect(updatedComponent).toBe(mockUpdatedComponent);
expect(mockDesignSystemManager.getComponent).toHaveBeenCalledWith(componentId);
expect(mockDesignSystemManager.updateComponent).toHaveBeenCalledWith(componentId, {
template: expect.stringContaining(logicDescription)
});
});
it('should throw error if component not found when implementing logic', async () => {
const componentId = 'nonexistent:component';
mockDesignSystemManager.getComponent.mockResolvedValue(null);
await expect(agent.implementUILogic(componentId, 'some logic')).rejects.toThrow(
`Component ${componentId} not found.`
);
});
it('should connect a component to an API endpoint', async () => {
const componentId = 'organism:user-profile';
const apiEndpointNodeId = 'api_endpoint:/api/users/123';
const dataMapping = { userId: 'props.userId' };
const initialComponent = { id: componentId, name: 'User Profile', template: '<div>Profile</div>' };
const mockApiNode = { id: apiEndpointNodeId, type: 'api_endpoint', data: { url: '/api/users/123' } };
mockDesignSystemManager.getComponent.mockResolvedValue(initialComponent);
mockKnowledgeGraph.findNodes.mockResolvedValue([mockApiNode]);
mockDesignSystemManager.updateComponent.mockResolvedValue(undefined); // Assuming update returns void or similar
mockKnowledgeGraph.addEdge.mockResolvedValue(undefined);
await agent.connectComponentToApi(componentId, apiEndpointNodeId, dataMapping);
expect(mockDesignSystemManager.getComponent).toHaveBeenCalledWith(componentId);
expect(mockKnowledgeGraph.findNodes).toHaveBeenCalledWith({ id: apiEndpointNodeId, type: 'api_endpoint' });
expect(mockDesignSystemManager.updateComponent).toHaveBeenCalledWith(componentId, {
template: expect.stringContaining('fetch('/api/users/123')')
});
expect(mockKnowledgeGraph.addEdge).toHaveBeenCalledWith({
source: `component:${componentId}`,
target: apiEndpointNodeId,
relationship: 'consumes_api'
});
});
it('should throw error if component not found when connecting to API', async () => {
const componentId = 'not:found';
const apiEndpointNodeId = 'api:valid';
mockDesignSystemManager.getComponent.mockResolvedValue(null);
// Ensure KG mock doesn't prevent the check
mockKnowledgeGraph.findNodes.mockResolvedValue([{ id: apiEndpointNodeId, type: 'api_endpoint' }]);
await expect(agent.connectComponentToApi(componentId, apiEndpointNodeId, {})).rejects.toThrow(
`Component ${componentId} not found.`
);
});
it('should throw error if API endpoint not found when connecting', async () => {
const componentId = 'component:ok';
const apiEndpointNodeId = 'api:notfound';
const initialComponent = { id: componentId, name: 'OK Comp', template: '' };
mockDesignSystemManager.getComponent.mockResolvedValue(initialComponent);
mockKnowledgeGraph.findNodes.mockResolvedValue([]); // API not found
await expect(agent.connectComponentToApi(componentId, apiEndpointNodeId, {})).rejects.toThrow(
`API Endpoint ${apiEndpointNodeId} not found.`
);
});
});