/ opencodehub / tests / unit / digest-delivery-retry.test.ts
swadhinbiswas Add digest delivery retries and observability metrics · 2/19/2026
4b685b4
26 lines · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { describe, expect, it, vi } from "vitest";
import { sendDigestWithRetry } from "@/lib/chat-notifications";

describe("digest delivery retry helper", () => {
  it("recovers after a retry when a later attempt succeeds", async () => {
    const send = vi
      .fn<() => Promise<boolean>>()
      .mockResolvedValueOnce(false)
      .mockResolvedValueOnce(true);

    const result = await sendDigestWithRetry(send, 2);
    expect(result.sent).toBe(true);
    expect(result.recovered).toBe(true);
    expect(result.attempts).toBe(2);
  });

  it("returns failure after exhausting retries", async () => {
    const send = vi.fn<() => Promise<boolean>>().mockResolvedValue(false);

    const result = await sendDigestWithRetry(send, 1);
    expect(result.sent).toBe(false);
    expect(result.attempts).toBe(2);
    expect(result.lastError).toBe("send_failed");
  });
});