16
16
import { TestDatabaseId , TestDatabases } from '@backstage/backend-test-utils' ;
17
17
import { DatabaseNotificationsStore } from './DatabaseNotificationsStore' ;
18
18
import { Knex } from 'knex' ;
19
- import { v4 as uuid } from 'uuid' ;
20
19
import { Notification } from '@backstage/plugin-notifications-common' ;
21
20
22
21
jest . setTimeout ( 60_000 ) ;
@@ -54,6 +53,15 @@ const otherUserNotification: Partial<Notification> = {
54
53
user : 'user:default/jane.doe' ,
55
54
} ;
56
55
56
+ const id1 = '01e0871e-e60a-4f68-8110-5ae3513f992e' ;
57
+ const id2 = '02e0871e-e60a-4f68-8110-5ae3513f992e' ;
58
+ const id3 = '03e0871e-e60a-4f68-8110-5ae3513f992e' ;
59
+ const id4 = '04e0871e-e60a-4f68-8110-5ae3513f992e' ;
60
+ const id5 = '05e0871e-e60a-4f68-8110-5ae3513f992e' ;
61
+ const id6 = '06e0871e-e60a-4f68-8110-5ae3513f992e' ;
62
+ const id7 = '07e0871e-e60a-4f68-8110-5ae3513f992e' ;
63
+ const id8 = '08e0871e-e60a-4f68-8110-5ae3513f992e' ;
64
+
57
65
describe . each ( databases . eachSupportedId ( ) ) (
58
66
'DatabaseNotificationsStore (%s)' ,
59
67
databaseId => {
@@ -94,11 +102,9 @@ describe.each(databases.eachSupportedId())(
94
102
95
103
describe ( 'getNotifications' , ( ) => {
96
104
it ( 'should return all notifications for user' , async ( ) => {
97
- const id1 = uuid ( ) ;
98
- const id2 = uuid ( ) ;
99
105
await insertNotification ( { id : id1 , ...testNotification } ) ;
100
106
await insertNotification ( { id : id2 , ...testNotification } ) ;
101
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
107
+ await insertNotification ( { id : id3 , ...otherUserNotification } ) ;
102
108
103
109
const notifications = await storage . getNotifications ( { user } ) ;
104
110
expect ( notifications . length ) . toBe ( 2 ) ;
@@ -107,13 +113,10 @@ describe.each(databases.eachSupportedId())(
107
113
} ) ;
108
114
109
115
it ( 'should return read notifications for user' , async ( ) => {
110
- const id1 = uuid ( ) ;
111
- const id2 = uuid ( ) ;
112
- const id3 = uuid ( ) ;
113
116
await insertNotification ( { id : id1 , ...testNotification } ) ;
114
117
await insertNotification ( { id : id2 , ...testNotification } ) ;
115
118
await insertNotification ( { id : id3 , ...testNotification } ) ;
116
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
119
+ await insertNotification ( { id : id4 , ...otherUserNotification } ) ;
117
120
118
121
await storage . markRead ( { ids : [ id1 , id3 ] , user } ) ;
119
122
@@ -127,13 +130,10 @@ describe.each(databases.eachSupportedId())(
127
130
} ) ;
128
131
129
132
it ( 'should return unread notifications for user' , async ( ) => {
130
- const id1 = uuid ( ) ;
131
- const id2 = uuid ( ) ;
132
- const id3 = uuid ( ) ;
133
133
await insertNotification ( { id : id1 , ...testNotification } ) ;
134
134
await insertNotification ( { id : id2 , ...testNotification } ) ;
135
135
await insertNotification ( { id : id3 , ...testNotification } ) ;
136
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
136
+ await insertNotification ( { id : id4 , ...otherUserNotification } ) ;
137
137
138
138
await storage . markRead ( { ids : [ id1 , id3 ] , user } ) ;
139
139
@@ -146,13 +146,10 @@ describe.each(databases.eachSupportedId())(
146
146
} ) ;
147
147
148
148
it ( 'should return both read and unread notifications for user' , async ( ) => {
149
- const id1 = uuid ( ) ;
150
- const id2 = uuid ( ) ;
151
- const id3 = uuid ( ) ;
152
149
await insertNotification ( { id : id1 , ...testNotification } ) ;
153
150
await insertNotification ( { id : id2 , ...testNotification } ) ;
154
151
await insertNotification ( { id : id3 , ...testNotification } ) ;
155
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
152
+ await insertNotification ( { id : id4 , ...otherUserNotification } ) ;
156
153
157
154
await storage . markRead ( { ids : [ id1 , id3 ] , user } ) ;
158
155
@@ -167,8 +164,6 @@ describe.each(databases.eachSupportedId())(
167
164
} ) ;
168
165
169
166
it ( 'should allow searching for notifications' , async ( ) => {
170
- const id1 = uuid ( ) ;
171
- const id2 = uuid ( ) ;
172
167
await insertNotification ( {
173
168
id : id1 ,
174
169
...testNotification ,
@@ -179,7 +174,7 @@ describe.each(databases.eachSupportedId())(
179
174
} ,
180
175
} ) ;
181
176
await insertNotification ( { id : id2 , ...testNotification } ) ;
182
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
177
+ await insertNotification ( { id : id3 , ...otherUserNotification } ) ;
183
178
184
179
const notifications = await storage . getNotifications ( {
185
180
user,
@@ -190,8 +185,6 @@ describe.each(databases.eachSupportedId())(
190
185
} ) ;
191
186
192
187
it ( 'should filter notifications based on created date' , async ( ) => {
193
- const id1 = uuid ( ) ;
194
- const id2 = uuid ( ) ;
195
188
await insertNotification ( {
196
189
id : id1 ,
197
190
...testNotification ,
@@ -206,7 +199,7 @@ describe.each(databases.eachSupportedId())(
206
199
} ,
207
200
created : new Date ( ) /* now */ ,
208
201
} ) ;
209
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
202
+ await insertNotification ( { id : id3 , ...otherUserNotification } ) ;
210
203
211
204
const notifications = await storage . getNotifications ( {
212
205
user,
@@ -215,19 +208,97 @@ describe.each(databases.eachSupportedId())(
215
208
expect ( notifications . length ) . toBe ( 1 ) ;
216
209
expect ( notifications . at ( 0 ) ?. id ) . toEqual ( id2 ) ;
217
210
} ) ;
211
+
212
+ it ( 'should apply pagination' , async ( ) => {
213
+ const now = Date . now ( ) ;
214
+ const timeDelay = 5 * 1000 ; /* 5 secs */
215
+
216
+ await insertNotification ( {
217
+ id : id1 ,
218
+ ...testNotification ,
219
+ created : new Date ( now - 1 * 60 * 60 * 1000 /* an hour ago */ ) ,
220
+ } ) ;
221
+ await insertNotification ( {
222
+ id : id2 ,
223
+ ...testNotification ,
224
+ created : new Date ( now ) ,
225
+ } ) ;
226
+ await insertNotification ( {
227
+ id : id3 ,
228
+ ...testNotification ,
229
+ created : new Date ( now - 5 * timeDelay ) ,
230
+ } ) ;
231
+ await insertNotification ( {
232
+ id : id4 ,
233
+ ...testNotification ,
234
+ created : new Date ( now - 4 * timeDelay ) ,
235
+ } ) ;
236
+ await insertNotification ( {
237
+ id : id5 ,
238
+ ...testNotification ,
239
+ created : new Date ( now - 3 * timeDelay ) ,
240
+ } ) ;
241
+ await insertNotification ( {
242
+ id : id6 ,
243
+ ...testNotification ,
244
+ created : new Date ( now - 2 * timeDelay ) ,
245
+ } ) ;
246
+ await insertNotification ( {
247
+ id : id7 ,
248
+ ...testNotification ,
249
+ created : new Date ( now - 1 * timeDelay ) ,
250
+ } ) ;
251
+
252
+ await insertNotification ( { id : id8 , ...otherUserNotification } ) ;
253
+
254
+ const allUserNotifications = await storage . getNotifications ( {
255
+ user,
256
+ } ) ;
257
+ expect ( allUserNotifications . length ) . toBe ( 7 ) ;
258
+
259
+ const notifications = await storage . getNotifications ( {
260
+ user,
261
+ createdAfter : new Date ( now - 5 * 60 * 1000 /* 5 mins */ ) ,
262
+ // so far no pagination
263
+ } ) ;
264
+ expect ( notifications . length ) . toBe ( 6 ) ;
265
+ expect ( notifications . at ( 0 ) ?. id ) . toEqual ( id2 ) ;
266
+ expect ( notifications . at ( 1 ) ?. id ) . toEqual ( id7 ) ;
267
+ expect ( notifications . at ( 2 ) ?. id ) . toEqual ( id6 ) ;
268
+ expect ( notifications . at ( 3 ) ?. id ) . toEqual ( id5 ) ;
269
+ expect ( notifications . at ( 4 ) ?. id ) . toEqual ( id4 ) ;
270
+
271
+ const allUserNotificationsPageOne = await storage . getNotifications ( {
272
+ user,
273
+ limit : 3 ,
274
+ offset : 0 ,
275
+ } ) ;
276
+ expect ( allUserNotificationsPageOne . length ) . toBe ( 3 ) ;
277
+ expect ( allUserNotificationsPageOne . at ( 0 ) ?. id ) . toEqual ( id2 ) ;
278
+ expect ( allUserNotificationsPageOne . at ( 1 ) ?. id ) . toEqual ( id7 ) ;
279
+ expect ( allUserNotificationsPageOne . at ( 2 ) ?. id ) . toEqual ( id6 ) ;
280
+
281
+ const allUserNotificationsPageTwo = await storage . getNotifications ( {
282
+ user,
283
+ limit : 3 ,
284
+ offset : 3 ,
285
+ } ) ;
286
+ expect ( allUserNotificationsPageTwo . length ) . toBe ( 3 ) ;
287
+ expect ( allUserNotificationsPageTwo . at ( 0 ) ?. id ) . toEqual ( id5 ) ;
288
+ expect ( allUserNotificationsPageTwo . at ( 1 ) ?. id ) . toEqual ( id4 ) ;
289
+ expect ( allUserNotificationsPageTwo . at ( 2 ) ?. id ) . toEqual ( id3 ) ;
290
+ } ) ;
218
291
} ) ;
219
292
220
293
describe ( 'getStatus' , ( ) => {
221
294
it ( 'should return status for user' , async ( ) => {
222
- const id1 = uuid ( ) ;
223
- const id2 = uuid ( ) ;
224
295
await insertNotification ( {
225
296
id : id1 ,
226
297
...testNotification ,
227
298
read : new Date ( ) ,
228
299
} ) ;
229
300
await insertNotification ( { id : id2 , ...testNotification } ) ;
230
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
301
+ await insertNotification ( { id : id3 , ...otherUserNotification } ) ;
231
302
232
303
const status = await storage . getStatus ( { user } ) ;
233
304
expect ( status . read ) . toEqual ( 1 ) ;
@@ -237,7 +308,6 @@ describe.each(databases.eachSupportedId())(
237
308
238
309
describe ( 'getExistingScopeNotification' , ( ) => {
239
310
it ( 'should return existing scope notification' , async ( ) => {
240
- const id1 = uuid ( ) ;
241
311
const notification : any = {
242
312
...testNotification ,
243
313
id : id1 ,
@@ -262,7 +332,6 @@ describe.each(databases.eachSupportedId())(
262
332
263
333
describe ( 'restoreExistingNotification' , ( ) => {
264
334
it ( 'should return restore existing scope notification' , async ( ) => {
265
- const id1 = uuid ( ) ;
266
335
const notification : any = {
267
336
...testNotification ,
268
337
id : id1 ,
@@ -296,7 +365,6 @@ describe.each(databases.eachSupportedId())(
296
365
297
366
describe ( 'getNotification' , ( ) => {
298
367
it ( 'should return notification by id' , async ( ) => {
299
- const id1 = uuid ( ) ;
300
368
await insertNotification ( { id : id1 , ...testNotification } ) ;
301
369
302
370
const notification = await storage . getNotification ( { id : id1 } ) ;
@@ -306,7 +374,6 @@ describe.each(databases.eachSupportedId())(
306
374
307
375
describe ( 'markRead' , ( ) => {
308
376
it ( 'should mark notification read' , async ( ) => {
309
- const id1 = uuid ( ) ;
310
377
await insertNotification ( { id : id1 , ...testNotification } ) ;
311
378
312
379
await storage . markRead ( { ids : [ id1 ] , user } ) ;
@@ -317,7 +384,6 @@ describe.each(databases.eachSupportedId())(
317
384
318
385
describe ( 'markUnread' , ( ) => {
319
386
it ( 'should mark notification unread' , async ( ) => {
320
- const id1 = uuid ( ) ;
321
387
await insertNotification ( {
322
388
id : id1 ,
323
389
...testNotification ,
@@ -332,7 +398,6 @@ describe.each(databases.eachSupportedId())(
332
398
333
399
describe ( 'markSaved' , ( ) => {
334
400
it ( 'should mark notification saved' , async ( ) => {
335
- const id1 = uuid ( ) ;
336
401
await insertNotification ( { id : id1 , ...testNotification } ) ;
337
402
338
403
await storage . markSaved ( { ids : [ id1 ] , user } ) ;
@@ -343,7 +408,6 @@ describe.each(databases.eachSupportedId())(
343
408
344
409
describe ( 'markUnsaved' , ( ) => {
345
410
it ( 'should mark notification not saved' , async ( ) => {
346
- const id1 = uuid ( ) ;
347
411
await insertNotification ( {
348
412
id : id1 ,
349
413
...testNotification ,
@@ -358,7 +422,6 @@ describe.each(databases.eachSupportedId())(
358
422
359
423
describe ( 'saveNotification' , ( ) => {
360
424
it ( 'should store a notification' , async ( ) => {
361
- const id1 = uuid ( ) ;
362
425
await storage . saveNotification ( {
363
426
id : id1 ,
364
427
user,
0 commit comments