Files
muninn/tests/Feature/Jobs/RunWorkflowStepJobTest.php
m105andClaude Sonnet 5 6a51fda61e Add thumbnail, metadata, and YouTube publishing handlers (#40, #41, #42)
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>
2026-08-06 21:26:56 +03:00

80 lines
2.9 KiB
PHP

<?php
use App\Jobs\RunWorkflowStepJob;
use App\Models\AiProvider;
use App\Models\VideoProject;
use App\Support\AiProviders\AiProviderManager;
use App\Support\VideoWorkflow\StepHandlers\StepHandlerManager;
use App\Support\VideoWorkflow\WorkflowEngine;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
beforeEach(function () {
Storage::fake();
$this->engine = new WorkflowEngine;
});
test('it runs the matching handler and completes the step', function () {
Http::fake(['api.openai.com/*' => Http::response(['choices' => [['message' => ['content' => 'a script']]]])]);
AiProvider::factory()->create(['provider' => 'openai']);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'script_generation']]);
(new RunWorkflowStepJob($step->id))->handle(app(StepHandlerManager::class), $this->engine);
expect($step->fresh()->status)->toBe('completed');
});
test('it fails the step when no handler is registered for its type', function () {
$project = VideoProject::factory()->create();
// A made-up type — not a real WorkflowStep::TYPES value — so this test
// stays valid regardless of which real types have handlers over time.
$step = $this->engine->start($project, [['type' => 'some_future_step_type']]);
(new RunWorkflowStepJob($step->id))->handle(app(StepHandlerManager::class), $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toContain('No handler registered');
});
test('it does nothing for a step that is no longer in_progress', function () {
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'script_generation']]);
$this->engine->skipStep($step);
(new RunWorkflowStepJob($step->id))->handle(app(StepHandlerManager::class), $this->engine);
expect($step->fresh()->status)->toBe('skipped');
});
test('it does nothing for a missing step', function () {
(new RunWorkflowStepJob(999999))->handle(app(StepHandlerManager::class), $this->engine);
})->throwsNoExceptions();
test('it fails the step when the handler throws unexpectedly', function () {
$badHandler = new class implements \App\Support\VideoWorkflow\StepHandlers\StepHandler
{
public function type(): string
{
return 'script_generation';
}
public function handle(\App\Models\WorkflowStep $step, WorkflowEngine $engine): void
{
throw new \RuntimeException('boom');
}
};
$manager = new StepHandlerManager([$badHandler]);
$project = VideoProject::factory()->create();
$step = $this->engine->start($project, [['type' => 'script_generation']]);
(new RunWorkflowStepJob($step->id))->handle($manager, $this->engine);
expect($step->fresh())
->status->toBe('failed')
->error->toBe('boom');
});