-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathOperationRepo.test.ts
More file actions
580 lines (489 loc) · 16.7 KB
/
OperationRepo.test.ts
File metadata and controls
580 lines (489 loc) · 16.7 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import { APP_ID, ONESIGNAL_ID, SUB_ID } from '__test__/constants';
import { db } from 'src/shared/database/client';
import type { IndexedDBSchema } from 'src/shared/database/types';
import { setConsentRequired } from 'src/shared/helpers/localStorage';
import Log from 'src/shared/libraries/Log';
import { SubscriptionType } from 'src/shared/subscriptions/constants';
import { describe, expect, type Mock, vi } from 'vitest';
import { OperationModelStore } from '../modelRepo/OperationModelStore';
import { CreateSubscriptionOperation } from '../operations/CreateSubscriptionOperation';
import {
GroupComparisonType,
type GroupComparisonValue,
Operation as OperationBase,
} from '../operations/Operation';
import { SetAliasOperation } from '../operations/SetAliasOperation';
import { ExecutionResult, type IOperationExecutor } from '../types/operation';
import {
OP_REPO_EXECUTION_INTERVAL,
OP_REPO_POST_CREATE_DELAY,
} from './constants';
import { NewRecordsState } from './NewRecordsState';
import { OperationQueueItem, OperationRepo } from './OperationRepo';
vi.spyOn(Log, '_error').mockImplementation((msg) => {
if (typeof msg === 'string' && msg.includes('Operation execution failed'))
return '';
return msg;
});
vi.useFakeTimers();
// for the sake of testing, we want to use a simple mock operation and execturo
vi.spyOn(OperationModelStore.prototype, 'create').mockImplementation(() => {
return null;
});
let mockOperationModelStore: OperationModelStore;
const executeOps = async (opRepo: OperationRepo) => {
await opRepo._start();
await vi.advanceTimersByTimeAsync(OP_REPO_EXECUTION_INTERVAL);
};
// need to mock this since it may timeout due to use of fake timers
const isConsentRequired = vi.hoisted(() => vi.fn(() => false));
vi.mock('src/shared/database/config', () => ({
isConsentRequired: isConsentRequired,
}));
describe('OperationRepo', () => {
let opRepo: OperationRepo;
const getGroupedOp = () => [
new Operation('1', GroupComparisonType.CREATE, 'abc'),
new Operation('2', GroupComparisonType.CREATE, 'abc'),
];
beforeEach(async () => {
setConsentRequired(false);
mockOperationModelStore = new OperationModelStore();
opRepo = new OperationRepo(
[mockExecutor],
mockOperationModelStore,
new NewRecordsState(),
);
});
afterEach(async () => {
// since the perist call in model store is not awaited, we need to flush the queue
// for tests that call start on the op repo
if (opRepo._timerID !== undefined) {
await vi.waitUntil(async () => {
const dbOps = await db.getAll('operations');
return dbOps.length === 0;
});
}
});
describe('Enqueue/Load Operations', () => {
test('can enqueue and load cached operations', async () => {
const cachedOperations = [new Operation('2'), new Operation('3')];
mockOperationModelStore.add(cachedOperations[0]);
mockOperationModelStore.add(cachedOperations[1]);
opRepo.enqueue(mockOperation);
expect(opRepo.queue).toEqual([
{
operation: mockOperation,
bucket: 0,
retries: 0,
},
]);
// cached operations are added to the front of the queue and should maintain order
await opRepo._start();
expect(opRepo.queue).toEqual([
{
operation: cachedOperations[0],
bucket: 0,
retries: 0,
},
{
operation: cachedOperations[1],
bucket: 0,
retries: 0,
},
{
operation: mockOperation,
bucket: 0,
retries: 0,
},
]);
});
test('enqueue should persist operations in IndexedDb', async () => {
await opRepo._loadSavedOperations();
const op1 = new SetAliasOperation(
APP_ID,
ONESIGNAL_ID,
'some-label',
'some-value',
);
opRepo.enqueue(op1);
const op2 = new CreateSubscriptionOperation({
appId: APP_ID,
onesignalId: ONESIGNAL_ID,
token: 'some-token',
type: SubscriptionType.ChromePush,
subscriptionId: SUB_ID,
});
opRepo.enqueue(op2);
expect(mockOperationModelStore.list()).toEqual([op1, op2]);
// persist happens in the background, so we need to wait for it to complete
let ops: IndexedDBSchema['operations']['value'][] = [];
await vi.waitUntil(async () => {
ops = await db.getAll('operations');
return ops.length === 2;
});
// IndexedDB returns operations in a random order
ops = ops.sort((a, b) => a.name.localeCompare(b.name));
expect(ops).toEqual([
{
...op2.toJSON(),
modelId: op2.modelId,
modelName: 'operations',
},
{
...op1.toJSON(),
modelId: op1.modelId,
modelName: 'operations',
},
]);
});
test('operations can be loaded from IndexedDb on start', async () => {
const op = new SetAliasOperation();
const op2 = new CreateSubscriptionOperation();
await db.put('operations', {
...op.toJSON(),
modelId: '1',
modelName: 'operations',
});
await db.put('operations', {
...op2.toJSON(),
modelId: '2',
modelName: 'operations',
});
await opRepo._loadSavedOperations();
const list = await db.getAll('operations');
expect(list).toEqual([
{
...op.toJSON(),
modelId: '1',
modelName: 'operations',
},
{
...op2.toJSON(),
modelId: '2',
modelName: 'operations',
},
]);
});
});
test('containsInstanceOf', async () => {
class MyOperation extends Operation {
constructor() {
super('id1');
}
}
class MyOperation2 extends MyOperation {}
opRepo.enqueue(new MyOperation());
expect(opRepo._containsInstanceOf(MyOperation)).toBe(true);
expect(opRepo._containsInstanceOf(MyOperation2)).toBe(false);
});
test('operations should be processed after start call', async () => {
const getNextOpsSpy = vi.spyOn(opRepo, '_getNextOps');
await opRepo._start();
opRepo.enqueue(mockOperation);
expect(opRepo.queue.length).toBe(1);
// index will be 1 if enqueue is after start
await vi.waitUntil(() => getNextOpsSpy.mock.calls.length > 0);
expect(getNextOpsSpy).toHaveBeenCalledWith(0);
expect(opRepo.queue.length).toBe(0);
});
test('can get grouped operations', () => {
const singleOp = new Operation('1', GroupComparisonType.NONE);
const groupedOps = getGroupedOp();
let op = new OperationQueueItem({
operation: singleOp,
bucket: 0,
});
// single operation should be returned as is
expect(opRepo._getGroupableOperations(op)).toEqual([op]);
// can group operations by same create comparison key
op = new OperationQueueItem({
operation: groupedOps[0],
bucket: 0,
});
let op2 = new OperationQueueItem({
operation: groupedOps[1],
bucket: 0,
});
opRepo.enqueue(op2.operation);
expect(opRepo._getGroupableOperations(op)).toEqual([op, op2]);
// can group operations by same modify comparison key
op = new OperationQueueItem({
operation: new Operation('1', GroupComparisonType.ALTER, '', 'abc'),
bucket: 0,
});
op2 = new OperationQueueItem({
operation: new Operation('2', GroupComparisonType.ALTER, '', 'abc'),
bucket: 0,
});
opRepo.enqueue(op2.operation);
expect(opRepo._getGroupableOperations(op)).toEqual([op, op2]);
// throws for no comparison keys
op = new OperationQueueItem({
operation: new Operation('1', GroupComparisonType.CREATE),
bucket: 0,
});
opRepo.enqueue(op2.operation);
expect(() => opRepo._getGroupableOperations(op)).toThrow(
'Both comparison keys cannot be blank!',
);
// returns the starting operation if other operations cant access record
const blockedId = '456';
const records = opRepo._records;
records.set(blockedId, Date.now());
op = new OperationQueueItem({
operation: new Operation('1', GroupComparisonType.CREATE, 'def'),
bucket: 0,
});
op2.operation.setProperty('onesignalId', blockedId);
opRepo.enqueue(op2.operation);
expect(opRepo._getGroupableOperations(op)).toEqual([op]);
});
describe('Executor Operations', () => {
test('can handle success operation and process additional operations', async () => {
const additionalOps = [
new Operation('3', GroupComparisonType.NONE),
new Operation('4', GroupComparisonType.NONE),
];
executeFn.mockResolvedValueOnce({
result: ExecutionResult.SUCCESS,
operations: additionalOps,
});
opRepo.enqueue(mockOperation);
expect(mockOperationModelStore.list()).toEqual([mockOperation]);
// execute the operation
await executeOps(opRepo);
// operation should be removed from the model store
// additional operations should be added to the model store
expect(mockOperationModelStore.list()).toEqual([
additionalOps[0],
additionalOps[1],
]);
expect(opRepo.queue).toEqual([
{
operation: additionalOps[0],
bucket: 0,
retries: 0,
},
{
operation: additionalOps[1],
bucket: 0,
retries: 0,
},
]);
});
test.each([
['FailUnauthorized', ExecutionResult.FAIL_UNAUTHORIZED],
['FailNoRetry', ExecutionResult.FAIL_NORETRY],
['FailConflict', ExecutionResult.FAIL_CONFLICT],
])('can handle failed operation: %s', async (_, failResult) => {
executeFn.mockResolvedValueOnce({
result: failResult,
});
opRepo.enqueue(mockOperation);
expect(mockOperationModelStore.list()).toEqual([mockOperation]);
// execute the operation
await executeOps(opRepo);
// operation should be removed from the model store
expect(mockOperationModelStore.list()).toEqual([]);
});
test('can handle success starting only operation', async () => {
executeFn.mockResolvedValueOnce({
result: ExecutionResult.SUCCESS_STARTING_ONLY,
});
const executeOperationsSpy = vi.spyOn(opRepo, '_executeOperations');
const groupedOps = getGroupedOp();
opRepo.enqueue(groupedOps[0]);
opRepo.enqueue(groupedOps[1]);
expect(mockOperationModelStore.list()).toEqual([
groupedOps[0],
groupedOps[1],
]);
await executeOps(opRepo);
expect(executeOperationsSpy).toHaveBeenCalledOnce();
// operation should be removed from the model store
expect(mockOperationModelStore.list()).toEqual([groupedOps[1]]);
// group operations will be added to the queue except for the first/starting item
expect(opRepo.queue).toEqual([
{
operation: groupedOps[1],
bucket: 0,
retries: 0,
},
]);
});
test('can handle fail retry operation and delay next execution', async () => {
executeFn.mockResolvedValueOnce({
result: ExecutionResult.FAIL_RETRY,
retryAfterSeconds: 30,
});
const executeOperationsSpy = vi.spyOn(opRepo, '_executeOperations');
const groupedOps = getGroupedOp();
opRepo.enqueue(groupedOps[0]);
opRepo.enqueue(groupedOps[1]);
await executeOps(opRepo);
// operations will be added back to the front of the queue
expect(executeOperationsSpy).toHaveBeenCalledOnce();
expect(opRepo.queue).toEqual([
{
operation: groupedOps[0],
bucket: 0,
retries: 1,
},
{
operation: groupedOps[1],
bucket: 0,
retries: 1,
},
]);
// should wait 30 seconds before executing again
await vi.advanceTimersByTimeAsync(OP_REPO_EXECUTION_INTERVAL);
expect(executeOperationsSpy).toHaveBeenCalledOnce(); // 30 seconds has not passed yet
await vi.advanceTimersByTimeAsync(30000);
expect(executeOperationsSpy).toHaveBeenCalledTimes(2); // 30 seconds has passed
});
test('can handle fail pause op repo operation', async () => {
executeFn.mockResolvedValueOnce({
result: ExecutionResult.FAIL_PAUSE_OPREPO,
});
const groupedOps = getGroupedOp();
opRepo.enqueue(groupedOps[0]);
opRepo.enqueue(groupedOps[1]);
await executeOps(opRepo);
// operations will be added back to the front of the queue
expect(opRepo.queue).toEqual([
{
operation: groupedOps[0],
bucket: 0,
retries: 0,
},
{
operation: groupedOps[1],
bucket: 0,
retries: 0,
},
]);
// op repo should be paused
expect(opRepo._timerID).toBe(undefined);
});
test('can process delay for translations', async () => {
const idTranslations = {
'1': '2',
};
executeFn.mockResolvedValueOnce({
result: ExecutionResult.SUCCESS,
idTranslations,
});
const executeOperationsSpy = vi.spyOn(opRepo, '_executeOperations');
const newOp = new Operation('2', GroupComparisonType.NONE);
const opTranslateIdsSpy = vi.spyOn(newOp, 'translateIds');
opRepo.enqueue(mockOperation);
opRepo.enqueue(newOp);
await executeOps(opRepo);
expect(opTranslateIdsSpy).toHaveBeenCalledWith(idTranslations);
expect(opRepo._records).toEqual(new Map([['2', Date.now()]]));
// should wait 5 seconds before processing the queue again
await vi.advanceTimersByTimeAsync(OP_REPO_POST_CREATE_DELAY);
expect(executeOperationsSpy).toHaveBeenCalledOnce();
// can now process operations again
await vi.advanceTimersByTimeAsync(OP_REPO_EXECUTION_INTERVAL);
expect(executeOperationsSpy).toHaveBeenCalledTimes(2);
});
test('should process non-groupable operations separately', async () => {
const executeOperationsSpy = vi.spyOn(opRepo, '_executeOperations');
const newOp = new Operation('2', GroupComparisonType.NONE);
opRepo.enqueue(mockOperation);
opRepo.enqueue(newOp);
await executeOps(opRepo);
// first operation should be processed
expect(executeOperationsSpy).toHaveBeenCalledExactlyOnceWith([
{
operation: mockOperation,
bucket: 0,
retries: 0,
},
]);
expect(opRepo.queue).toEqual([
{ operation: newOp, bucket: 0, retries: 0 },
]);
// next operation should be processed
await vi.advanceTimersByTimeAsync(OP_REPO_EXECUTION_INTERVAL);
expect(executeOperationsSpy).toHaveBeenNthCalledWith(2, [
{
operation: newOp,
bucket: 0,
retries: 0,
},
]);
expect(opRepo.queue).toEqual([]);
// queue is clear so no more operations should be processed
await vi.advanceTimersByTimeAsync(OP_REPO_EXECUTION_INTERVAL);
expect(executeOperationsSpy).toHaveBeenCalledTimes(2);
});
});
});
const translateIdsFn = vi.fn();
class Operation extends OperationBase<{ value: string }> {
private _groupComparisonTypeValue: GroupComparisonValue;
private _createComparisonKey: string;
private _modifyComparisonKey: string;
private _applyToRecordId: string;
private _canStartExecute: boolean;
constructor(
value: string,
groupComparisonTypeValue: GroupComparisonValue = GroupComparisonType.NONE,
createComparisonKey = '',
modifyComparisonKey = '',
applyToRecordId = '',
canStartExecute = true,
) {
super('mock-op', APP_ID, ONESIGNAL_ID);
this.value = value;
this._groupComparisonTypeValue = groupComparisonTypeValue;
this._createComparisonKey = createComparisonKey;
this._modifyComparisonKey = modifyComparisonKey;
this._applyToRecordId = applyToRecordId;
this._canStartExecute = canStartExecute;
}
get value(): string {
return this.getProperty('value');
}
set value(value: string) {
this.setProperty('value', value);
}
get groupComparisonType(): GroupComparisonValue {
return this._groupComparisonTypeValue;
}
get createComparisonKey(): string {
return this._createComparisonKey;
}
get modifyComparisonKey(): string {
return this._modifyComparisonKey;
}
get applyToRecordId(): string {
return this._applyToRecordId;
}
set applyToRecordId(value: string) {
this._applyToRecordId = value;
}
get canStartExecute(): boolean {
return this._canStartExecute;
}
translateIds() {
translateIdsFn();
}
}
const mockOperation = new Operation(
'1',
GroupComparisonType.CREATE,
'abc',
'',
'123',
);
const executeFn: Mock<IOperationExecutor['execute']> = vi.fn(async () => ({
result: ExecutionResult.SUCCESS,
}));
const mockExecutor: IOperationExecutor = {
operations: [mockOperation.name],
execute: executeFn,
};