|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import * as repositories from "../../src/db/repositories"; |
| 3 | +import { DLQ_RECENT_WINDOW_MS, isoNowMinus, sampleRecentDeadLetters } from "../../src/selfhost/dlq-recent"; |
| 4 | + |
| 5 | +describe("dlq-recent gauge helpers (#2083)", () => { |
| 6 | + afterEach(() => vi.restoreAllMocks()); |
| 7 | + |
| 8 | + describe("isoNowMinus", () => { |
| 9 | + it("returns the ISO timestamp windowMs before the injected now", () => { |
| 10 | + const now = Date.parse("2026-07-04T12:00:00.000Z"); |
| 11 | + expect(isoNowMinus(15 * 60 * 1000, now)).toBe("2026-07-04T11:45:00.000Z"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("defaults to the current clock when no now is given", () => { |
| 15 | + // Default-parameter path: a window before "now" is always in the past. |
| 16 | + expect(isoNowMinus(1000) <= new Date().toISOString()).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it("exposes a 15-minute default window", () => { |
| 20 | + expect(DLQ_RECENT_WINDOW_MS).toBe(900_000); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe("sampleRecentDeadLetters", () => { |
| 25 | + it("returns the count over the trailing window, queried at the window start", async () => { |
| 26 | + const now = Date.parse("2026-07-04T12:00:00.000Z"); |
| 27 | + const spy = vi.spyOn(repositories, "countRecentDeadLetters").mockResolvedValue(7); |
| 28 | + expect(await sampleRecentDeadLetters({} as Env, now)).toBe(7); |
| 29 | + expect(spy).toHaveBeenCalledWith({}, "2026-07-04T11:45:00.000Z"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("degrades to 0 when the query throws, so a DB hiccup never breaks the scrape", async () => { |
| 33 | + vi.spyOn(repositories, "countRecentDeadLetters").mockRejectedValue(new Error("db down")); |
| 34 | + expect(await sampleRecentDeadLetters({} as Env)).toBe(0); |
| 35 | + }); |
| 36 | + }); |
| 37 | +}); |
0 commit comments