UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

190 lines (189 loc) โ€ข 8.51 kB
import { AudioGenerationTool } from '../services/tools/AudioGenerationTool.js'; import fs from 'fs/promises'; import path from 'path'; /** * Test the hybrid music generation functionality */ export class HybridMusicGenerationTest { constructor() { this.testDir = path.join(process.cwd(), 'test_hybrid_music'); this.audioGenerationTool = new AudioGenerationTool(this.testDir); } async runTests() { console.log('๐Ÿงช Starting Hybrid Music Generation tests...\n'); try { // Setup test environment await this.setupTestEnvironment(); // Test 1: Speech generation (existing functionality) await this.testSpeechGeneration(); // Test 2: Music generation with cloud-first approach await this.testMusicGenerationCloudFirst(); // Test 3: Music generation with local preference await this.testMusicGenerationLocalPreference(); // Test 4: Music generation with style and negative prompt await this.testMusicGenerationWithOptions(); // Test 5: Error handling await this.testErrorHandling(); console.log('โœ… All Hybrid Music Generation tests passed!\n'); } catch (error) { console.error('โŒ Hybrid Music Generation test failed:', error); throw error; } finally { // Cleanup await this.cleanup(); } } async setupTestEnvironment() { console.log('๐Ÿ“ Setting up test environment...'); // Create test directory await fs.mkdir(this.testDir, { recursive: true }); await fs.mkdir(path.join(this.testDir, 'audio'), { recursive: true }); await fs.mkdir(path.join(this.testDir, 'music'), { recursive: true }); console.log(` Created test directory: ${this.testDir}`); } async testSpeechGeneration() { console.log('๐ŸŽ™๏ธ Test 1: Speech generation (existing functionality)...'); const result = await this.audioGenerationTool.execute({ text: 'This is a test of the hybrid audio generation system.', output_path: 'audio/test_speech.wav', content_type: 'speech', voice: 'zephyr' }); if (!result.success) { console.log(` โš ๏ธ Speech generation failed (expected if no Gemini config): ${result.error}`); console.log(' โœ… Speech generation test completed (configuration dependent)'); return; } console.log(` โœ… Speech generated: ${result.data.output_file_path}`); console.log(` ๐Ÿ“Š File size: ${result.data.file_size} bytes, Duration: ${result.data.duration_estimate}`); // Verify file exists const outputPath = path.join(this.testDir, 'audio/test_speech.wav'); const stats = await fs.stat(outputPath); if (stats.size === 0) { throw new Error('Speech output file is empty'); } console.log(' โœ… Speech generation validation passed'); } async testMusicGenerationCloudFirst() { console.log('๐ŸŒ Test 2: Music generation with cloud-first approach...'); const result = await this.audioGenerationTool.execute({ text: 'Upbeat electronic background music for technology podcast', output_path: 'music/cloud_first_music.wav', content_type: 'music', style: 'electronic' }); // This will likely fail without proper Lyria 2 setup, but should show the fallback logic console.log(` Result: ${result.success ? 'Success' : 'Failed'}`); console.log(` Message: ${result.message || result.error}`); if (result.success) { console.log(` โœ… Music generated with provider: ${result.data.provider}`); console.log(` ๐Ÿ“Š File size: ${result.data.file_size} bytes`); // Verify file exists const outputPath = path.join(this.testDir, result.data.output_file_path); const stats = await fs.stat(outputPath); if (stats.size === 0) { throw new Error('Music output file is empty'); } } else { console.log(' โš ๏ธ Music generation failed (expected without proper API setup)'); } console.log(' โœ… Cloud-first music generation test completed'); } async testMusicGenerationLocalPreference() { console.log('๐Ÿ  Test 3: Music generation with local preference...'); const result = await this.audioGenerationTool.execute({ text: 'Calm ambient music for meditation', output_path: 'music/local_preferred_music.wav', content_type: 'music', style: 'ambient', prefer_local: true }); console.log(` Result: ${result.success ? 'Success' : 'Failed'}`); console.log(` Message: ${result.message || result.error}`); if (result.success) { console.log(` โœ… Music generated with provider: ${result.data.provider}`); if (result.data.auto_setup) { console.log(' ๐Ÿณ Local MusicGen was automatically set up'); } } else { console.log(' โš ๏ธ Local music generation failed (expected without Docker setup)'); } console.log(' โœ… Local preference music generation test completed'); } async testMusicGenerationWithOptions() { console.log('๐ŸŽต Test 4: Music generation with style and negative prompt...'); const result = await this.audioGenerationTool.execute({ text: 'Energetic rock music with guitar', output_path: 'music/rock_with_options.wav', content_type: 'music', style: 'rock', negative_prompt: 'slow, quiet, ambient', seed: 12345 }); console.log(` Result: ${result.success ? 'Success' : 'Failed'}`); console.log(` Message: ${result.message || result.error}`); if (result.success) { console.log(` โœ… Music generated with options:`); console.log(` ๐ŸŽธ Style: ${result.data.style}`); console.log(` ๐Ÿšซ Negative prompt: ${result.data.negative_prompt}`); console.log(` ๐ŸŽฒ Seed: ${result.data.seed}`); } else { console.log(' โš ๏ธ Music generation with options failed (expected without proper setup)'); } console.log(' โœ… Music generation with options test completed'); } async testErrorHandling() { console.log('โš ๏ธ Test 5: Error handling...'); // Test invalid content type const invalidTypeResult = await this.audioGenerationTool.execute({ text: 'Test content', output_path: 'audio/invalid.wav', content_type: 'invalid_type' }); if (invalidTypeResult.success) { throw new Error('Should have failed with invalid content type'); } console.log(' โœ… Invalid content type properly rejected'); // Test empty text const emptyTextResult = await this.audioGenerationTool.execute({ text: '', output_path: 'audio/empty.wav', content_type: 'speech' }); if (emptyTextResult.success) { throw new Error('Should have failed with empty text'); } console.log(' โœ… Empty text properly rejected'); // Test invalid path const invalidPathResult = await this.audioGenerationTool.execute({ text: 'Test content', output_path: '../../../etc/passwd', content_type: 'speech' }); if (invalidPathResult.success) { throw new Error('Should have failed with invalid path'); } console.log(' โœ… Invalid path properly rejected'); console.log(' โœ… All error cases handled correctly'); } async cleanup() { console.log('๐Ÿงน Cleaning up test files...'); try { await fs.rm(this.testDir, { recursive: true, force: true }); console.log(' โœ… Test directory cleaned up'); } catch (error) { console.log(' โš ๏ธ Cleanup warning:', error); } } } // Export test function for CLI usage export async function testHybridMusicGeneration() { const test = new HybridMusicGenerationTest(); await test.runTests(); }