contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
273 lines (271 loc) โข 9.49 kB
JavaScript
/**
* TextToImageTool Test
*
* This script tests the TextToImageTool functionality:
* 1. Basic text-to-image generation with different templates
* 2. Parameter validation
* 3. Custom styling options
* 4. Error handling scenarios
*/
import { ToolManager } from '../services/tools/ToolManager.js';
import { BrowserServiceManager } from '../services/tools/BrowserService.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_BASE_DIR = path.join(__dirname, 'test_text_to_image_output');
class TextToImageToolTest {
constructor() {
this.toolManager = new ToolManager(TEST_BASE_DIR);
}
async setup() {
console.log('๐ง Setting up test environment...');
// Create test output directory
await fs.mkdir(TEST_BASE_DIR, { recursive: true });
console.log(`๐ Test directory: ${TEST_BASE_DIR}`);
console.log('');
}
async cleanup() {
console.log('๐งน Cleaning up...');
// Close browser service
await BrowserServiceManager.cleanup();
console.log('โ
Cleanup completed');
}
async testBasicCardGeneration() {
console.log('๐จ Test 1: Basic Card Generation');
console.log('-'.repeat(35));
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'Hello World!\nThis is a test card.',
output_path: 'test_card.png',
template: 'card',
width: 800,
height: 600,
background_color: '#4f46e5',
text_color: '#ffffff',
font_size: 32
}
};
console.log('๐ Tool Call Parameters:');
console.log(JSON.stringify(toolCall.parameters, null, 2));
console.log('');
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Card generation successful!');
console.log('๐ Result data:', JSON.stringify(result.data, null, 2));
console.log('๐ฌ Message:', result.message);
}
else {
console.log('โ Card generation failed:', result.error);
}
console.log('');
}
async testBannerGeneration() {
console.log('๐ท๏ธ Test 2: Banner Generation');
console.log('-'.repeat(30));
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'WELCOME TO CONTAIGENTS',
output_path: 'test_banner.png',
template: 'banner',
width: 1200,
height: 300,
background_color: '#059669',
text_color: '#ffffff',
font_size: 48
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Banner generation successful!');
console.log('๐ File size:', result.data.file_size, 'bytes');
console.log('๐ Dimensions:', result.data.dimensions);
}
else {
console.log('โ Banner generation failed:', result.error);
}
console.log('');
}
async testQuoteGeneration() {
console.log('๐ฌ Test 3: Quote Generation');
console.log('-'.repeat(28));
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'The best way to predict the future is to create it.',
output_path: 'test_quote.png',
template: 'quote',
width: 800,
height: 600,
background_color: '#f8fafc',
text_color: '#1e293b',
font_size: 28
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Quote generation successful!');
console.log('๐ Template used:', result.data.template);
}
else {
console.log('โ Quote generation failed:', result.error);
}
console.log('');
}
async testCodeBlockGeneration() {
console.log('๐ป Test 4: Code Block Generation');
console.log('-'.repeat(33));
const codeText = `function greet(name) {
console.log(\`Hello, \${name}!\`);
return \`Welcome to Contaigents!\`;
}
greet('Developer');`;
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: codeText,
output_path: 'test_code.png',
template: 'code-block',
width: 800,
height: 500,
font_size: 16
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Code block generation successful!');
console.log('๐ Text length:', result.data.text_length, 'characters');
}
else {
console.log('โ Code block generation failed:', result.error);
}
console.log('');
}
async testCustomTemplate() {
console.log('๐จ Test 5: Custom Template');
console.log('-'.repeat(26));
const customHtml = `<!DOCTYPE html>
<html>
<head>
<style>
body {
width: {{width}}px;
height: {{height}}px;
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
display: flex;
align-items: center;
justify-content: center;
margin: 0;
font-family: {{font_family}};
}
.custom-text {
color: {{text_color}};
font-size: {{font_size}}px;
text-align: center;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
font-weight: bold;
}
</style>
</head>
<body>
<div class="custom-text">{{text}}</div>
</body>
</html>`;
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'Custom Design!',
output_path: 'test_custom.png',
template: 'custom',
custom_html: customHtml,
width: 600,
height: 400,
text_color: '#ffffff',
font_size: 36
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Custom template generation successful!');
console.log('๐ Output path:', result.data.output_file_path);
}
else {
console.log('โ Custom template generation failed:', result.error);
}
console.log('');
}
async testParameterValidation() {
console.log('โ
Test 6: Parameter Validation');
console.log('-'.repeat(32));
// Test missing required parameter
console.log('Testing missing text parameter:');
const result1 = await this.toolManager.executeTool({
tool_name: 'text_to_image',
parameters: {
output_path: 'test.png'
// Missing 'text' parameter
}
});
if (!result1.success && result1.error?.includes('Missing required parameter: text')) {
console.log('โ
Correctly validates missing text parameter');
}
else {
console.log('โ Failed to validate missing text parameter');
}
// Test invalid template
console.log('Testing invalid template:');
const result2 = await this.toolManager.executeTool({
tool_name: 'text_to_image',
parameters: {
text: 'Test',
output_path: 'test.png',
template: 'invalid_template'
}
});
if (!result2.success && result2.error?.includes('Invalid template')) {
console.log('โ
Correctly validates invalid template');
}
else {
console.log('โ Failed to validate invalid template');
}
console.log('');
}
async runAllTests() {
try {
await this.setup();
await this.testBasicCardGeneration();
await this.testBannerGeneration();
await this.testQuoteGeneration();
await this.testCodeBlockGeneration();
await this.testCustomTemplate();
await this.testParameterValidation();
await this.cleanup();
console.log('๐ All TextToImageTool tests completed!');
console.log('');
console.log('๐ Generated test images are available in:');
console.log(` ${TEST_BASE_DIR}`);
}
catch (error) {
console.error('โ Test failed:', error);
await this.cleanup();
process.exit(1);
}
}
}
// Run the tests
async function runTests() {
console.log('๐ Starting TextToImageTool Tests');
console.log('');
const test = new TextToImageToolTest();
await test.runAllTests();
}
// Execute if run directly
if (import.meta.url === `file://${process.argv[1]}`) {
runTests().catch(console.error);
}
export { TextToImageToolTest };