Files
m105andClaude Sonnet 5 c249a14d78 Add storyboard/video-gen/video-editing handlers and asset previews (#37-39, #45, #46)
StoryboardHandler (#37): breaks the project's script into a numbered
scene-by-scene breakdown (description + image prompt per scene) via
OpenAI in JSON mode (added json_mode support to OpenAiDriver for
reliable structured output), stored as a JSON asset.

VideoGenerationHandler (#38): generates one still image per storyboard
scene via StabilityAI — the scene *visuals*, not motion/video clips
(no text-to-video model integrated; see the task comment for why).

VideoEditingHandler (#39): the step that actually produces a video
file — assembles the scene stills into an MP4 via ffmpeg's concat
demuxer, timed to the voiceover audio's duration (probed via ffprobe)
or a fixed per-scene duration if there's no audio, then muxes the
audio track in. Verified against the real ffmpeg/ffprobe binaries on
this box, not just Process::fake().

Asset previews (#45, #46): a reusable partial renders each asset
inline by type — image/audio/video get real playback elements, text/
JSON gets a truncated <pre> block, everything else a download link.
Wired into both the awaiting_approval step view (so approving/
rejecting is an actual content review, not just a word count) and a
general project assets grid.

Also fixes a latent bug in AssetStorage::url(), found while wiring up
previews: it called Storage::url() on the default 'local' disk, which
has no configured URL driver and throws in production — only worked
before because Storage::fake() is more permissive than the real local
driver. Now serves assets through a new authenticated streaming route
instead (VideoProjectAssetController).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-06 21:15:31 +03:00

112 lines
4.3 KiB
PHP

<?php
use App\Models\AiProvider;
use App\Models\Asset;
use App\Models\VideoProject;
use App\Support\AiProviders\AiProviderManager;
use App\Support\AssetStorage\AssetStorage;
use App\Support\VideoWorkflow\StepHandlers\VideoGenerationHandler;
use App\Support\VideoWorkflow\WorkflowEngine;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
beforeEach(function () {
Storage::fake();
$this->engine = new WorkflowEngine;
$this->assets = new AssetStorage;
$this->handler = new VideoGenerationHandler(app(AiProviderManager::class), $this->assets);
$this->storeStoryboard = function (VideoProject $project, array $scenes) {
return $this->assets->store(
$project,
'other',
json_encode(['scenes' => $scenes]),
'json',
mimeType: 'application/json',
metadata: ['kind' => 'storyboard'],
);
};
});
test('it generates one image per scene and completes the step', function () {
Http::fake(['api.stability.ai/*' => Http::response(['image' => base64_encode('fake-bytes')])]);
AiProvider::factory()->create(['provider' => 'stability_ai']);
$project = VideoProject::factory()->create();
($this->storeStoryboard)($project, [
['scene_number' => 1, 'description' => 'a', 'visual_prompt' => 'a red bicycle'],
['scene_number' => 2, 'description' => 'b', 'visual_prompt' => 'a blue car'],
]);
$step = $this->engine->start($project, [['type' => 'video_generation']]);
$this->handler->handle($step, $this->engine);
$fresh = $step->fresh();
expect($fresh->status)->toBe('completed');
expect($fresh->output['scene_count'])->toBe(2);
expect($fresh->output['asset_ids'])->toHaveCount(2);
$assets = Asset::whereIn('id', $fresh->output['asset_ids'])->get();
expect($assets)->toHaveCount(2);
$assets->each(fn ($a) => expect($a->type)->toBe('image'));
Http::assertSentCount(2);
});
test('a style hint is appended to every scene prompt', function () {
Http::fake(['api.stability.ai/*' => Http::response(['image' => base64_encode('bytes')])]);
AiProvider::factory()->create(['provider' => 'stability_ai']);
$project = VideoProject::factory()->create();
($this->storeStoryboard)($project, [
['scene_number' => 1, 'description' => 'a', 'visual_prompt' => 'a red bicycle'],
]);
$step = $this->engine->start($project, [['type' => 'video_generation']]);
$step->update(['input' => ['style' => 'watercolor']]);
$this->handler->handle($step->fresh(), $this->engine);
// Multipart fields aren't queryable via $request['field'] — check the raw body instead.
Http::assertSent(fn ($request) => str_contains($request->body(), 'watercolor'));
});
test('it fails the step when there is no storyboard', function () {
AiProvider::factory()->create(['provider' => 'stability_ai']);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'video_generation']]);
$this->handler->handle($step, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('No storyboard available');
});
test('it fails the step when no active StabilityAI provider is configured', function () {
$project = VideoProject::factory()->create();
($this->storeStoryboard)($project, [['scene_number' => 1, 'description' => 'a', 'visual_prompt' => 'x']]);
$step = $this->engine->start($project, [['type' => 'video_generation']]);
$this->handler->handle($step, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('No active StabilityAI provider');
});
test('it fails the step and reports which scene failed when generation errors', function () {
Http::fake(['api.stability.ai/*' => Http::response(['errors' => ['bad prompt']], 400)]);
AiProvider::factory()->create(['provider' => 'stability_ai']);
$project = VideoProject::factory()->create();
($this->storeStoryboard)($project, [['scene_number' => 3, 'description' => 'a', 'visual_prompt' => 'x']]);
$step = $this->engine->start($project, [['type' => 'video_generation']]);
$this->handler->handle($step, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('scene 3');
});