contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
141 lines (140 loc) โข 5.75 kB
JavaScript
/**
* Chat Image Generation End-to-End Test
*
* This script tests image generation through the chat service
*/
import { ChatService } from '../services/chatService.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, 'chat_image_output');
class ChatImageGenerationE2ETest {
constructor() {
this.chatService = new ChatService(TEST_OUTPUT_DIR);
}
async setup() {
console.log('๐ฌ Setting up Chat Image Generation E2E Test');
console.log('='.repeat(50));
// Create test output directory
await fs.mkdir(TEST_OUTPUT_DIR, { recursive: true });
console.log(`๐ Test directory created: ${TEST_OUTPUT_DIR}`);
console.log('');
}
async testChatImageGeneration() {
console.log('๐จ Test: Image Generation via Chat Service');
console.log('-'.repeat(45));
// Simulate a user message requesting image generation
const userMessage = `Can you generate an image of a cute robot holding a flower? Save it as "cute_robot.png" in the current directory.`;
console.log('๐ค User message:');
console.log(` "${userMessage}"`);
console.log('');
try {
// Create a chat session
const session = await this.chatService.createSession({
provider: 'google',
model: 'gemini-2.5-flash'
});
// Process the message through chat service
console.log('๐ค Processing message through chat service...');
const response = await this.chatService.sendMessage(session.id, userMessage);
console.log('โ
Chat service response received');
console.log('๐ Response:');
console.log(` ${response.content.slice(0, 200)}${response.content.length > 200 ? '...' : ''}`);
console.log('');
// Check if image was generated
const expectedImagePath = path.join(TEST_OUTPUT_DIR, 'cute_robot.png');
try {
const stats = await fs.stat(expectedImagePath);
console.log('๐ผ๏ธ Image successfully generated!');
console.log(` File: cute_robot.png`);
console.log(` Size: ${this.formatFileSize(stats.size)}`);
}
catch (error) {
console.log('โ Image file not found - checking for other generated images...');
// List all PNG files in the directory
const files = await fs.readdir(TEST_OUTPUT_DIR);
const imageFiles = files.filter(file => file.endsWith('.png'));
if (imageFiles.length > 0) {
console.log('๐ Found generated images:');
for (const file of imageFiles) {
const filePath = path.join(TEST_OUTPUT_DIR, file);
const stats = await fs.stat(filePath);
console.log(` - ${file} (${this.formatFileSize(stats.size)})`);
}
}
else {
console.log('โ No image files found in output directory');
}
}
}
catch (error) {
console.log('โ Chat service error:');
console.log(` ${error.message}`);
}
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! Check the output files in:');
console.log(` ${TEST_OUTPUT_DIR}`);
console.log('');
try {
const files = await fs.readdir(TEST_OUTPUT_DIR);
const imageFiles = files.filter(file => file.endsWith('.png'));
if (imageFiles.length > 0) {
console.log('Generated images:');
for (const file of imageFiles) {
const filePath = path.join(TEST_OUTPUT_DIR, file);
const stats = await fs.stat(filePath);
console.log(` - ${file} (${this.formatFileSize(stats.size)})`);
}
}
else {
console.log('No images were generated.');
}
}
catch (error) {
console.log('Could not list generated files.');
}
console.log('');
}
async runTest() {
try {
await this.setup();
await this.testChatImageGeneration();
await this.cleanup();
console.log('๐ Chat image generation test completed!');
}
catch (error) {
console.error('โ Test failed:', error);
process.exit(1);
}
}
}
// Run the test
async function runChatImageTest() {
console.log('๐ Starting Chat Image Generation End-to-End Test');
console.log('');
console.log('This test verifies that:');
console.log(' - Chat service can process image generation requests');
console.log(' - Image generation tool is properly integrated');
console.log(' - Generated images are saved correctly');
console.log('');
const test = new ChatImageGenerationE2ETest();
await test.runTest();
}
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
runChatImageTest();
}