contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
202 lines (201 loc) โข 8.15 kB
JavaScript
/**
* Image Generation End-to-End Test
*
* This script tests actual image generation using the configured API key
*/
import { ToolManager } from '../services/tools/ToolManager.js';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Test configuration
const TEST_OUTPUT_DIR = path.join(__dirname, 'e2e_image_output');
class ImageGenerationE2ETest {
constructor() {
this.toolManager = new ToolManager(TEST_OUTPUT_DIR);
}
async setup() {
console.log('๐จ Setting up Image Generation E2E Test');
console.log('='.repeat(45));
// Create test output directory
await fs.mkdir(TEST_OUTPUT_DIR, { recursive: true });
console.log(`๐ Test directory created: ${TEST_OUTPUT_DIR}`);
console.log('');
}
async testBasicImageGeneration() {
console.log('๐ผ๏ธ Test 1: Basic Image Generation');
console.log('-'.repeat(35));
const toolCall = {
tool_name: 'image_generation',
parameters: {
prompt: 'A simple geometric pattern with blue and white colors, minimalist design',
output_path: 'test_geometric_pattern.png',
provider: 'gemini'
},
id: 'e2e_test_1'
};
console.log('๐ Executing tool call:');
console.log(` Tool: ${toolCall.tool_name}`);
console.log(` Prompt: ${toolCall.parameters.prompt}`);
console.log(` Output: ${toolCall.parameters.output_path}`);
console.log(` Provider: ${toolCall.parameters.provider}`);
console.log('');
const startTime = Date.now();
const result = await this.toolManager.executeTool(toolCall);
const endTime = Date.now();
const duration = endTime - startTime;
if (result.success) {
console.log('โ
Image generation successful!');
console.log(` File: ${result.data.output_file_path}`);
console.log(` Size: ${this.formatFileSize(result.data.file_size)}`);
console.log(` Provider: ${result.data.provider}`);
console.log(` Model: ${result.data.model}`);
console.log(` Dimensions: ${result.data.dimensions}`);
console.log(` Duration: ${duration}ms`);
// Verify file exists
const fullPath = path.join(TEST_OUTPUT_DIR, result.data.output_file_path);
try {
const stats = await fs.stat(fullPath);
console.log(` File verified: ${stats.size} bytes`);
}
catch (error) {
console.log('โ File verification failed:', error);
}
}
else {
console.log('โ Image generation failed:');
console.log(` Error: ${result.error}`);
return;
}
console.log('');
}
async testImageGenerationWithDimensions() {
console.log('๐ Test 2: Image Generation with Custom Dimensions');
console.log('-'.repeat(50));
const toolCall = {
tool_name: 'image_generation',
parameters: {
prompt: 'A peaceful landscape with mountains and a lake, digital art style',
output_path: 'test_landscape_custom.png',
provider: 'gemini',
width: 1024,
height: 768,
style: 'digital_art'
},
id: 'e2e_test_2'
};
console.log('๐ Executing tool call with custom dimensions:');
console.log(` Prompt: ${toolCall.parameters.prompt}`);
console.log(` Dimensions: ${toolCall.parameters.width}x${toolCall.parameters.height}`);
console.log(` Style: ${toolCall.parameters.style}`);
console.log('');
const startTime = Date.now();
const result = await this.toolManager.executeTool(toolCall);
const endTime = Date.now();
const duration = endTime - startTime;
if (result.success) {
console.log('โ
Custom dimension image generation successful!');
console.log(` File: ${result.data.output_file_path}`);
console.log(` Size: ${this.formatFileSize(result.data.file_size)}`);
console.log(` Requested: ${toolCall.parameters.width}x${toolCall.parameters.height}`);
console.log(` Actual: ${result.data.dimensions}`);
console.log(` Duration: ${duration}ms`);
}
else {
console.log('โ Custom dimension image generation failed:');
console.log(` Error: ${result.error}`);
}
console.log('');
}
async testMultipleImageGeneration() {
console.log('๐ Test 3: Multiple Image Generation');
console.log('-'.repeat(35));
const prompts = [
'A red apple on a white background',
'A blue butterfly on a flower',
'A green tree in a field'
];
for (let i = 0; i < prompts.length; i++) {
const toolCall = {
tool_name: 'image_generation',
parameters: {
prompt: prompts[i],
output_path: `batch/image_${i + 1}.png`,
provider: 'gemini'
},
id: `batch_test_${i + 1}`
};
console.log(`๐จ Generating image ${i + 1}/3: "${prompts[i]}"`);
const startTime = Date.now();
const result = await this.toolManager.executeTool(toolCall);
const endTime = Date.now();
const duration = endTime - startTime;
if (result.success) {
console.log(` โ
Success: ${result.data.output_file_path} (${this.formatFileSize(result.data.file_size)}, ${duration}ms)`);
}
else {
console.log(` โ Failed: ${result.error}`);
}
}
console.log('');
}
formatFileSize(bytes) {
if (bytes === 0)
return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}
async cleanup() {
console.log('๐งน Test completed! Generated images are in:');
console.log(` ${TEST_OUTPUT_DIR}`);
console.log('');
console.log('Generated files:');
try {
const files = await fs.readdir(TEST_OUTPUT_DIR, { recursive: true });
for (const file of files) {
if (typeof file === 'string' && file.endsWith('.png')) {
const filePath = path.join(TEST_OUTPUT_DIR, file);
const stats = await fs.stat(filePath);
console.log(` - ${file} (${this.formatFileSize(stats.size)})`);
}
}
}
catch (error) {
console.log(' (Could not list files)');
}
console.log('');
}
async runAllTests() {
try {
await this.setup();
await this.testBasicImageGeneration();
await this.testImageGenerationWithDimensions();
await this.testMultipleImageGeneration();
await this.cleanup();
console.log('๐ All E2E tests completed successfully!');
}
catch (error) {
console.error('โ E2E test failed:', error);
process.exit(1);
}
}
}
// Run the tests
async function runE2ETests() {
console.log('๐ Starting Image Generation End-to-End Tests');
console.log('');
console.log('Prerequisites:');
console.log(' - Google provider must be configured');
console.log(' - Valid GEMINI_API_KEY must be available');
console.log('');
const test = new ImageGenerationE2ETest();
await test.runAllTests();
}
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
runE2ETests();
}