contaigents
Version:
Modular AI Content Ecosystem with Audio Generation
190 lines (189 loc) โข 8.51 kB
JavaScript
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();
}