Spaces:
Sleeping
Sleeping
| /** | |
| * Frontend Service Tests für SAAP API Client | |
| * Tests für API-Kommunikation mit dem Backend | |
| */ | |
| import { describe, it, expect, beforeEach, vi } from 'vitest' | |
| import axios from 'axios' | |
| // Create mock axios instance | |
| const mockAxiosInstance = { | |
| get: vi.fn(), | |
| post: vi.fn(), | |
| put: vi.fn(), | |
| delete: vi.fn(), | |
| interceptors: { | |
| request: { use: vi.fn(), eject: vi.fn() }, | |
| response: { use: vi.fn(), eject: vi.fn() } | |
| } | |
| } | |
| // Mock axios completely | |
| vi.mock('axios', () => { | |
| return { | |
| default: { | |
| create: vi.fn(() => mockAxiosInstance), | |
| get: vi.fn(), | |
| post: vi.fn() | |
| } | |
| } | |
| }) | |
| // Import after mocking | |
| const { saapApi } = await import('../src/services/saapApi.js') | |
| describe('SAAP API Service', () => { | |
| beforeEach(() => { | |
| // Clear all mocks before each test | |
| vi.clearAllMocks() | |
| }) | |
| describe('getAgents', () => { | |
| it('should fetch all agents successfully', async () => { | |
| const mockAgents = { | |
| agents: [ | |
| { id: 'jane_alesi', name: 'Jane Alesi', status: 'active' }, | |
| { id: 'john_alesi', name: 'John Alesi', status: 'inactive' } | |
| ], | |
| total: 2 | |
| } | |
| mockAxiosInstance.get.mockResolvedValue({ data: mockAgents }) | |
| const result = await saapApi.getAgents() | |
| expect(mockAxiosInstance.get).toHaveBeenCalledWith('/agents') | |
| expect(result).toEqual(mockAgents) | |
| expect(result.agents).toHaveLength(2) | |
| }) | |
| it('should handle API errors gracefully', async () => { | |
| mockAxiosInstance.get.mockRejectedValue(new Error('Network error')) | |
| await expect(saapApi.getAgents()).rejects.toThrow('Network error') | |
| }) | |
| }) | |
| describe('getAgent', () => { | |
| it('should fetch specific agent by ID', async () => { | |
| const mockAgent = { | |
| id: 'jane_alesi', | |
| name: 'Jane Alesi', | |
| type: 'coordinator', | |
| status: 'active' | |
| } | |
| mockAxiosInstance.get.mockResolvedValue({ data: mockAgent }) | |
| const result = await saapApi.getAgent('jane_alesi') | |
| expect(mockAxiosInstance.get).toHaveBeenCalledWith('/agents/jane_alesi') | |
| expect(result).toEqual(mockAgent) | |
| }) | |
| it('should handle 404 for non-existent agent', async () => { | |
| mockAxiosInstance.get.mockRejectedValue({ | |
| response: { status: 404, data: { detail: 'Agent not found' } } | |
| }) | |
| await expect(saapApi.getAgent('nonexistent')).rejects.toThrow() | |
| }) | |
| }) | |
| describe('createAgent', () => { | |
| it('should create new agent successfully', async () => { | |
| const newAgent = { | |
| id: 'test_agent', | |
| name: 'Test Agent', | |
| type: 'developer', | |
| description: 'Test agent' | |
| } | |
| const mockResponse = { | |
| success: true, | |
| agent: { ...newAgent, status: 'inactive' } | |
| } | |
| mockAxiosInstance.post.mockResolvedValue({ data: mockResponse }) | |
| const result = await saapApi.createAgent(newAgent) | |
| expect(mockAxiosInstance.post).toHaveBeenCalledWith('/agents', newAgent) | |
| expect(result.success).toBe(true) | |
| expect(result.agent.id).toBe('test_agent') | |
| }) | |
| it('should handle validation errors', async () => { | |
| mockAxiosInstance.post.mockRejectedValue({ | |
| response: { status: 422, data: { detail: 'Validation error' } } | |
| }) | |
| await expect(saapApi.createAgent({})).rejects.toThrow() | |
| }) | |
| }) | |
| describe('startAgent', () => { | |
| it('should start agent successfully', async () => { | |
| const mockResponse = { | |
| success: true, | |
| message: 'Agent started', | |
| status: 'active' | |
| } | |
| mockAxiosInstance.post.mockResolvedValue({ data: mockResponse }) | |
| const result = await saapApi.startAgent('jane_alesi') | |
| expect(mockAxiosInstance.post).toHaveBeenCalledWith('/agents/jane_alesi/start') | |
| expect(result.success).toBe(true) | |
| }) | |
| }) | |
| describe('stopAgent', () => { | |
| it('should stop agent successfully', async () => { | |
| const mockResponse = { | |
| success: true, | |
| message: 'Agent stopped', | |
| status: 'inactive' | |
| } | |
| mockAxiosInstance.post.mockResolvedValue({ data: mockResponse }) | |
| const result = await saapApi.stopAgent('jane_alesi') | |
| expect(mockAxiosInstance.post).toHaveBeenCalledWith('/agents/jane_alesi/stop') | |
| expect(result.success).toBe(true) | |
| }) | |
| }) | |
| describe('chatWithAgent', () => { | |
| it('should send chat message to agent', async () => { | |
| const mockResponse = { | |
| success: true, | |
| response: { | |
| content: 'Hello! How can I help you?', | |
| provider: 'colossus' | |
| } | |
| } | |
| mockAxiosInstance.post.mockResolvedValue({ data: mockResponse }) | |
| const result = await saapApi.chatWithAgent('jane_alesi', 'Hello') | |
| expect(mockAxiosInstance.post).toHaveBeenCalledWith( | |
| '/agents/jane_alesi/chat', | |
| { message: 'Hello' } | |
| ) | |
| expect(result.success).toBe(true) | |
| expect(result.response.content).toBeDefined() | |
| }) | |
| it('should handle empty message', async () => { | |
| mockAxiosInstance.post.mockRejectedValue({ | |
| response: { status: 400, data: { detail: 'Message required' } } | |
| }) | |
| await expect(saapApi.chatWithAgent('jane_alesi', '')).rejects.toThrow() | |
| }) | |
| }) | |
| describe('getAgentTemplates', () => { | |
| it('should fetch all agent templates', async () => { | |
| const mockTemplates = { | |
| templates: { | |
| jane_alesi: { id: 'jane_alesi', name: 'Jane Alesi' }, | |
| john_alesi: { id: 'john_alesi', name: 'John Alesi' } | |
| }, | |
| count: 2 | |
| } | |
| mockAxiosInstance.get.mockResolvedValue({ data: mockTemplates }) | |
| const result = await saapApi.getAgentTemplates() | |
| expect(mockAxiosInstance.get).toHaveBeenCalledWith('/templates/agents') | |
| expect(result.count).toBe(2) | |
| expect(result.templates).toHaveProperty('jane_alesi') | |
| }) | |
| }) | |
| describe('multiAgentChat', () => { | |
| it('should send multi-agent chat request', async () => { | |
| const mockResponse = { | |
| success: true, | |
| coordinator: 'jane_alesi', | |
| specialist_agent: 'john_alesi', | |
| delegation_used: true, | |
| response: { | |
| content: 'Coordinated response' | |
| } | |
| } | |
| mockAxiosInstance.post.mockResolvedValue({ data: mockResponse }) | |
| const result = await saapApi.multiAgentChat('Create a website') | |
| // Verify the call was made to the correct endpoint | |
| expect(mockAxiosInstance.post).toHaveBeenCalledWith( | |
| '/multi-agent/chat', | |
| expect.objectContaining({ | |
| preferred_agent: null, | |
| task_priority: 'normal' | |
| }), | |
| expect.objectContaining({ | |
| timeout: 240000 | |
| }) | |
| ) | |
| expect(result.success).toBe(true) | |
| expect(result.delegation_used).toBe(true) | |
| }) | |
| }) | |
| }) | |
| describe('SAAP API Error Handling', () => { | |
| it('should handle network errors', async () => { | |
| mockAxiosInstance.get.mockRejectedValue(new Error('Network Error')) | |
| await expect(saapApi.getAgents()).rejects.toThrow('Network Error') | |
| }) | |
| it('should handle timeout errors', async () => { | |
| mockAxiosInstance.get.mockRejectedValue({ code: 'ECONNABORTED' }) | |
| await expect(saapApi.getAgents()).rejects.toThrow() | |
| }) | |
| it('should handle server errors (500)', async () => { | |
| mockAxiosInstance.get.mockRejectedValue({ | |
| response: { status: 500, data: { detail: 'Internal Server Error' } } | |
| }) | |
| await expect(saapApi.getAgents()).rejects.toThrow() | |
| }) | |
| }) | |