|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Feature\V2; |
| 6 | + |
| 7 | +use Illuminate\Support\Facades\Queue; |
| 8 | +use Tests\Fixtures\V2\TestGreetingWorkflow; |
| 9 | +use Tests\TestCase; |
| 10 | +use Workflow\V2\Contracts\WorkflowControlPlane; |
| 11 | +use Workflow\V2\Models\WorkflowRun; |
| 12 | +use Workflow\V2\Models\WorkflowTask; |
| 13 | +use Workflow\V2\Support\WorkerCompatibility; |
| 14 | +use Workflow\V2\Support\WorkerCompatibilityFleet; |
| 15 | + |
| 16 | +/** |
| 17 | + * Replay 2026 worker-versioning parity: a server (or other caller) |
| 18 | + * driving a start through the control plane must be able to pin the |
| 19 | + * new run to a specific worker build id, so subsequent worker pools |
| 20 | + * running a different build cannot break replay. |
| 21 | + * |
| 22 | + * The pin is expressed as the `build_id` option on the start |
| 23 | + * contract; the legacy `WorkerCompatibility::current()` config |
| 24 | + * fallback is preserved when no pin is supplied. |
| 25 | + */ |
| 26 | +final class V2WorkflowVersionPinningTest extends TestCase |
| 27 | +{ |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + config() |
| 33 | + ->set('workflows.v2.compatibility.namespace', null); |
| 34 | + config() |
| 35 | + ->set('workflows.v2.types.workflows', [ |
| 36 | + 'test-greeting-workflow' => TestGreetingWorkflow::class, |
| 37 | + ]); |
| 38 | + WorkerCompatibilityFleet::clear(); |
| 39 | + Queue::fake(); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_start_option_pins_run_and_first_task_to_supplied_build_id(): void |
| 43 | + { |
| 44 | + // No worker context — WorkerCompatibility::current() returns null, |
| 45 | + // so without an explicit pin the run would stay unversioned. |
| 46 | + $this->assertNull(WorkerCompatibility::current()); |
| 47 | + |
| 48 | + $controlPlane = $this->app->make(WorkflowControlPlane::class); |
| 49 | + |
| 50 | + $result = $controlPlane->start('test-greeting-workflow', 'pin-by-build-id', [ |
| 51 | + 'build_id' => 'v2026.05.01-rc1', |
| 52 | + 'arguments' => null, |
| 53 | + ]); |
| 54 | + |
| 55 | + $this->assertTrue($result['started']); |
| 56 | + |
| 57 | + $run = WorkflowRun::query() |
| 58 | + ->where('workflow_instance_id', 'pin-by-build-id') |
| 59 | + ->firstOrFail(); |
| 60 | + |
| 61 | + $this->assertSame('v2026.05.01-rc1', $run->compatibility); |
| 62 | + |
| 63 | + $task = WorkflowTask::query() |
| 64 | + ->where('workflow_run_id', $run->id) |
| 65 | + ->firstOrFail(); |
| 66 | + |
| 67 | + $this->assertSame('v2026.05.01-rc1', $task->compatibility); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_start_option_accepts_compatibility_alias(): void |
| 71 | + { |
| 72 | + $controlPlane = $this->app->make(WorkflowControlPlane::class); |
| 73 | + |
| 74 | + $result = $controlPlane->start('test-greeting-workflow', 'pin-by-compat', [ |
| 75 | + 'compatibility' => 'v2026.05.01-rc1', |
| 76 | + 'arguments' => null, |
| 77 | + ]); |
| 78 | + |
| 79 | + $this->assertTrue($result['started']); |
| 80 | + |
| 81 | + $run = WorkflowRun::query() |
| 82 | + ->where('workflow_instance_id', 'pin-by-compat') |
| 83 | + ->firstOrFail(); |
| 84 | + |
| 85 | + $this->assertSame('v2026.05.01-rc1', $run->compatibility); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_start_falls_back_to_worker_compatibility_current_when_no_pin_supplied(): void |
| 89 | + { |
| 90 | + config() |
| 91 | + ->set('workflows.v2.compatibility.current', 'build-from-worker-context'); |
| 92 | + |
| 93 | + $controlPlane = $this->app->make(WorkflowControlPlane::class); |
| 94 | + |
| 95 | + $controlPlane->start('test-greeting-workflow', 'pin-fallback', [ |
| 96 | + 'arguments' => null, |
| 97 | + ]); |
| 98 | + |
| 99 | + $run = WorkflowRun::query() |
| 100 | + ->where('workflow_instance_id', 'pin-fallback') |
| 101 | + ->firstOrFail(); |
| 102 | + |
| 103 | + $this->assertSame('build-from-worker-context', $run->compatibility); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_explicit_pin_overrides_worker_compatibility_current(): void |
| 107 | + { |
| 108 | + config() |
| 109 | + ->set('workflows.v2.compatibility.current', 'build-from-worker-context'); |
| 110 | + |
| 111 | + $controlPlane = $this->app->make(WorkflowControlPlane::class); |
| 112 | + |
| 113 | + $controlPlane->start('test-greeting-workflow', 'pin-overrides', [ |
| 114 | + 'build_id' => 'operator-routed-build', |
| 115 | + 'arguments' => null, |
| 116 | + ]); |
| 117 | + |
| 118 | + $run = WorkflowRun::query() |
| 119 | + ->where('workflow_instance_id', 'pin-overrides') |
| 120 | + ->firstOrFail(); |
| 121 | + |
| 122 | + $this->assertSame('operator-routed-build', $run->compatibility); |
| 123 | + } |
| 124 | + |
| 125 | + public function test_blank_pin_value_falls_through_to_legacy_resolution(): void |
| 126 | + { |
| 127 | + config() |
| 128 | + ->set('workflows.v2.compatibility.current', 'build-from-worker-context'); |
| 129 | + |
| 130 | + $controlPlane = $this->app->make(WorkflowControlPlane::class); |
| 131 | + |
| 132 | + $controlPlane->start('test-greeting-workflow', 'pin-blank', [ |
| 133 | + 'build_id' => ' ', |
| 134 | + 'arguments' => null, |
| 135 | + ]); |
| 136 | + |
| 137 | + $run = WorkflowRun::query() |
| 138 | + ->where('workflow_instance_id', 'pin-blank') |
| 139 | + ->firstOrFail(); |
| 140 | + |
| 141 | + $this->assertSame('build-from-worker-context', $run->compatibility); |
| 142 | + } |
| 143 | +} |
0 commit comments