set('monitoring.alerts.enabled', true); config()->set('monitoring.alerts.mail_to', 'ops@example.com'); Mail::fake(); $monitoringLog = Mockery::spy(LoggerInterface::class); Log::shouldReceive('channel')->with('monitoring')->andReturn($monitoringLog); OperationalAlert::send('Geplanter Task fehlgeschlagen: billing:generate-manual-invoices', ['exit_code' => 1]); $monitoringLog->shouldHaveReceived('critical')->once(); Mail::assertSent(OperationalAlertMail::class, function (OperationalAlertMail $mail) { return $mail->hasTo('ops@example.com') && str_contains($mail->summary, 'billing:generate-manual-invoices') && $mail->context === ['exit_code' => 1]; }); }); test('no mail is sent when alerts are disabled', function () { config()->set('monitoring.alerts.enabled', false); config()->set('monitoring.alerts.mail_to', 'ops@example.com'); Mail::fake(); OperationalAlert::send('Etwas ist kaputt'); Mail::assertNothingSent(); }); test('no mail is sent when no recipient is configured', function () { config()->set('monitoring.alerts.enabled', true); config()->set('monitoring.alerts.mail_to', null); Mail::fake(); OperationalAlert::send('Etwas ist kaputt'); Mail::assertNothingSent(); }); test('a mail failure never bubbles up to the caller', function () { config()->set('monitoring.alerts.enabled', true); config()->set('monitoring.alerts.mail_to', 'ops@example.com'); // Mailserver down: der Versand wirft — der Alert muss das schlucken, // damit der auslösende Scheduler-/Queue-Prozess weiterläuft. Mail::shouldReceive('to')->andReturnSelf(); Mail::shouldReceive('send')->andThrow(new RuntimeException('mail down')); OperationalAlert::send('Etwas ist kaputt'); expect(true)->toBeTrue(); }); test('a failing queue job triggers an operational alert via the registered listener', function () { config()->set('monitoring.alerts.enabled', true); config()->set('monitoring.alerts.mail_to', 'ops@example.com'); Mail::fake(); $job = Mockery::mock(Job::class); $job->shouldReceive('resolveName')->andReturn('App\\Jobs\\ClassifyPressRelease'); $job->shouldReceive('getQueue')->andReturn('classification'); event(new JobFailed('database', $job, new RuntimeException('job boom'))); Mail::assertSent(OperationalAlertMail::class, function (OperationalAlertMail $mail) { return str_contains($mail->summary, 'ClassifyPressRelease'); }); }); test('an alert is also pushed to ntfy as json with token and priority', function () { config()->set('monitoring.alerts.enabled', true); config()->set('monitoring.alerts.mail_to', null); config()->set('monitoring.alerts.ntfy', [ 'url' => 'https://ntfy.example.com/', 'topic' => 'pressekonto-ops', 'token' => 'tk_geheim', 'priority' => 4, ]); Http::fake(['ntfy.example.com/*' => Http::response(['id' => 'abc'])]); OperationalAlert::send('Geplanter Task fehlgeschlagen: archive:purge', ['exit_code' => 1, 'queue' => ['a' => 'b']]); Http::assertSent(function (Request $request) { return $request->url() === 'https://ntfy.example.com' && $request->hasHeader('Authorization', 'Bearer tk_geheim') && $request['topic'] === 'pressekonto-ops' && $request['priority'] === 4 && str_contains($request['message'], 'archive:purge') && str_contains($request['message'], 'exit_code: 1') && str_contains($request['message'], 'queue: {"a":"b"}'); }); }); test('a failing ntfy push neither bubbles up nor stops the mail', function () { config()->set('monitoring.alerts.enabled', true); config()->set('monitoring.alerts.mail_to', 'ops@example.com'); config()->set('monitoring.alerts.ntfy.url', 'https://ntfy.example.com'); config()->set('monitoring.alerts.ntfy.topic', 'pressekonto-ops'); Mail::fake(); Http::fake(['ntfy.example.com/*' => Http::response('down', 502)]); OperationalAlert::send('Etwas ist kaputt'); Mail::assertSent(OperationalAlertMail::class); Http::assertSentCount(1); }); test('no push is sent when alerts are disabled or ntfy is not configured', function () { Http::fake(); config()->set('monitoring.alerts.ntfy.url', 'https://ntfy.example.com'); config()->set('monitoring.alerts.ntfy.topic', null); config()->set('monitoring.alerts.enabled', true); OperationalAlert::send('Etwas ist kaputt'); config()->set('monitoring.alerts.ntfy.topic', 'pressekonto-ops'); config()->set('monitoring.alerts.enabled', false); OperationalAlert::send('Etwas ist kaputt'); Http::assertNothingSent(); });