mock(GraphClientInterface::class, function ($mock): void { $mock->shouldReceive('listPolicies')->never(); $mock->shouldReceive('getPolicy')->never(); $mock->shouldReceive('getOrganization')->never(); $mock->shouldReceive('applyPolicy')->never(); $mock->shouldReceive('getServicePrincipalPermissions')->never(); $mock->shouldReceive('request')->never(); }); }); test('operator can run now and it persists a database notification', function () { Queue::fake([RunBackupScheduleJob::class]); [$user, $tenant] = createUserWithTenant(role: 'operator'); $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, ]); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableAction('runNow', $schedule); $operationRun = OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->first(); expect($operationRun)->not->toBeNull(); expect($operationRun->user_id)->toBe($user->id); expect($operationRun->context)->toMatchArray([ 'backup_schedule_id' => (int) $schedule->id, 'trigger' => 'run_now', ]); Queue::assertPushed(RunBackupScheduleJob::class, function (RunBackupScheduleJob $job) use ($schedule, $operationRun): bool { return $job->backupScheduleRunId === 0 && $job->backupScheduleId === (int) $schedule->getKey() && $job->operationRun instanceof OperationRun && $job->operationRun->is($operationRun); }); $this->assertDatabaseCount('notifications', 1); $this->assertDatabaseHas('notifications', [ 'notifiable_id' => $user->id, 'notifiable_type' => User::class, 'type' => OperationRunQueued::class, 'data->format' => 'filament', 'data->title' => 'Backup schedule run queued', ]); $notification = $user->notifications()->latest('id')->first(); expect($notification)->not->toBeNull(); expect($notification->data['actions'][0]['url'] ?? null) ->toBe(OperationRunLinks::view($operationRun, $tenant)); }); test('run now is unique per click (no dedupe)', function () { Queue::fake([RunBackupScheduleJob::class]); [$user, $tenant] = createUserWithTenant(role: 'operator'); $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, ]); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableAction('runNow', $schedule); Livewire::test(ListBackupSchedules::class) ->callTableAction('runNow', $schedule); $runs = OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->pluck('id') ->all(); expect($runs)->toHaveCount(2); expect($runs[0])->not->toBe($runs[1]); Queue::assertPushed(RunBackupScheduleJob::class, 2); $this->assertDatabaseCount('notifications', 2); }); test('operator can retry and it persists a database notification', function () { Queue::fake([RunBackupScheduleJob::class]); [$user, $tenant] = createUserWithTenant(role: 'operator'); $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, ]); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableAction('retry', $schedule); $operationRun = OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->first(); expect($operationRun)->not->toBeNull(); expect($operationRun->user_id)->toBe($user->id); expect($operationRun->context)->toMatchArray([ 'backup_schedule_id' => (int) $schedule->id, 'trigger' => 'retry', ]); Queue::assertPushed(RunBackupScheduleJob::class, function (RunBackupScheduleJob $job) use ($schedule, $operationRun): bool { return $job->backupScheduleRunId === 0 && $job->backupScheduleId === (int) $schedule->getKey() && $job->operationRun instanceof OperationRun && $job->operationRun->is($operationRun); }); $this->assertDatabaseCount('notifications', 1); $this->assertDatabaseHas('notifications', [ 'notifiable_id' => $user->id, 'notifiable_type' => User::class, 'type' => OperationRunQueued::class, 'data->format' => 'filament', 'data->title' => 'Backup schedule run queued', ]); $notification = $user->notifications()->latest('id')->first(); expect($notification)->not->toBeNull(); expect($notification->data['actions'][0]['url'] ?? null) ->toBe(OperationRunLinks::view($operationRun, $tenant)); }); test('retry is unique per click (no dedupe)', function () { Queue::fake([RunBackupScheduleJob::class]); [$user, $tenant] = createUserWithTenant(role: 'operator'); $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, ]); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableAction('retry', $schedule); Livewire::test(ListBackupSchedules::class) ->callTableAction('retry', $schedule); $runs = OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->pluck('id') ->all(); expect($runs)->toHaveCount(2); expect($runs[0])->not->toBe($runs[1]); Queue::assertPushed(RunBackupScheduleJob::class, 2); $this->assertDatabaseCount('notifications', 2); }); test('readonly cannot dispatch run now or retry', function () { [$user, $tenant] = createUserWithTenant(role: 'readonly'); $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, ]); $this->actingAs($user); Filament::setTenant($tenant, true); try { Livewire::test(ListBackupSchedules::class) ->callTableAction('runNow', $schedule); } catch (\Throwable) { // Action should be hidden/blocked for readonly users. } try { Livewire::test(ListBackupSchedules::class) ->callTableAction('retry', $schedule); } catch (\Throwable) { // Action should be hidden/blocked for readonly users. } expect(OperationRun::query() ->where('tenant_id', $tenant->id) ->whereIn('type', ['backup_schedule_run', 'backup_schedule_run']) ->count()) ->toBe(0); }); test('operator can bulk run now and it persists a database notification', function () { Queue::fake([RunBackupScheduleJob::class]); [$user, $tenant] = createUserWithTenant(role: 'operator'); $scheduleA = BackupSchedule::query()->create([ 'tenant_id' => $tenant->id, 'name' => 'Nightly A', '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, ]); $scheduleB = BackupSchedule::query()->create([ 'tenant_id' => $tenant->id, 'name' => 'Nightly B', 'is_enabled' => true, 'timezone' => 'UTC', 'frequency' => 'daily', 'time_of_day' => '02:00:00', 'days_of_week' => null, 'policy_types' => ['deviceConfiguration'], 'include_foundations' => true, 'retention_keep_last' => 30, ]); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableBulkAction('bulk_run_now', collect([$scheduleA, $scheduleB])); expect(OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->count()) ->toBe(2); expect(OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->pluck('user_id') ->unique() ->values() ->all()) ->toBe([$user->id]); Queue::assertPushed(RunBackupScheduleJob::class, 2); $this->assertDatabaseCount('notifications', 1); $this->assertDatabaseHas('notifications', [ 'notifiable_id' => $user->id, 'data->format' => 'filament', 'data->title' => 'Runs dispatched', ]); $notification = $user->notifications()->latest('id')->first(); expect($notification)->not->toBeNull(); expect($notification->data['actions'][0]['url'] ?? null) ->toBe(OperationRunLinks::index($tenant)); }); test('operator can bulk retry and it persists a database notification', function () { Queue::fake([RunBackupScheduleJob::class]); [$user, $tenant] = createUserWithTenant(role: 'operator'); $scheduleA = BackupSchedule::query()->create([ 'tenant_id' => $tenant->id, 'name' => 'Nightly A', '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, ]); $scheduleB = BackupSchedule::query()->create([ 'tenant_id' => $tenant->id, 'name' => 'Nightly B', 'is_enabled' => true, 'timezone' => 'UTC', 'frequency' => 'daily', 'time_of_day' => '02:00:00', 'days_of_week' => null, 'policy_types' => ['deviceConfiguration'], 'include_foundations' => true, 'retention_keep_last' => 30, ]); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableBulkAction('bulk_retry', collect([$scheduleA, $scheduleB])); expect(OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->count()) ->toBe(2); expect(OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->pluck('user_id') ->unique() ->values() ->all()) ->toBe([$user->id]); Queue::assertPushed(RunBackupScheduleJob::class, 2); $this->assertDatabaseCount('notifications', 1); $this->assertDatabaseHas('notifications', [ 'notifiable_id' => $user->id, 'data->format' => 'filament', 'data->title' => 'Retries dispatched', ]); $notification = $user->notifications()->latest('id')->first(); expect($notification)->not->toBeNull(); expect($notification->data['actions'][0]['url'] ?? null) ->toBe(OperationRunLinks::index($tenant)); }); test('operator can bulk retry even if a previous canonical run exists', function () { Queue::fake([RunBackupScheduleJob::class]); $frozenNow = CarbonImmutable::parse('2026-02-10 01:04:06', 'UTC'); CarbonImmutable::setTestNow($frozenNow); try { [$user, $tenant] = createUserWithTenant(role: 'operator'); $scheduleA = BackupSchedule::query()->create([ 'tenant_id' => $tenant->id, 'name' => 'Nightly A', '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, ]); $scheduleB = BackupSchedule::query()->create([ 'tenant_id' => $tenant->id, 'name' => 'Nightly B', 'is_enabled' => true, 'timezone' => 'UTC', 'frequency' => 'daily', 'time_of_day' => '02:00:00', 'days_of_week' => null, 'policy_types' => ['deviceConfiguration'], 'include_foundations' => true, 'retention_keep_last' => 30, ]); /** @var OperationRunService $operationRunService */ $operationRunService = app(OperationRunService::class); $existing = $operationRunService->ensureRunWithIdentity( tenant: $tenant, type: 'backup_schedule_run', identityInputs: [ 'backup_schedule_id' => (int) $scheduleA->getKey(), 'nonce' => 'existing', ], context: [ 'backup_schedule_id' => (int) $scheduleA->getKey(), 'trigger' => 'retry', ], initiator: $user, ); $operationRunService->updateRun($existing, status: 'completed', outcome: 'succeeded'); $this->actingAs($user); Filament::setTenant($tenant, true); Livewire::test(ListBackupSchedules::class) ->callTableBulkAction('bulk_retry', collect([$scheduleA, $scheduleB])); expect(OperationRun::query() ->where('tenant_id', $tenant->id) ->where('type', 'backup_schedule_run') ->count()) ->toBe(3); Queue::assertPushed(RunBackupScheduleJob::class, 2); } finally { CarbonImmutable::setTestNow(); } });