/ opencodehub / tests / integration / user-digest-test-route.test.ts
swadhinbiswas Add digest delivery retries and observability metrics · 2/19/2026
4b685b4
66 lines · 1.7 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { beforeEach, describe, expect, it, vi } from "vitest";

const { runUserDigestMock } = vi.hoisted(() => ({
  runUserDigestMock: vi.fn(async () => ({
    sent: true,
    dryRun: true,
    period: "daily",
    itemCount: 3,
    attempts: 0,
  })),
}));

vi.mock("@/lib/chat-notifications", () => ({
  runUserDigest: runUserDigestMock,
}));

import { POST as testDigestPost } from "@/pages/api/user/notification-digests/test";

async function readJson(response: Response): Promise<any> {
  return response.json();
}

describe("user digest test route", () => {
  beforeEach(() => {
    runUserDigestMock.mockResolvedValue({
      sent: true,
      dryRun: true,
      period: "daily",
      itemCount: 3,
      attempts: 0,
    });
  });

  it("returns 401 when unauthenticated", async () => {
    const response = await testDigestPost({
      locals: {},
      request: new Request("http://localhost/api/user/notification-digests/test", {
        method: "POST",
      }),
    } as any);

    expect(response.status).toBe(401);
  });

  it("runs digest test with dryRun true by default", async () => {
    const response = await testDigestPost({
      locals: { user: { id: "user-1" } },
      request: new Request("http://localhost/api/user/notification-digests/test", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({}),
      }),
    } as any);

    const body = await readJson(response);
    expect(response.status).toBe(200);
    expect(body?.data?.sent).toBe(true);
    expect(runUserDigestMock).toHaveBeenCalledWith({
      userId: "user-1",
      dryRun: true,
      period: undefined,
      maxRetries: undefined,
    });
  });
});