kundencenter/apps/api/test/monitor.test.ts
Claude (KI) e9836f0efd Betriebsalarme mit Entwarnung: Lizenzserver, Warteschlange, Worker-Ausfall (#20, #38)
- system_alerts: Zustand je Alarm, Meldung nach Karenzzeit genau einmal,
  Entwarnung nur nach vorheriger Meldung; aktive Alarme im Dashboard.
- Lizenzprüfung unterscheidet config/outage/rejected; Ausfall wird alle
  10 min wiederholt und nach 30 min gemeldet, Konfigurations-/Ablehnungs-
  fehler und ungültige Lizenz sofort; Testmodus ohne Schlüssel bleibt still.
- Worker meldet Warteschlangen-Stau und needs_review per Discord.
- Worker-Herzschlag; die API meldet einen ausgefallenen Worker per Mail an
  die Superadmins (Discord läuft im Worker).
- Migration 040, Doku-Tabelle in betrieb-backup-restore.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 11:35:02 +02:00

48 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { run } from '../src/core/db.js';
import { applyConditions, heartbeat, workerConditions } from '@kc/platform/monitor';
import { workerDownCondition } from '../src/ops/watchdog.js';
describe('Betriebsalarme mit Entwarnung (#20/#38)', () => {
it('meldet erst nach der Karenzzeit, genau einmal, und entwarnt danach einmal', async () => {
const sent: string[] = []; const notify = async (k: string, kind: string) => { sent.push(`${k}:${kind}`); };
const t0 = new Date('2026-10-06T10:00:00Z'); const at = (min: number) => new Date(t0.getTime() + min * 60_000);
const c = (active: boolean) => [{ key: 'test.queue', active, detail: active ? 'Stau' : null, graceMs: 5 * 60_000 }];
await applyConditions(c(true), notify, at(0)); await applyConditions(c(true), notify, at(3));
expect(sent).toEqual([]); // kurzer Aussetzer: noch nichts
await applyConditions(c(true), notify, at(6)); await applyConditions(c(true), notify, at(7));
expect(sent).toEqual(['test.queue:alarm']); // dedupliziert
await applyConditions(c(false), notify, at(8)); await applyConditions(c(false), notify, at(9));
expect(sent).toEqual(['test.queue:alarm', 'test.queue:recovery']);
});
it('kurzer Vorfall unterhalb der Karenz: weder Alarm noch Entwarnung', async () => {
const sent: string[] = []; const notify = async (k: string, kind: string) => { sent.push(`${k}:${kind}`); };
const t = new Date('2026-10-06T11:00:00Z');
await applyConditions([{ key: 'test.blip', active: true, graceMs: 60_000 }], notify, t);
await applyConditions([{ key: 'test.blip', active: false }], notify, new Date(t.getTime() + 30_000));
expect(sent).toEqual([]);
});
it('unterscheidet Lizenzserver-Ausfall von Konfigurationsproblemen; ohne Schlüssel (Testmodus) kein Alarm', async () => {
await run("UPDATE license_state SET license_key_enc = 'x', last_error = 'Netzwerkfehler: timeout', last_error_kind = 'outage', valid = 1, checked_at = UTC_TIMESTAMP(3) WHERE id = 1");
let c = Object.fromEntries((await workerConditions()).map((x) => [x.key, x.active]));
expect(c['license.unreachable']).toBe(true); expect(c['license.problem']).toBe(false);
await run("UPDATE license_state SET last_error = 'Program-Key ungültig', last_error_kind = 'config' WHERE id = 1");
c = Object.fromEntries((await workerConditions()).map((x) => [x.key, x.active]));
expect(c['license.unreachable']).toBe(false); expect(c['license.problem']).toBe(true);
await run('UPDATE license_state SET license_key_enc = NULL WHERE id = 1');
c = Object.fromEntries((await workerConditions()).map((x) => [x.key, x.active]));
expect(c['license.unreachable']).toBe(false); expect(c['license.problem']).toBe(false);
await run('UPDATE license_state SET last_error = NULL, last_error_kind = NULL WHERE id = 1');
});
it('Warteschlangen-Stau und Aufträge zur Prüfung werden erkannt', async () => {
await run("INSERT INTO jobs (id, type, payload, status, run_at) VALUES (UUID(), 'x.test', '{}', 'scheduled', DATE_SUB(UTC_TIMESTAMP(3), INTERVAL 30 MINUTE)), (UUID(), 'x.test', '{}', 'needs_review', UTC_TIMESTAMP(3))");
const c = Object.fromEntries((await workerConditions()).map((x) => [x.key, x.active]));
expect(c['jobs.backlog']).toBe(true); expect(c['jobs.needs_review']).toBe(true);
await run("DELETE FROM jobs WHERE type = 'x.test'");
});
it('Worker-Herzschlag: frisch = in Ordnung, veraltet = ausgefallen', async () => {
await heartbeat('worker'); expect((await workerDownCondition()).active).toBe(false);
await run("UPDATE system_heartbeats SET at = DATE_SUB(UTC_TIMESTAMP(3), INTERVAL 10 MINUTE) WHERE name = 'worker'");
expect((await workerDownCondition()).active).toBe(true);
});
});