also looking at this
meemalabs/laravel-text-to-speech
feat: add 60db text-to-speech driver
#46
5 files
+403
-9
| @@ -0,0 +1,164 @@ | ||
| 1 | <?php | |
| 2 | ||
| 3 | namespace Cion\TextToSpeech\Tests; | |
| 4 | ||
| 5 | use Cion\TextToSpeech\Contracts\Source; | |
| 6 | use Cion\TextToSpeech\Converters\SixtyDBConverter; | |
| 7 | use Cion\TextToSpeech\Sources\TextSource; | |
| 8 | use Cion\TextToSpeech\TextToSpeechManager; | |
| 9 | use GuzzleHttp\Client; | |
| 10 | use GuzzleHttp\Handler\MockHandler; | |
| 11 | use GuzzleHttp\HandlerStack; | |
| 12 | use GuzzleHttp\Middleware; | |
| 13 | use GuzzleHttp\Psr7\Response; | |
| 14 | use Illuminate\Config\Repository; | |
| 15 | use Illuminate\Container\Container; | |
| 16 | use Illuminate\Filesystem\FilesystemManager; | |
| 17 | use Illuminate\Support\Facades\Facade; | |
| 18 | use PHPUnit\Framework\TestCase; | |
| 19 | ||
| 20 | class SixtyDBConverterTest extends TestCase | |
| 21 | { | |
| 22 | private $root; | |
| 23 | private $previousContainer; | |
| 24 | private $previousFacade; | |
| 25 | private $container; | |
| 26 | private $history = []; | |
| 27 | ||
| 28 | protected function setUp(): void | |
| 29 | { | |
| 30 | $this->root = sys_get_temp_dir().'/sixtydb-'.bin2hex(random_bytes(8)); | |
| 31 | mkdir($this->root); | |
| 32 | $this->previousContainer = Container::getInstance(); | |
| 33 | $this->previousFacade = Facade::getFacadeApplication(); | |
| 34 | $this->container = new Container(); | |
| 35 | Container::setInstance($this->container); | |
| 36 | $this->container->instance('config', new Repository([ | |
| 37 | 'tts' => ['disk' => 'local', 'text_type' => 'text', 'driver' => 'sixtydb', 'services' => ['sixtydb' => ['api_key' => 'test', 'voice_id' => 'workspace-voice']]], | |
| 38 | 'filesystems' => ['default' => 'local', 'disks' => ['local' => ['driver' => 'local', 'root' => $this->root]]], | |
| 39 | ])); | |
| 40 | $this->container->instance('filesystem', new FilesystemManager($this->container)); | |
| 41 | $this->container->bind(Source::class, TextSource::class); | |
| 42 | Facade::clearResolvedInstances(); | |
| 43 | Facade::setFacadeApplication($this->container); | |
| 44 | } | |
| 45 | ||
| 46 | protected function tearDown(): void | |
| 47 | { | |
| 48 | foreach (glob($this->root.'/*') as $file) { | |
| 49 | unlink($file); | |
| 50 | } | |
| 51 | rmdir($this->root); | |
| 52 | Facade::clearResolvedInstances(); | |
| 53 | Facade::setFacadeApplication($this->previousFacade); | |
| 54 | Container::setInstance($this->previousContainer); | |
| 55 | } | |
| 56 | ||
| 57 | private function converter($body, $type = 'application/json', $status = 200) | |
| 58 | { | |
| 59 | $stack = HandlerStack::create(new MockHandler([new Response($status, ['Content-Type' => $type], $body)])); | |
| 60 | $stack->push(Middleware::history($this->history)); | |
| 61 | ||
| 62 | return (new SixtyDBConverter(new Client(['handler' => $stack]), ['api_key' => 'test', 'voice_id' => 'workspace-voice']))->saveTo('speech.wav'); | |
| 63 | } | |
| 64 | ||
| 65 | public function testItStoresPcmAsWavAndUsesWorkspaceVoice(): void | |
| 66 | { | |
| 67 | $converter = $this->converter(json_encode(['audioContent' => base64_encode("\x01\x00\x02\x00")])); | |
| 68 | $this->assertSame('speech.wav', $converter->language('en-US')->convert('Hello', ['speed' => 1.5, 'model' => 'chosen-model'])); | |
| 69 | $wav = file_get_contents($this->root.'/speech.wav'); | |
| 70 | $this->assertSame('RIFF', substr($wav, 0, 4)); | |
| 71 | $this->assertSame("\x01\x00\x02\x00", substr($wav, 44)); | |
| 72 | $request = $this->history[0]['request']; | |
| 73 | $this->assertSame('https://api.60db.ai/tts-synthesize', (string) $request->getUri()); | |
| 74 | $this->assertSame('Bearer test', $request->getHeaderLine('Authorization')); | |
| 75 | $payload = json_decode((string) $request->getBody(), true); | |
| 76 | $this->assertSame('workspace-voice', $payload['voice_id']); | |
| 77 | $this->assertSame('en', $payload['target_language']); | |
| 78 | $this->assertSame(24000, $payload['audio_config']['sample_rate_hertz']); | |
| 79 | $this->assertSame('chosen-model', $payload['model_id']); | |
| 80 | $this->assertFalse($this->history[0]['options']['allow_redirects']); | |
| 81 | } | |
| 82 | ||
| 83 | public function testItHandlesNdjsonAndDoubleEncodedEnvelopes(): void | |
| 84 | { | |
| 85 | $inner = json_encode(['audioContent' => base64_encode("\x01\x00")]); | |
| 86 | $body = json_encode(['audioContent' => base64_encode($inner)])."\n".json_encode(['audio_base64' => base64_encode("\x02\x00")]); | |
| 87 | $this->converter($body, 'application/x-ndjson')->convert('Hello'); | |
| 88 | $this->assertSame("\x01\x00\x02\x00", substr(file_get_contents($this->root.'/speech.wav'), 44)); | |
| 89 | } | |
| 90 | ||
| 91 | public function testItAcceptsValidWavAndPcmBeginningWithBrace(): void | |
| 92 | { | |
| 93 | $pcm = "{\x00\x02\x00"; | |
| 94 | $wav = 'RIFF'.pack('V', 40).'WAVEfmt '.pack('VvvVVvv', 16, 1, 1, 24000, 48000, 2, 16).'data'.pack('V', 4).$pcm; | |
| 95 | $this->converter($wav, 'audio/wav')->convert('Hello'); | |
| 96 | $this->assertSame($wav, file_get_contents($this->root.'/speech.wav')); | |
| 97 | $this->converter(json_encode(['audioContent' => base64_encode($pcm)]))->convert('Hello'); | |
| 98 | $this->assertSame($wav, file_get_contents($this->root.'/speech.wav')); | |
| 99 | } | |
| 100 | ||
| 101 | public function testItRejectsErrorAndInvalidAudioWithoutSaving(): void | |
| 102 | { | |
| 103 | foreach (['{"success":false}', '{"audioContent":"!"}', '{"sample_rate":16000,"audioContent":"AAA="}', '{"audioContent":"AA=="}', '{}', 'not-json'] as $body) { | |
| 104 | try { | |
| 105 | $this->converter($body)->convert('Hello'); | |
| 106 | $this->fail('Invalid synthesis response accepted'); | |
| 107 | } catch (\RuntimeException $error) { | |
| 108 | $this->assertFileDoesNotExist($this->root.'/speech.wav'); | |
| 109 | } | |
| 110 | } | |
| 111 | $this->expectException(\RuntimeException::class); | |
| 112 | $this->converter('denied', 'application/json', 401)->convert('Hello'); | |
| 113 | } | |
| 114 | ||
| 115 | public function testItRejectsUnsupportedInputBeforeSending(): void | |
| 116 | { | |
| 117 | foreach ([['', []], ['Hello', ['speed' => 3]], ['Hello', ['format' => 'mp3']], ['Hello', ['model' => []]], [str_repeat('a', 5001), []]] as $case) { | |
| 118 | try { | |
| 119 | $this->converter('{}')->convert($case[0], $case[1]); | |
| 120 | $this->fail('Invalid input accepted'); | |
| 121 | } catch (\InvalidArgumentException $error) { | |
| 122 | $this->assertSame([], $this->history); | |
| 123 | } | |
| 124 | } | |
| 125 | $this->expectException(\InvalidArgumentException::class); | |
| 126 | $this->converter('{}')->ssml()->convert('<speak>Hello</speak>'); | |
| 127 | } | |
| 128 | ||
| 129 | public function testItRejectsCompressedTruncatedAndOversizedBodies(): void | |
| 130 | { | |
| 131 | foreach ([['ID3compressed', 'audio/pcm'], ['RIFF'.pack('V', 99).'WAVE', 'audio/wav'], ['unexpected', 'text/html'], [str_repeat("\x00", 32 * 1024 * 1024 + 2), 'audio/pcm']] as $case) { | |
| 132 | try { | |
| 133 | $this->converter($case[0], $case[1])->convert('Hello'); | |
| 134 | $this->fail('Invalid binary audio accepted'); | |
| 135 | } catch (\RuntimeException $error) { | |
| 136 | $this->assertFileDoesNotExist($this->root.'/speech.wav'); | |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | public function testStorageFailureDoesNotReturnASuccessPath(): void | |
| 142 | { | |
| 143 | $this->container->instance('filesystem', new class | |
| 144 | { | |
| 145 | public function disk($name) | |
| 146 | { | |
| 147 | return $this; | |
| 148 | } | |
| 149 | ||
| 150 | public function put($path, $bytes) | |
| 151 | { | |
| 152 | return false; | |
| 153 | } | |
| 154 | }); | |
| 155 | Facade::clearResolvedInstances(); | |
| 156 | $this->expectException(\RuntimeException::class); | |
| 157 | $this->converter('{"audioContent":"AAA="}')->convert('Hello'); | |
| 158 | } | |
| 159 | ||
| 160 | public function testManagerResolvesTheNewDriver(): void | |
| 161 | { | |
| 162 | $this->assertInstanceOf(SixtyDBConverter::class, (new TextToSpeechManager($this->container))->engine('sixtydb')); | |
| 163 | } | |
| 164 | } | |