contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
239 lines (238 loc) โข 9.18 kB
JavaScript
/**
* TextToImageTool Phase 2 Test
*
* This script tests the Phase 2 enhancements:
* 1. Template variables and dynamic content injection
* 2. Social media presets
* 3. Enhanced templates (quote-enhanced, social-post, certificate)
* 4. Advanced styling and layout options
*/
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_phase2_output');
class TextToImagePhase2Test {
constructor() {
this.toolManager = new ToolManager(TEST_BASE_DIR);
}
async setup() {
console.log('๐ง Setting up Phase 2 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 testSocialMediaPresets() {
console.log('๐ฑ Test 1: Social Media Presets');
console.log('-'.repeat(35));
const presets = [
{ preset: 'instagram-square', text: 'Perfect square for Instagram!' },
{ preset: 'twitter-post', text: 'Optimized for Twitter engagement ๐ฆ' },
{ preset: 'linkedin-post', text: 'Professional LinkedIn content ๐ผ' }
];
for (const { preset, text } of presets) {
console.log(`Testing preset: ${preset}`);
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text,
output_path: `preset_${preset.replace('-', '_')}.png`,
preset,
background_color: '#4f46e5',
text_color: '#ffffff',
font_size: 32
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log(`โ
${preset}: ${result.data.dimensions} - ${result.data.file_size} bytes`);
}
else {
console.log(`โ ${preset} failed: ${result.error}`);
}
}
console.log('');
}
async testEnhancedQuoteTemplate() {
console.log('๐ฌ Test 2: Enhanced Quote with Variables');
console.log('-'.repeat(42));
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'The future belongs to those who believe in the beauty of their dreams.',
output_path: 'enhanced_quote.png',
template: 'quote-enhanced',
width: 1000,
height: 600,
background_color: '#2c3e50',
text_color: '#ecf0f1',
font_size: 28,
variables: JSON.stringify({
author: 'Eleanor Roosevelt',
date: '1945'
})
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Enhanced quote generated successfully!');
console.log(`๐ Variables used: ${result.data.variables_count}`);
console.log(`๐ Dimensions: ${result.data.dimensions}`);
}
else {
console.log('โ Enhanced quote failed:', result.error);
}
console.log('');
}
async testSocialPostTemplate() {
console.log('๐ฒ Test 3: Social Media Post Template');
console.log('-'.repeat(38));
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'Just launched Phase 2 of TextToImageTool! ๐ Now with advanced templates, social media presets, and dynamic variables. The future of content creation is here! #AI #ContentCreation #Innovation',
output_path: 'social_post.png',
template: 'social-post',
width: 1200,
height: 800,
background_color: '#ffffff',
text_color: '#1a202c',
font_size: 24,
variables: JSON.stringify({
username: 'Contaigents',
handle: 'contaigents',
profile_initial: 'C',
timestamp: '2h',
likes: '127',
shares: '23',
comments: '45',
hashtags: '#TextToImage #AI #Phase2'
})
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Social post generated successfully!');
console.log(`๐ Template: ${result.data.template}`);
console.log(`๐ File size: ${result.data.file_size} bytes`);
}
else {
console.log('โ Social post failed:', result.error);
}
console.log('');
}
async testCertificateTemplate() {
console.log('๐ Test 4: Certificate Template');
console.log('-'.repeat(30));
const toolCall = {
tool_name: 'text_to_image',
parameters: {
text: 'Advanced Text-to-Image Generation\nwith Dynamic Templates and Variables',
output_path: 'certificate.png',
template: 'certificate',
width: 1200,
height: 900,
background_color: '#f8f9fa',
text_color: '#2d3748',
font_size: 24,
variables: JSON.stringify({
certificate_type: 'of Achievement',
recipient: 'Phase 2 Implementation',
authority: 'Contaigents Development Team',
date: 'August 2024'
})
}
};
const result = await this.toolManager.executeTool(toolCall);
if (result.success) {
console.log('โ
Certificate generated successfully!');
console.log(`๐ Template: ${result.data.template}`);
console.log(`๐ Dimensions: ${result.data.dimensions}`);
}
else {
console.log('โ Certificate failed:', result.error);
}
console.log('');
}
async testVariableValidation() {
console.log('โ
Test 5: Variable Validation');
console.log('-'.repeat(30));
// Test invalid JSON
console.log('Testing invalid JSON variables:');
const result1 = await this.toolManager.executeTool({
tool_name: 'text_to_image',
parameters: {
text: 'Test',
output_path: 'test.png',
variables: '{"invalid": json}'
}
});
if (!result1.success && result1.error?.includes('Invalid variables JSON')) {
console.log('โ
Correctly validates invalid JSON');
}
else {
console.log('โ Failed to validate invalid JSON');
}
// Test invalid preset
console.log('Testing invalid preset:');
const result2 = await this.toolManager.executeTool({
tool_name: 'text_to_image',
parameters: {
text: 'Test',
output_path: 'test.png',
preset: 'invalid-preset'
}
});
if (!result2.success && result2.error?.includes('Invalid preset')) {
console.log('โ
Correctly validates invalid preset');
}
else {
console.log('โ Failed to validate invalid preset');
}
console.log('');
}
async runAllTests() {
try {
await this.setup();
await this.testSocialMediaPresets();
await this.testEnhancedQuoteTemplate();
await this.testSocialPostTemplate();
await this.testCertificateTemplate();
await this.testVariableValidation();
await this.cleanup();
console.log('๐ All Phase 2 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 Phase 2 Tests');
console.log('');
const test = new TextToImagePhase2Test();
await test.runAllTests();
}
// Execute if run directly
if (import.meta.url === `file://${process.argv[1]}`) {
runTests().catch(console.error);
}
export { TextToImagePhase2Test };