forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification.test.js
More file actions
282 lines (240 loc) · 8.84 KB
/
Copy pathnotification.test.js
File metadata and controls
282 lines (240 loc) · 8.84 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { jest } from "@jest/globals";
import express from "express";
import mongoose from "mongoose";
import { MongoMemoryServer } from "mongodb-memory-server";
import Notification from "../src/models/Notification.js";
import User from "../src/models/User.js";
import Course from "../src/models/Course.js";
import Book from "../src/models/Book.js";
import {
sseNotifications,
getUserNotifications,
createFollowNotification,
createUnfollowNotification,
createNewCourseNotification,
createNewBookNotification,
} from "../src/controllers/notificationController.js";
describe("Notification System & Event Wiring", () => {
let mongoServer;
beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
await mongoose.connect(mongoUri);
}, 30000);
afterAll(async () => {
await mongoose.disconnect();
if (mongoServer) {
await mongoServer.stop();
}
});
beforeEach(async () => {
await Notification.deleteMany({});
await User.deleteMany({});
await Course.deleteMany({});
await Book.deleteMany({});
});
describe("Mongoose Schema Compound Indexes", () => {
it("has expected compound indexes declared on Notification schema", () => {
const indexes = Notification.schema.indexes();
const indexFields = indexes.map(([spec]) => Object.keys(spec).join(","));
expect(indexFields).toContain("recipient,createdAt");
expect(indexFields).toContain("recipient,isRead");
expect(indexFields).toContain("recipient,isDeleted");
});
});
describe("Event Producer Notifications", () => {
it("creates a follow notification for the followed user", async () => {
const follower = await User.create({
name: "Follower User",
email: "follower@example.com",
password: "Qx7#vLmp92Zt",
});
const followed = await User.create({
name: "Followed User",
email: "followed@example.com",
password: "Qx7#vLmp92Zt",
});
await createFollowNotification(follower._id, followed._id);
const notifs = await Notification.find({ recipient: followed._id });
expect(notifs).toHaveLength(1);
expect(notifs[0].type).toBe("follow");
expect(notifs[0].sender.toString()).toBe(follower._id.toString());
expect(notifs[0].message).toContain("Follower User started following you");
});
it("creates an unfollow notification for the unfollowed user", async () => {
const unfollower = await User.create({
name: "Unfollower User",
email: "unfollower@example.com",
password: "Qx7#vLmp92Zt",
});
const unfollowed = await User.create({
name: "Unfollowed User",
email: "unfollowed@example.com",
password: "Qx7#vLmp92Zt",
});
await createUnfollowNotification(unfollower._id, unfollowed._id);
const notifs = await Notification.find({ recipient: unfollowed._id });
expect(notifs).toHaveLength(1);
expect(notifs[0].type).toBe("unfollow");
expect(notifs[0].sender.toString()).toBe(unfollower._id.toString());
expect(notifs[0].message).toContain("Unfollower User unfollowed you");
});
it("creates batched new_course notifications for all followers", async () => {
const follower1 = await User.create({
name: "Follower 1",
email: "f1@example.com",
password: "Qx7#vLmp92Zt",
});
const follower2 = await User.create({
name: "Follower 2",
email: "f2@example.com",
password: "Qx7#vLmp92Zt",
});
const creator = await User.create({
name: "Course Creator",
email: "creator@example.com",
password: "Qx7#vLmp92Zt",
followers: [follower1._id, follower2._id],
});
const course = await Course.create({
title: "Mastering Node.js",
description: "Comprehensive Node.js course",
category: "Programming",
createdBy: creator._id,
});
const result = await createNewCourseNotification(
course._id,
creator._id,
course.title
);
expect(result).toHaveLength(2);
const notifs = await Notification.find({ sender: creator._id });
expect(notifs).toHaveLength(2);
const recipientIds = notifs.map((n) => n.recipient.toString());
expect(recipientIds).toContain(follower1._id.toString());
expect(recipientIds).toContain(follower2._id.toString());
expect(notifs[0].type).toBe("new_course");
expect(notifs[0].message).toContain("Course Creator created a new course: Mastering Node.js");
});
it("creates batched new_book notifications for all followers", async () => {
const follower1 = await User.create({
name: "Book Reader 1",
email: "r1@example.com",
password: "Qx7#vLmp92Zt",
});
const follower2 = await User.create({
name: "Book Reader 2",
email: "r2@example.com",
password: "Qx7#vLmp92Zt",
});
const author = await User.create({
name: "Book Author",
email: "author@example.com",
password: "Qx7#vLmp92Zt",
followers: [follower1._id, follower2._id],
});
const book = await Book.create({
title: "Clean Architecture in JS",
description: "Guide to scalable code architecture",
category: "Tech",
price: 15,
author: author._id,
thumbnail: "https://example.com/thumb.jpg",
image: "https://example.com/thumb.jpg",
fileUrl: "https://example.com/book.pdf",
});
const result = await createNewBookNotification(
book._id,
author._id,
book.title
);
expect(result).toHaveLength(2);
const notifs = await Notification.find({ sender: author._id });
expect(notifs).toHaveLength(2);
const recipientIds = notifs.map((n) => n.recipient.toString());
expect(recipientIds).toContain(follower1._id.toString());
expect(recipientIds).toContain(follower2._id.toString());
expect(notifs[0].type).toBe("new_book");
expect(notifs[0].message).toContain("Book Author published a new book: Clean Architecture in JS");
});
});
describe("getUserNotifications Pagination & Bounding", () => {
it("caps limit to 100 and validates invalid page/limit params", async () => {
const recipient = await User.create({
name: "Recipient User",
email: "recipient@example.com",
password: "Qx7#vLmp92Zt",
});
const sender = await User.create({
name: "Sender User",
email: "sender@example.com",
password: "Qx7#vLmp92Zt",
});
// Insert 120 notifications for recipient
const docs = Array.from({ length: 120 }, (_, i) => ({
recipient: recipient._id,
sender: sender._id,
type: "system",
title: `Notification ${i + 1}`,
message: `Message ${i + 1}`,
}));
await Notification.insertMany(docs);
// Request with limit=500 -> should be capped at 100
const reqLarge = {
user: { _id: recipient._id },
query: { limit: "500", page: "1" },
};
const resLarge = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
await getUserNotifications(reqLarge, resLarge);
expect(resLarge.status).toHaveBeenCalledWith(200);
const dataLarge = resLarge.json.mock.calls[0][0];
expect(dataLarge.notifications).toHaveLength(100);
expect(dataLarge.pagination.currentPage).toBe(1);
// Request with negative page and non-numeric limit -> should default to page=1, limit=20
const reqInvalid = {
user: { _id: recipient._id },
query: { limit: "invalid", page: "-5" },
};
const resInvalid = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
await getUserNotifications(reqInvalid, resInvalid);
const dataInvalid = resInvalid.json.mock.calls[0][0];
expect(dataInvalid.notifications).toHaveLength(20);
expect(dataInvalid.pagination.currentPage).toBe(1);
});
});
describe("SSE Connection & Delivery", () => {
it("establishes SSE connection stream and handles payload delivery", async () => {
const user = await User.create({
name: "SSE User",
email: "sse@example.com",
password: "Qx7#vLmp92Zt",
});
const writeHeadMock = jest.fn();
const writeMock = jest.fn();
const reqMock = {
user: { _id: user._id },
on: jest.fn(),
};
const resMock = {
writeHead: writeHeadMock,
write: writeMock,
};
await sseNotifications(reqMock, resMock);
expect(writeHeadMock).toHaveBeenCalledWith(
200,
expect.objectContaining({
"Content-Type": "text/event-stream",
})
);
expect(writeMock).toHaveBeenCalledWith(
expect.stringContaining("Connected to notifications")
);
});
});
});