|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Events\ServerReachabilityChanged; |
| 4 | +use App\Models\Server; |
| 5 | +use App\Models\Team; |
| 6 | +use App\Notifications\Channels\EmailChannel; |
| 7 | +use App\Notifications\Server\Reachable; |
| 8 | +use App\Notifications\Server\Unreachable; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Illuminate\Support\Facades\Notification; |
| 11 | + |
| 12 | +uses(RefreshDatabase::class); |
| 13 | + |
| 14 | +beforeEach(function () { |
| 15 | + $this->team = Team::factory()->create(); |
| 16 | + $this->team->emailNotificationSettings()->update([ |
| 17 | + 'use_instance_email_settings' => true, |
| 18 | + 'server_unreachable_email_notifications' => true, |
| 19 | + 'server_reachable_email_notifications' => true, |
| 20 | + ]); |
| 21 | + |
| 22 | + $this->server = Server::factory()->create(['team_id' => $this->team->id]); |
| 23 | + |
| 24 | + Notification::fake(); |
| 25 | +}); |
| 26 | + |
| 27 | +it('sends Unreachable notification when threshold reached and not yet notified', function () { |
| 28 | + $this->server->settings()->update(['is_reachable' => false]); |
| 29 | + $this->server->forceFill([ |
| 30 | + 'unreachable_count' => 2, |
| 31 | + 'unreachable_notification_sent' => false, |
| 32 | + ])->save(); |
| 33 | + |
| 34 | + ServerReachabilityChanged::dispatch($this->server->fresh()); |
| 35 | + |
| 36 | + Notification::assertSentTo($this->team, Unreachable::class); |
| 37 | + expect($this->server->fresh()->unreachable_notification_sent)->toBeTrue(); |
| 38 | +}); |
| 39 | + |
| 40 | +it('does not send Unreachable on first transient failure (count=1)', function () { |
| 41 | + $this->server->settings()->update(['is_reachable' => false]); |
| 42 | + $this->server->forceFill([ |
| 43 | + 'unreachable_count' => 1, |
| 44 | + 'unreachable_notification_sent' => false, |
| 45 | + ])->save(); |
| 46 | + |
| 47 | + ServerReachabilityChanged::dispatch($this->server->fresh()); |
| 48 | + |
| 49 | + Notification::assertNothingSent(); |
| 50 | +}); |
| 51 | + |
| 52 | +it('does not send Unreachable when already notified', function () { |
| 53 | + $this->server->settings()->update(['is_reachable' => false]); |
| 54 | + $this->server->forceFill([ |
| 55 | + 'unreachable_count' => 5, |
| 56 | + 'unreachable_notification_sent' => true, |
| 57 | + ])->save(); |
| 58 | + |
| 59 | + ServerReachabilityChanged::dispatch($this->server->fresh()); |
| 60 | + |
| 61 | + Notification::assertNothingSent(); |
| 62 | +}); |
| 63 | + |
| 64 | +it('sends Reachable notification on recovery when previously notified', function () { |
| 65 | + $this->server->settings()->update(['is_reachable' => true]); |
| 66 | + $this->server->forceFill([ |
| 67 | + 'unreachable_count' => 0, |
| 68 | + 'unreachable_notification_sent' => true, |
| 69 | + ])->save(); |
| 70 | + |
| 71 | + $fresh = $this->server->fresh(); |
| 72 | + expect($fresh->unreachable_notification_sent)->toBeTrue(); |
| 73 | + expect((bool) $fresh->settings->is_reachable)->toBeTrue(); |
| 74 | + |
| 75 | + ServerReachabilityChanged::dispatch($fresh); |
| 76 | + |
| 77 | + Notification::assertSentTo($this->team, Reachable::class); |
| 78 | + expect($this->server->fresh()->unreachable_notification_sent)->toBeFalse(); |
| 79 | +}); |
| 80 | + |
| 81 | +it('does not send Reachable when never notified', function () { |
| 82 | + $this->server->settings()->update(['is_reachable' => true]); |
| 83 | + $this->server->forceFill([ |
| 84 | + 'unreachable_count' => 0, |
| 85 | + 'unreachable_notification_sent' => false, |
| 86 | + ])->save(); |
| 87 | + |
| 88 | + ServerReachabilityChanged::dispatch($this->server->fresh()); |
| 89 | + |
| 90 | + Notification::assertNothingSent(); |
| 91 | +}); |
| 92 | + |
| 93 | +it('routes Unreachable notification through EmailChannel when email toggle is on', function () { |
| 94 | + $this->server->settings()->update(['is_reachable' => false]); |
| 95 | + $this->server->forceFill([ |
| 96 | + 'unreachable_count' => 2, |
| 97 | + 'unreachable_notification_sent' => false, |
| 98 | + ])->save(); |
| 99 | + |
| 100 | + ServerReachabilityChanged::dispatch($this->server->fresh()); |
| 101 | + |
| 102 | + Notification::assertSentTo($this->team, Unreachable::class, function ($notification, $channels) { |
| 103 | + return in_array(EmailChannel::class, $channels); |
| 104 | + }); |
| 105 | +}); |
0 commit comments