Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / test / calendar / veventHelper.spec.ts
blob99f822ec22d5067b8adee84e7d98358c92a72084
1 import type { VcalVeventComponent } from '@proton/shared/lib/interfaces/calendar';
3 import { CALENDAR_CARD_TYPE } from '../../lib/calendar/constants';
4 import { parse } from '../../lib/calendar/vcal';
5 import { getVeventParts, withMandatoryPublishFields, withoutRedundantDtEnd } from '../../lib/calendar/veventHelper';
6 import { toCRLF } from '../../lib/helpers/string';
8 const { ENCRYPTED_AND_SIGNED, SIGNED, CLEAR_TEXT } = CALENDAR_CARD_TYPE;
10 describe('getVeventParts()', () => {
11     it('should split shared parts', () => {
12         const y = parse(`BEGIN:VEVENT
13 UID:abc
14 DTSTART;TZID=Europe/Zurich:20200311T100000
15 DTEND;TZID=Europe/Zurich:20200312T100000
16 DESCRIPTION:bca
17 SUMMARY:dcf
18 SEQUENCE:2
19 LOCATION:asd
20 END:VEVENT
21 `) as VcalVeventComponent;
22         const result = getVeventParts(y);
24         expect(result).toEqual({
25             sharedPart: {
26                 [SIGNED]: toCRLF(`BEGIN:VCALENDAR
27 VERSION:2.0
28 BEGIN:VEVENT
29 UID:abc
30 DTSTART;TZID=Europe/Zurich:20200311T100000
31 DTEND;TZID=Europe/Zurich:20200312T100000
32 SEQUENCE:2
33 END:VEVENT
34 END:VCALENDAR`),
35                 [ENCRYPTED_AND_SIGNED]: toCRLF(`BEGIN:VCALENDAR
36 VERSION:2.0
37 BEGIN:VEVENT
38 UID:abc
39 DESCRIPTION:bca
40 SUMMARY:dcf
41 LOCATION:asd
42 END:VEVENT
43 END:VCALENDAR`),
44             },
45             calendarPart: { [SIGNED]: undefined, [ENCRYPTED_AND_SIGNED]: undefined },
46             personalPart: { [SIGNED]: undefined, [ENCRYPTED_AND_SIGNED]: undefined },
47             attendeesPart: { [CLEAR_TEXT]: [], [SIGNED]: undefined, [ENCRYPTED_AND_SIGNED]: undefined },
48             notificationsPart: [],
49         });
50     });
52     it('should split shared, calendar, and personal parts', () => {
53         const y = parse(`BEGIN:VEVENT
54 UID:abc
55 DTSTART;TZID=Europe/Zurich:20200311T100000
56 DTEND;TZID=Europe/Zurich:20200312T100000
57 STATUS:TENTATIVE
58 COMMENT:my comment
59 DESCRIPTION:bca
60 TRANSP:TRANSPARENT
61 SUMMARY:dcf
62 LOCATION:asd
63 SEQUENCE:1
64 BEGIN:VALARM
65 UID:abc
66 TRIGGER:-PT15H
67 ACTION:DISPLAY
68 DESCRIPTION:asd
69 END:VALARM
70 END:VEVENT
71 `) as VcalVeventComponent;
72         const result = getVeventParts(y);
73         expect(result).toEqual({
74             sharedPart: {
75                 [SIGNED]: toCRLF(`BEGIN:VCALENDAR
76 VERSION:2.0
77 BEGIN:VEVENT
78 UID:abc
79 DTSTART;TZID=Europe/Zurich:20200311T100000
80 DTEND;TZID=Europe/Zurich:20200312T100000
81 SEQUENCE:1
82 END:VEVENT
83 END:VCALENDAR`),
84                 [ENCRYPTED_AND_SIGNED]: toCRLF(`BEGIN:VCALENDAR
85 VERSION:2.0
86 BEGIN:VEVENT
87 UID:abc
88 DESCRIPTION:bca
89 SUMMARY:dcf
90 LOCATION:asd
91 END:VEVENT
92 END:VCALENDAR`),
93             },
94             calendarPart: {
95                 [SIGNED]: toCRLF(`BEGIN:VCALENDAR
96 VERSION:2.0
97 BEGIN:VEVENT
98 UID:abc
99 STATUS:TENTATIVE
100 TRANSP:TRANSPARENT
101 END:VEVENT
102 END:VCALENDAR`),
103                 [ENCRYPTED_AND_SIGNED]: toCRLF(`BEGIN:VCALENDAR
104 VERSION:2.0
105 BEGIN:VEVENT
106 UID:abc
107 COMMENT:my comment
108 END:VEVENT
109 END:VCALENDAR`),
110             },
111             personalPart: {
112                 [SIGNED]: toCRLF(`BEGIN:VCALENDAR
113 VERSION:2.0
114 BEGIN:VEVENT
115 UID:abc
116 BEGIN:VALARM
117 UID:abc
118 TRIGGER:-PT15H
119 ACTION:DISPLAY
120 DESCRIPTION:asd
121 END:VALARM
122 END:VEVENT
123 END:VCALENDAR`),
124                 [ENCRYPTED_AND_SIGNED]: undefined,
125             },
126             attendeesPart: { [CLEAR_TEXT]: [], [SIGNED]: undefined, [ENCRYPTED_AND_SIGNED]: undefined },
127             notificationsPart: [{ Type: 1, Trigger: '-PT15H' }],
128         });
129     });
132 describe('withMandatoryPublishFields()', () => {
133     it('should add description for display alarms, and description, summary and attendee for email ones', () => {
134         const vevent = parse(`BEGIN:VEVENT
135 UID:abc
136 DTSTART;TZID=Europe/Zurich:20200311T100000
137 DTSTAMP:20200308T134254Z
138 STATUS:TENTATIVE
139 COMMENT:my comment
140 DESCRIPTION:bca
141 TRANSP:TRANSPARENT
142 SUMMARY:dcf
143 LOCATION:asd
144 SEQUENCE:1
145 BEGIN:VALARM
146 TRIGGER:-PT15M
147 ACTION:DISPLAY
148 END:VALARM
149 BEGIN:VALARM
150 TRIGGER:PT15H
151 ACTION:DISPLAY
152 END:VALARM
153 BEGIN:VALARM
154 TRIGGER:-PT1W2D
155 ACTION:EMAIL
156 END:VALARM
157 END:VEVENT
158 `) as VcalVeventComponent;
159         const expected: VcalVeventComponent = {
160             component: 'vevent',
161             uid: { value: 'abc' },
162             dtstart: {
163                 value: { year: 2020, month: 3, day: 11, hours: 10, minutes: 0, seconds: 0, isUTC: false },
164                 parameters: { tzid: 'Europe/Zurich' },
165             },
166             dtstamp: {
167                 value: { year: 2020, month: 3, day: 8, hours: 13, minutes: 42, seconds: 54, isUTC: true },
168             },
169             status: { value: 'TENTATIVE' },
170             comment: [{ value: 'my comment' }],
171             description: { value: 'bca' },
172             transp: { value: 'TRANSPARENT' },
173             summary: { value: 'dcf' },
174             location: { value: 'asd' },
175             sequence: { value: 1 },
176             components: [
177                 {
178                     component: 'valarm',
179                     action: {
180                         value: 'DISPLAY',
181                     },
182                     trigger: {
183                         value: {
184                             days: 0,
185                             hours: 0,
186                             isNegative: true,
187                             minutes: 15,
188                             seconds: 0,
189                             weeks: 0,
190                         },
191                     },
192                     description: { value: 'dcf' },
193                 },
194                 {
195                     component: 'valarm',
196                     action: {
197                         value: 'DISPLAY',
198                     },
199                     trigger: {
200                         value: {
201                             days: 0,
202                             hours: 15,
203                             isNegative: false,
204                             minutes: 0,
205                             seconds: 0,
206                             weeks: 0,
207                         },
208                     },
209                     description: { value: 'dcf' },
210                 },
211                 {
212                     component: 'valarm',
213                     action: {
214                         value: 'EMAIL',
215                     },
216                     trigger: {
217                         value: {
218                             days: 2,
219                             hours: 0,
220                             isNegative: true,
221                             minutes: 0,
222                             seconds: 0,
223                             weeks: 1,
224                         },
225                     },
226                     summary: { value: 'dcf' },
227                     description: { value: 'dcf' },
228                     attendee: [{ value: 'mailto:protonlovestesting@proton.me' }],
229                 },
230             ],
231         };
233         expect(withMandatoryPublishFields(vevent, 'protonlovestesting@proton.me')).toEqual(expected);
234     });
236     it('should add a null summary if the event has no title', () => {
237         const vevent = parse(`BEGIN:VEVENT
238 UID:abc
239 DTSTART;TZID=Europe/Zurich:20200311T100000
240 DTSTAMP:20200308T134254Z
241 BEGIN:VALARM
242 TRIGGER:-PT15M
243 ACTION:DISPLAY
244 END:VALARM
245 BEGIN:VALARM
246 TRIGGER:-PT1W2D
247 ACTION:EMAIL
248 END:VALARM
249 END:VEVENT
250 `) as VcalVeventComponent;
251         const expected: VcalVeventComponent = {
252             component: 'vevent',
253             uid: { value: 'abc' },
254             dtstart: {
255                 value: { year: 2020, month: 3, day: 11, hours: 10, minutes: 0, seconds: 0, isUTC: false },
256                 parameters: { tzid: 'Europe/Zurich' },
257             },
258             dtstamp: {
259                 value: { year: 2020, month: 3, day: 8, hours: 13, minutes: 42, seconds: 54, isUTC: true },
260             },
261             summary: { value: '' },
262             components: [
263                 {
264                     component: 'valarm',
265                     action: {
266                         value: 'DISPLAY',
267                     },
268                     trigger: {
269                         value: {
270                             days: 0,
271                             hours: 0,
272                             isNegative: true,
273                             minutes: 15,
274                             seconds: 0,
275                             weeks: 0,
276                         },
277                     },
278                     description: { value: '(no title)' },
279                 },
280                 {
281                     component: 'valarm',
282                     action: {
283                         value: 'EMAIL',
284                     },
285                     trigger: {
286                         value: {
287                             days: 2,
288                             hours: 0,
289                             isNegative: true,
290                             minutes: 0,
291                             seconds: 0,
292                             weeks: 1,
293                         },
294                     },
295                     summary: { value: '(no title)' },
296                     description: { value: '(no title)' },
297                     attendee: [{ value: 'mailto:protonlovestesting@proton.me' }],
298                 },
299             ],
300         };
302         expect(withMandatoryPublishFields(vevent, 'protonlovestesting@proton.me')).toEqual(expected);
303     });
306 describe('withoutRedundantDtEnd', () => {
307     describe('full day', () => {
308         it('should remove redundant dtend', () => {
309             const ALL_DAY_COMPONENT = parse(`BEGIN:VEVENT
310 UID:abc
311 DTSTAMP:20230614T101702Z
312 DTSTART;VALUE=DATE:20190812
313 DTEND;VALUE=DATE:20190813
314 SUMMARY:text
315 RRULE:FREQ=DAILY
316 END:VEVENT`) as VcalVeventComponent;
318             expect(withoutRedundantDtEnd(ALL_DAY_COMPONENT)).toEqual({
319                 component: 'vevent',
320                 uid: { value: 'abc' },
321                 dtstart: { value: { year: 2019, month: 8, day: 12 }, parameters: { type: 'date' } },
322                 dtstamp: { value: { year: 2023, month: 6, day: 14, hours: 10, minutes: 17, seconds: 2, isUTC: true } },
323                 summary: { value: 'text' },
324                 rrule: { value: { freq: 'DAILY' } },
325             });
326         });
328         it('should not remove dtend', () => {
329             const ALL_DAY_COMPONENT = parse(`BEGIN:VEVENT
330 UID:abc
331 DTSTAMP:20230614T101702Z
332 DTSTART;VALUE=DATE:20190812
333 DTEND;VALUE=DATE:20190814
334 SUMMARY:text
335 RRULE:FREQ=DAILY
336 END:VEVENT`) as VcalVeventComponent;
338             expect(withoutRedundantDtEnd(ALL_DAY_COMPONENT)).toEqual({
339                 component: 'vevent',
340                 uid: { value: 'abc' },
341                 dtstart: { value: { year: 2019, month: 8, day: 12 }, parameters: { type: 'date' } },
342                 dtstamp: { value: { year: 2023, month: 6, day: 14, hours: 10, minutes: 17, seconds: 2, isUTC: true } },
343                 dtend: { value: { year: 2019, month: 8, day: 14 }, parameters: { type: 'date' } },
344                 summary: { value: 'text' },
345                 rrule: { value: { freq: 'DAILY' } },
346             });
347         });
348     });
350     describe('part day', () => {
351         it('should remove redundant dtend', () => {
352             const PART_DAY_COMPONENT = parse(`BEGIN:VEVENT
353 UID:abc
354 DTSTAMP:20230614T101702Z
355 DTSTART;TZID=Europe/Zurich:20190719T120000
356 DTEND;TZID=Europe/Zurich:20190719T120000
357 SUMMARY:text
358 RRULE:FREQ=DAILY
359 END:VEVENT`) as VcalVeventComponent;
361             expect(withoutRedundantDtEnd(PART_DAY_COMPONENT)).toEqual({
362                 component: 'vevent',
363                 uid: { value: 'abc' },
364                 dtstamp: { value: { year: 2023, month: 6, day: 14, hours: 10, minutes: 17, seconds: 2, isUTC: true } },
365                 dtstart: {
366                     value: { year: 2019, month: 7, day: 19, hours: 12, minutes: 0, seconds: 0, isUTC: false },
367                     parameters: {
368                         tzid: 'Europe/Zurich',
369                     },
370                 },
371                 summary: { value: 'text' },
372                 rrule: { value: { freq: 'DAILY' } },
373             });
374         });
376         it('should not remove dtend', () => {
377             const PART_DAY_COMPONENT = parse(`BEGIN:VEVENT
378 UID:abc
379 DTSTAMP:20230614T101702Z
380 DTSTART;TZID=Europe/Zurich:20190719T120000
381 DTEND:20190719T160000Z
382 SUMMARY:text
383 RRULE:FREQ=DAILY
384 END:VEVENT`) as VcalVeventComponent;
386             expect(withoutRedundantDtEnd(PART_DAY_COMPONENT)).toEqual({
387                 component: 'vevent',
388                 uid: { value: 'abc' },
389                 dtstamp: { value: { year: 2023, month: 6, day: 14, hours: 10, minutes: 17, seconds: 2, isUTC: true } },
390                 dtstart: {
391                     value: { year: 2019, month: 7, day: 19, hours: 12, minutes: 0, seconds: 0, isUTC: false },
392                     parameters: { tzid: 'Europe/Zurich' },
393                 },
394                 dtend: { value: { year: 2019, month: 7, day: 19, hours: 16, minutes: 0, seconds: 0, isUTC: true } },
395                 summary: { value: 'text' },
396                 rrule: { value: { freq: 'DAILY' } },
397             });
398         });
399     });