Add Video Projects schema and workflow engine core (tasks #31, #32)

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>
This commit is contained in:
m105
2026-08-06 20:38:52 +03:00
co-authored by Claude Sonnet 5
parent efdec9eabd
commit 6ff7f78c9c
25 changed files with 1049 additions and 25 deletions
@@ -0,0 +1,98 @@
<?php
use App\Models\AiProvider;
use App\Models\Asset;
use App\Models\VideoProject;
use App\Models\WorkflowStep;
use App\Models\WorkflowTemplate;
test('a video project has ordered steps', function () {
$project = VideoProject::factory()->create();
WorkflowStep::factory()->for($project)->create(['type' => 'video_generation', 'order' => 1]);
WorkflowStep::factory()->for($project)->create(['type' => 'script_generation', 'order' => 0]);
expect($project->steps->pluck('type')->all())->toBe(['script_generation', 'video_generation']);
});
test('a video project belongs to a workflow template', function () {
$template = WorkflowTemplate::factory()->create();
$project = VideoProject::factory()->create(['workflow_template_id' => $template->id]);
expect($project->workflowTemplate->id)->toBe($template->id);
expect($template->videoProjects->first()->id)->toBe($project->id);
});
test('deleting a video project cascades to its steps and assets', function () {
$project = VideoProject::factory()->create();
$step = WorkflowStep::factory()->for($project)->create();
$asset = Asset::factory()->for($project)->create(['workflow_step_id' => $step->id]);
$project->forceDelete();
expect(WorkflowStep::find($step->id))->toBeNull();
expect(Asset::find($asset->id))->toBeNull();
});
test('a video project is soft deleted by default', function () {
$project = VideoProject::factory()->create();
$project->delete();
expect(VideoProject::find($project->id))->toBeNull();
expect(VideoProject::withTrashed()->find($project->id))->not->toBeNull();
});
test('deleting the workflow step an asset belongs to nulls the reference instead of deleting the asset', function () {
$project = VideoProject::factory()->create();
$step = WorkflowStep::factory()->for($project)->create();
$asset = Asset::factory()->for($project)->create(['workflow_step_id' => $step->id]);
$step->delete();
expect($asset->fresh()->workflow_step_id)->toBeNull();
});
test('workflow step input/output/requires_approval cast correctly', function () {
$step = WorkflowStep::factory()->create([
'requires_approval' => true,
'input' => ['prompt' => 'write a script'],
'output' => ['script' => 'once upon a time'],
]);
expect($step->fresh())
->requires_approval->toBeTrue()
->input->toBe(['prompt' => 'write a script'])
->output->toBe(['script' => 'once upon a time']);
});
test('asset metadata casts to an array', function () {
$asset = Asset::factory()->create(['metadata' => ['duration_seconds' => 42]]);
expect($asset->fresh()->metadata)->toBe(['duration_seconds' => 42]);
});
test('an ai provider stores its config as an array and defaults to active', function () {
$provider = AiProvider::factory()->create([
'provider' => 'elevenlabs',
'config' => ['voice_id' => 'abc123'],
]);
expect($provider->fresh())
->provider->toBe('elevenlabs')
->config->toBe(['voice_id' => 'abc123'])
->is_active->toBeTrue();
});
test('a workflow template stores its steps as an ordered array', function () {
$template = WorkflowTemplate::factory()->create([
'steps' => [
['type' => 'script_generation', 'requires_approval' => true],
['type' => 'youtube_publish', 'requires_approval' => false],
],
]);
expect($template->fresh()->steps)->toBe([
['type' => 'script_generation', 'requires_approval' => true],
['type' => 'youtube_publish', 'requires_approval' => false],
]);
});
@@ -0,0 +1,257 @@
<?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();
});