engine = new WorkflowEngine; $this->assets = new AssetStorage; $this->handler = new StoryboardHandler(app(AiProviderManager::class), $this->assets); }); test('it breaks the script into scenes, stores them, and completes the step', function () { $scenesJson = json_encode(['scenes' => [ ['scene_number' => 1, 'description' => 'Intro shot', 'visual_prompt' => 'a bright kitchen'], ['scene_number' => 2, 'description' => 'Demo', 'visual_prompt' => 'a chef cooking'], ]]); Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => $scenesJson]]]])]); AiProvider::factory()->create(['provider' => 'openai']); $project = VideoProject::factory()->create(); $this->assets->store($project, 'script', 'A script about cooking.', 'txt'); $step = $this->engine->start($project, [['type' => 'storyboard']]); $this->handler->handle($step, $this->engine); $fresh = $step->fresh(); expect($fresh->status)->toBe('completed'); expect($fresh->output['scene_count'])->toBe(2); $asset = \App\Models\Asset::find($fresh->output['asset_id']); expect($asset->type)->toBe('other'); expect($asset->metadata['kind'])->toBe('storyboard'); $stored = json_decode(Storage::get($asset->disk_path), true); expect($stored['scenes'])->toHaveCount(2); Http::assertSent(function ($request) { return str_contains($request['messages'][0]['content'], 'A script about cooking.') && ($request['response_format']['type'] ?? null) === 'json_object'; }); }); test('a style hint is included in the prompt when given', function () { Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => json_encode(['scenes' => [['scene_number' => 1, 'description' => 'x', 'visual_prompt' => 'y']]])]]]])]); AiProvider::factory()->create(['provider' => 'openai']); $project = VideoProject::factory()->create(); $step = $this->engine->start($project, [['type' => 'storyboard']]); $step->update(['input' => ['script' => 'a script', 'style' => 'noir black and white']]); $this->handler->handle($step->fresh(), $this->engine); Http::assertSent(fn ($request) => str_contains($request['messages'][0]['content'], 'noir black and white')); }); test('it fails when there is no script to storyboard', function () { AiProvider::factory()->create(['provider' => 'openai']); $project = VideoProject::factory()->create(); $step = $this->engine->start($project, [['type' => 'storyboard']]); $this->handler->handle($step, $this->engine); expect($step->fresh()) ->status->toBe('failed') ->error->toContain('No script available'); }); test('it fails when no active OpenAI provider is configured', function () { $project = VideoProject::factory()->create(); $this->assets->store($project, 'script', 'a script', 'txt'); $step = $this->engine->start($project, [['type' => 'storyboard']]); $this->handler->handle($step, $this->engine); expect($step->fresh()) ->status->toBe('failed') ->error->toContain('No active OpenAI provider'); }); test('it fails when the AI response has no usable scenes', function () { Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => '{"not_scenes": []}']]]])]); AiProvider::factory()->create(['provider' => 'openai']); $project = VideoProject::factory()->create(); $this->assets->store($project, 'script', 'a script', 'txt'); $step = $this->engine->start($project, [['type' => 'storyboard']]); $this->handler->handle($step, $this->engine); expect($step->fresh()) ->status->toBe('failed') ->error->toContain('unusable storyboard'); });