/ opencodehub / tests / unit / notifications-route.test.ts
Swadhin Biswas test(qa): add e2e, integration, unit, and load coverage · 3/6/2026
1c29e63
89 lines · 3.0 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
80
81
82
83
84
85
86
87
88
89
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 listNotificationsGet } from "@/pages/api/notifications/index";

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

  it("returns unauthorized when no auth token is present", async () => {
    getUserFromRequestMock.mockResolvedValue(null);

    const response = await listNotificationsGet({
      request: new Request("http://localhost/api/notifications"),
      url: new URL("http://localhost/api/notifications"),
    } as any);

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

  it("supports filter=blocking and returns unread count", async () => {
    getUserFromRequestMock.mockResolvedValue({ userId: "user-1" });
    mockDb.query.notifications.findMany
      .mockResolvedValueOnce([{ id: "notif-1", type: "ci_failed" }])
      .mockResolvedValueOnce([{ id: "history-1", type: "ci_failed", isRead: false }])
      .mockResolvedValueOnce([{ id: "notif-2" }, { id: "notif-3" }]);

    const response = await listNotificationsGet({
      request: new Request("http://localhost/api/notifications?filter=blocking"),
      url: new URL("http://localhost/api/notifications?filter=blocking"),
    } as any);

    const body = await response.json();

    expect(response.status).toBe(200);
    expect(body?.success).toBe(true);
    expect(body?.data?.notifications).toHaveLength(1);
    expect(body?.data?.unreadCount).toBe(2);
    expect(mockDb.query.notifications.findMany).toHaveBeenCalledTimes(3);
  });

  it("sorts by priority score when prioritize=true", async () => {
    getUserFromRequestMock.mockResolvedValue({ userId: "user-1" });
    mockDb.query.notifications.findMany
      .mockResolvedValueOnce([
        { id: "notif-low", type: "comment", reason: "subscribed", isRead: false, createdAt: new Date("2026-02-18T10:00:00Z") },
        { id: "notif-critical", type: "security_alert", reason: "security_alert", isRead: false, createdAt: new Date("2026-02-18T08:00:00Z") },
      ])
      .mockResolvedValueOnce([]);

    const response = await listNotificationsGet({
      request: new Request("http://localhost/api/notifications?filter=all&prioritize=true"),
      url: new URL("http://localhost/api/notifications?filter=all&prioritize=true"),
    } as any);

    const body = await response.json();
    const ids = body?.data?.notifications?.map((n: any) => n.id) || [];

    expect(response.status).toBe(200);
    expect(ids[0]).toBe("notif-critical");
    expect(body?.data?.notifications?.[0]?.priority).toBe("critical");
  });
});