import { describe, expect, it, vi } from "vitest"; import type { AlertPayload } from "@common/nats-subjects"; import { AGENT_ALERT_SUBJECT } from "@common/nats-subjects"; import { AlertService } from "./alert.js"; const payload: AlertPayload = { kind: "latency", metric: "shadow_eval.p95_latency_ms", severity: "warn", threshold: 3000, timestamp: "2026-04-17T00:00:00.000Z", value: 4100, window: "5m", }; describe("AlertService", () => { it("publishes when dedupe key is claimed", async () => { const setStringIfAbsent = vi.fn().mockResolvedValue(true); const publish = vi.fn().mockResolvedValue(undefined); const service = new AlertService({ setStringIfAbsent }, { publish }); const result = await service.sendAlert(payload); expect(result).toBe(true); expect(setStringIfAbsent).toHaveBeenCalledTimes(1); expect(publish).toHaveBeenCalledWith(AGENT_ALERT_SUBJECT, payload); }); it("suppresses duplicate alerts", async () => { const setStringIfAbsent = vi.fn().mockResolvedValue(false); const publish = vi.fn().mockResolvedValue(undefined); const service = new AlertService({ setStringIfAbsent }, { publish }); const result = await service.sendAlert(payload); expect(result).toBe(false); expect(publish).not.toHaveBeenCalled(); }); });