TenantAtlas/tests/Feature/Drift/GenerateDriftFindingsJobNotificationTest.php
Ahmed Darrazi 0b0c2b70b9 feat: 053 unify runs monitoring
- Add statusBucket/runType semantics for BulkOperationRun\n- Add Monitoring/Operations hub UI (view-only) with filters + related links\n- Add Drift run context + lifecycle notifications and guardrails\n- Add Pest coverage for status buckets and drift notifications
2026-01-16 16:04:33 +01:00

144 lines
5.0 KiB
PHP

<?php
use App\Filament\Resources\BulkOperationRunResource;
use App\Jobs\GenerateDriftFindingsJob;
use App\Models\BulkOperationRun;
use App\Models\InventorySyncRun;
use App\Notifications\RunStatusChangedNotification;
use App\Services\BulkOperationService;
use App\Services\Drift\DriftFindingGenerator;
use Mockery\MockInterface;
test('drift generation job sends completion notification with view link', function () {
[$user, $tenant] = createUserWithTenant(role: 'manager');
$scopeKey = hash('sha256', 'scope-job-notification-success');
$baseline = InventorySyncRun::factory()->for($tenant)->create([
'selection_hash' => $scopeKey,
'status' => InventorySyncRun::STATUS_SUCCESS,
'finished_at' => now()->subDays(2),
]);
$current = InventorySyncRun::factory()->for($tenant)->create([
'selection_hash' => $scopeKey,
'status' => InventorySyncRun::STATUS_SUCCESS,
'finished_at' => now()->subDay(),
]);
$run = BulkOperationRun::factory()->create([
'tenant_id' => $tenant->getKey(),
'user_id' => $user->getKey(),
'resource' => 'drift',
'action' => 'generate',
'status' => 'pending',
'total_items' => 1,
'processed_items' => 0,
'succeeded' => 0,
'failed' => 0,
'skipped' => 0,
'failures' => [],
]);
$this->mock(DriftFindingGenerator::class, function (MockInterface $mock) {
$mock->shouldReceive('generate')->once()->andReturn(0);
});
$job = new GenerateDriftFindingsJob(
tenantId: (int) $tenant->getKey(),
userId: (int) $user->getKey(),
baselineRunId: (int) $baseline->getKey(),
currentRunId: (int) $current->getKey(),
scopeKey: $scopeKey,
bulkOperationRunId: (int) $run->getKey(),
);
$job->handle(app(DriftFindingGenerator::class), app(BulkOperationService::class));
expect($run->refresh()->status)->toBe('completed');
$this->assertDatabaseHas('notifications', [
'notifiable_id' => $user->getKey(),
'notifiable_type' => $user->getMorphClass(),
'type' => RunStatusChangedNotification::class,
]);
$notification = $user->notifications()->latest('id')->first();
expect($notification)->not->toBeNull();
expect($notification->data['actions'][0]['url'] ?? null)
->toBe(BulkOperationRunResource::getUrl('view', ['record' => $run->getKey()], tenant: $tenant));
});
test('drift generation job sends failure notification with view link', function () {
[$user, $tenant] = createUserWithTenant(role: 'manager');
$scopeKey = hash('sha256', 'scope-job-notification-failure');
$baseline = InventorySyncRun::factory()->for($tenant)->create([
'selection_hash' => $scopeKey,
'status' => InventorySyncRun::STATUS_SUCCESS,
'finished_at' => now()->subDays(2),
]);
$current = InventorySyncRun::factory()->for($tenant)->create([
'selection_hash' => $scopeKey,
'status' => InventorySyncRun::STATUS_SUCCESS,
'finished_at' => now()->subDay(),
]);
$run = BulkOperationRun::factory()->create([
'tenant_id' => $tenant->getKey(),
'user_id' => $user->getKey(),
'resource' => 'drift',
'action' => 'generate',
'status' => 'pending',
'total_items' => 1,
'processed_items' => 0,
'succeeded' => 0,
'failed' => 0,
'skipped' => 0,
'failures' => [],
]);
$this->mock(DriftFindingGenerator::class, function (MockInterface $mock) {
$mock->shouldReceive('generate')->once()->andThrow(new RuntimeException('boom'));
});
$job = new GenerateDriftFindingsJob(
tenantId: (int) $tenant->getKey(),
userId: (int) $user->getKey(),
baselineRunId: (int) $baseline->getKey(),
currentRunId: (int) $current->getKey(),
scopeKey: $scopeKey,
bulkOperationRunId: (int) $run->getKey(),
);
try {
$job->handle(app(DriftFindingGenerator::class), app(BulkOperationService::class));
} catch (RuntimeException) {
// Expected.
}
$run->refresh();
expect($run->status)->toBe('failed')
->and($run->processed_items)->toBe(1)
->and($run->failed)->toBe(1)
->and($run->failures)->toBeArray()
->and($run->failures)->toHaveCount(1)
->and($run->failures[0]['item_id'] ?? null)->toBe($scopeKey)
->and($run->failures[0]['reason_code'] ?? null)->toBe('unknown')
->and($run->failures[0]['reason'] ?? null)->toBe('boom');
$this->assertDatabaseHas('notifications', [
'notifiable_id' => $user->getKey(),
'notifiable_type' => $user->getMorphClass(),
'type' => RunStatusChangedNotification::class,
]);
$notification = $user->notifications()->latest('id')->first();
expect($notification)->not->toBeNull();
expect($notification->data['actions'][0]['url'] ?? null)
->toBe(BulkOperationRunResource::getUrl('view', ['record' => $run->getKey()], tenant: $tenant));
});