|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Joomla\AI\Tests\Provider\OpenAIProvider; |
| 4 | + |
| 5 | +use Joomla\AI\AIFactory; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | + |
| 8 | +class AudioInterfaceTest extends TestCase |
| 9 | +{ |
| 10 | + protected $provider; |
| 11 | + protected $config; |
| 12 | + |
| 13 | + protected function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + $configFile = __DIR__ . '/../../config.json'; |
| 18 | + $this->config = json_decode((string) file_get_contents($configFile), true); |
| 19 | + $apiKey = $this->config['openai_api_key'] ?? null; |
| 20 | + |
| 21 | + $this->provider = AIFactory::getAI('openai', ['api_key' => $apiKey]); |
| 22 | + } |
| 23 | + |
| 24 | + public function testBasicSpeechGeneration() |
| 25 | + { |
| 26 | + echo "Test 1: Basic Speech Generation\n"; |
| 27 | + $text = "Hello world! This is a test of the OpenAI text-to-speech capability."; |
| 28 | + $options = ['model' => 'tts-1', 'voice' => 'alloy']; |
| 29 | + |
| 30 | + $response = $this->provider->speech($text, $options); |
| 31 | + |
| 32 | + $this->assertEquals(200, $response->getStatusCode()); |
| 33 | + $this->assertEquals('OpenAI', $response->getProvider()); |
| 34 | + $this->assertNotEmpty($response->getContent(), 'Speech response content must not be empty'); |
| 35 | + |
| 36 | + $meta = $response->getMetadata(); |
| 37 | + $this->assertArrayHasKey('model', $meta); |
| 38 | + $this->assertArrayHasKey('voice', $meta); |
| 39 | + $this->assertEquals('alloy', $meta['voice']); |
| 40 | + } |
| 41 | + |
| 42 | + public function testDifferentVoiceAndWavFormat() |
| 43 | + { |
| 44 | + echo "Test 2: Different Voice and WAV Format\n"; |
| 45 | + $text = "This is a WAV format test."; |
| 46 | + $options = ['model' => 'tts-1-hd', 'voice' => 'nova', 'response_format' => 'wav']; |
| 47 | + |
| 48 | + $response = $this->provider->speech($text, $options); |
| 49 | + |
| 50 | + $this->assertEquals(200, $response->getStatusCode()); |
| 51 | + $this->assertEquals('OpenAI', $response->getProvider()); |
| 52 | + $this->assertNotEmpty($response->getContent()); |
| 53 | + |
| 54 | + $meta = $response->getMetadata(); |
| 55 | + $this->assertArrayHasKey('voice', $meta); |
| 56 | + $this->assertEquals('wav', $meta['format']); |
| 57 | + $this->assertEquals('nova', $meta['voice']); |
| 58 | + } |
| 59 | + |
| 60 | + public function testGpt4oMiniTtsWithInstructions() |
| 61 | + { |
| 62 | + echo "Test 3: GPT-4o-mini-tts with Instructions\n"; |
| 63 | + $text = "Short instruction test for TTS model."; |
| 64 | + $options = [ |
| 65 | + 'model' => 'gpt-4o-mini-tts', |
| 66 | + 'voice' => 'coral', |
| 67 | + 'instructions' => 'Speak cheerfully.', |
| 68 | + 'response_format' => 'mp3' |
| 69 | + ]; |
| 70 | + |
| 71 | + $response = $this->provider->speech($text, $options); |
| 72 | + |
| 73 | + $this->assertEquals(200, $response->getStatusCode()); |
| 74 | + $this->assertEquals('OpenAI', $response->getProvider()); |
| 75 | + $this->assertNotEmpty($response->getContent()); |
| 76 | + |
| 77 | + $meta = $response->getMetadata(); |
| 78 | + $this->assertArrayHasKey('model', $meta); |
| 79 | + $this->assertArrayHasKey('voice', $meta); |
| 80 | + $this->assertEquals('coral', $meta['voice']); |
| 81 | + $this->assertEquals('mp3', $meta['format']); |
| 82 | + $this->assertEquals('gpt-4o-mini-tts', $meta['model']); |
| 83 | + $this->assertArrayHasKey('instructions', $meta); |
| 84 | + } |
| 85 | + |
| 86 | + public function testHelperMethodsReturnArrays() |
| 87 | + { |
| 88 | + echo "Test 4: Helper Methods Return Arrays\n"; |
| 89 | + $voices = $this->provider->getAvailableVoices(); |
| 90 | + $this->assertIsArray($voices); |
| 91 | + |
| 92 | + if (method_exists($this->provider, 'getTTSModels')) { |
| 93 | + $models = $this->provider->getTTSModels(); |
| 94 | + $this->assertIsArray($models); |
| 95 | + } |
| 96 | + |
| 97 | + if (method_exists($this->provider, 'getSupportedAudioFormats')) { |
| 98 | + $formats = $this->provider->getSupportedAudioFormats(); |
| 99 | + $this->assertIsArray($formats); |
| 100 | + } |
| 101 | + |
| 102 | + if (method_exists($this->provider, 'getAvailableModels')) { |
| 103 | + $avail = $this->provider->getAvailableModels(); |
| 104 | + $this->assertIsArray($avail); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public function testAudioTranscription() |
| 109 | + { |
| 110 | + echo "Test 5: Audio Transcription in Multiple Formats\n"; |
| 111 | + $audioFile = __DIR__ . '/../test_files/test_audio.wav'; |
| 112 | + |
| 113 | + // Transcribe in multiple formats and assert basic expectations |
| 114 | + $formats = ['text', 'srt', 'vtt']; |
| 115 | + foreach ($formats as $format) { |
| 116 | + $options = ['model' => 'whisper-1', 'response_format' => $format]; |
| 117 | + $response = $this->provider->transcribe($audioFile, $options); |
| 118 | + |
| 119 | + $this->assertEquals(200, $response->getStatusCode(), "Transcribe returned non-200 for format {$format}"); |
| 120 | + $this->assertEquals('OpenAI', $response->getProvider(), "Unexpected provider for format {$format}"); |
| 121 | + |
| 122 | + $meta = $response->getMetadata(); |
| 123 | + $this->assertArrayHasKey('response_format', $meta, "Missing response_format metadata for {$format}"); |
| 124 | + $this->assertNotEmpty($response->getContent(), "Empty transcription content for format {$format}"); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public function testCreateGermanAudioAndTranslate() |
| 129 | + { |
| 130 | + echo "Test 6: Create German Audio and Translate\n"; |
| 131 | + |
| 132 | + $testText = "Hallo, hiermit testen wir die Übersetzungsfunktion von OpenAI. Audio wird ins Englische übersetzt. Wir geben die Dateien und das zu verwendende Modell ein; aktuell ist nur Whisper-1 verfügbar. Ein optionaler Text dient zur Orientierung des Modells oder zur Fortsetzung eines vorherigen Audiosegments. Die Eingabeaufforderung sollte auf Englisch sein. Das Ausgabeformat kann in einer der folgenden Optionen gewählt werden: JSON, Text, SRT, Verbose_JSON oder VTT. Wir hoffen, dies funktioniert."; |
| 133 | + $speechOptions = ['model' => 'tts-1', 'voice' => 'alloy', 'response_format' => 'wav']; |
| 134 | + |
| 135 | + $speechResponse = $this->provider->speech($testText, $speechOptions); |
| 136 | + $this->assertEquals(200, $speechResponse->getStatusCode()); |
| 137 | + $this->assertEquals('OpenAI', $speechResponse->getProvider()); |
| 138 | + $this->assertNotEmpty($speechResponse->getContent(), 'Speech response content must not be empty'); |
| 139 | + |
| 140 | + $testDir = __DIR__ . '/../test_files'; |
| 141 | + if (!is_dir($testDir)) { |
| 142 | + mkdir($testDir, 0777, true); |
| 143 | + } |
| 144 | + $audioFile = $testDir . '/test_german_audio.wav'; |
| 145 | + |
| 146 | + if (method_exists($speechResponse, 'saveFile')) { |
| 147 | + $speechResponse->saveFile($audioFile); |
| 148 | + $this->assertFileExists($audioFile); |
| 149 | + } else { |
| 150 | + $written = file_put_contents($audioFile, $speechResponse->getContent()); |
| 151 | + $this->assertNotFalse($written, 'Failed to write test audio file'); |
| 152 | + $this->assertFileExists($audioFile); |
| 153 | + } |
| 154 | + |
| 155 | + $translateOptions = ['model' => 'whisper-1', 'response_format' => 'text']; |
| 156 | + $translateResponse = $this->provider->translate($audioFile, $translateOptions); |
| 157 | + |
| 158 | + $this->assertEquals(200, $translateResponse->getStatusCode()); |
| 159 | + $this->assertEquals('OpenAI', $translateResponse->getProvider()); |
| 160 | + $this->assertNotEmpty($translateResponse->getContent(), 'Translation content should not be empty'); |
| 161 | + |
| 162 | + $meta = $translateResponse->getMetadata(); |
| 163 | + $this->assertArrayHasKey('model', $meta); |
| 164 | + $this->assertArrayHasKey('response_format', $meta); |
| 165 | + } |
| 166 | +} |
0 commit comments