|
| 1 | +import { ObjectId } from "mongodb"; |
| 2 | +import { faker } from "@faker-js/faker"; |
| 3 | +import { GCAL_MAX_RECURRENCES } from "@core/constants/core.constants"; |
| 4 | +import dayjs from "@core/util/date/dayjs"; |
| 5 | +import { parseCompassEventDate } from "@core/util/event/event.util"; |
| 6 | +import { |
| 7 | + createMockBaseEvent, |
| 8 | + generateCompassEventDates, |
| 9 | +} from "@core/util/test/ccal.event.factory"; |
| 10 | +import { CompassEventRRule } from "@backend/event/classes/compass.event.rrule"; |
| 11 | + |
| 12 | +describe("CompassEventRRule: ", () => { |
| 13 | + it(`should return the correct number of events based on rrule count`, () => { |
| 14 | + const count = faker.number.int({ min: 1, max: GCAL_MAX_RECURRENCES }); |
| 15 | + const rruleString = `RRULE:FREQ=DAILY;COUNT=${count}`; |
| 16 | + |
| 17 | + const baseEvent = createMockBaseEvent({ |
| 18 | + recurrence: { rule: [rruleString] }, |
| 19 | + }); |
| 20 | + |
| 21 | + const rrule = new CompassEventRRule({ |
| 22 | + ...baseEvent, |
| 23 | + _id: new ObjectId(baseEvent._id), |
| 24 | + }); |
| 25 | + |
| 26 | + expect(rrule.toString()).toContain("RRULE:FREQ=DAILY"); |
| 27 | + expect(rrule.toString()).toContain(`COUNT=${count}`); |
| 28 | + expect(rrule.count()).toBe(count); |
| 29 | + expect(rrule.all()).toHaveLength(count); |
| 30 | + }); |
| 31 | + |
| 32 | + it(`should adjust the COUNT in the rrule string to a maximum of ${GCAL_MAX_RECURRENCES}`, () => { |
| 33 | + const rruleString = "RRULE:FREQ=DAILY;COUNT=1000"; |
| 34 | + |
| 35 | + const baseEvent = createMockBaseEvent({ |
| 36 | + recurrence: { rule: [rruleString] }, |
| 37 | + }); |
| 38 | + |
| 39 | + const rrule = new CompassEventRRule({ |
| 40 | + ...baseEvent, |
| 41 | + _id: new ObjectId(baseEvent._id), |
| 42 | + }); |
| 43 | + |
| 44 | + expect(rrule.toString()).toContain("RRULE:FREQ=DAILY"); |
| 45 | + expect(rrule.toString()).toContain(`COUNT=${GCAL_MAX_RECURRENCES}`); |
| 46 | + expect(rrule.count()).toBe(GCAL_MAX_RECURRENCES); |
| 47 | + expect(rrule.all()).toHaveLength(GCAL_MAX_RECURRENCES); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return the rrule in system timezone", () => { |
| 51 | + const baseEvent = createMockBaseEvent(); |
| 52 | + const rrule = new CompassEventRRule({ |
| 53 | + ...baseEvent, |
| 54 | + _id: new ObjectId(baseEvent._id), |
| 55 | + }); |
| 56 | + const startDate = parseCompassEventDate(baseEvent.startDate!); |
| 57 | + const events = rrule.all(); |
| 58 | + |
| 59 | + expect(rrule.options.dtstart.toISOString()).toEqual( |
| 60 | + startDate.toISOString(), |
| 61 | + ); |
| 62 | + |
| 63 | + expect(events).toEqual(expect.arrayContaining([startDate.toDate()])); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return the rrule without DTSTART and DTEND", () => { |
| 67 | + const baseEvent = createMockBaseEvent(); |
| 68 | + const rrule = new CompassEventRRule({ |
| 69 | + ...baseEvent, |
| 70 | + _id: new ObjectId(baseEvent._id), |
| 71 | + }); |
| 72 | + |
| 73 | + expect(rrule.toString().includes("DTSTART")).toEqual(false); |
| 74 | + expect(rrule.toString().includes("DTEND")).toEqual(false); |
| 75 | + |
| 76 | + expect( |
| 77 | + rrule.toRecurrence().some((rule) => rule.includes("DTSTART")), |
| 78 | + ).toEqual(false); |
| 79 | + |
| 80 | + expect(rrule.toRecurrence().some((rule) => rule.includes("DTEND"))).toEqual( |
| 81 | + false, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("base", () => { |
| 86 | + it("should return the recurrence string as an array", () => { |
| 87 | + const baseEvent = createMockBaseEvent(); |
| 88 | + const rrule = new CompassEventRRule({ |
| 89 | + ...baseEvent, |
| 90 | + _id: new ObjectId(baseEvent._id), |
| 91 | + }); |
| 92 | + const recurrence = rrule.toRecurrence(); |
| 93 | + |
| 94 | + expect(recurrence).toBeInstanceOf(Array); |
| 95 | + expect(recurrence.length).toBeGreaterThan(0); |
| 96 | + |
| 97 | + expect(recurrence.some((rule) => rule.startsWith("RRULE:"))).toEqual( |
| 98 | + true, |
| 99 | + ); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("instances", () => { |
| 104 | + it(`should return a maximum of ${GCAL_MAX_RECURRENCES} compass instances if no count is supplied in the recurrence`, () => { |
| 105 | + const rule = ["RRULE:FREQ=DAILY"]; |
| 106 | + const baseEvent = createMockBaseEvent({ recurrence: { rule } }); |
| 107 | + const rrule = new CompassEventRRule({ |
| 108 | + ...baseEvent, |
| 109 | + _id: new ObjectId(baseEvent._id), |
| 110 | + }); |
| 111 | + const instances = rrule.instances(); |
| 112 | + |
| 113 | + expect(instances).toBeInstanceOf(Array); |
| 114 | + expect(instances).toHaveLength(GCAL_MAX_RECURRENCES); |
| 115 | + }); |
| 116 | + |
| 117 | + it(`should return a maximum of ${GCAL_MAX_RECURRENCES} compass instances if count exceeds maximum recurrence`, () => { |
| 118 | + const rule = ["RRULE:FREQ=DAILY;COUNT=1000"]; |
| 119 | + const baseEvent = createMockBaseEvent({ recurrence: { rule } }); |
| 120 | + const rrule = new CompassEventRRule({ |
| 121 | + ...baseEvent, |
| 122 | + _id: new ObjectId(baseEvent._id), |
| 123 | + }); |
| 124 | + const instances = rrule.instances(); |
| 125 | + |
| 126 | + expect(instances).toBeInstanceOf(Array); |
| 127 | + expect(instances).toHaveLength(GCAL_MAX_RECURRENCES); |
| 128 | + }); |
| 129 | + |
| 130 | + it("should return the correct number of compass instances based on rrule count", () => { |
| 131 | + const count = faker.number.int({ min: 1, max: GCAL_MAX_RECURRENCES }); |
| 132 | + const rule = [`RRULE:FREQ=DAILY;COUNT=${count}`]; |
| 133 | + const baseEvent = createMockBaseEvent({ recurrence: { rule } }); |
| 134 | + const rrule = new CompassEventRRule({ |
| 135 | + ...baseEvent, |
| 136 | + _id: new ObjectId(baseEvent._id), |
| 137 | + }); |
| 138 | + const instances = rrule.instances(); |
| 139 | + |
| 140 | + expect(instances).toBeInstanceOf(Array); |
| 141 | + expect(instances).toHaveLength(count); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should return compass instances with the correct date format and timezone for an ALLDAY base event", () => { |
| 145 | + const rule = ["RRULE:FREQ=DAILY;COUNT=10"]; |
| 146 | + const date = dayjs().startOf("year"); // specific date for testing |
| 147 | + const dates = generateCompassEventDates({ date, allDay: true }); |
| 148 | + const baseEvent = createMockBaseEvent({ ...dates, recurrence: { rule } }); |
| 149 | + const rrule = new CompassEventRRule({ |
| 150 | + ...baseEvent, |
| 151 | + _id: new ObjectId(baseEvent._id), |
| 152 | + }); |
| 153 | + const instances = rrule.instances(); |
| 154 | + const startDate = parseCompassEventDate(baseEvent.startDate!); |
| 155 | + const endDate = parseCompassEventDate(baseEvent.endDate!); |
| 156 | + const dateFormat = dayjs.DateFormat.YEAR_MONTH_DAY_FORMAT; |
| 157 | + |
| 158 | + instances.forEach((instance, index) => { |
| 159 | + expect(instance.startDate).toBeDefined(); |
| 160 | + expect(instance.endDate).toBeDefined(); |
| 161 | + |
| 162 | + expect(instance.startDate).toEqual( |
| 163 | + startDate.add(index, "day").format(dateFormat), |
| 164 | + ); |
| 165 | + |
| 166 | + expect(instance.endDate).toEqual( |
| 167 | + endDate.add(index, "day").format(dateFormat), |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should return compass instances with the correct date format and timezone for a TIMED base event", () => { |
| 173 | + const rule = ["RRULE:FREQ=DAILY;COUNT=10"]; |
| 174 | + const date = dayjs().startOf("year"); // specific date for testing |
| 175 | + const dates = generateCompassEventDates({ date }); |
| 176 | + const baseEvent = createMockBaseEvent({ ...dates, recurrence: { rule } }); |
| 177 | + const rrule = new CompassEventRRule({ |
| 178 | + ...baseEvent, |
| 179 | + _id: new ObjectId(baseEvent._id), |
| 180 | + }); |
| 181 | + const instances = rrule.instances(); |
| 182 | + const startDate = parseCompassEventDate(baseEvent.startDate!); |
| 183 | + const endDate = parseCompassEventDate(baseEvent.endDate!); |
| 184 | + const dateFormat = dayjs.DateFormat.RFC3339_OFFSET; |
| 185 | + |
| 186 | + instances.forEach((instance, index) => { |
| 187 | + expect(instance.startDate).toBeDefined(); |
| 188 | + expect(instance.endDate).toBeDefined(); |
| 189 | + |
| 190 | + expect(instance.startDate).toEqual( |
| 191 | + startDate.add(index, "day").format(dateFormat), |
| 192 | + ); |
| 193 | + |
| 194 | + expect(instance.endDate).toEqual( |
| 195 | + endDate.add(index, "day").format(dateFormat), |
| 196 | + ); |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments