ThumbnailGenerationHandler (#40): generates thumbnail candidates via StabilityAI, one per style (default: two contrasting styles) — all stored as assets on the same step, so the existing approval UI's "show every asset on this step side by side" behavior is the A/B comparison the task calls for, no extra UI needed. MetadataGenerationHandler (#41): SEO title/description/tags/hashtags from the project's script via OpenAI in JSON mode, same pattern as StoryboardHandler. YoutubePublishHandler (#42): real OAuth2 + YouTube Data API v3 resumable upload (token refresh -> init session -> PUT video bytes), supporting scheduling via publishAt (forces privacyStatus=private per YouTube's own constraint). Reuses the AiProvider table for credentials (provider='youtube', api_key=refresh_token, config=client_id/secret) rather than a new table — added 'youtube' to AiProvider::PROVIDERS. Does not cover analytics retrieval: that's an ongoing query against a published video, not a one-shot publish action, so it doesn't fit the per-video step model at all. All 8 WorkflowStep::TYPES now have real handlers, so the "unhandled type" test fixtures (which used real-but-not-yet-implemented types) would go stale every time a new module lands — switched them to fabricated type strings so they stay meaningful regardless of what gets built next. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?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\MetadataGenerationHandler;
|
||||
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 MetadataGenerationHandler(app(AiProviderManager::class), $this->assets);
|
||||
});
|
||||
|
||||
test('it generates metadata from the script, stores it, and completes the step', function () {
|
||||
$metadata = [
|
||||
'title' => 'The Best Coffee Gadgets of 2026',
|
||||
'description' => 'A deep dive into coffee gadgets.',
|
||||
'tags' => ['coffee', 'gadgets'],
|
||||
'hashtags' => ['#coffee', '#gadgets'],
|
||||
];
|
||||
Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => json_encode($metadata)]]]])]);
|
||||
AiProvider::factory()->create(['provider' => 'openai']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'script', 'A script about coffee gadgets.', 'txt');
|
||||
$step = $this->engine->start($project, [['type' => 'metadata_generation']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
$fresh = $step->fresh();
|
||||
expect($fresh->status)->toBe('completed');
|
||||
expect($fresh->output['title'])->toBe('The Best Coffee Gadgets of 2026');
|
||||
|
||||
$asset = Asset::find($fresh->output['asset_id']);
|
||||
expect($asset->type)->toBe('other');
|
||||
expect($asset->metadata['kind'])->toBe('metadata');
|
||||
$stored = json_decode(Storage::get($asset->disk_path), true);
|
||||
expect($stored)->toBe($metadata);
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request['messages'][0]['content'], 'A script about coffee gadgets.')
|
||||
&& ($request['response_format']['type'] ?? null) === 'json_object';
|
||||
});
|
||||
});
|
||||
|
||||
test('it fails when there is no script to generate metadata from', function () {
|
||||
AiProvider::factory()->create(['provider' => 'openai']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$step = $this->engine->start($project, [['type' => 'metadata_generation']]);
|
||||
|
||||
$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' => 'metadata_generation']]);
|
||||
|
||||
$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 is missing title or description', function () {
|
||||
Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => '{"tags": []}']]]])]);
|
||||
AiProvider::factory()->create(['provider' => 'openai']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'script', 'a script', 'txt');
|
||||
$step = $this->engine->start($project, [['type' => 'metadata_generation']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toContain('unusable metadata');
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
<?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\ThumbnailGenerationHandler;
|
||||
use App\Support\VideoWorkflow\WorkflowEngine;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
beforeEach(function () {
|
||||
Storage::fake();
|
||||
$this->engine = new WorkflowEngine;
|
||||
$this->handler = new ThumbnailGenerationHandler(app(AiProviderManager::class), new AssetStorage);
|
||||
});
|
||||
|
||||
test('it generates two default-style variants and completes the step', function () {
|
||||
Http::fake(['api.stability.ai/*' => Http::response(['image' => base64_encode('bytes')])]);
|
||||
AiProvider::factory()->create(['provider' => 'stability_ai']);
|
||||
|
||||
$project = VideoProject::factory()->create(['title' => 'Top 10 Gadgets']);
|
||||
$step = $this->engine->start($project, [['type' => 'thumbnail_generation']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
$fresh = $step->fresh();
|
||||
expect($fresh->status)->toBe('completed');
|
||||
expect($fresh->output['variant_count'])->toBe(2);
|
||||
expect($fresh->output['asset_ids'])->toHaveCount(2);
|
||||
|
||||
$assets = Asset::whereIn('id', $fresh->output['asset_ids'])->get();
|
||||
$assets->each(fn ($a) => expect($a->type)->toBe('image'));
|
||||
expect($assets->pluck('metadata.style')->sort()->values()->all())
|
||||
->toBe(['minimalist and clean', 'vibrant and colorful']);
|
||||
|
||||
Http::assertSentCount(2);
|
||||
Http::assertSent(fn ($request) => str_contains($request->body(), 'Top 10 Gadgets'));
|
||||
});
|
||||
|
||||
test('explicit styles in input override the default variants', function () {
|
||||
Http::fake(['api.stability.ai/*' => Http::response(['image' => base64_encode('bytes')])]);
|
||||
AiProvider::factory()->create(['provider' => 'stability_ai']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$step = $this->engine->start($project, [['type' => 'thumbnail_generation']]);
|
||||
$step->update(['input' => ['styles' => ['noir', 'retro', 'futuristic']]]);
|
||||
|
||||
$this->handler->handle($step->fresh(), $this->engine);
|
||||
|
||||
expect($step->fresh()->output['variant_count'])->toBe(3);
|
||||
Http::assertSentCount(3);
|
||||
});
|
||||
|
||||
test('it fails the step when no active StabilityAI provider is configured', function () {
|
||||
$project = VideoProject::factory()->create();
|
||||
$step = $this->engine->start($project, [['type' => 'thumbnail_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 variant failed', function () {
|
||||
Http::fake(['api.stability.ai/*' => Http::response(['errors' => ['bad prompt']], 400)]);
|
||||
AiProvider::factory()->create(['provider' => 'stability_ai']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$step = $this->engine->start($project, [['type' => 'thumbnail_generation']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toContain('variant 1');
|
||||
});
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
use App\Models\AiProvider;
|
||||
use App\Models\VideoProject;
|
||||
use App\Support\AssetStorage\AssetStorage;
|
||||
use App\Support\VideoWorkflow\StepHandlers\YoutubePublishHandler;
|
||||
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 YoutubePublishHandler($this->assets);
|
||||
|
||||
$this->fakeYoutube = function (array $overrides = []) {
|
||||
Http::fake(array_merge([
|
||||
'oauth2.googleapis.com/*' => Http::response(['access_token' => 'access-token-123']),
|
||||
'https://www.googleapis.com/upload/youtube/v3/videos*' => Http::response('', 200, [
|
||||
'Location' => 'https://upload.example.com/session/abc123',
|
||||
]),
|
||||
'upload.example.com/*' => Http::response(['id' => 'yt-video-id']),
|
||||
], $overrides));
|
||||
};
|
||||
});
|
||||
|
||||
test('it uploads the video with metadata and completes the step', function () {
|
||||
($this->fakeYoutube)();
|
||||
AiProvider::factory()->create([
|
||||
'provider' => 'youtube',
|
||||
'api_key' => 'refresh-token-abc',
|
||||
'config' => ['client_id' => 'cid', 'client_secret' => 'csecret'],
|
||||
]);
|
||||
|
||||
$project = VideoProject::factory()->create(['title' => 'Fallback Title']);
|
||||
$this->assets->store($project, 'video', 'fake-mp4-bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$this->assets->store(
|
||||
$project, 'other',
|
||||
json_encode(['title' => 'Great Video', 'description' => 'A great description', 'tags' => ['a', 'b']]),
|
||||
'json', mimeType: 'application/json', metadata: ['kind' => 'metadata'],
|
||||
);
|
||||
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
$fresh = $step->fresh();
|
||||
expect($fresh->status)->toBe('completed');
|
||||
expect($fresh->output['youtube_video_id'])->toBe('yt-video-id');
|
||||
expect($fresh->output['youtube_url'])->toBe('https://www.youtube.com/watch?v=yt-video-id');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://oauth2.googleapis.com/token'
|
||||
&& $request['client_id'] === 'cid'
|
||||
&& $request['client_secret'] === 'csecret'
|
||||
&& $request['refresh_token'] === 'refresh-token-abc'
|
||||
&& $request['grant_type'] === 'refresh_token';
|
||||
});
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_starts_with($request->url(), 'https://www.googleapis.com/upload/youtube/v3/videos')
|
||||
&& $request->hasHeader('Authorization', 'Bearer access-token-123')
|
||||
&& $request->hasHeader('X-Upload-Content-Type', 'video/mp4')
|
||||
&& $request['snippet']['title'] === 'Great Video'
|
||||
&& $request['snippet']['description'] === 'A great description'
|
||||
&& $request['status']['privacyStatus'] === 'private';
|
||||
});
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://upload.example.com/session/abc123'
|
||||
&& $request->body() === 'fake-mp4-bytes';
|
||||
});
|
||||
});
|
||||
|
||||
test('it falls back to the project title/description when there is no metadata asset', function () {
|
||||
($this->fakeYoutube)();
|
||||
AiProvider::factory()->create(['provider' => 'youtube', 'config' => ['client_id' => 'cid', 'client_secret' => 'cs']]);
|
||||
|
||||
$project = VideoProject::factory()->create(['title' => 'Fallback Title', 'description' => 'Fallback description']);
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh()->status)->toBe('completed');
|
||||
|
||||
Http::assertSent(fn ($request) => ($request['snippet']['title'] ?? null) === 'Fallback Title'
|
||||
&& ($request['snippet']['description'] ?? null) === 'Fallback description');
|
||||
});
|
||||
|
||||
test('publish_at input schedules the video and forces privacyStatus to private', function () {
|
||||
($this->fakeYoutube)();
|
||||
AiProvider::factory()->create(['provider' => 'youtube', 'config' => ['client_id' => 'cid', 'client_secret' => 'cs']]);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
$step->update(['input' => ['publish_at' => '2026-12-01T09:00:00Z', 'privacy_status' => 'public']]);
|
||||
|
||||
$this->handler->handle($step->fresh(), $this->engine);
|
||||
|
||||
expect($step->fresh()->status)->toBe('completed');
|
||||
|
||||
Http::assertSent(fn ($request) => ($request['status']['publishAt'] ?? null) === '2026-12-01T09:00:00Z'
|
||||
&& ($request['status']['privacyStatus'] ?? null) === 'private');
|
||||
});
|
||||
|
||||
test('it fails when there is no finished video', function () {
|
||||
AiProvider::factory()->create(['provider' => 'youtube']);
|
||||
$project = VideoProject::factory()->create();
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toContain('No finished video available');
|
||||
});
|
||||
|
||||
test('it fails when no active YouTube credential is configured', function () {
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toContain('No active YouTube credential');
|
||||
});
|
||||
|
||||
test('it fails when the token refresh fails', function () {
|
||||
($this->fakeYoutube)(['oauth2.googleapis.com/*' => Http::response(['error_description' => 'invalid_grant'], 400)]);
|
||||
AiProvider::factory()->create(['provider' => 'youtube']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toContain('invalid_grant');
|
||||
});
|
||||
|
||||
test('it fails when starting the upload session fails', function () {
|
||||
($this->fakeYoutube)(['https://www.googleapis.com/upload/youtube/v3/videos*' => Http::response(['error' => ['message' => 'quota exceeded']], 403)]);
|
||||
AiProvider::factory()->create(['provider' => 'youtube']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toBe('quota exceeded');
|
||||
});
|
||||
|
||||
test('it fails when the upload session response has no Location header', function () {
|
||||
($this->fakeYoutube)(['https://www.googleapis.com/upload/youtube/v3/videos*' => Http::response('', 200)]);
|
||||
AiProvider::factory()->create(['provider' => 'youtube']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toContain('missing upload session location');
|
||||
});
|
||||
|
||||
test('it fails when the final upload fails', function () {
|
||||
($this->fakeYoutube)(['upload.example.com/*' => Http::response(['error' => ['message' => 'upload interrupted']], 500)]);
|
||||
AiProvider::factory()->create(['provider' => 'youtube']);
|
||||
|
||||
$project = VideoProject::factory()->create();
|
||||
$this->assets->store($project, 'video', 'bytes', 'mp4', mimeType: 'video/mp4');
|
||||
$step = $this->engine->start($project, [['type' => 'youtube_publish']]);
|
||||
|
||||
$this->handler->handle($step, $this->engine);
|
||||
|
||||
expect($step->fresh())
|
||||
->status->toBe('failed')
|
||||
->error->toBe('upload interrupted');
|
||||
});
|
||||
Reference in New Issue
Block a user