engine = new WorkflowEngine; $this->assets = new AssetStorage; $this->handler = new TtsVoiceoverHandler(app(AiProviderManager::class), $this->assets); }); test('it voices explicit input text and completes the step', function () { Http::fake(['api.elevenlabs.io/*' => Http::response('fake-mp3-bytes', 200, ['Content-Type' => 'audio/mpeg'])]); AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'v1']]); $project = VideoProject::factory()->create(); $step = $this->engine->start($project, [['type' => 'tts_voiceover']]); $step->update(['input' => ['text' => 'Hello and welcome']]); $this->handler->handle($step->fresh(), $this->engine); $fresh = $step->fresh(); expect($fresh->status)->toBe('completed'); expect($fresh->output['characters'])->toBe(strlen('Hello and welcome')); $asset = \App\Models\Asset::find($fresh->output['asset_id']); expect($asset->type)->toBe('audio'); expect($asset->mime_type)->toBe('audio/mpeg'); expect(Storage::get($asset->disk_path))->toBe('fake-mp3-bytes'); Http::assertSent(fn ($request) => $request['text'] === 'Hello and welcome'); }); test('it falls back to the latest script asset when no input text is given', function () { Http::fake(['api.elevenlabs.io/*' => Http::response('bytes')]); AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'v1']]); $project = VideoProject::factory()->create(); $this->assets->store($project, 'script', 'Old draft', 'txt'); $this->assets->store($project, 'script', 'Latest draft', 'txt'); $step = $this->engine->start($project, [['type' => 'tts_voiceover']]); $this->handler->handle($step, $this->engine); Http::assertSent(fn ($request) => $request['text'] === 'Latest draft'); }); test('a voice_id in step input overrides the provider default', function () { Http::fake(['api.elevenlabs.io/*' => Http::response('bytes')]); AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'default-voice']]); $project = VideoProject::factory()->create(); $step = $this->engine->start($project, [['type' => 'tts_voiceover']]); $step->update(['input' => ['text' => 'hi', 'voice_id' => 'custom-voice']]); $this->handler->handle($step->fresh(), $this->engine); Http::assertSent(fn ($request) => str_contains($request->url(), '/text-to-speech/custom-voice')); }); test('it fails the step when there is no text to voice', function () { AiProvider::factory()->create(['provider' => 'elevenlabs', 'config' => ['voice_id' => 'v1']]); $project = VideoProject::factory()->create(); $step = $this->engine->start($project, [['type' => 'tts_voiceover']]); $this->handler->handle($step, $this->engine); expect($step->fresh()) ->status->toBe('failed') ->error->toContain('No script text available'); }); test('it fails the step when no active ElevenLabs provider is configured', function () { $project = VideoProject::factory()->create(); $step = $this->engine->start($project, [['type' => 'tts_voiceover']]); $step->update(['input' => ['text' => 'hi']]); $this->handler->handle($step->fresh(), $this->engine); expect($step->fresh()) ->status->toBe('failed') ->error->toContain('No active ElevenLabs provider'); });