TenantAtlas/tests/Feature/OperationRunServiceStaleQueuedRunTest.php
ahmido 81c010fa00 fix: Harden SyncPoliciesJob supported types handling (#75)
Harden SyncPoliciesJob type input parsing + fail fast when supported types are empty/mismatched. Pass supported policy types from Tenant sync action and add regression tests.

Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.fritz.box>
Reviewed-on: #75
2026-01-26 19:23:40 +00:00

54 lines
1.7 KiB
PHP

<?php
use App\Models\OperationRun;
use App\Services\OperationRunService;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
it('detects a stale queued run that never started', function () {
$run = OperationRun::factory()->create([
'status' => OperationRunStatus::Queued->value,
'started_at' => null,
'created_at' => now()->subMinutes(6),
]);
$service = app(OperationRunService::class);
expect($service->isStaleQueuedRun($run))->toBeTrue();
});
it('does not treat recent queued runs as stale', function () {
$run = OperationRun::factory()->create([
'status' => OperationRunStatus::Queued->value,
'started_at' => null,
'created_at' => now()->subMinute(),
]);
$service = app(OperationRunService::class);
expect($service->isStaleQueuedRun($run))->toBeFalse();
});
it('fails a stale queued run and marks it completed', function () {
$run = OperationRun::factory()->create([
'status' => OperationRunStatus::Queued->value,
'outcome' => OperationRunOutcome::Pending->value,
'started_at' => null,
'completed_at' => null,
'failure_summary' => [],
]);
$service = app(OperationRunService::class);
$updated = $service->failStaleQueuedRun($run, message: 'Stale test run');
expect($updated->status)->toBe(OperationRunStatus::Completed->value);
expect($updated->outcome)->toBe(OperationRunOutcome::Failed->value);
expect($updated->completed_at)->not->toBeNull();
$failures = is_array($updated->failure_summary ?? null) ? $updated->failure_summary : [];
expect($failures)->not->toBeEmpty();
expect($failures[0]['code'] ?? null)->toBe('run.stale_queued');
});