Foundational layer for the YouTube Content Creation System project, built as a fully independent domain per the earlier decision (task #47) — no foreign keys into the existing projects/tasks/ideas tables. Schema: video_projects, workflow_steps, assets, ai_providers, workflow_templates (named to avoid colliding with the existing Post-templates `templates` table). WorkflowEngine (app/Support/VideoWorkflow/) drives a project's step sequence: start/startFromTemplate, currentStep, completeStep (with optional approval gating), approveStep/rejectStep, failStep/retryStep, skipStep. It only manages state transitions — actual step execution (AI calls, rendering, etc.) is out of scope here (tasks #33+). Also fixes four Publisher classes (WordPress/Reddit/Twitter/LinkedIn) that were missing a `use App\Models\SocialAccount;` import after an earlier interface change, which broke the entire test suite at bootstrap; and PostFactory, which still set the now-removed `social_account_id` column instead of the SocialAccount pivot. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
258 lines
9.1 KiB
PHP
258 lines
9.1 KiB
PHP
<?php
|
|
|
|
use App\Models\VideoProject;
|
|
use App\Models\WorkflowTemplate;
|
|
use App\Support\VideoWorkflow\WorkflowEngine;
|
|
use App\Support\VideoWorkflow\WorkflowTransitionException;
|
|
|
|
beforeEach(function () {
|
|
$this->engine = new WorkflowEngine;
|
|
});
|
|
|
|
test('starting a workflow creates ordered steps and activates the first one', function () {
|
|
$project = VideoProject::factory()->create();
|
|
|
|
$first = $this->engine->start($project, [
|
|
['type' => 'script_generation', 'requires_approval' => true],
|
|
['type' => 'tts_voiceover'],
|
|
['type' => 'video_generation'],
|
|
]);
|
|
|
|
expect($project->fresh()->status)->toBe('in_progress');
|
|
expect($project->steps)->toHaveCount(3);
|
|
|
|
expect($first)
|
|
->type->toBe('script_generation')
|
|
->order->toBe(0)
|
|
->status->toBe('in_progress')
|
|
->requires_approval->toBeTrue();
|
|
expect($first->started_at)->not->toBeNull();
|
|
|
|
expect($project->steps()->where('order', 1)->first())
|
|
->type->toBe('tts_voiceover')
|
|
->status->toBe('pending');
|
|
});
|
|
|
|
test('it refuses to start a project that already has steps', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$this->engine->start($project, [['type' => 'script_generation']]);
|
|
|
|
$this->engine->start($project, [['type' => 'tts_voiceover']]);
|
|
})->throws(WorkflowTransitionException::class);
|
|
|
|
test('it refuses to start a workflow with no steps', function () {
|
|
$project = VideoProject::factory()->create();
|
|
|
|
$this->engine->start($project, []);
|
|
})->throws(WorkflowTransitionException::class);
|
|
|
|
test('startFromTemplate uses the template steps and links the project to it', function () {
|
|
$template = WorkflowTemplate::factory()->create([
|
|
'steps' => [
|
|
['type' => 'script_generation'],
|
|
['type' => 'youtube_publish'],
|
|
],
|
|
]);
|
|
$project = VideoProject::factory()->create();
|
|
|
|
$this->engine->startFromTemplate($project, $template);
|
|
|
|
expect($project->fresh())
|
|
->workflow_template_id->toBe($template->id)
|
|
->status->toBe('in_progress');
|
|
expect($project->steps)->toHaveCount(2);
|
|
});
|
|
|
|
test('currentStep returns the earliest non-terminal step', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$this->engine->start($project, [
|
|
['type' => 'script_generation'],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
|
|
expect($this->engine->currentStep($project)->type)->toBe('script_generation');
|
|
});
|
|
|
|
test('currentStep is null once every step is completed', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation']]);
|
|
|
|
$this->engine->completeStep($step);
|
|
|
|
expect($this->engine->currentStep($project->fresh()))->toBeNull();
|
|
});
|
|
|
|
test('completing a step without approval advances straight to the next step', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [
|
|
['type' => 'script_generation'],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
|
|
$completed = $this->engine->completeStep($step, ['script' => 'Hello world']);
|
|
|
|
expect($completed)
|
|
->status->toBe('completed')
|
|
->output->toBe(['script' => 'Hello world']);
|
|
expect($completed->completed_at)->not->toBeNull();
|
|
|
|
$next = $project->steps()->where('order', 1)->first();
|
|
expect($next->status)->toBe('in_progress');
|
|
});
|
|
|
|
test('completing a step that requires approval stops at awaiting_approval', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [
|
|
['type' => 'script_generation', 'requires_approval' => true],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
|
|
$completed = $this->engine->completeStep($step, ['script' => 'Draft']);
|
|
|
|
expect($completed->status)->toBe('awaiting_approval');
|
|
|
|
$next = $project->steps()->where('order', 1)->first();
|
|
expect($next->status)->toBe('pending');
|
|
});
|
|
|
|
test('completing a step that is not in_progress throws', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation']]);
|
|
$this->engine->completeStep($step);
|
|
|
|
$this->engine->completeStep($step->fresh());
|
|
})->throws(WorkflowTransitionException::class);
|
|
|
|
test('approving a step advances the pipeline', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [
|
|
['type' => 'script_generation', 'requires_approval' => true],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
$this->engine->completeStep($step, ['script' => 'Draft']);
|
|
|
|
$approved = $this->engine->approveStep($step->fresh());
|
|
|
|
expect($approved->status)->toBe('completed');
|
|
expect($project->steps()->where('order', 1)->first()->status)->toBe('in_progress');
|
|
});
|
|
|
|
test('rejecting a step blocks the pipeline without advancing', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [
|
|
['type' => 'script_generation', 'requires_approval' => true],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
$this->engine->completeStep($step, ['script' => 'Bad draft']);
|
|
|
|
$rejected = $this->engine->rejectStep($step->fresh(), 'Tone is wrong');
|
|
|
|
expect($rejected)
|
|
->status->toBe('rejected')
|
|
->error->toBe('Tone is wrong');
|
|
expect($project->steps()->where('order', 1)->first()->status)->toBe('pending');
|
|
});
|
|
|
|
test('approving a step that is not awaiting_approval throws', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation']]);
|
|
|
|
$this->engine->approveStep($step);
|
|
})->throws(WorkflowTransitionException::class);
|
|
|
|
test('a failed step blocks the pipeline and records the error', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [
|
|
['type' => 'script_generation'],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
|
|
$failed = $this->engine->failStep($step, 'OpenAI API timed out');
|
|
|
|
expect($failed)
|
|
->status->toBe('failed')
|
|
->error->toBe('OpenAI API timed out');
|
|
expect($project->steps()->where('order', 1)->first()->status)->toBe('pending');
|
|
});
|
|
|
|
test('a failed step can be retried', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation']]);
|
|
$this->engine->failStep($step, 'timed out');
|
|
|
|
$retried = $this->engine->retryStep($step->fresh());
|
|
|
|
expect($retried)
|
|
->status->toBe('in_progress')
|
|
->error->toBeNull();
|
|
});
|
|
|
|
test('a rejected step can be retried', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation', 'requires_approval' => true]]);
|
|
$this->engine->completeStep($step, ['script' => 'v1']);
|
|
$this->engine->rejectStep($step->fresh(), 'no good');
|
|
|
|
$retried = $this->engine->retryStep($step->fresh());
|
|
|
|
expect($retried)
|
|
->status->toBe('in_progress')
|
|
->output->toBeNull();
|
|
});
|
|
|
|
test('retrying a step that is not failed or rejected throws', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation']]);
|
|
|
|
$this->engine->retryStep($step);
|
|
})->throws(WorkflowTransitionException::class);
|
|
|
|
test('skipping a pending or in_progress step advances the pipeline', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [
|
|
['type' => 'script_generation'],
|
|
['type' => 'tts_voiceover'],
|
|
]);
|
|
|
|
$skipped = $this->engine->skipStep($step);
|
|
|
|
expect($skipped->status)->toBe('skipped');
|
|
expect($project->steps()->where('order', 1)->first()->status)->toBe('in_progress');
|
|
});
|
|
|
|
test('completing the last step marks the project completed', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$step = $this->engine->start($project, [['type' => 'script_generation']]);
|
|
|
|
$this->engine->completeStep($step);
|
|
|
|
expect($project->fresh()->status)->toBe('completed');
|
|
});
|
|
|
|
test('a full multi-step pipeline runs end to end', function () {
|
|
$project = VideoProject::factory()->create();
|
|
$script = $this->engine->start($project, [
|
|
['type' => 'script_generation', 'requires_approval' => true],
|
|
['type' => 'tts_voiceover'],
|
|
['type' => 'video_generation', 'requires_approval' => true],
|
|
]);
|
|
|
|
$this->engine->completeStep($script, ['script' => 'Hello']);
|
|
$script = $script->fresh();
|
|
expect($script->status)->toBe('awaiting_approval');
|
|
$this->engine->approveStep($script);
|
|
|
|
$tts = $this->engine->currentStep($project->fresh());
|
|
expect($tts->type)->toBe('tts_voiceover');
|
|
$this->engine->completeStep($tts, ['audio_path' => '/tmp/a.mp3']);
|
|
|
|
$video = $this->engine->currentStep($project->fresh());
|
|
expect($video->type)->toBe('video_generation');
|
|
$this->engine->completeStep($video, ['video_path' => '/tmp/v.mp4']);
|
|
$this->engine->approveStep($video->fresh());
|
|
|
|
expect($project->fresh())
|
|
->status->toBe('completed');
|
|
expect($this->engine->currentStep($project->fresh()))->toBeNull();
|
|
});
|