1 import type { DecryptedMessage } from '@proton/docs-shared'
2 import { BatchDocumentUpdates } from './BatchDocumentUpdates'
4 describe('BatchDocumentUpdates', () => {
5 const batchDocumentUpdates = new BatchDocumentUpdates()
7 it('should create batches of updates based on the threshold provided', () => {
8 const twoHundredMessages = Array.from(
12 content: new Uint8Array(),
13 }) as DecryptedMessage,
16 const hundredThreshold = 100
17 const hundredThresholdResult = batchDocumentUpdates.execute(twoHundredMessages, hundredThreshold)
18 expect(hundredThresholdResult.isFailed()).toBeFalsy()
19 expect(hundredThresholdResult.getValue().length).toBe(2)
20 expect(hundredThresholdResult.getValue()[0].length).toBe(100)
21 expect(hundredThresholdResult.getValue()[1].length).toBe(100)
23 const fiftyThreshold = 50
24 const fiftyThresholdResult = batchDocumentUpdates.execute(twoHundredMessages, fiftyThreshold)
25 expect(fiftyThresholdResult.isFailed()).toBeFalsy()
26 expect(fiftyThresholdResult.getValue().length).toBe(4)
27 expect(fiftyThresholdResult.getValue()[0].length).toBe(50)
28 expect(fiftyThresholdResult.getValue()[1].length).toBe(50)
29 expect(fiftyThresholdResult.getValue()[2].length).toBe(50)
30 expect(fiftyThresholdResult.getValue()[3].length).toBe(50)
33 it('should create batches even if number of messages is not perfectly divisible by threshold', () => {
34 const twoTwentyFiveMessages = Array.from(
38 content: new Uint8Array(),
39 }) as DecryptedMessage,
42 const hundredThreshold = 100
43 const hundredThresholdResult = batchDocumentUpdates.execute(twoTwentyFiveMessages, hundredThreshold)
44 expect(hundredThresholdResult.isFailed()).toBeFalsy()
45 expect(hundredThresholdResult.getValue().length).toBe(3)
46 expect(hundredThresholdResult.getValue()[0].length).toBe(100)
47 expect(hundredThresholdResult.getValue()[1].length).toBe(100)
48 expect(hundredThresholdResult.getValue()[2].length).toBe(25)
50 const twoHundredMessages = Array.from(
54 content: new Uint8Array(),
55 }) as DecryptedMessage,
58 const sixtyThreshold = 60
59 const sixtyThresholdResult = batchDocumentUpdates.execute(twoHundredMessages, sixtyThreshold)
60 expect(sixtyThresholdResult.isFailed()).toBeFalsy()
61 expect(sixtyThresholdResult.getValue().length).toBe(4)
62 expect(sixtyThresholdResult.getValue()[0].length).toBe(60)
63 expect(sixtyThresholdResult.getValue()[1].length).toBe(60)
64 expect(sixtyThresholdResult.getValue()[2].length).toBe(60)
65 expect(sixtyThresholdResult.getValue()[3].length).toBe(20)