|
2 | 2 |
|
3 | 3 | namespace Waterline\Tests\Feature; |
4 | 4 |
|
| 5 | +use Illuminate\Support\Carbon; |
| 6 | +use Illuminate\Support\Str; |
5 | 7 | use Waterline\Tests\TestCase; |
6 | 8 | use Waterline\Tests\Fixtures\V2\TestCommandContractWorkflow; |
7 | 9 | use Workflow\V2\Enums\HistoryEventType; |
| 10 | +use Workflow\V2\Enums\TaskStatus; |
| 11 | +use Workflow\V2\Enums\TaskType; |
8 | 12 | use Workflow\V2\Models\WorkflowInstance; |
9 | 13 | use Workflow\V2\Models\WorkflowHistoryEvent; |
10 | 14 | use Workflow\V2\Models\WorkflowRun; |
11 | 15 | use Workflow\V2\Models\WorkflowRunSummary; |
| 16 | +use Workflow\V2\Models\WorkflowTask; |
| 17 | +use Workflow\V2\Support\RunSummaryProjector; |
12 | 18 | use Workflow\V2\Support\WorkerCompatibilityFleet; |
13 | 19 |
|
14 | 20 | class V2HealthControllerTest extends TestCase |
@@ -40,6 +46,29 @@ public function testHealthEndpointReturnsV2HealthSnapshot(): void |
40 | 46 | ->assertJsonPath('operator_metrics.backend.supported', true); |
41 | 47 | } |
42 | 48 |
|
| 49 | + public function testHealthEndpointScopesSnapshotToConfiguredNamespace(): void |
| 50 | + { |
| 51 | + config()->set('queue.default', 'redis'); |
| 52 | + config()->set('queue.connections.redis.driver', 'redis'); |
| 53 | + config()->set('cache.default', 'file'); |
| 54 | + config()->set('waterline.namespace', 'billing'); |
| 55 | + |
| 56 | + Carbon::setTestNow('2026-04-09 12:00:00'); |
| 57 | + $this->beforeApplicationDestroyed(static function (): void { |
| 58 | + Carbon::setTestNow(); |
| 59 | + }); |
| 60 | + |
| 61 | + $this->createRunSummaryWithReadyTask(namespace: 'billing', availableSecondsAgo: 1); |
| 62 | + $this->createRunSummaryWithReadyTask(namespace: 'shipping', availableSecondsAgo: 10); |
| 63 | + |
| 64 | + $this->get('/waterline/api/v2/health') |
| 65 | + ->assertStatus(200) |
| 66 | + ->assertJsonPath('status', 'ok') |
| 67 | + ->assertJsonPath('operator_metrics.runs.total', 1) |
| 68 | + ->assertJsonPath('operator_metrics.tasks.ready_due', 1) |
| 69 | + ->assertJsonPath('operator_metrics.tasks.oldest_ready_due_at', now()->subSecond()->toJSON()); |
| 70 | + } |
| 71 | + |
43 | 72 | public function testHealthEndpointCategorizesEveryCheckAndExposesWakeAcceleration(): void |
44 | 73 | { |
45 | 74 | config()->set('queue.default', 'redis'); |
@@ -306,4 +335,61 @@ public function testHealthEndpointWarnsForCommandContractSnapshotsNeedingBackfil |
306 | 335 | ->assertJsonPath('operator_metrics.command_contracts.backfill_available_runs', 1) |
307 | 336 | ->assertJsonPath('operator_metrics.command_contracts.backfill_unavailable_runs', 1); |
308 | 337 | } |
| 338 | + |
| 339 | + private function createRunSummaryWithReadyTask(string $namespace, int $availableSecondsAgo): void |
| 340 | + { |
| 341 | + $instanceId = 'waterline-health-'.Str::lower(Str::random(12)); |
| 342 | + $runId = (string) Str::ulid(); |
| 343 | + $workflowType = sprintf('workflow.health.%s', $namespace); |
| 344 | + |
| 345 | + $instance = WorkflowInstance::create([ |
| 346 | + 'id' => $instanceId, |
| 347 | + 'namespace' => $namespace, |
| 348 | + 'workflow_class' => 'WorkflowClass', |
| 349 | + 'workflow_type' => $workflowType, |
| 350 | + 'run_count' => 1, |
| 351 | + ]); |
| 352 | + |
| 353 | + $run = WorkflowRun::create([ |
| 354 | + 'id' => $runId, |
| 355 | + 'workflow_instance_id' => $instance->id, |
| 356 | + 'run_number' => 1, |
| 357 | + 'workflow_class' => 'WorkflowClass', |
| 358 | + 'workflow_type' => $workflowType, |
| 359 | + 'status' => 'running', |
| 360 | + 'namespace' => $namespace, |
| 361 | + 'started_at' => now()->subMinutes(10), |
| 362 | + 'last_progress_at' => now()->subMinute(), |
| 363 | + ]); |
| 364 | + |
| 365 | + $instance->update(['current_run_id' => $run->id]); |
| 366 | + |
| 367 | + WorkflowRunSummary::create([ |
| 368 | + 'id' => $run->id, |
| 369 | + 'workflow_instance_id' => $instance->id, |
| 370 | + 'run_number' => 1, |
| 371 | + 'is_current_run' => true, |
| 372 | + 'engine_source' => 'v2', |
| 373 | + 'class' => 'WorkflowClass', |
| 374 | + 'workflow_type' => $workflowType, |
| 375 | + 'status' => 'running', |
| 376 | + 'status_bucket' => 'running', |
| 377 | + 'namespace' => $namespace, |
| 378 | + 'started_at' => now()->subMinutes(10), |
| 379 | + 'liveness_state' => 'running', |
| 380 | + 'projection_schema_version' => RunSummaryProjector::SCHEMA_VERSION, |
| 381 | + 'created_at' => now()->subMinutes(10), |
| 382 | + 'updated_at' => now(), |
| 383 | + ]); |
| 384 | + |
| 385 | + WorkflowTask::create([ |
| 386 | + 'id' => (string) Str::ulid(), |
| 387 | + 'workflow_run_id' => $run->id, |
| 388 | + 'namespace' => $namespace, |
| 389 | + 'task_type' => TaskType::Workflow->value, |
| 390 | + 'status' => TaskStatus::Ready->value, |
| 391 | + 'queue' => 'default', |
| 392 | + 'available_at' => now()->subSeconds($availableSecondsAgo), |
| 393 | + ]); |
| 394 | + } |
309 | 395 | } |
0 commit comments