Spaces:
Sleeping
Sleeping
File size: 7,432 Bytes
4343907 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
/**
* 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()
})
})
|