test: Integration tests for handleNewBooking (#11044)
Co-authored-by: Shivam Kalra <[email protected]> Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
17 files changed
+1526
-441
No files matched your search
@@ -1,23 +1,14 @@
|
||||
import type {
|
||||
EventType as PrismaEventType,
|
||||
User as PrismaUser,
|
||||
Booking as PrismaBooking,
|
||||
App as PrismaApp,
|
||||
} from "@prisma/client";
|
||||
|
||||
import CalendarManagerMock from "../../../../tests/libs/__mocks__/CalendarManager";
|
||||
import prismaMock from "../../../../tests/libs/__mocks__/prisma";
|
||||
|
||||
import { diff } from "jest-diff";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { describe, expect, vi, beforeEach, afterEach, test } from "vitest";
|
||||
|
||||
import logger from "@calcom/lib/logger";
|
||||
import prisma from "@calcom/prisma";
|
||||
import type { SchedulingType } from "@calcom/prisma/enums";
|
||||
import type { BookingStatus } from "@calcom/prisma/enums";
|
||||
import type { Slot } from "@calcom/trpc/server/routers/viewer/slots/types";
|
||||
import { getAvailableSlots as getSchedule } from "@calcom/trpc/server/routers/viewer/slots/util";
|
||||
import { getDate, getGoogleCalendarCredential, createBookingScenario} from "../utils/bookingScenario";
|
||||
|
||||
// TODO: Mock properly
|
||||
prismaMock.eventType.findUnique.mockResolvedValue(null);
|
||||
@@ -129,6 +120,7 @@ const TestData = {
|
||||
},
|
||||
users: {
|
||||
example: {
|
||||
name: "Example",
|
||||
username: "example",
|
||||
defaultScheduleId: 1,
|
||||
email: "[email protected]",
|
||||
@@ -151,63 +143,6 @@ const TestData = {
|
||||
},
|
||||
};
|
||||
|
||||
type App = {
|
||||
slug: string;
|
||||
dirName: string;
|
||||
};
|
||||
|
||||
type InputCredential = typeof TestData.credentials.google;
|
||||
|
||||
type InputSelectedCalendar = typeof TestData.selectedCalendars.google;
|
||||
|
||||
type InputUser = typeof TestData.users.example & { id: number } & {
|
||||
credentials?: InputCredential[];
|
||||
selectedCalendars?: InputSelectedCalendar[];
|
||||
schedules: {
|
||||
id: number;
|
||||
name: string;
|
||||
availability: {
|
||||
userId: number | null;
|
||||
eventTypeId: number | null;
|
||||
days: number[];
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
date: string | null;
|
||||
}[];
|
||||
timeZone: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
type InputEventType = {
|
||||
id: number;
|
||||
title?: string;
|
||||
length?: number;
|
||||
offsetStart?: number;
|
||||
slotInterval?: number;
|
||||
minimumBookingNotice?: number;
|
||||
users?: { id: number }[];
|
||||
hosts?: { id: number }[];
|
||||
schedulingType?: SchedulingType;
|
||||
beforeEventBuffer?: number;
|
||||
afterEventBuffer?: number;
|
||||
};
|
||||
|
||||
type InputBooking = {
|
||||
userId?: number;
|
||||
eventTypeId: number;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
title?: string;
|
||||
status: BookingStatus;
|
||||
attendees?: { email: string }[];
|
||||
};
|
||||
|
||||
type InputHost = {
|
||||
id: number;
|
||||
userId: number;
|
||||
eventTypeId: number;
|
||||
isFixed: boolean;
|
||||
};
|
||||
|
||||
const cleanup = async () => {
|
||||
await prisma.eventType.deleteMany();
|
||||
@@ -241,7 +176,6 @@ describe("getSchedule", () => {
|
||||
]);
|
||||
|
||||
const scenarioData = {
|
||||
hosts: [],
|
||||
eventTypes: [
|
||||
{
|
||||
id: 1,
|
||||
@@ -350,7 +284,6 @@ describe("getSchedule", () => {
|
||||
endTime: `${plus2DateString}T06:15:00.000Z`,
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
});
|
||||
|
||||
// Day Plus 2 is completely free - It only has non accepted bookings
|
||||
@@ -449,7 +382,6 @@ describe("getSchedule", () => {
|
||||
schedules: [TestData.schedules.IstWorkHours],
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
});
|
||||
const { dateString: plus1DateString } = getDate({ dateIncrement: 1 });
|
||||
const { dateString: plus2DateString } = getDate({ dateIncrement: 2 });
|
||||
@@ -550,7 +482,6 @@ describe("getSchedule", () => {
|
||||
schedules: [TestData.schedules.IstWorkHours],
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
});
|
||||
const { dateString: todayDateString } = getDate();
|
||||
const { dateString: minus1DateString } = getDate({ dateIncrement: -1 });
|
||||
@@ -634,7 +565,6 @@ describe("getSchedule", () => {
|
||||
selectedCalendars: [TestData.selectedCalendars.google],
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
apps: [TestData.apps.googleCalendar],
|
||||
};
|
||||
|
||||
@@ -710,7 +640,6 @@ describe("getSchedule", () => {
|
||||
},
|
||||
],
|
||||
apps: [TestData.apps.googleCalendar],
|
||||
hosts: [],
|
||||
};
|
||||
|
||||
createBookingScenario(scenarioData);
|
||||
@@ -768,7 +697,6 @@ describe("getSchedule", () => {
|
||||
selectedCalendars: [TestData.selectedCalendars.google],
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
apps: [TestData.apps.googleCalendar],
|
||||
};
|
||||
|
||||
@@ -834,7 +762,6 @@ describe("getSchedule", () => {
|
||||
schedules: [TestData.schedules.IstWorkHoursWithDateOverride(plus2DateString)],
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
};
|
||||
|
||||
createBookingScenario(scenarioData);
|
||||
@@ -913,15 +840,6 @@ describe("getSchedule", () => {
|
||||
endTime: `${plus2DateString}T04:15:00.000Z`,
|
||||
},
|
||||
],
|
||||
hosts: [
|
||||
// This user is a host of our Collective event
|
||||
{
|
||||
id: 1,
|
||||
eventTypeId: 1,
|
||||
userId: 101,
|
||||
isFixed: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Requesting this user's availability for their
|
||||
@@ -1022,7 +940,6 @@ describe("getSchedule", () => {
|
||||
endTime: `${plus2DateString}T05:45:00.000Z`,
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
});
|
||||
|
||||
const scheduleForTeamEventOnADayWithNoBooking = await getSchedule({
|
||||
@@ -1162,7 +1079,6 @@ describe("getSchedule", () => {
|
||||
endTime: `${plus3DateString}T04:15:00.000Z`,
|
||||
},
|
||||
],
|
||||
hosts: [],
|
||||
});
|
||||
const scheduleForTeamEventOnADayWithOneBookingForEachUserButOnDifferentTimeslots = await getSchedule({
|
||||
input: {
|
||||
@@ -1224,219 +1140,3 @@ describe("getSchedule", () => {
|
||||
});
|
||||
});
|
||||
|
||||
function getGoogleCalendarCredential() {
|
||||
return {
|
||||
type: "google_calendar",
|
||||
key: {
|
||||
scope:
|
||||
"https://www.googleapis.com/auth/calendar.events https://www.googleapis.com/auth/calendar.readonly",
|
||||
token_type: "Bearer",
|
||||
expiry_date: 1656999025367,
|
||||
access_token: "ACCESS_TOKEN",
|
||||
refresh_token: "REFRESH_TOKEN",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function addEventTypes(eventTypes: InputEventType[], usersStore: InputUser[]) {
|
||||
const baseEventType = {
|
||||
title: "Base EventType Title",
|
||||
slug: "base-event-type-slug",
|
||||
timeZone: null,
|
||||
beforeEventBuffer: 0,
|
||||
afterEventBuffer: 0,
|
||||
schedulingType: null,
|
||||
|
||||
//TODO: What is the purpose of periodStartDate and periodEndDate? Test these?
|
||||
periodStartDate: new Date("2022-01-21T09:03:48.000Z"),
|
||||
periodEndDate: new Date("2022-01-21T09:03:48.000Z"),
|
||||
periodCountCalendarDays: false,
|
||||
periodDays: 30,
|
||||
seatsPerTimeSlot: null,
|
||||
metadata: {},
|
||||
minimumBookingNotice: 0,
|
||||
offsetStart: 0,
|
||||
};
|
||||
const foundEvents: Record<number, boolean> = {};
|
||||
const eventTypesWithUsers = eventTypes.map((eventType) => {
|
||||
if (!eventType.slotInterval && !eventType.length) {
|
||||
throw new Error("eventTypes[number]: slotInterval or length must be defined");
|
||||
}
|
||||
if (foundEvents[eventType.id]) {
|
||||
throw new Error(`eventTypes[number]: id ${eventType.id} is not unique`);
|
||||
}
|
||||
foundEvents[eventType.id] = true;
|
||||
const users =
|
||||
eventType.users?.map((userWithJustId) => {
|
||||
return usersStore.find((user) => user.id === userWithJustId.id);
|
||||
}) || [];
|
||||
return {
|
||||
...baseEventType,
|
||||
...eventType,
|
||||
users,
|
||||
};
|
||||
});
|
||||
|
||||
logger.silly("TestData: Creating EventType", eventTypes);
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
prismaMock.eventType.findUnique.mockImplementation(({ where }) => {
|
||||
return new Promise((resolve) => {
|
||||
const eventType = eventTypesWithUsers.find((e) => e.id === where.id) as unknown as PrismaEventType & {
|
||||
users: PrismaUser[];
|
||||
};
|
||||
resolve(eventType);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function addBookings(bookings: InputBooking[], eventTypes: InputEventType[]) {
|
||||
logger.silly("TestData: Creating Bookings", bookings);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
prismaMock.booking.findMany.mockImplementation((findManyArg) => {
|
||||
const where = findManyArg?.where || {};
|
||||
return new Promise((resolve) => {
|
||||
resolve(
|
||||
bookings
|
||||
// We can improve this filter to support the entire where clause but that isn't necessary yet. So, handle what we know we pass to `findMany` and is needed
|
||||
.filter((booking) => {
|
||||
/**
|
||||
* A user is considered busy within a given time period if there
|
||||
* is a booking they own OR host. This function mocks some of the logic
|
||||
* for each condition. For details see the following ticket:
|
||||
* https://github.com/calcom/cal.com/issues/6374
|
||||
*/
|
||||
|
||||
// ~~ FIRST CONDITION ensures that this booking is owned by this user
|
||||
// and that the status is what we want
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
const statusIn = where.OR[0].status?.in || [];
|
||||
const firstConditionMatches =
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
statusIn.includes(booking.status) && booking.userId === where.OR[0].userId;
|
||||
|
||||
// We return this booking if either condition is met
|
||||
return firstConditionMatches;
|
||||
})
|
||||
.map((booking) => ({
|
||||
uid: uuidv4(),
|
||||
title: "Test Booking Title",
|
||||
...booking,
|
||||
eventType: eventTypes.find((eventType) => eventType.id === booking.eventTypeId),
|
||||
})) as unknown as PrismaBooking[]
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function addUsers(users: InputUser[]) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
prismaMock.user.findUniqueOrThrow.mockImplementation((findUniqueArgs) => {
|
||||
return new Promise((resolve) => {
|
||||
resolve({
|
||||
email: `IntegrationTestUser${findUniqueArgs?.where.id}@example.com`,
|
||||
} as unknown as PrismaUser);
|
||||
});
|
||||
});
|
||||
|
||||
prismaMock.user.findMany.mockResolvedValue(
|
||||
users.map((user) => {
|
||||
return {
|
||||
...user,
|
||||
username: `IntegrationTestUser${user.id}`,
|
||||
email: `IntegrationTestUser${user.id}@example.com`,
|
||||
};
|
||||
}) as unknown as PrismaUser[]
|
||||
);
|
||||
}
|
||||
type ScenarioData = {
|
||||
// TODO: Support multiple bookings and add tests with that.
|
||||
bookings?: InputBooking[];
|
||||
users: InputUser[];
|
||||
hosts: InputHost[];
|
||||
credentials?: InputCredential[];
|
||||
apps?: App[];
|
||||
selectedCalendars?: InputSelectedCalendar[];
|
||||
eventTypes: InputEventType[];
|
||||
calendarBusyTimes?: {
|
||||
start: string;
|
||||
end: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
function createBookingScenario(data: ScenarioData) {
|
||||
logger.silly("TestData: Creating Scenario", data);
|
||||
|
||||
addUsers(data.users);
|
||||
|
||||
const eventType = addEventTypes(data.eventTypes, data.users);
|
||||
if (data.apps) {
|
||||
prismaMock.app.findMany.mockResolvedValue(data.apps as PrismaApp[]);
|
||||
// FIXME: How do we know which app to return?
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
prismaMock.app.findUnique.mockImplementation(({ where: { slug: whereSlug } }) => {
|
||||
return new Promise((resolve) => {
|
||||
if (!data.apps) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
resolve((data.apps.find(({ slug }) => slug == whereSlug) as PrismaApp) || null);
|
||||
});
|
||||
});
|
||||
}
|
||||
data.bookings = data.bookings || [];
|
||||
addBookings(data.bookings, data.eventTypes);
|
||||
|
||||
return {
|
||||
eventType,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This fn indents to dynamically compute day, month, year for the purpose of testing.
|
||||
* We are not using DayJS because that's actually being tested by this code.
|
||||
* - `dateIncrement` adds the increment to current day
|
||||
* - `monthIncrement` adds the increment to current month
|
||||
* - `yearIncrement` adds the increment to current year
|
||||
*/
|
||||
const getDate = (param: { dateIncrement?: number; monthIncrement?: number; yearIncrement?: number } = {}) => {
|
||||
let { dateIncrement, monthIncrement, yearIncrement } = param;
|
||||
dateIncrement = dateIncrement || 0;
|
||||
monthIncrement = monthIncrement || 0;
|
||||
yearIncrement = yearIncrement || 0;
|
||||
|
||||
let _date = new Date().getDate() + dateIncrement;
|
||||
let year = new Date().getFullYear() + yearIncrement;
|
||||
|
||||
// Make it start with 1 to match with DayJS requiremet
|
||||
let _month = new Date().getMonth() + monthIncrement + 1;
|
||||
|
||||
// If last day of the month(As _month is plus 1 already it is going to be the 0th day of next month which is the last day of current month)
|
||||
const lastDayOfMonth = new Date(year, _month, 0).getDate();
|
||||
const numberOfDaysForNextMonth = +_date - +lastDayOfMonth;
|
||||
if (numberOfDaysForNextMonth > 0) {
|
||||
_date = numberOfDaysForNextMonth;
|
||||
_month = _month + 1;
|
||||
}
|
||||
|
||||
if (_month === 13) {
|
||||
_month = 1;
|
||||
year = year + 1;
|
||||
}
|
||||
|
||||
const date = _date < 10 ? "0" + _date : _date;
|
||||
const month = _month < 10 ? "0" + _month : _month;
|
||||
|
||||
return {
|
||||
date,
|
||||
month,
|
||||
year,
|
||||
dateString: `${year}-${month}-${date}`,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user