TenantAtlas/tests/Feature/BulkProgressNotificationTest.php
ahmido a62c855851 feat/032-backup-scheduling-mvp (#36)
Adds Backup Scheduling MVP (CRUD, dispatcher, run job, retention, audit logs)
Run now / Retry persist Filament DB notifications
Bulk Run/Retry now create BulkOperationRun so bottom-right progress widget shows them
Progress widget includes “recent finished” window + reconciles stale backup bulk runs
Adds purge command + migration backup_schedule_runs.user_id + tests updates

Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local>
Reviewed-on: #36
2026-01-07 01:12:12 +00:00

106 lines
3.1 KiB
PHP

<?php
use App\Livewire\BulkOperationProgress;
use App\Models\BackupSchedule;
use App\Models\BackupScheduleRun;
use App\Models\BulkOperationRun;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
test('progress widget shows running operations for current tenant and user', function () {
$tenant = Tenant::factory()->create();
$tenant->makeCurrent();
$user = User::factory()->create();
// Own running op
BulkOperationRun::factory()->create([
'tenant_id' => $tenant->id,
'user_id' => $user->id,
'status' => 'running',
'resource' => 'policy',
'action' => 'delete',
'total_items' => 100,
'processed_items' => 50,
]);
// Completed op (should not show)
BulkOperationRun::factory()->create([
'tenant_id' => $tenant->id,
'user_id' => $user->id,
'status' => 'completed',
'updated_at' => now()->subMinutes(5),
]);
// Other user's op (should not show)
$otherUser = User::factory()->create();
BulkOperationRun::factory()->create([
'tenant_id' => $tenant->id,
'user_id' => $otherUser->id,
'status' => 'running',
]);
auth()->login($user); // Login user explicitly for auth()->id() call in component
Livewire::actingAs($user)
->test(BulkOperationProgress::class)
->assertSee('Delete Policy')
->assertSee('50 / 100');
});
test('progress widget reconciles stale pending backup schedule runs', function () {
$tenant = Tenant::factory()->create();
$tenant->makeCurrent();
$user = User::factory()->create();
$schedule = BackupSchedule::query()->create([
'tenant_id' => $tenant->id,
'name' => 'Nightly',
'is_enabled' => true,
'timezone' => 'UTC',
'frequency' => 'daily',
'time_of_day' => '01:00:00',
'days_of_week' => null,
'policy_types' => ['deviceConfiguration'],
'include_foundations' => true,
'retention_keep_last' => 30,
'next_run_at' => now()->addHour(),
]);
$bulkRun = BulkOperationRun::factory()->create([
'tenant_id' => $tenant->id,
'user_id' => $user->id,
'status' => 'pending',
'resource' => 'backup_schedule',
'action' => 'run',
'total_items' => 1,
'processed_items' => 0,
'item_ids' => [(string) $schedule->id],
'created_at' => now()->subMinutes(2),
'updated_at' => now()->subMinutes(2),
]);
BackupScheduleRun::query()->create([
'backup_schedule_id' => $schedule->id,
'tenant_id' => $tenant->id,
'user_id' => $user->id,
'scheduled_for' => now()->startOfMinute(),
'started_at' => now()->subMinute(),
'finished_at' => now(),
'status' => BackupScheduleRun::STATUS_SUCCESS,
'summary' => null,
]);
auth()->login($user);
Livewire::actingAs($user)
->test(BulkOperationProgress::class)
->assertSee('Run Backup schedule')
->assertSee('1 / 1');
expect($bulkRun->refresh()->status)->toBe('completed');
});