mirror of
https://github.com/hanzoai/calendar.git
synced 2026-10-30 14:46:43 +00:00
* fix: remove @calcom/web imports from packages/features to eliminate circular dependency - Migrate UserTableUser and MemberPermissions types to packages/features/users/types/user-table.ts - Migrate useGeo hook to packages/features/geo/GeoContext.tsx - Migrate buildLegacyRequest to packages/lib/buildLegacyCtx.ts - Migrate Calendar component to packages/features/calendars/weeklyview/components/ - Move test utilities (bookingScenario, fixtures) to packages/features/test/ - Update all imports in packages/features to use new locations - Add re-exports in apps/web for backward compatibility Co-Authored-By: [email protected] <[email protected]> * fix: delete original implementation files and fix type issues - Delete original calendar component files in apps/web (keep only re-export stubs) - Migrate OutOfOfficeInSlots to packages/features/bookings/components - Convert apps/web OutOfOfficeInSlots to re-export stub - Fix className vs class issue in Calendar.tsx - Fix @calcom/trpc import violation in user-table.ts by using structural type Co-Authored-By: [email protected] <[email protected]> * fix: add missing isGroup and contains fields to UserTableUser attributes type Co-Authored-By: [email protected] <[email protected]> * fix: update customRole type to match actual Prisma Role model Co-Authored-By: [email protected] <[email protected]> * fix build * fix * fix * cleanup weeklyview * fix * refactor to mv MemberPermissions to types package * add types dependency to features * fix * fix * fix * fix * fix * fix * rename * rename * migrate * migrate * migrate * fix * fix * fix * refactor: move test utilities from packages/features/test to tests/libs - Move bookingScenario utilities to tests/libs/bookingScenario - Move fixtures to tests/libs/fixtures - Update all imports in packages/features test files to use new location - Update all imports in apps/web test files to use new location - Eliminates duplication of test utilities between packages/features and apps/web Co-Authored-By: [email protected] <[email protected]> * fix: correct relative import paths for tests/libs in test files Co-Authored-By: [email protected] <[email protected]> * refactor: replace test utility implementations with re-exports to tests/libs Co-Authored-By: [email protected] <[email protected]> * fix: fix test import paths and move signup handler tests to apps/web Co-Authored-By: [email protected] <[email protected]> * fix: move buildLegacyCtx to packages/lib and restore handlers to packages/features - Move buildLegacyCtx from apps/web/lib to packages/lib to break circular dependency - Update apps/web/lib/buildLegacyCtx.ts to re-export from @calcom/lib - Restore signup handlers and tests to packages/features/auth/signup/handlers - Update handler imports to use @calcom/lib/buildLegacyCtx instead of @calcom/web/lib/buildLegacyCtx Co-Authored-By: [email protected] <[email protected]> * fix: update recurring-event.test.ts imports to use tests/libs path Co-Authored-By: [email protected] <[email protected]> * refactor: delete test re-export files and update imports to use tests/libs directly Co-Authored-By: [email protected] <[email protected]> * fix: update remaining test imports to use tests/libs directly Co-Authored-By: [email protected] <[email protected]> * fix: update handleRecurringEventBooking calls to match function signature (1 arg) Co-Authored-By: [email protected] <[email protected]> * remove * migrate tests * migrate tests * refactor: update test mock imports by removing and using async for mock creators. * fix type errors * fix: add type assertion for MockUser in p2002.test-suite.ts Co-Authored-By: [email protected] <[email protected]> * fix: restore locale import in compareReminderBodyToTemplate.test.ts using relative path Co-Authored-By: [email protected] <[email protected]> * feat: create @calcom/testing package and migrate tests from /tests directory - Created new @calcom/testing package in /packages/testing - Moved all files from /tests to /packages/testing - Updated all imports across the codebase to use @calcom/testing alias - Removed /tests directory at root level This allows other packages like @calcom/features and @calcom/web to import testing utilities using the @calcom/testing alias instead of relative paths. Co-Authored-By: [email protected] <[email protected]> * fix * fix * fix: add missing useBookings export to @calcom/atoms package Co-Authored-By: [email protected] <[email protected]> * fix: add missing useCalendarsBusyTimes and useConnectedCalendars exports to @calcom/atoms Co-Authored-By: [email protected] <[email protected]> * chore: add @calcom/testing as explicit devDependency to packages that use it Co-Authored-By: [email protected] <[email protected]> * refactor: move setupVitest.ts into @calcom/testing package Co-Authored-By: [email protected] <[email protected]> * fix * chore: add biome rules to restrict @calcom/testing imports Co-Authored-By: [email protected] <[email protected]> * fix * rename libs to lib * rename libs to lib * add rule * add rule * refactor: remove @calcom/features imports from @calcom/testing - Move mockPaymentSuccessWebhookFromStripe to fresh-booking.test.ts - Replace ProfileRepository.generateProfileUid() with uuidv4() - Clone Tracking type into @calcom/testing/src/lib/types.ts - Update imports in expects.ts and getMockRequestDataForBooking.ts - Move source files into src/ folder - Move CalendarManager mock to @calcom/features Co-Authored-By: [email protected] <[email protected]> * fix: add explicit exports for nested paths in @calcom/testing Co-Authored-By: [email protected] <[email protected]> * improve * improve * fix * fix * fix type checks * fix type checks * fix type checks * fix tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
474 lines
14 KiB
TypeScript
474 lines
14 KiB
TypeScript
/**
|
|
* Post-Booking handling tests
|
|
*
|
|
* These tests focus specifically on testing what happens after a successful booking or rescheduling.
|
|
*/
|
|
import prismaMock from "@calcom/testing/lib/__mocks__/prisma";
|
|
|
|
import {
|
|
createBookingScenario,
|
|
getDate,
|
|
getGoogleCalendarCredential,
|
|
TestData,
|
|
getOrganizer,
|
|
getBooker,
|
|
getScenarioData,
|
|
mockCalendarToHaveNoBusySlots,
|
|
BookingLocations,
|
|
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
|
|
import { expectBookingToBeInDatabase } from "@calcom/testing/lib/bookingScenario/expects";
|
|
import { getMockRequestDataForBooking } from "@calcom/testing/lib/bookingScenario/getMockRequestDataForBooking";
|
|
import { setupAndTeardown } from "@calcom/testing/lib/bookingScenario/setupAndTeardown";
|
|
|
|
import type { Request, Response } from "express";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import { describe, expect, beforeEach, vi, test } from "vitest";
|
|
|
|
import { resetTestEmails } from "@calcom/lib/testEmails";
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
|
|
|
import { getNewBookingHandler } from "./getNewBookingHandler";
|
|
|
|
export type CustomNextApiRequest = NextApiRequest & Request;
|
|
export type CustomNextApiResponse = NextApiResponse & Response;
|
|
|
|
// Local test runs sometimes get too slow
|
|
const timeout = process.env.CI ? 5000 : 20000;
|
|
|
|
// Helper function to create hashed link test data
|
|
function createHashedLinkTestData({
|
|
eventTypeId,
|
|
usageLimit = 5,
|
|
currentUsage = 0,
|
|
}: {
|
|
eventTypeId: number;
|
|
usageLimit?: number;
|
|
currentUsage?: number;
|
|
}) {
|
|
const link = "test-hashed-link-abc123";
|
|
return {
|
|
id: 1,
|
|
link,
|
|
eventTypeId,
|
|
maxUsageCount: usageLimit,
|
|
usageCount: currentUsage,
|
|
expiresAt: null,
|
|
};
|
|
}
|
|
|
|
// Helper function to verify hashed link usage count
|
|
async function expectHashedLinkUsageToBe(linkId: string, expectedUsageCount: number) {
|
|
const hashedLink = await prismaMock.hashedLink.findUnique({
|
|
where: { link: linkId },
|
|
});
|
|
expect(hashedLink?.usageCount).toBe(expectedUsageCount);
|
|
}
|
|
|
|
describe("Post-Booking Events - Hashed Link Usage", () => {
|
|
setupAndTeardown();
|
|
beforeEach(() => {
|
|
// Reset test state before each test
|
|
resetTestEmails();
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("BookingEventHandler Hashed Link Usage Tracking", () => {
|
|
test(
|
|
`should increment hashed link usage when booking is created with hashed link
|
|
1. Should create a booking in the database
|
|
2. Should increment hashed link usage count via BookingEventHandler.onBookingCreated
|
|
`,
|
|
async () => {
|
|
const handleNewBooking = getNewBookingHandler();
|
|
const booker = getBooker({
|
|
email: "[email protected]",
|
|
name: "Booker",
|
|
});
|
|
|
|
const organizer = getOrganizer({
|
|
name: "Organizer",
|
|
email: "[email protected]",
|
|
id: 101,
|
|
schedules: [TestData.schedules.IstWorkHours],
|
|
credentials: [getGoogleCalendarCredential()],
|
|
selectedCalendars: [TestData.selectedCalendars.google],
|
|
});
|
|
|
|
const hashedLinkData = createHashedLinkTestData({
|
|
eventTypeId: 1,
|
|
usageLimit: 5,
|
|
currentUsage: 2, // Should be incremented to 3 after booking
|
|
});
|
|
|
|
await createBookingScenario(
|
|
getScenarioData({
|
|
eventTypes: [
|
|
{
|
|
id: 1,
|
|
slotInterval: 45,
|
|
length: 30,
|
|
users: [
|
|
{
|
|
id: 101,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
users: [organizer],
|
|
})
|
|
);
|
|
|
|
// Create the hashed link in the database
|
|
await prismaMock.hashedLink.create({
|
|
data: hashedLinkData,
|
|
});
|
|
|
|
mockCalendarToHaveNoBusySlots("googlecalendar", {
|
|
create: {
|
|
uid: "HASHED_LINK_BOOKING_UID",
|
|
},
|
|
update: {
|
|
iCalUID: "HASHED_LINK_BOOKING_UID",
|
|
uid: "HASHED_LINK_BOOKING_UID",
|
|
},
|
|
});
|
|
|
|
const mockBookingRequest = getMockRequestDataForBooking({
|
|
data: {
|
|
eventTypeId: 1,
|
|
responses: {
|
|
email: booker.email,
|
|
name: booker.name,
|
|
location: { optionValue: "", value: BookingLocations.CalVideo },
|
|
},
|
|
hashedLink: hashedLinkData.link,
|
|
hasHashedBookingLink: true,
|
|
},
|
|
});
|
|
|
|
const bookingResponse = await handleNewBooking({
|
|
bookingData: mockBookingRequest,
|
|
});
|
|
|
|
await expectBookingToBeInDatabase({
|
|
description: "",
|
|
uid: bookingResponse.uid,
|
|
location: BookingLocations.CalVideo,
|
|
responses: expect.objectContaining({
|
|
email: booker.email,
|
|
name: booker.name,
|
|
}),
|
|
status: BookingStatus.ACCEPTED,
|
|
});
|
|
|
|
await expectHashedLinkUsageToBe(hashedLinkData.link, hashedLinkData.usageCount + 1);
|
|
|
|
expect(bookingResponse.status).toEqual(BookingStatus.ACCEPTED);
|
|
expect(bookingResponse.uid).toBeDefined();
|
|
},
|
|
timeout
|
|
);
|
|
|
|
test(
|
|
`should increment hashed link usage when rescheduling a booking with hashed link
|
|
1. Should reschedule the booking in the database
|
|
2. Should increment hashed link usage count via BookingEventHandler.onBookingRescheduled
|
|
`,
|
|
async () => {
|
|
const handleNewBooking = getNewBookingHandler();
|
|
const booker = getBooker({
|
|
email: "[email protected]",
|
|
name: "Booker",
|
|
});
|
|
|
|
const organizer = getOrganizer({
|
|
name: "Organizer",
|
|
email: "[email protected]",
|
|
id: 101,
|
|
schedules: [TestData.schedules.IstWorkHours],
|
|
credentials: [getGoogleCalendarCredential()],
|
|
selectedCalendars: [TestData.selectedCalendars.google],
|
|
});
|
|
|
|
const hashedLinkData = createHashedLinkTestData({
|
|
eventTypeId: 1,
|
|
usageLimit: 5,
|
|
currentUsage: 2,
|
|
});
|
|
|
|
// Create an existing booking to reschedule
|
|
const existingBookingUid = "existing-booking-uid";
|
|
|
|
await createBookingScenario(
|
|
getScenarioData({
|
|
eventTypes: [
|
|
{
|
|
id: 1,
|
|
slotInterval: 45,
|
|
length: 30,
|
|
users: [
|
|
{
|
|
id: 101,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
users: [organizer],
|
|
bookings: [
|
|
{
|
|
uid: existingBookingUid,
|
|
eventTypeId: 1,
|
|
userId: organizer.id,
|
|
status: BookingStatus.ACCEPTED,
|
|
startTime: `${getDate({ dateIncrement: 1 }).dateString}T05:00:00.000Z`,
|
|
endTime: `${getDate({ dateIncrement: 1 }).dateString}T05:45:00.000Z`,
|
|
},
|
|
],
|
|
})
|
|
);
|
|
|
|
// Create the hashed link in the database
|
|
await prismaMock.hashedLink.create({
|
|
data: hashedLinkData,
|
|
});
|
|
|
|
mockCalendarToHaveNoBusySlots("googlecalendar", {
|
|
create: {
|
|
uid: "RESCHEDULED_BOOKING_UID",
|
|
},
|
|
update: {
|
|
iCalUID: "RESCHEDULED_BOOKING_UID",
|
|
uid: "RESCHEDULED_BOOKING_UID",
|
|
},
|
|
});
|
|
|
|
const mockBookingData = getMockRequestDataForBooking({
|
|
data: {
|
|
eventTypeId: 1,
|
|
responses: {
|
|
email: booker.email,
|
|
name: booker.name,
|
|
location: { optionValue: "", value: BookingLocations.CalVideo },
|
|
},
|
|
hashedLink: hashedLinkData.link,
|
|
hasHashedBookingLink: true,
|
|
rescheduleUid: existingBookingUid,
|
|
},
|
|
});
|
|
|
|
const bookingResponse = await handleNewBooking({
|
|
bookingData: mockBookingData,
|
|
});
|
|
|
|
if (bookingResponse.uid) {
|
|
await expectBookingToBeInDatabase({
|
|
description: "",
|
|
location: BookingLocations.CalVideo,
|
|
responses: expect.objectContaining({
|
|
email: booker.email,
|
|
name: booker.name,
|
|
}),
|
|
status: BookingStatus.ACCEPTED,
|
|
uid: bookingResponse.uid,
|
|
});
|
|
}
|
|
|
|
await expectHashedLinkUsageToBe(hashedLinkData.link, hashedLinkData.usageCount + 1);
|
|
|
|
expect(bookingResponse.status).toEqual(BookingStatus.ACCEPTED);
|
|
expect(bookingResponse.uid).toBeDefined();
|
|
},
|
|
timeout
|
|
);
|
|
|
|
test(
|
|
`should not increment hashed link usage during dry run
|
|
1. Should not increment hashed link usage (BookingEventHandler should skip processing)
|
|
`,
|
|
async () => {
|
|
const handleNewBooking = getNewBookingHandler();
|
|
const booker = getBooker({
|
|
email: "[email protected]",
|
|
name: "Booker",
|
|
});
|
|
|
|
const organizer = getOrganizer({
|
|
name: "Organizer",
|
|
email: "[email protected]",
|
|
id: 101,
|
|
schedules: [TestData.schedules.IstWorkHours],
|
|
credentials: [getGoogleCalendarCredential()],
|
|
selectedCalendars: [TestData.selectedCalendars.google],
|
|
});
|
|
|
|
const hashedLinkData = createHashedLinkTestData({
|
|
eventTypeId: 1,
|
|
usageLimit: 5,
|
|
currentUsage: 2,
|
|
});
|
|
|
|
await createBookingScenario(
|
|
getScenarioData({
|
|
eventTypes: [
|
|
{
|
|
id: 1,
|
|
slotInterval: 45,
|
|
length: 30,
|
|
users: [
|
|
{
|
|
id: 101,
|
|
},
|
|
],
|
|
// hashedLink will be created separately in database
|
|
},
|
|
],
|
|
users: [organizer],
|
|
})
|
|
);
|
|
|
|
mockCalendarToHaveNoBusySlots("googlecalendar", {
|
|
create: {
|
|
uid: "DRY_RUN_BOOKING_UID",
|
|
},
|
|
});
|
|
|
|
// Create the hashed link in the database
|
|
await prismaMock.hashedLink.create({
|
|
data: hashedLinkData,
|
|
});
|
|
|
|
const mockBookingData = getMockRequestDataForBooking({
|
|
data: {
|
|
eventTypeId: 1,
|
|
responses: {
|
|
email: booker.email,
|
|
name: booker.name,
|
|
location: { optionValue: "", value: BookingLocations.CalVideo },
|
|
},
|
|
hashedLink: hashedLinkData.link,
|
|
hasHashedBookingLink: true,
|
|
_isDryRun: true,
|
|
},
|
|
});
|
|
|
|
const bookingResponse = await handleNewBooking({
|
|
bookingData: mockBookingData,
|
|
});
|
|
|
|
expect(bookingResponse.uid).toBe("DRY_RUN_UID");
|
|
// Verify hashed link usage was NOT incremented during dry run
|
|
await expectHashedLinkUsageToBe(hashedLinkData.link, hashedLinkData.usageCount);
|
|
|
|
expect(bookingResponse.status).toEqual(BookingStatus.ACCEPTED);
|
|
},
|
|
timeout
|
|
);
|
|
|
|
test(
|
|
`should handle hashed link service errors gracefully without failing booking creation
|
|
1. Should create a booking successfully
|
|
2. Should handle hashed link service errors gracefully in BookingEventHandler
|
|
3. Should log errors but continue booking flow
|
|
`,
|
|
async () => {
|
|
const handleNewBooking = getNewBookingHandler();
|
|
const booker = getBooker({
|
|
email: "[email protected]",
|
|
name: "Booker",
|
|
});
|
|
|
|
const organizer = getOrganizer({
|
|
name: "Organizer",
|
|
email: "[email protected]",
|
|
id: 101,
|
|
schedules: [TestData.schedules.IstWorkHours],
|
|
credentials: [getGoogleCalendarCredential()],
|
|
selectedCalendars: [TestData.selectedCalendars.google],
|
|
});
|
|
|
|
const hashedLinkData = createHashedLinkTestData({
|
|
eventTypeId: 1,
|
|
usageLimit: 5,
|
|
currentUsage: 2,
|
|
});
|
|
|
|
await createBookingScenario(
|
|
getScenarioData({
|
|
eventTypes: [
|
|
{
|
|
id: 1,
|
|
slotInterval: 45,
|
|
length: 30,
|
|
users: [
|
|
{
|
|
id: 101,
|
|
},
|
|
],
|
|
// hashedLink will be created separately in database
|
|
},
|
|
],
|
|
users: [organizer],
|
|
})
|
|
);
|
|
|
|
mockCalendarToHaveNoBusySlots("googlecalendar", {
|
|
create: {
|
|
uid: "ERROR_HANDLING_BOOKING_UID",
|
|
},
|
|
update: {
|
|
iCalUID: "ERROR_HANDLING_BOOKING_UID",
|
|
uid: "ERROR_HANDLING_BOOKING_UID",
|
|
},
|
|
});
|
|
|
|
// Create an invalid hashed link that will cause validation to fail
|
|
// (maxUsageCount = 0 means it should be expired)
|
|
const invalidHashedLinkData = {
|
|
...hashedLinkData,
|
|
maxUsageCount: 0, // This will cause validation to fail
|
|
};
|
|
|
|
await prismaMock.hashedLink.create({
|
|
data: invalidHashedLinkData,
|
|
});
|
|
|
|
const mockBookingRequest = getMockRequestDataForBooking({
|
|
data: {
|
|
eventTypeId: 1,
|
|
responses: {
|
|
email: booker.email,
|
|
name: booker.name,
|
|
location: { optionValue: "", value: BookingLocations.CalVideo },
|
|
},
|
|
hasHashedBookingLink: true,
|
|
hashedLink: hashedLinkData.link,
|
|
},
|
|
});
|
|
|
|
const bookingResponse = await handleNewBooking({
|
|
bookingData: mockBookingRequest,
|
|
});
|
|
|
|
// Booking should still be created successfully despite hashed link error
|
|
await expectBookingToBeInDatabase({
|
|
description: "",
|
|
uid: bookingResponse.uid,
|
|
location: BookingLocations.CalVideo,
|
|
responses: expect.objectContaining({
|
|
email: booker.email,
|
|
name: booker.name,
|
|
}),
|
|
status: BookingStatus.ACCEPTED,
|
|
});
|
|
|
|
// Verify that even though hashed link processing failed,
|
|
await expectHashedLinkUsageToBe(hashedLinkData.link, invalidHashedLinkData.usageCount);
|
|
|
|
expect(bookingResponse.status).toEqual(BookingStatus.ACCEPTED);
|
|
expect(bookingResponse.uid).toBeDefined();
|
|
},
|
|
timeout
|
|
);
|
|
});
|
|
});
|