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,
@@ -218,13 +211,8 @@ describe.each(databases.eachSupportedId())(
218
211
219
212
it ( 'should apply pagination' , async ( ) => {
220
213
const now = Date . now ( ) ;
221
- const id1 = uuid ( ) ;
222
- const id2 = uuid ( ) ;
223
- const id3 = uuid ( ) ;
224
- const id4 = uuid ( ) ;
225
- const id5 = uuid ( ) ;
226
- const id6 = uuid ( ) ;
227
- const id7 = uuid ( ) ;
214
+ const timeDelay = 5 * 1000 ; /* 5 secs */
215
+
228
216
await insertNotification ( {
229
217
id : id1 ,
230
218
...testNotification ,
@@ -238,30 +226,30 @@ describe.each(databases.eachSupportedId())(
238
226
await insertNotification ( {
239
227
id : id3 ,
240
228
...testNotification ,
241
- created : new Date ( now + 1 ) ,
229
+ created : new Date ( now - 5 * timeDelay ) ,
242
230
} ) ;
243
231
await insertNotification ( {
244
232
id : id4 ,
245
233
...testNotification ,
246
- created : new Date ( now + 2 ) ,
234
+ created : new Date ( now - 4 * timeDelay ) ,
247
235
} ) ;
248
236
await insertNotification ( {
249
237
id : id5 ,
250
238
...testNotification ,
251
- created : new Date ( now + 3 ) ,
239
+ created : new Date ( now - 3 * timeDelay ) ,
252
240
} ) ;
253
241
await insertNotification ( {
254
242
id : id6 ,
255
243
...testNotification ,
256
- created : new Date ( now + 4 ) ,
244
+ created : new Date ( now - 2 * timeDelay ) ,
257
245
} ) ;
258
246
await insertNotification ( {
259
247
id : id7 ,
260
248
...testNotification ,
261
- created : new Date ( now + 5 ) ,
249
+ created : new Date ( now - 1 * timeDelay ) ,
262
250
} ) ;
263
251
264
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
252
+ await insertNotification ( { id : id8 , ...otherUserNotification } ) ;
265
253
266
254
const allUserNotifications = await storage . getNotifications ( {
267
255
user,
@@ -271,44 +259,46 @@ describe.each(databases.eachSupportedId())(
271
259
const notifications = await storage . getNotifications ( {
272
260
user,
273
261
createdAfter : new Date ( now - 5 * 60 * 1000 /* 5 mins */ ) ,
262
+ // so far no pagination
274
263
} ) ;
275
264
expect ( notifications . length ) . toBe ( 6 ) ;
276
- expect ( notifications . at ( 0 ) ?. id ) . toEqual ( id7 ) ;
277
- expect ( notifications . at ( 1 ) ?. id ) . toEqual ( id6 ) ;
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 ) ;
278
270
279
271
const allUserNotificationsPageOne = await storage . getNotifications ( {
280
272
user,
281
273
limit : 3 ,
282
274
offset : 0 ,
283
275
} ) ;
284
276
expect ( allUserNotificationsPageOne . length ) . toBe ( 3 ) ;
285
- expect ( allUserNotificationsPageOne . at ( 0 ) ?. id ) . toEqual ( id7 ) ;
286
- expect ( allUserNotificationsPageOne . at ( 1 ) ?. id ) . toEqual ( id6 ) ;
287
- expect ( allUserNotificationsPageOne . at ( 2 ) ?. id ) . toEqual ( id5 ) ;
277
+ expect ( allUserNotificationsPageOne . at ( 0 ) ?. id ) . toEqual ( id2 ) ;
278
+ expect ( allUserNotificationsPageOne . at ( 1 ) ?. id ) . toEqual ( id7 ) ;
279
+ expect ( allUserNotificationsPageOne . at ( 2 ) ?. id ) . toEqual ( id6 ) ;
288
280
289
281
const allUserNotificationsPageTwo = await storage . getNotifications ( {
290
282
user,
291
283
limit : 3 ,
292
284
offset : 3 ,
293
285
} ) ;
294
286
expect ( allUserNotificationsPageTwo . length ) . toBe ( 3 ) ;
295
- expect ( allUserNotificationsPageTwo . at ( 0 ) ?. id ) . toEqual ( id4 ) ;
296
- expect ( allUserNotificationsPageTwo . at ( 1 ) ?. id ) . toEqual ( id3 ) ;
297
- expect ( allUserNotificationsPageTwo . at ( 2 ) ?. id ) . toEqual ( id2 ) ;
287
+ expect ( allUserNotificationsPageTwo . at ( 0 ) ?. id ) . toEqual ( id5 ) ;
288
+ expect ( allUserNotificationsPageTwo . at ( 1 ) ?. id ) . toEqual ( id4 ) ;
289
+ expect ( allUserNotificationsPageTwo . at ( 2 ) ?. id ) . toEqual ( id3 ) ;
298
290
} ) ;
299
291
} ) ;
300
292
301
293
describe ( 'getStatus' , ( ) => {
302
294
it ( 'should return status for user' , async ( ) => {
303
- const id1 = uuid ( ) ;
304
- const id2 = uuid ( ) ;
305
295
await insertNotification ( {
306
296
id : id1 ,
307
297
...testNotification ,
308
298
read : new Date ( ) ,
309
299
} ) ;
310
300
await insertNotification ( { id : id2 , ...testNotification } ) ;
311
- await insertNotification ( { id : uuid ( ) , ...otherUserNotification } ) ;
301
+ await insertNotification ( { id : id3 , ...otherUserNotification } ) ;
312
302
313
303
const status = await storage . getStatus ( { user } ) ;
314
304
expect ( status . read ) . toEqual ( 1 ) ;
@@ -318,7 +308,6 @@ describe.each(databases.eachSupportedId())(
318
308
319
309
describe ( 'getExistingScopeNotification' , ( ) => {
320
310
it ( 'should return existing scope notification' , async ( ) => {
321
- const id1 = uuid ( ) ;
322
311
const notification : any = {
323
312
...testNotification ,
324
313
id : id1 ,
@@ -343,7 +332,6 @@ describe.each(databases.eachSupportedId())(
343
332
344
333
describe ( 'restoreExistingNotification' , ( ) => {
345
334
it ( 'should return restore existing scope notification' , async ( ) => {
346
- const id1 = uuid ( ) ;
347
335
const notification : any = {
348
336
...testNotification ,
349
337
id : id1 ,
@@ -377,7 +365,6 @@ describe.each(databases.eachSupportedId())(
377
365
378
366
describe ( 'getNotification' , ( ) => {
379
367
it ( 'should return notification by id' , async ( ) => {
380
- const id1 = uuid ( ) ;
381
368
await insertNotification ( { id : id1 , ...testNotification } ) ;
382
369
383
370
const notification = await storage . getNotification ( { id : id1 } ) ;
@@ -387,7 +374,6 @@ describe.each(databases.eachSupportedId())(
387
374
388
375
describe ( 'markRead' , ( ) => {
389
376
it ( 'should mark notification read' , async ( ) => {
390
- const id1 = uuid ( ) ;
391
377
await insertNotification ( { id : id1 , ...testNotification } ) ;
392
378
393
379
await storage . markRead ( { ids : [ id1 ] , user } ) ;
@@ -398,7 +384,6 @@ describe.each(databases.eachSupportedId())(
398
384
399
385
describe ( 'markUnread' , ( ) => {
400
386
it ( 'should mark notification unread' , async ( ) => {
401
- const id1 = uuid ( ) ;
402
387
await insertNotification ( {
403
388
id : id1 ,
404
389
...testNotification ,
@@ -413,7 +398,6 @@ describe.each(databases.eachSupportedId())(
413
398
414
399
describe ( 'markSaved' , ( ) => {
415
400
it ( 'should mark notification saved' , async ( ) => {
416
- const id1 = uuid ( ) ;
417
401
await insertNotification ( { id : id1 , ...testNotification } ) ;
418
402
419
403
await storage . markSaved ( { ids : [ id1 ] , user } ) ;
@@ -424,7 +408,6 @@ describe.each(databases.eachSupportedId())(
424
408
425
409
describe ( 'markUnsaved' , ( ) => {
426
410
it ( 'should mark notification not saved' , async ( ) => {
427
- const id1 = uuid ( ) ;
428
411
await insertNotification ( {
429
412
id : id1 ,
430
413
...testNotification ,
@@ -439,7 +422,6 @@ describe.each(databases.eachSupportedId())(
439
422
440
423
describe ( 'saveNotification' , ( ) => {
441
424
it ( 'should store a notification' , async ( ) => {
442
- const id1 = uuid ( ) ;
443
425
await storage . saveNotification ( {
444
426
id : id1 ,
445
427
user,
0 commit comments