1 import { getIsValidAlarm, getSupportedAlarm } from '../../lib/calendar/icsSurgery/valarm';
2 import type { VcalDateProperty, VcalValarmComponent } from '../../lib/interfaces/calendar';
4 describe('getIsValidAlarm', () => {
5 const baseTriggerValue = { weeks: 0, days: 1, hours: 0, minutes: 0, seconds: 0, isNegative: true };
8 action: { value: 'DISPLAY' },
9 trigger: { value: baseTriggerValue },
10 } as VcalValarmComponent;
12 it('it reject alarms with unknown action', () => {
13 const alarm = { ...baseAlarm, action: { value: 'WHATEVER' } };
14 expect(getIsValidAlarm(alarm)).toEqual(false);
17 it('it should reject alarms with repeat but no duration', () => {
18 const alarm = { ...baseAlarm, repeat: { value: '1' } };
19 expect(getIsValidAlarm(alarm)).toEqual(false);
22 it('it should reject alarms with a malformed trigger', () => {
26 value: { year: 2020, month: 5, day: 2 },
27 parameters: { type: 'date-time' },
29 } as VcalValarmComponent;
30 expect(getIsValidAlarm(alarm)).toEqual(false);
34 describe('getSupportedAlarm', () => {
35 const dtstartPartDay = {
36 value: { year: 2020, month: 5, day: 11, hours: 12, minutes: 30, seconds: 0, isUTC: true },
38 const dtstartAllDay = {
39 value: { year: 2020, month: 5, day: 11 },
40 parameters: { type: 'date' },
41 } as VcalDateProperty;
42 const baseTriggerValue = { weeks: 0, days: 1, hours: 0, minutes: 0, seconds: 0, isNegative: true };
45 action: { value: 'DISPLAY' },
46 trigger: { value: baseTriggerValue },
47 } as VcalValarmComponent;
49 it('it should filter out alarms with trigger related to end time', () => {
53 value: { ...baseTriggerValue },
54 parameters: { related: 'END' },
57 expect(getSupportedAlarm(alarm, dtstartPartDay)).toEqual(undefined);
60 // Duplicating EMAIL and DISPLAY to ensure both work
61 it('it should filter out attendees, description and summary', () => {
64 action: { value: 'EMAIL' },
65 description: { value: 'test' },
66 summary: { value: 'test' },
67 attendee: [{ value: 'mailto:wild@west.org' }],
69 value: { ...baseTriggerValue },
72 const emailExpected = {
74 action: { value: 'EMAIL' },
76 value: { ...baseTriggerValue },
79 const displayAlarm = {
81 action: { value: 'DISPLAY' },
82 description: { value: 'test' },
83 summary: { value: 'test' },
84 attendee: [{ value: 'mailto:wild@west.org' }],
86 value: { ...baseTriggerValue },
89 const displayExpected = {
91 action: { value: 'DISPLAY' },
93 value: { ...baseTriggerValue },
97 expect(getSupportedAlarm(emailAlarm, dtstartPartDay)).toEqual(emailExpected);
98 expect(getSupportedAlarm(displayAlarm, dtstartPartDay)).toEqual(displayExpected);
101 it('it should filter out future notifications', () => {
105 value: { ...baseTriggerValue, isNegative: false },
108 expect(getSupportedAlarm(alarm, dtstartPartDay)).toEqual(undefined);
109 expect(getSupportedAlarm(alarm, dtstartAllDay)).toEqual(undefined);
112 it('it should normalize triggers for part-day events', () => {
113 const alarms: VcalValarmComponent[] = [
117 value: { weeks: 0, days: 1, hours: 2, minutes: 1, seconds: 30, isNegative: true },
123 value: { year: 2020, month: 4, day: 12, hours: 9, minutes: 30, seconds: 0, isUTC: true },
124 parameters: { type: 'date-time' },
132 value: { weeks: 0, days: 0, hours: 0, minutes: 1561, seconds: 0, isNegative: true },
138 value: { weeks: 0, days: 0, hours: 699, minutes: 0, seconds: 0, isNegative: true },
142 const results = alarms.map((alarm) => getSupportedAlarm(alarm, dtstartPartDay));
143 expect(results).toEqual(expected);
146 it('it should normalize triggers for all-day events', () => {
147 const alarms: VcalValarmComponent[] = [
151 value: { weeks: 0, days: 1, hours: 2, minutes: 1, seconds: 30, isNegative: true },
157 value: { year: 2020, month: 4, day: 12, hours: 9, minutes: 30, seconds: 0, isUTC: true },
158 parameters: { type: 'date-time' },
166 value: { weeks: 0, days: 1, hours: 2, minutes: 1, seconds: 0, isNegative: true },
172 value: { weeks: 0, days: 28, hours: 14, minutes: 30, seconds: 0, isNegative: true },
176 const results = alarms.map((alarm) => getSupportedAlarm(alarm, dtstartAllDay));
177 expect(results).toEqual(expected);
180 it('it should filter out notifications out of bounds for part-day events', () => {
181 const alarms: VcalValarmComponent[] = [
185 value: { weeks: 1000, days: 1, hours: 2, minutes: 1, seconds: 30, isNegative: true },
191 value: { year: 1851, month: 4, day: 12, hours: 9, minutes: 30, seconds: 0, isUTC: true },
192 parameters: { type: 'date-time' },
196 const expected = alarms.map(() => undefined);
197 const results = alarms.map((alarm) => getSupportedAlarm(alarm, dtstartPartDay));
198 expect(results).toEqual(expected);
201 it('it should filter out notifications out of bounds for all-day events', () => {
202 const alarms: VcalValarmComponent[] = [
206 value: { weeks: 1000, days: 1, hours: 2, minutes: 1, seconds: 30, isNegative: true },
212 value: { year: 1851, month: 4, day: 12, hours: 9, minutes: 30, seconds: 0, isUTC: true },
213 parameters: { type: 'date-time' },
217 const expected = alarms.map(() => undefined);
218 const results = alarms.map((alarm) => getSupportedAlarm(alarm, dtstartAllDay));
219 expect(results).toEqual(expected);