From 6ca76707dadab644014dc6c27a454be0a353a71f Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Fri, 7 Feb 2025 16:50:29 +0530 Subject: [PATCH] test: Scenario that replicates the crash in production (#19071) * Add a case that crashes * Add proper crash test case --------- Co-authored-by: Hariom Balhara Co-authored-by: Alex van Andel --- apps/web/test/lib/getSchedule.test.ts | 71 +++++++++++++++++++ .../utils/bookingScenario/bookingScenario.ts | 31 +++++++- 2 files changed, 99 insertions(+), 3 deletions(-) diff --git a/apps/web/test/lib/getSchedule.test.ts b/apps/web/test/lib/getSchedule.test.ts index 69bd8f0db4..10f626fe56 100644 --- a/apps/web/test/lib/getSchedule.test.ts +++ b/apps/web/test/lib/getSchedule.test.ts @@ -1753,6 +1753,77 @@ describe("getSchedule", () => { } ); }); + + test("Edge Case: Same day OOO entries that could accidentally be considered in past shouldn't crash the getSchedule", async () => { + // Set a fixed date for testing + vi.setSystemTime("2024-05-21T00:00:13Z"); + + const { dateString: plus1DateString } = getDate({ dateIncrement: 1 }); + const { dateString: todayDateString } = getDate({ dateIncrement: 0 }); + const { dateString: plus2DateString } = getDate({ dateIncrement: 2 }); + + await createBookingScenario({ + eventTypes: [ + { + id: 1, + slotInterval: 45, + length: 45, + users: [ + { + id: 101, + }, + ], + }, + ], + users: [ + { + ...TestData.users.example, + id: 101, + schedules: [TestData.schedules.IstWorkHours], + outOfOffice: { + dateRanges: [ + { + start: `${todayDateString}T00:00:00.000Z`, + end: `${todayDateString}T23:59:59.999Z`, + }, + ], + }, + }, + ], + }); + + const schedule = await getSchedule({ + input: { + eventTypeId: 1, + eventTypeSlug: "", + startTime: `${plus1DateString}T18:30:00.000Z`, + endTime: `${plus2DateString}T18:29:59.999Z`, + timeZone: Timezones["+5:30"], + isTeamEvent: false, + orgSlug: null, + }, + }); + + // Verify that we get slots for the requested dates + expect(schedule).toHaveTimeSlots( + [ + "04:00:00.000Z", + "04:45:00.000Z", + "05:30:00.000Z", + "06:15:00.000Z", + "07:00:00.000Z", + "07:45:00.000Z", + "08:30:00.000Z", + "09:15:00.000Z", + "10:00:00.000Z", + "10:45:00.000Z", + "11:30:00.000Z", + ], + { + dateString: plus2DateString, + } + ); + }); }); describe("Team Event", () => { diff --git a/apps/web/test/utils/bookingScenario/bookingScenario.ts b/apps/web/test/utils/bookingScenario/bookingScenario.ts index 4aec51d3e5..cbcb836fc8 100644 --- a/apps/web/test/utils/bookingScenario/bookingScenario.ts +++ b/apps/web/test/utils/bookingScenario/bookingScenario.ts @@ -166,6 +166,12 @@ type InputUser = Omit & { weekStart?: string; profiles?: Prisma.ProfileUncheckedCreateWithoutUserInput[]; completedOnboarding?: boolean; + outOfOffice?: { + dateRanges: { + start: string; + end: string; + }[]; + }; }; export type InputEventType = { @@ -652,14 +658,33 @@ export async function addWorkflowReminders(workflowReminders: InputWorkflowRemin }); } -export async function addUsersToDb( - users: (Prisma.UserCreateInput & { schedules: Prisma.ScheduleCreateInput[]; id?: number })[] -) { +export async function addUsersToDb(users: InputUser[]) { log.silly("TestData: Creating Users", JSON.stringify(users)); await prismock.user.createMany({ data: users, }); + // Create OutOfOfficeEntry for users with outOfOffice data + for (const user of users) { + if (user.outOfOffice) { + log.debug("Creating OutOfOfficeEntry for user", user.id); + for (const dateRange of user.outOfOffice.dateRanges) { + await prismock.outOfOfficeEntry.create({ + data: { + uuid: uuidv4(), + start: new Date(dateRange.start), + end: new Date(dateRange.end), + user: { + connect: { + id: user.id, + }, + }, + }, + }); + } + } + } + const allUsers = await prismock.user.findMany({ include: { credentials: true,