Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / utils / transfer.test.ts
blobdde244b548293142bef65fac8c5f81651f5d9af0
1 import { ProgressBarStatus } from '../components/TransferManager/ProgressBar';
2 import type { Transfer, TransfersStats } from '../components/TransferManager/transfer';
3 import { TransferState } from '../components/TransferManager/transfer';
4 import {
5     calculateProgress,
6     getProgressBarStatus,
7     isTransferActive,
8     isTransferCancelError,
9     isTransferCanceled,
10     isTransferDone,
11     isTransferError,
12     isTransferFailed,
13     isTransferFinalizing,
14     isTransferFinished,
15     isTransferInitializing,
16     isTransferPaused,
17     isTransferProgress,
18     isTransferRetry,
19     isTransferSkipped,
20 } from './transfer';
22 describe('trasfer utils', () => {
23     const allStatesList = [
24         TransferState.Canceled,
25         TransferState.Skipped,
26         TransferState.Done,
27         TransferState.Error,
28         TransferState.Finalizing,
29         TransferState.Initializing,
30         TransferState.Paused,
31         TransferState.Pending,
32         TransferState.Progress,
33     ];
35     describe('isTransferFinished', () => {
36         [TransferState.Error, TransferState.Canceled, TransferState.Done].forEach((state) => {
37             it(`should return true for transfer state ${state}`, () => {
38                 expect(isTransferFinished({ state })).toBeTruthy();
39             });
40         });
42         allStatesList
43             .filter(
44                 (state) =>
45                     ![TransferState.Error, TransferState.Canceled, TransferState.Skipped, TransferState.Done].includes(
46                         state
47                     )
48             )
49             .forEach((state) => {
50                 it(`should return flase for transfer state ${state}`, () => {
51                     expect(isTransferFinished({ state })).toBeFalsy();
52                 });
53             });
54     });
56     describe('isTransferActive', () => {
57         [TransferState.Pending, TransferState.Progress, TransferState.Initializing, TransferState.Finalizing].forEach(
58             (state) => {
59                 it(`should return true for transfer state ${state}`, () => {
60                     expect(isTransferActive({ state })).toBeTruthy();
61                 });
62             }
63         );
65         allStatesList
66             .filter(
67                 (state) =>
68                     ![
69                         TransferState.Pending,
70                         TransferState.Progress,
71                         TransferState.Initializing,
72                         TransferState.Finalizing,
73                     ].includes(state)
74             )
75             .forEach((state) => {
76                 it(`should return flase for transfer state ${state}`, () => {
77                     expect(isTransferActive({ state })).toBeFalsy();
78                 });
79             });
80     });
82     describe('isTransferFailed', () => {
83         [TransferState.Canceled, TransferState.Error].forEach((state) => {
84             it(`should return true for transfer state ${state}`, () => {
85                 expect(isTransferFailed({ state })).toBeTruthy();
86             });
87         });
89         allStatesList
90             .filter((state) => ![TransferState.Canceled, TransferState.Error].includes(state))
91             .forEach((state) => {
92                 it(`should return flase for transfer state ${state}`, () => {
93                     expect(isTransferFailed({ state })).toBeFalsy();
94                 });
95             });
96     });
98     describe('isTransferDone', () => {
99         it(`should return true for transfer state ${TransferState.Done}`, () => {
100             expect(isTransferDone({ state: TransferState.Done })).toBeTruthy();
101         });
103         allStatesList
104             .filter((state) => state !== TransferState.Done)
105             .forEach((state) => {
106                 it(`should return flase for transfer state ${state}`, () => {
107                     expect(isTransferDone({ state })).toBeFalsy();
108                 });
109             });
110     });
112     describe('isTransferError', () => {
113         it(`should return true for transfer state ${TransferState.Error}`, () => {
114             expect(isTransferError({ state: TransferState.Error })).toBeTruthy();
115         });
117         allStatesList
118             .filter((state) => state !== TransferState.Error)
119             .forEach((state) => {
120                 it(`should return flase for transfer state ${state}`, () => {
121                     expect(isTransferError({ state })).toBeFalsy();
122                 });
123             });
124     });
126     describe('isTransferCanceled', () => {
127         it(`should return true for transfer state ${TransferState.Canceled}`, () => {
128             expect(isTransferCanceled({ state: TransferState.Canceled })).toBeTruthy();
129         });
131         allStatesList
132             .filter((state) => state !== TransferState.Canceled)
133             .forEach((state) => {
134                 it(`should return flase for transfer state ${state}`, () => {
135                     expect(isTransferCanceled({ state })).toBeFalsy();
136                 });
137             });
138     });
140     describe('isTransferSkipped', () => {
141         it(`should return true for transfer state ${TransferState.Skipped}`, () => {
142             expect(isTransferSkipped({ state: TransferState.Skipped })).toBe(true);
143         });
145         allStatesList
146             .filter((state) => state !== TransferState.Skipped)
147             .forEach((state) => {
148                 it(`should return false for transfer state ${state}`, () => {
149                     expect(isTransferSkipped({ state })).toBe(false);
150                 });
151             });
152     });
154     describe('isTransferProgress', () => {
155         it(`should return true for transfer state ${TransferState.Progress}`, () => {
156             expect(isTransferProgress({ state: TransferState.Progress })).toBeTruthy();
157         });
159         allStatesList
160             .filter((state) => state !== TransferState.Progress)
161             .forEach((state) => {
162                 it(`should return flase for transfer state ${state}`, () => {
163                     expect(isTransferProgress({ state })).toBeFalsy();
164                 });
165             });
166     });
168     describe('isTransferInitializing', () => {
169         it(`should return true for transfer state ${TransferState.Initializing}`, () => {
170             expect(isTransferInitializing({ state: TransferState.Initializing })).toBeTruthy();
171         });
173         allStatesList
174             .filter((state) => state !== TransferState.Initializing)
175             .forEach((state) => {
176                 it(`should return flase for transfer state ${state}`, () => {
177                     expect(isTransferInitializing({ state })).toBeFalsy();
178                 });
179             });
180     });
182     describe('isTransferPaused', () => {
183         it(`should return true for transfer state ${TransferState.Paused}`, () => {
184             expect(isTransferPaused({ state: TransferState.Paused })).toBeTruthy();
185         });
187         allStatesList
188             .filter((state) => state !== TransferState.Paused)
189             .forEach((state) => {
190                 it(`should return flase for transfer state ${state}`, () => {
191                     expect(isTransferPaused({ state })).toBeFalsy();
192                 });
193             });
194     });
196     describe('isTransferFinalizing', () => {
197         it(`should return true for transfer state ${TransferState.Finalizing}`, () => {
198             expect(isTransferFinalizing({ state: TransferState.Finalizing })).toBeTruthy();
199         });
201         allStatesList
202             .filter((state) => state !== TransferState.Finalizing)
203             .forEach((state) => {
204                 it(`should return flase for transfer state ${state}`, () => {
205                     expect(isTransferFinalizing({ state })).toBeFalsy();
206                 });
207             });
208     });
210     describe('isTransferCancelError', () => {
211         ['TransferCancel', 'AbortError'].forEach((name) => {
212             it(`should return true for error with name ${name}`, () => {
213                 const error = {
214                     name,
215                     message: `${name} error accured.`,
216                 };
217                 expect(isTransferCancelError(error)).toBeTruthy();
218             });
219         });
221         ['TypeError', 'SyntaxError'].forEach((name) => {
222             it(`should return false for error with name ${name}`, () => {
223                 const error = {
224                     name,
225                     message: `${name} error accured.`,
226                 };
227                 expect(isTransferCancelError(error)).toBeFalsy();
228             });
229         });
230     });
232     describe('isTransferRetry', () => {
233         ['TransferRetry'].forEach((name) => {
234             it(`should return true for error with name ${name}`, () => {
235                 const error = {
236                     name,
237                     message: `${name} error accured.`,
238                 };
239                 expect(isTransferRetry(error)).toBeTruthy();
240             });
241         });
243         ['TypeError', 'SyntaxError'].forEach((name) => {
244             it(`should return false for error with name ${name}`, () => {
245                 const error = {
246                     name,
247                     message: `${name} error accured.`,
248                 };
249                 expect(isTransferRetry(error)).toBeFalsy();
250             });
251         });
252     });
254     describe('getProgressBarStatus', () => {
255         [
256             { state: TransferState.Done, status: ProgressBarStatus.Success },
257             { state: TransferState.Canceled, status: ProgressBarStatus.Disabled },
258             { state: TransferState.Error, status: ProgressBarStatus.Error },
259             { state: TransferState.Finalizing, status: ProgressBarStatus.Running },
260             { state: TransferState.Initializing, status: ProgressBarStatus.Running },
261             { state: TransferState.Paused, status: ProgressBarStatus.Running },
262             { state: TransferState.Pending, status: ProgressBarStatus.Running },
263             { state: TransferState.Progress, status: ProgressBarStatus.Running },
264         ].forEach(({ state, status }) => {
265             it(`should return progress bar status ${status} for transfer state ${state}`, () => {
266                 expect(getProgressBarStatus(state)).toEqual(status);
267             });
268         });
269     });
271     it(`calculateProgress should calculate progress of active trasfers`, () => {
272         const size1 = 734003200;
273         const size2 = 83404340;
274         const progress1 = 279297577;
275         const progress2 = 8340324;
276         const progress = Math.floor(100 * ((progress1 + progress2) / (size1 + size2 || 1)));
277         const stats: TransfersStats = {
278             'drive-transfers-5740': {
279                 averageSpeed: 2588514,
280                 progress: progress1,
281             },
282             'drive-transfers-7456': {
283                 averageSpeed: 734032,
284                 progress: progress2,
285             },
286         };
288         const transfers: Transfer[] = [
289             {
290                 id: 'drive-transfers-5740',
291                 meta: {
292                     size: size1,
293                     mimeType: 'application/octet-stream',
294                     filename: 'Pregenerated File',
295                 },
296                 state: TransferState.Finalizing,
297             } as any,
298             {
299                 id: 'drive-transfers-7456',
300                 meta: {
301                     size: size2,
302                     mimeType: 'application/octet-stream',
303                     filename: 'Pregenerated File 2',
304                 },
305                 state: TransferState.Progress,
306             } as any,
307         ];
309         expect(calculateProgress(stats, transfers)).toEqual(progress);
310     });