UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

229 lines (228 loc) 9.26 kB
#!/usr/bin/env node /** * Image Generation Tool Integration Test * * This script tests the ImageGenerationTool integration without requiring API keys. * It verifies: * 1. Tool registration and availability * 2. Parameter validation * 3. Error handling * 4. Tool definition structure */ import { ToolManager } from '../services/tools/ToolManager.js'; import { ImageGenerationTool } from '../services/tools/ImageGenerationTool.js'; class ImageGenerationIntegrationTest { constructor() { this.toolManager = new ToolManager(); } async testToolRegistration() { console.log('🔧 Test 1: Tool Registration'); console.log('-'.repeat(30)); const availableTools = this.toolManager.getAvailableTools(); const imageGenerationTool = availableTools.find(tool => tool.name === 'image_generation'); if (imageGenerationTool) { console.log('✅ ImageGenerationTool is properly registered'); console.log(` Name: ${imageGenerationTool.name}`); console.log(` Description: ${imageGenerationTool.description}`); console.log(` Parameters: ${imageGenerationTool.parameters.length}`); } else { console.log('❌ ImageGenerationTool is NOT registered'); console.log('Available tools:', availableTools.map(t => t.name).join(', ')); } console.log(''); } async testToolDefinition() { console.log('📋 Test 2: Tool Definition Structure'); console.log('-'.repeat(35)); const tool = new ImageGenerationTool(); const definition = tool.getDefinition(); console.log(`Name: ${definition.name}`); console.log(`Description: ${definition.description}`); console.log('Parameters:'); for (const param of definition.parameters) { const required = param.required ? '(required)' : '(optional)'; const defaultValue = param.default !== undefined ? ` [default: ${param.default}]` : ''; console.log(` - ${param.name} (${param.type}) ${required}${defaultValue}: ${param.description}`); } console.log(''); } async testParameterValidation() { console.log('✅ Test 3: Parameter Validation'); console.log('-'.repeat(30)); // Test missing required parameter console.log('Testing missing required parameter (prompt):'); const result1 = await this.toolManager.executeTool({ tool_name: 'image_generation', parameters: { output_path: 'test.png' // Missing 'prompt' parameter } }); if (!result1.success && result1.error?.includes('Missing required parameter: prompt')) { console.log('✅ Correctly validates missing prompt parameter'); } else { console.log('❌ Failed to validate missing prompt parameter'); console.log('Result:', result1); } // Test missing required parameter console.log('Testing missing required parameter (output_path):'); const result2 = await this.toolManager.executeTool({ tool_name: 'image_generation', parameters: { prompt: 'A test image' // Missing 'output_path' parameter } }); if (!result2.success && result2.error?.includes('Missing required parameter: output_path')) { console.log('✅ Correctly validates missing output_path parameter'); } else { console.log('❌ Failed to validate missing output_path parameter'); console.log('Result:', result2); } // Test invalid parameter type console.log('Testing invalid parameter type (width as string):'); const result3 = await this.toolManager.executeTool({ tool_name: 'image_generation', parameters: { prompt: 'A test image', output_path: 'test.png', width: 'invalid' } }); if (!result3.success && result3.error?.includes('must be a number')) { console.log('✅ Correctly validates parameter types'); } else { console.log('❌ Failed to validate parameter types'); console.log('Result:', result3); } console.log(''); } async testSecurityValidation() { console.log('🔒 Test 4: Security Validation'); console.log('-'.repeat(30)); // Test path traversal attempt console.log('Testing path traversal protection:'); const result1 = await this.toolManager.executeTool({ tool_name: 'image_generation', parameters: { prompt: 'A test image', output_path: '../../../etc/passwd.png' } }); if (!result1.success && result1.error?.includes('cannot contain ".."')) { console.log('✅ Correctly blocks path traversal attempts'); } else { console.log('❌ Failed to block path traversal attempts'); console.log('Result:', result1); } // Test absolute path attempt console.log('Testing absolute path protection:'); const result2 = await this.toolManager.executeTool({ tool_name: 'image_generation', parameters: { prompt: 'A test image', output_path: '/tmp/test.png' } }); if (!result2.success && result2.error?.includes('cannot contain ".." or be absolute')) { console.log('✅ Correctly blocks absolute paths'); } else { console.log('❌ Failed to block absolute paths'); console.log('Result:', result2); } console.log(''); } async testProviderConfiguration() { console.log('⚙️ Test 5: Provider Configuration'); console.log('-'.repeat(35)); // Test with unconfigured provider (should fail gracefully) console.log('Testing provider configuration handling:'); const result = await this.toolManager.executeTool({ tool_name: 'image_generation', parameters: { prompt: 'A test image', output_path: 'test.png', provider: 'gemini' } }); if (!result.success) { if (result.error?.includes('No configured')) { console.log('✅ Correctly handles unconfigured provider'); } else if (result.error?.includes('API error') || result.error?.includes('Failed to generate image')) { console.log('✅ Provider is configured, API call attempted (expected behavior)'); } else { console.log('❌ Unexpected error handling'); console.log('Result:', result); } } else { console.log('✅ Provider is configured and working'); } console.log(''); } async testToolInstructions() { console.log('📖 Test 6: Tool Instructions Generation'); console.log('-'.repeat(40)); const instructions = this.toolManager.generateToolInstructions(); if (instructions.includes('image_generation')) { console.log('✅ Tool instructions include image_generation tool'); } else { console.log('❌ Tool instructions missing image_generation tool'); } if (instructions.includes('Generate images from text prompts')) { console.log('✅ Tool instructions include proper description'); } else { console.log('❌ Tool instructions missing proper description'); } if (instructions.includes('<tool_call name=') && instructions.includes('</tool_call>')) { console.log('✅ Tool instructions include XML format examples'); } else { console.log('❌ Tool instructions missing XML format examples'); } console.log(''); } async runAllTests() { console.log('🎨 Image Generation Tool Integration Tests'); console.log('='.repeat(50)); console.log(''); await this.testToolRegistration(); await this.testToolDefinition(); await this.testParameterValidation(); await this.testSecurityValidation(); await this.testProviderConfiguration(); await this.testToolInstructions(); console.log('🎉 Integration tests completed!'); console.log(''); console.log('To test actual image generation, configure a provider:'); console.log(' contaigents configure'); console.log(''); console.log('Then run the full demo:'); console.log(' node dist/tests/imageGenerationDemo.js'); } } // Run the tests async function runTests() { const test = new ImageGenerationIntegrationTest(); try { await test.runAllTests(); } catch (error) { console.error('❌ Tests failed:', error); process.exit(1); } } // Run if called directly if (import.meta.url === `file://${process.argv[1]}`) { runTests(); }