fix: Broken integration test due to month change (#17949)

This commit is contained in:
Alex van Andel authored and GitHub committed 2024-12-02 18:26:45 +00:00
1 parent 8052d27d0b
commit 82f64f7d6a
3 files changed
+94 -85

No files matched your search

@@ -32,6 +32,11 @@ beforeAll(async () => {
commonEventTypeId = event.id; commonEventTypeId = event.id;
}); });
beforeEach(() => {
// tell vitest we use mocked time
vi.useFakeTimers();
});
afterEach(async () => { afterEach(async () => {
await deleteUsers(); await deleteUsers();
vi.useRealTimers(); vi.useRealTimers();
@@ -68,6 +73,7 @@ type OptionalBookingProps = {
type UserProps = { type UserProps = {
email: string; email: string;
createdDate?: Date;
}; };
const commonBookingData = { const commonBookingData = {
@@ -84,7 +90,7 @@ const commonAttendeesData = [
]; ];
const createUserWithBookings = async ({ const createUserWithBookings = async ({
user: { email }, user: { email, createdDate },
bookings, bookings,
}: { }: {
user: UserProps; user: UserProps;
@@ -93,6 +99,7 @@ const createUserWithBookings = async ({
const user = await prisma.user.create({ const user = await prisma.user.create({
data: { data: {
email, email,
createdDate,
bookings: { bookings: {
create: bookings.map(({ eventTypeId, ...booking }, index) => ({ create: bookings.map(({ eventTypeId, ...booking }, index) => ({
...commonBookingData, ...commonBookingData,
@@ -117,21 +124,30 @@ const createUserWithBookings = async ({
const createHostWithBookings = async ({ const createHostWithBookings = async ({
user: userData, user: userData,
bookings, bookings,
weight, priority = 2,
weight = null,
createdAt, createdAt,
}: { }: {
user: UserProps; user: UserProps;
bookings: (BookingPropsRelatedToLuckyUserAlgorithm & OptionalBookingProps)[]; bookings: (BookingPropsRelatedToLuckyUserAlgorithm & OptionalBookingProps)[];
weight?: number; weight?: number | null;
priority?: number;
createdAt?: Date; createdAt?: Date;
}) => { }) => {
const user = await createUserWithBookings({ user: userData, bookings }); const user = await createUserWithBookings({
user: {
createdDate: userData.createdDate || new Date(),
email: userData.email,
},
bookings,
});
const host = await prisma.host.create({ const host = await prisma.host.create({
data: { data: {
user: { connect: { id: user.id } }, user: { connect: { id: user.id } },
eventType: { connect: { id: commonEventTypeId } }, eventType: { connect: { id: commonEventTypeId } },
weight, weight,
priority,
createdAt: createdAt ?? new Date(), createdAt: createdAt ?? new Date(),
}, },
include: { include: {
@@ -142,13 +158,11 @@ const createHostWithBookings = async ({
}, },
}, },
}); });
console.log({
[user.id]: user.email,
});
return { return {
...host, ...host,
user: { user: {
...host.user, ...host.user,
priority,
weight, weight,
}, },
}; };
@@ -185,18 +199,7 @@ describe("getLuckyUser Integration tests", () => {
const organizerHostThatDidntShowUp = await createOrganizerThatDidntShowUp("[email protected]"); const organizerHostThatDidntShowUp = await createOrganizerThatDidntShowUp("[email protected]");
const organizerThatShowedUp = organizerHostThatShowedUp.user; const organizerThatShowedUp = organizerHostThatShowedUp.user;
const organizerThatDidntShowUp = organizerHostThatDidntShowUp.user; const organizerThatDidntShowUp = organizerHostThatDidntShowUp.user;
console.log({
organizerHostThatShowedUp: {
id: organizerThatShowedUp.id,
email: organizerThatShowedUp.email,
bookings: JSON.stringify(organizerThatShowedUp.bookings),
},
organizerThatDidntShowUp: {
id: organizerThatDidntShowUp.id,
email: organizerThatDidntShowUp.email,
bookings: JSON.stringify(organizerThatDidntShowUp.bookings),
},
});
const luckyUser = await getLuckyUser({ const luckyUser = await getLuckyUser({
availableUsers: [organizerThatShowedUp, organizerThatDidntShowUp], availableUsers: [organizerThatShowedUp, organizerThatDidntShowUp],
eventType: { eventType: {
@@ -607,12 +610,14 @@ describe("getOrderedListOfLuckyUsers Integration tests", () => {
eventType: { eventType: {
id: commonEventTypeId, id: commonEventTypeId,
isRRWeightsEnabled, isRRWeightsEnabled,
team: null,
}, },
allRRHosts: [ allRRHosts: [
hostWithOneBookingAndWeight200, hostWithOneBookingAndWeight200,
hostWithTwoBookingsAndWeight100, hostWithTwoBookingsAndWeight100,
hostWithThreeBookingsAndWeight100, hostWithThreeBookingsAndWeight100,
], ],
routingFormResponse: null,
}; };
const { users: luckyUsers, perUserData } = await getOrderedListOfLuckyUsers({ const { users: luckyUsers, perUserData } = await getOrderedListOfLuckyUsers({
@@ -644,13 +649,13 @@ describe("getOrderedListOfLuckyUsers Integration tests", () => {
hostWithThreeBookingsInPreviousMonthAndWeight100, hostWithThreeBookingsInPreviousMonthAndWeight100,
] = await Promise.all([ ] = await Promise.all([
createHostWithBookings({ createHostWithBookings({
user: { email: "[email protected]" }, user: { email: "[email protected]", createdDate: new Date() },
bookings: [{ eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") }], bookings: [{ eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") }],
weight: 200, weight: 200,
createdAt: new Date(), createdAt: new Date(),
}), }),
createHostWithBookings({ createHostWithBookings({
user: { email: "[email protected]" }, user: { email: "[email protected]", createdDate: new Date() },
bookings: [ bookings: [
{ eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") }, { eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") },
{ eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") }, { eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") },
@@ -659,7 +664,7 @@ describe("getOrderedListOfLuckyUsers Integration tests", () => {
createdAt: new Date(), createdAt: new Date(),
}), }),
createHostWithBookings({ createHostWithBookings({
user: { email: "[email protected]" }, user: { email: "[email protected]", createdDate: new Date() },
bookings: [ bookings: [
{ eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") }, { eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") },
{ eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") }, { eventTypeId: commonEventTypeId, createdAt: new Date("2024-10-01T00:00:00.000Z") },
@@ -688,12 +693,14 @@ describe("getOrderedListOfLuckyUsers Integration tests", () => {
eventType: { eventType: {
id: commonEventTypeId, id: commonEventTypeId,
isRRWeightsEnabled, isRRWeightsEnabled,
team: null,
}, },
allRRHosts: [ allRRHosts: [
hostWithOneBookingInPreviousMonthAndWeight200, hostWithOneBookingInPreviousMonthAndWeight200,
hostWithTwoBookingsInPreviousMonthAndWeight100, hostWithTwoBookingsInPreviousMonthAndWeight100,
hostWithThreeBookingsInPreviousMonthAndWeight100, hostWithThreeBookingsInPreviousMonthAndWeight100,
], ],
routingFormResponse: null,
}; };
const { users: luckyUsers, perUserData } = await getOrderedListOfLuckyUsers({ const { users: luckyUsers, perUserData } = await getOrderedListOfLuckyUsers({
@@ -719,70 +726,73 @@ describe("getOrderedListOfLuckyUsers Integration tests", () => {
expect(perUserData.bookingShortfalls[userWithTwoBookingsInPreviousMonthAndWeight100.id]).toBe(0); expect(perUserData.bookingShortfalls[userWithTwoBookingsInPreviousMonthAndWeight100.id]).toBe(0);
expect(perUserData.bookingShortfalls[userWithOneBookingInPreviousMonthAndWeight200.id]).toBe(0); expect(perUserData.bookingShortfalls[userWithOneBookingInPreviousMonthAndWeight200.id]).toBe(0);
}); });
});
describe("should sort as per host creation data calibration", () => {
const isRRWeightsEnabled = true;
describe("should sort as per host creation data calibration", () => { it("not considering bookings that were created in previous months", async () => {
it("not considering bookings that were created in previous months", async () => { const today = new Date();
const today = new Date(); const tenthOfTheMonth = new Date(today.getFullYear(), today.getMonth(), 10);
const tenthOfTheMonth = new Date(today.getFullYear(), today.getMonth(), 10); const secondsInDay = 24 * 60 * 60 * 1000;
const secondsInDay = 24 * 60 * 60 * 1000; const ninthOfTheMonth = new Date(tenthOfTheMonth.getTime() - secondsInDay);
const ninthOfTheMonth = new Date(tenthOfTheMonth.getTime() - secondsInDay); const eighthOfTheMonth = new Date(tenthOfTheMonth.getTime() - 2 * secondsInDay);
const eighthOfTheMonth = new Date(tenthOfTheMonth.getTime() - 2 * secondsInDay); const [host1, host2, host3] = await Promise.all([
const [host1, host2, host3] = await Promise.all([ createHostWithBookings({
createHostWithBookings({ user: { email: "[email protected]", createdDate: tenthOfTheMonth },
user: { email: "[email protected]" }, bookings: [
bookings: [ { eventTypeId: commonEventTypeId, createdAt: new Date(tenthOfTheMonth.getTime() + 1000) },
{ eventTypeId: commonEventTypeId, createdAt: new Date(tenthOfTheMonth.getTime() + 1000) }, ],
], weight: 200,
weight: 200, createdAt: tenthOfTheMonth,
createdAt: tenthOfTheMonth, }),
}), createHostWithBookings({
createHostWithBookings({ user: { email: "[email protected]", createdDate: ninthOfTheMonth },
user: { email: "[email protected]" }, bookings: [
bookings: [ { eventTypeId: commonEventTypeId, createdAt: new Date(ninthOfTheMonth.getTime() + 1000) },
{ eventTypeId: commonEventTypeId, createdAt: new Date(ninthOfTheMonth.getTime() + 1000) }, { eventTypeId: commonEventTypeId, createdAt: new Date(ninthOfTheMonth.getTime() + 2000) },
{ eventTypeId: commonEventTypeId, createdAt: new Date(ninthOfTheMonth.getTime() + 2000) }, ],
], weight: 100,
weight: 100, createdAt: ninthOfTheMonth,
createdAt: ninthOfTheMonth, }),
}), createHostWithBookings({
createHostWithBookings({ user: { email: "[email protected]", createdDate: eighthOfTheMonth },
user: { email: "[email protected]" }, bookings: [
bookings: [ { eventTypeId: commonEventTypeId, createdAt: new Date(eighthOfTheMonth.getTime() + 1000) },
{ eventTypeId: commonEventTypeId, createdAt: new Date(eighthOfTheMonth.getTime() + 1000) }, { eventTypeId: commonEventTypeId, createdAt: new Date(eighthOfTheMonth.getTime() + 2000) },
{ eventTypeId: commonEventTypeId, createdAt: new Date(eighthOfTheMonth.getTime() + 2000) }, { eventTypeId: commonEventTypeId, createdAt: new Date(eighthOfTheMonth.getTime() + 3000) },
{ eventTypeId: commonEventTypeId, createdAt: new Date(eighthOfTheMonth.getTime() + 3000) }, ],
], weight: 100,
weight: 100, createdAt: eighthOfTheMonth,
createdAt: eighthOfTheMonth, }),
}), ]);
]);
const availableUsers = [host3.user, host2.user, host1.user]; const availableUsers = [host3.user, host2.user, host1.user];
const getLuckUserParams = { const getLuckyUserParams = {
availableUsers, availableUsers,
eventType: { eventType: {
id: commonEventTypeId, id: commonEventTypeId,
isRRWeightsEnabled, isRRWeightsEnabled,
}, team: null,
allRRHosts: [host1, host2, host3], },
}; allRRHosts: [host1, host2, host3],
routingFormResponse: null,
};
const { users: luckyUsers, perUserData } = await getOrderedListOfLuckyUsers({ const { users: luckyUsers, perUserData } = await getOrderedListOfLuckyUsers({
...getLuckUserParams, ...getLuckyUserParams,
availableUsers: [getLuckUserParams.availableUsers[0], ...getLuckUserParams.availableUsers.slice(1)], availableUsers: [getLuckyUserParams.availableUsers[0], ...getLuckyUserParams.availableUsers.slice(1)],
});
if (!perUserData?.bookingShortfalls || !perUserData?.calibrations) {
throw new Error("bookingShortfalls or calibrations is not defined");
}
expect(perUserData.calibrations[host1.user.id]).toBe(2.5);
expect(perUserData.calibrations[host2.user.id]).toBe(3);
expect(perUserData.calibrations[host3.user.id]).toBe(0);
expectLuckyUsers(luckyUsers, [host1.user, host3.user, host2.user]);
}); });
if (!perUserData?.bookingShortfalls || !perUserData?.calibrations) {
throw new Error("bookingShortfalls or calibrations is not defined");
}
expect(perUserData.calibrations[host1.user.id]).toBe(2.5);
expect(perUserData.calibrations[host2.user.id]).toBe(3);
expect(perUserData.calibrations[host3.user.id]).toBe(0);
expectLuckyUsers(luckyUsers, [host1.user, host3.user, host2.user]);
}); });
}); });
}); });
+3 -3
View File
@@ -66,7 +66,7 @@ interface GetLuckyUserParams<T extends PartialUser> {
routingFormResponse: RoutingFormResponse | null; routingFormResponse: RoutingFormResponse | null;
} }
// === dayjs.utc().startOf("month").toDate(); // === dayjs.utc().startOf("month").toDate();
const startOfMonth = new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), 1)); const startOfMonth = () => new Date(Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth(), 1));
// TS helper function. // TS helper function.
const isNonEmptyArray = <T>(arr: T[]): arr is [T, ...T[]] => arr.length > 0; const isNonEmptyArray = <T>(arr: T[]): arr is [T, ...T[]] => arr.length > 0;
@@ -342,7 +342,7 @@ async function getCurrentMonthsBookings({
return await BookingRepository.getAllBookingsForRoundRobin({ return await BookingRepository.getAllBookingsForRoundRobin({
eventTypeId: eventTypeId, eventTypeId: eventTypeId,
users, users,
startDate: startOfMonth, startDate: startOfMonth(),
endDate: new Date(), endDate: new Date(),
virtualQueuesData, virtualQueuesData,
}); });
@@ -525,7 +525,7 @@ async function fetchAllDataNeededForCalculations<
eventTypeId: eventType.id, eventTypeId: eventType.id,
isFixed: false, isFixed: false,
createdAt: { createdAt: {
gte: startOfMonth, gte: startOfMonth(),
}, },
}, },
}), }),
@@ -227,7 +227,6 @@ export class BookingRepository {
} }
}); });
} }
console.log(`queueBookings ${JSON.stringify(queueBookings.map((booking) => booking.id))}`);
return queueBookings; return queueBookings;
} }