Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | #!/usr/bin/env node /** * OpenFang Backend API Connection Test Script * * Tests all API endpoints used by the ZCLAW desktop client against * the OpenFang Kernel backend. * * Usage: * node desktop/scripts/test-api-connection.mjs [options] * * Options: * --url=URL Base URL for OpenFang API (default: http://127.0.0.1:50051) * --verbose Show detailed output * --json Output results as JSON * --timeout=MS Request timeout in milliseconds (default: 5000) */ import { WebSocket } from 'ws'; // Configuration const DEFAULT_BASE_URL = 'http://127.0.0.1:50051'; const DEFAULT_TIMEOUT = 5000; // Parse command line arguments const args = process.argv.slice(2); const config = { baseUrl: DEFAULT_BASE_URL, verbose: false, json: false, timeout: DEFAULT_TIMEOUT, }; for (const arg of args) { if (arg.startsWith('--url=')) { config.baseUrl = arg.slice(6); } else if (arg === '--verbose' || arg === '-v') { config.verbose = true; } else if (arg === '--json') { config.json = true; } else if (arg.startsWith('--timeout=')) { config.timeout = parseInt(arg.slice(10), 10); } else if (arg === '--help' || arg === '-h') { console.log(` OpenFang API Connection Tester Usage: node test-api-connection.mjs [options] Options: --url=URL Base URL for OpenFang API (default: ${DEFAULT_BASE_URL}) --verbose Show detailed output including response bodies --json Output results as JSON for programmatic processing --timeout=MS Request timeout in milliseconds (default: ${DEFAULT_TIMEOUT}) --help, -h Show this help message `); process.exit(0); } } // Test result tracking const results = { timestamp: new Date().toISOString(), baseUrl: config.baseUrl, summary: { total: 0, passed: 0, failed: 0, skipped: 0, }, categories: {}, errors: [], }; /** * Make an HTTP request with timeout */ async function makeRequest(method, path, body = null, expectedStatus = null) { const url = `${config.baseUrl}${path}`; const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), config.timeout); const startTime = Date.now(); try { const fetchOptions = { method, signal: controller.signal, headers: { 'Content-Type': 'application/json', }, }; if (body && (method === 'POST' || method === 'PUT')) { fetchOptions.body = JSON.stringify(body); } const response = await fetch(url, fetchOptions); clearTimeout(timeoutId); const duration = Date.now() - startTime; let responseBody = null; try { responseBody = await response.json(); } catch { responseBody = await response.text().catch(() => null); } const statusMatch = expectedStatus ? response.status === expectedStatus : response.status < 500; return { success: statusMatch, status: response.status, statusText: response.statusText, duration, body: responseBody, error: null, }; } catch (error) { clearTimeout(timeoutId); const duration = Date.now() - startTime; return { success: false, status: 0, statusText: 'Network Error', duration, body: null, error: error.name === 'AbortError' ? 'Timeout' : error.message, }; } } /** * Test a single endpoint and record results */ async function testEndpoint(category, method, path, testOptions = {}) { const { body = null, expectedStatus = null, description = '' } = testOptions; results.summary.total++; if (!results.categories[category]) { results.categories[category] = { total: 0, passed: 0, failed: 0, tests: [] }; } results.categories[category].total++; const result = await makeRequest(method, path, body, expectedStatus); const testResult = { method, path, description, status: result.status, statusText: result.statusText, duration: result.duration, success: result.success, error: result.error, }; if (config.verbose && result.body) { testResult.responseBody = result.body; } results.categories[category].tests.push(testResult); if (result.success) { results.summary.passed++; results.categories[category].passed++; } else { results.summary.failed++; results.categories[category].failed++; if (result.error) { results.errors.push({ category, method, path, error: result.error, status: result.status, }); } } // Print progress if (!config.json) { const statusIcon = result.success ? '[PASS]' : '[FAIL]'; const statusColor = result.success ? '\x1b[32m' : '\x1b[31m'; const reset = '\x1b[0m'; const dim = '\x1b[2m'; console.log( `${statusColor}${statusIcon}${reset} ${method.padEnd(6)} ${path} ${dim}${result.status} ${result.duration}ms${reset}` ); if (config.verbose && result.body) { const bodyStr = JSON.stringify(result.body, null, 2); const indented = bodyStr.split('\n').join('\n '); console.log(` Response: ${indented}`); } if (!result.success && result.error) { console.log(` Error: ${result.error}`); } } return result; } /** * Test WebSocket connection */ async function testWebSocketConnection(url) { return new Promise((resolve) => { const startTime = Date.now(); let resolved = false; const ws = new WebSocket(url); const timeout = setTimeout(() => { if (!resolved) { resolved = true; ws.close(); resolve({ method: 'CONNECT', path: '/ws', description: 'WebSocket connection test', status: 0, statusText: 'Timeout', duration: Date.now() - startTime, success: false, error: 'Connection timeout', }); } }, config.timeout); ws.on('open', () => { if (!resolved) { resolved = true; clearTimeout(timeout); ws.close(); resolve({ method: 'CONNECT', path: '/ws', description: 'WebSocket connection test', status: 101, statusText: 'Switching Protocols', duration: Date.now() - startTime, success: true, error: null, }); } }); ws.on('error', (error) => { if (!resolved) { resolved = true; clearTimeout(timeout); resolve({ method: 'CONNECT', path: '/ws', description: 'WebSocket connection test', status: 0, statusText: 'Connection Failed', duration: Date.now() - startTime, success: false, error: error.message, }); } }); }); } /** * Print test summary */ function printSummary() { console.log(`\n`); console.log(`=== Test Summary ===`); console.log(`\n`); const categories = Object.entries(results.categories); const maxCategoryLen = Math.max(...categories.map(([name]) => name.length)); for (const [name, data] of categories) { const passRate = data.total > 0 ? ((data.passed / data.total) * 100).toFixed(0) : '0'; const statusIcon = data.failed === 0 ? '\x1b[32m' : '\x1b[31m'; const reset = '\x1b[0m'; console.log( `${statusIcon}${name.padEnd(maxCategoryLen + 1)}${reset} ` + `${data.passed}/${data.total} passed (${passRate}%)` ); } console.log(`\n`); const totalPassRate = results.summary.total > 0 ? ((results.summary.passed / results.summary.total) * 100).toFixed(0) : '0'; console.log( `Total: ${results.summary.passed}/${results.summary.total} passed ` + `(${totalPassRate}%)` ); if (results.errors.length > 0) { console.log(`\n\x1b[31m=== Errors ===\x1b[0m\n`); for (const error of results.errors) { console.log(` [${error.category}] ${error.method} ${error.path}`); console.log(` Status: ${error.status || 'N/A'}`); console.log(` Error: ${error.error}`); console.log(); } } // JSON output if requested if (config.json) { console.log(`\n=== JSON Output ===\n`); console.log(JSON.stringify(results, null, 2)); } // Exit with appropriate code process.exit(results.summary.failed > 0 ? 1 : 0); } /** * Run all API tests */ async function runAllTests() { console.log(`\n=== OpenFang API Connection Test ===`); console.log(`Base URL: ${config.baseUrl}`); console.log(`Timeout: ${config.timeout}ms`); console.log(`\n`); // ========================================= // Health & System Endpoints // ========================================= console.log(`\n--- Health & System ---`); await testEndpoint('System', 'GET', '/api/health', { expectedStatus: 200, description: 'Health check endpoint', }); await testEndpoint('System', 'GET', '/api/version', { description: 'Version information', }); // ========================================= // Agent Endpoints // ========================================= console.log(`\n--- Agent API ---`); await testEndpoint('Agents', 'GET', '/api/agents', { expectedStatus: 200, description: 'List all agents', }); // Test agent creation (will fail if not authenticated) await testEndpoint('Agents', 'POST', '/api/agents', { body: { name: 'test-agent', role: 'assistant', }, description: 'Create agent (expect 401 without auth)', }); // ========================================= // Team API Endpoints // ========================================= console.log(`\n--- Team API ---`); await testEndpoint('Teams', 'GET', '/api/teams', { description: 'List all teams', }); await testEndpoint('Teams', 'POST', '/api/teams', { body: { name: 'Test Team', pattern: 'sequential', memberAgents: [], }, description: 'Create team (expect 401 without auth)', }); await testEndpoint('Teams', 'GET', '/api/teams/test-team-id', { description: 'Get team by ID', }); await testEndpoint('Teams', 'PUT', '/api/teams/test-team-id', { body: { name: 'Updated Team' }, description: 'Update team', }); await testEndpoint('Teams', 'DELETE', '/api/teams/test-team-id', { description: 'Delete team', }); // Team member endpoints await testEndpoint('Teams', 'POST', '/api/teams/test-team-id/members', { body: { agentId: 'test-agent', role: 'developer' }, description: 'Add team member', }); await testEndpoint('Teams', 'DELETE', '/api/teams/test-team-id/members/test-member-id', { description: 'Remove team member', }); // Team task endpoints await testEndpoint('Teams', 'GET', '/api/teams/test-team-id/metrics', { description: 'Get team metrics', }); await testEndpoint('Teams', 'GET', '/api/teams/test-team-id/events', { description: 'Get team events', }); // ========================================= // Config API Endpoints // ========================================= console.log(`\n--- Config API ---`); await testEndpoint('Config', 'GET', '/api/config', { description: 'Get full configuration', }); await testEndpoint('Config', 'GET', '/api/config/quick', { description: 'Get quick configuration', }); await testEndpoint('Config', 'PUT', '/api/config/quick', { body: { default_model: 'gpt-4' }, description: 'Update quick configuration', }); // ========================================= // Trigger API Endpoints // ========================================= console.log(`\n--- Trigger API ---`); await testEndpoint('Triggers', 'GET', '/api/triggers', { description: 'List all triggers', }); await testEndpoint('Triggers', 'POST', '/api/triggers', { body: { type: 'schedule', enabled: true, schedule: '0 * * * *', }, description: 'Create trigger', }); await testEndpoint('Triggers', 'GET', '/api/triggers/test-trigger-id', { description: 'Get trigger by ID', }); await testEndpoint('Triggers', 'PUT', '/api/triggers/test-trigger-id', { body: { enabled: false }, description: 'Update trigger', }); await testEndpoint('Triggers', 'DELETE', '/api/triggers/test-trigger-id', { description: 'Delete trigger', }); // ========================================= // Audit API Endpoints // ========================================= console.log(`\n--- Audit API ---`); await testEndpoint('Audit', 'GET', '/api/audit/logs', { description: 'Get audit logs', }); await testEndpoint('Audit', 'GET', '/api/audit/logs?limit=10', { description: 'Get audit logs with limit', }); await testEndpoint('Audit', 'GET', '/api/audit/verify/test-log-id', { description: 'Verify audit chain for log ID', }); // ========================================= // Skills & Plugins API Endpoints // ========================================= console.log(`\n--- Skills & Plugins ---`); await testEndpoint('Skills', 'GET', '/api/skills', { description: 'List all skills', }); await testEndpoint('Plugins', 'GET', '/api/plugins/status', { description: 'Get plugin status', }); // ========================================= // Hands API Endpoints // ========================================= console.log(`\n--- Hands API ---`); await testEndpoint('Hands', 'GET', '/api/hands', { description: 'List all hands', }); await testEndpoint('Hands', 'POST', '/api/hands/researcher/trigger', { body: { query: 'test query' }, description: 'Trigger researcher hand', }); // ========================================= // Workflow API Endpoints // ========================================= console.log(`\n--- Workflow API ---`); await testEndpoint('Workflows', 'GET', '/api/workflows', { description: 'List all workflows', }); await testEndpoint('Workflows', 'POST', '/api/workflows', { body: { name: 'Test Workflow', steps: [], }, description: 'Create workflow', }); await testEndpoint('Workflows', 'GET', '/api/workflows/test-workflow-id', { description: 'Get workflow by ID', }); // ========================================= // Stats API Endpoints // ========================================= console.log(`\n--- Stats API ---`); await testEndpoint('Stats', 'GET', '/api/stats/usage', { description: 'Get usage statistics', }); await testEndpoint('Stats', 'GET', '/api/stats/sessions', { description: 'Get session statistics', }); // ========================================= // Channel API Endpoints // ========================================= console.log(`\n--- Channels API ---`); await testEndpoint('Channels', 'GET', '/api/channels', { description: 'List all channels', }); // ========================================= // WebSocket Endpoint // ========================================= console.log(`\n--- WebSocket ---`); // WebSocket test is different - we need to check if the endpoint exists const wsUrl = `${config.baseUrl.replace(/^http/, 'ws')}/ws`; const wsResult = await testWebSocketConnection(wsUrl); results.categories['WebSocket'] = { total: 1, passed: wsResult.success ? 1 : 0, failed: wsResult.success ? 0 : 1, tests: [wsResult], }; results.summary.total++; if (wsResult.success) { results.summary.passed++; if (!config.json) { console.log(`\x1b[32m[PASS]\x1b[0m CONNECT /ws \x1b[2m${wsResult.status} ${wsResult.duration}ms\x1b[0m`); } } else { results.summary.failed++; results.errors.push({ category: 'WebSocket', method: 'CONNECT', path: '/ws', error: wsResult.error, status: wsResult.status, }); if (!config.json) { console.log(`\x1b[31m[FAIL]\x1b[0m CONNECT /ws \x1b[2m${wsResult.status} ${wsResult.duration}ms\x1b[0m`); console.log(` Error: ${wsResult.error}`); } } // Print summary printSummary(); } // Run tests runAllTests().catch((error) => { console.error('Failed to run tests:', error.message); process.exit(1); }); |