|
9 | 9 | use Illuminate\Support\Facades\Schema; |
10 | 10 | use Illuminate\Support\Str; |
11 | 11 | use Tests\TestCase; |
| 12 | +use Workflow\V2\Contracts\HistoryProjectionRole; |
12 | 13 | use Workflow\V2\Enums\HistoryEventType; |
13 | 14 | use Workflow\V2\Enums\RunStatus; |
| 15 | +use Workflow\V2\Models\ActivityAttempt; |
14 | 16 | use Workflow\V2\Models\ActivityExecution; |
15 | 17 | use Workflow\V2\Models\WorkflowHistoryEvent; |
16 | 18 | use Workflow\V2\Models\WorkflowInstance; |
|
19 | 21 | use Workflow\V2\Models\WorkflowRunSummary; |
20 | 22 | use Workflow\V2\Models\WorkflowRunTimerEntry; |
21 | 23 | use Workflow\V2\Models\WorkflowRunWait; |
| 24 | +use Workflow\V2\Models\WorkflowTask; |
22 | 25 | use Workflow\V2\Models\WorkflowTimelineEntry; |
23 | 26 | use Workflow\V2\Models\WorkflowTimer; |
| 27 | +use Workflow\V2\Support\DefaultHistoryProjectionRole; |
24 | 28 | use Workflow\V2\Support\RunSummaryProjector; |
25 | 29 |
|
26 | 30 | final class V2RebuildProjectionsCommandTest extends TestCase |
27 | 31 | { |
| 32 | + public function testItUsesTheHistoryProjectionRoleBindingForRebuilds(): void |
| 33 | + { |
| 34 | + [, $run] = $this->createCompletedRun('projection-command-history-role'); |
| 35 | + |
| 36 | + $customRole = new class(new DefaultHistoryProjectionRole()) implements HistoryProjectionRole { |
| 37 | + /** |
| 38 | + * @var list<string> |
| 39 | + */ |
| 40 | + public array $projectedRunIds = []; |
| 41 | + |
| 42 | + public function __construct( |
| 43 | + private readonly DefaultHistoryProjectionRole $delegate, |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + public function projectRun(WorkflowRun $run): WorkflowRunSummary |
| 48 | + { |
| 49 | + $this->projectedRunIds[] = $run->id; |
| 50 | + |
| 51 | + return $this->delegate->projectRun($run); |
| 52 | + } |
| 53 | + |
| 54 | + public function recordActivityStarted( |
| 55 | + WorkflowRun $run, |
| 56 | + ActivityExecution $execution, |
| 57 | + ActivityAttempt $attempt, |
| 58 | + WorkflowTask $task, |
| 59 | + ): WorkflowRunSummary { |
| 60 | + return $this->delegate->recordActivityStarted($run, $execution, $attempt, $task); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + $this->app->instance(HistoryProjectionRole::class, $customRole); |
| 65 | + |
| 66 | + $this->artisan('workflow:v2:rebuild-projections', [ |
| 67 | + '--run-id' => [$run->id], |
| 68 | + ]) |
| 69 | + ->expectsOutput('Rebuilt 1 run-summary projection row(s).') |
| 70 | + ->assertSuccessful(); |
| 71 | + |
| 72 | + $this->assertSame([$run->id], $customRole->projectedRunIds); |
| 73 | + $this->assertDatabaseHas('workflow_run_summaries', [ |
| 74 | + 'id' => $run->id, |
| 75 | + 'workflow_instance_id' => $run->workflow_instance_id, |
| 76 | + 'status' => RunStatus::Completed->value, |
| 77 | + ]); |
| 78 | + } |
| 79 | + |
| 80 | + public function testItReportsHistoryProjectionRoleFailures(): void |
| 81 | + { |
| 82 | + [, $run] = $this->createCompletedRun('projection-command-history-role-failure'); |
| 83 | + |
| 84 | + $failingRole = new class() implements HistoryProjectionRole { |
| 85 | + public function projectRun(WorkflowRun $run): WorkflowRunSummary |
| 86 | + { |
| 87 | + throw new \RuntimeException('projection seam exploded'); |
| 88 | + } |
| 89 | + |
| 90 | + public function recordActivityStarted( |
| 91 | + WorkflowRun $run, |
| 92 | + ActivityExecution $execution, |
| 93 | + ActivityAttempt $attempt, |
| 94 | + WorkflowTask $task, |
| 95 | + ): WorkflowRunSummary { |
| 96 | + throw new \RuntimeException('unused'); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + $this->app->instance(HistoryProjectionRole::class, $failingRole); |
| 101 | + |
| 102 | + $this->artisan('workflow:v2:rebuild-projections', [ |
| 103 | + '--run-id' => [$run->id], |
| 104 | + ]) |
| 105 | + ->expectsOutput('Rebuilt 0 run-summary projection row(s).') |
| 106 | + ->expectsOutput(sprintf('Failed to rebuild run [%s]: projection seam exploded', $run->id)) |
| 107 | + ->assertFailed(); |
| 108 | + |
| 109 | + $this->assertDatabaseMissing('workflow_run_summaries', [ |
| 110 | + 'id' => $run->id, |
| 111 | + ]); |
| 112 | + } |
| 113 | + |
28 | 114 | public function testItRebuildsMissingRunSummariesAndPrunesStaleRows(): void |
29 | 115 | { |
30 | 116 | Carbon::setTestNow('2026-04-09 12:00:00'); |
|
0 commit comments