/ opencodehub / tests / unit / notifications-blocking-summary-route.test.ts
swadhinbiswas Add blocking notification summary API · 2/19/2026
c9f745e
79 lines · 2.1 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
67
68
69
70
71
72
73
74
75
76
77
78
79
import { beforeEach, describe, expect, it, vi } from "vitest";

const { getUserFromRequestMock } = vi.hoisted(() => ({
  getUserFromRequestMock: vi.fn(),
}));

let mockDb: any;

vi.mock("@/lib/auth", () => ({
  getUserFromRequest: getUserFromRequestMock,
}));

vi.mock("@/db", async () => {
  const actual = await vi.importActual<typeof import("@/db")>("@/db");
  return {
    ...actual,
    getDatabase: () => mockDb,
  };
});

import { GET as blockingSummaryGet } from "@/pages/api/notifications/blocking/summary";

describe("notifications blocking summary route", () => {
  beforeEach(() => {
    getUserFromRequestMock.mockReset();
    mockDb = {
      query: {
        notifications: {
          findMany: vi.fn(),
        },
      },
    };
  });

  it("returns unauthorized when not authenticated", async () => {
    getUserFromRequestMock.mockResolvedValue(null);

    const response = await blockingSummaryGet({
      request: new Request("http://localhost/api/notifications/blocking/summary"),
    } as any);

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

  it("returns blocking summary aggregates", async () => {
    getUserFromRequestMock.mockResolvedValue({ userId: "user-1" });
    mockDb.query.notifications.findMany.mockResolvedValue([
      {
        id: "n1",
        type: "security_alert",
        reason: "security_alert",
        isRead: false,
        isArchived: false,
        createdAt: new Date("2026-02-19T00:00:00Z"),
      },
      {
        id: "n2",
        type: "ci_failed",
        reason: "ci_failed",
        isRead: true,
        isArchived: false,
        createdAt: new Date("2026-02-18T00:00:00Z"),
      },
    ]);

    const response = await blockingSummaryGet({
      request: new Request("http://localhost/api/notifications/blocking/summary"),
    } as any);
    const body = await response.json();

    expect(response.status).toBe(200);
    expect(body?.data?.totalBlocking).toBe(2);
    expect(body?.data?.unreadBlockingCount).toBe(1);
    expect(body?.data?.countsByType?.security_alert).toBe(1);
    expect(body?.data?.topPriority).toBe("critical");
    expect(body?.data?.items).toHaveLength(2);
  });
});