Files
calendar/apps/web/test/lib/getSchedule/restrictionSchedule.test.ts
T
Benny JooGitHubbenny@cal.com <sldisek783@gmail.com>benny@cal.com <sldisek783@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f5d345b133 refactor: remove @calcom/web imports from @calcom/features and add @calcom/testing package (#26480)
* 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: benny@cal.com <sldisek783@gmail.com>

* 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: benny@cal.com <sldisek783@gmail.com>

* fix: add missing isGroup and contains fields to UserTableUser attributes type

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update customRole type to match actual Prisma Role model

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* 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: benny@cal.com <sldisek783@gmail.com>

* fix: correct relative import paths for tests/libs in test files

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: replace test utility implementations with re-exports to tests/libs

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: fix test import paths and move signup handler tests to apps/web

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* 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: benny@cal.com <sldisek783@gmail.com>

* fix: update recurring-event.test.ts imports to use tests/libs path

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: delete test re-export files and update imports to use tests/libs directly

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update remaining test imports to use tests/libs directly

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update handleRecurringEventBooking calls to match function signature (1 arg)

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* 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: benny@cal.com <sldisek783@gmail.com>

* fix: restore locale import in compareReminderBodyToTemplate.test.ts using relative path

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* 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: benny@cal.com <sldisek783@gmail.com>

* fix

* fix

* fix: add missing useBookings export to @calcom/atoms package

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: add missing useCalendarsBusyTimes and useConnectedCalendars exports to @calcom/atoms

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* chore: add @calcom/testing as explicit devDependency to packages that use it

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: move setupVitest.ts into @calcom/testing package

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix

* chore: add biome rules to restrict @calcom/testing imports

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* 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: benny@cal.com <sldisek783@gmail.com>

* fix: add explicit exports for nested paths in @calcom/testing

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* 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>
2026-01-08 16:59:11 +09:00

477 lines
15 KiB
TypeScript

import prismock from "@calcom/testing/lib/__mocks__/prisma";
import {
createBookingScenario,
Timezones,
TestData,
type ScenarioData,
} from "@calcom/testing/lib/bookingScenario/bookingScenario";
import { describe, test, vi } from "vitest";
import type { z } from "zod";
import { getAvailableSlotsService } from "@calcom/features/di/containers/AvailableSlots";
import type { getScheduleSchema } from "@calcom/trpc/server/routers/viewer/slots/types";
import { expect } from "./expects";
import { setupAndTeardown } from "./setupAndTeardown";
// Mock the FeaturesRepository to enable restriction-schedule feature
vi.mock("@calcom/features/flags/features.repository", () => ({
FeaturesRepository: vi.fn().mockImplementation(function() {
return {
checkIfTeamHasFeature: vi.fn().mockResolvedValue(true),
checkIfFeatureIsEnabledGlobally: vi.fn().mockResolvedValue(true),
getAllFeatures: vi.fn().mockResolvedValue([]),
getFeatureFlagMap: vi.fn().mockResolvedValue({}),
checkIfUserHasFeature: vi.fn().mockResolvedValue(true),
};
}),
}));
type ScheduleScenario = {
eventTypes: ScenarioData["eventTypes"];
users: ScenarioData["users"];
apps: ScenarioData["apps"];
selectedSlots?: ScenarioData["selectedSlots"];
};
const getTestScheduleInput = ({
yesterdayDateString,
plus5DateString,
}: {
yesterdayDateString: string;
plus5DateString: string;
}): z.infer<typeof getScheduleSchema> => ({
eventTypeId: 1,
eventTypeSlug: "",
startTime: `${yesterdayDateString}T18:30:00.000Z`,
endTime: `${plus5DateString}T18:29:59.999Z`,
timeZone: Timezones["+5:30"],
isTeamEvent: true,
orgSlug: null,
});
const getBaseScenarioData = (): ScheduleScenario => ({
eventTypes: [
{
id: 1,
slotInterval: 60,
length: 60,
teamId: 1,
hosts: [{ userId: 101 }],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [TestData.schedules.IstWorkHours],
teams: [
{
membership: { role: "ADMIN", accepted: true },
team: { id: 1, name: "Test Team", slug: "test-team" },
},
],
},
],
apps: [],
});
// Helper function to set up team for the tests
async function setupTeamAndFeatures() {
// Create the team (needed for event type configuration)
await prismock.team.create({
data: {
id: 1,
name: "Test Team",
slug: "test-team",
},
});
}
describe("getSchedule", () => {
const availableSlotsService = getAvailableSlotsService();
setupAndTeardown();
describe("Restriction Schedule", () => {
test("should respect date override rule in restrictionSchedule (Europe/London, useBookerTimezone=false)", async () => {
await setupTeamAndFeatures();
vi.setSystemTime("2025-06-01T23:30:00Z");
const plus2DateString = "2025-06-02"; // Date with override
const plus5DateString = "2025-06-05";
const scenarioData: ScheduleScenario = {
...getBaseScenarioData(),
eventTypes: [
{
id: 1,
length: 60,
teamId: 1,
restrictionScheduleId: 50,
useBookerTimezone: false,
hosts: [{ userId: 101 }],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
id: 1,
name: "Test Schedule",
timeZone: "Europe/London",
availability: [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T17:00:00.000Z"),
date: null,
},
],
},
],
teams: [
{
membership: { role: "ADMIN", accepted: true },
team: { id: 1, name: "Test Team", slug: "test-team" },
},
],
},
],
};
scenarioData.users[0].schedules.push({
id: 50,
name: "Restriction Schedule - Only Date Override",
timeZone: "Asia/Dubai",
availability: [
{
date: "2025-06-02T00:00:00.000Z",
days: [],
startTime: new Date("1970-01-01T10:00:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
},
],
});
await createBookingScenario(scenarioData);
const result = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: "2025-06-01T00:00:00.000Z",
endTime: `${plus5DateString}T23:59:59.999Z`,
timeZone: "Asia/Kolkata",
isTeamEvent: true,
orgSlug: null,
},
});
// Expected slots based on intersection of:
// - User availability: 9 AM - 5 PM London
// - Restriction: 10 AM - 6 PM Dubai
const expectedUtcSlots = [
"2025-06-02T08:30:00.000Z", // 9:30 AM London, 12:30 PM Dubai, 2:00 PM Kolkata
"2025-06-02T09:30:00.000Z", // 10:30 AM London, 1:30 PM Dubai, 3:00 PM Kolkata
"2025-06-02T10:30:00.000Z", // 11:30 AM London, 2:30 PM Dubai, 4:00 PM Kolkata
"2025-06-02T11:30:00.000Z", // 12:30 PM London, 3:30 PM Dubai, 5:00 PM Kolkata
"2025-06-02T12:30:00.000Z", // 1:30 PM London, 4:30 PM Dubai, 6:00 PM Kolkata
];
expect(result).toHaveTimeSlots(expectedUtcSlots, {
dateString: plus2DateString,
doExactMatch: false,
});
expect(result).toHaveDateDisabled({ dateString: "2025-06-03" });
});
test("should respect recurring rule in restrictionSchedule (Europe/London, useBookerTimezone=false)", async () => {
await setupTeamAndFeatures();
vi.setSystemTime("2025-06-01T23:30:00Z");
const plus2DateString = "2025-06-02"; // Monday
const plus6DateString = "2025-06-06"; // Friday
const scenarioData: ScheduleScenario = {
...getBaseScenarioData(),
eventTypes: [
{
id: 1,
length: 60,
teamId: 1,
restrictionScheduleId: 50,
useBookerTimezone: false,
hosts: [{ userId: 101 }],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
id: 1,
name: "Test Schedule",
timeZone: "Europe/London",
availability: [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T17:00:00.000Z"),
date: null,
},
],
},
],
teams: [
{
membership: { role: "ADMIN", accepted: true },
team: { id: 1, name: "Test Team", slug: "test-team" },
},
],
},
],
};
scenarioData.users[0].schedules.push({
id: 50,
name: "Test Schedule",
timeZone: "Asia/Dubai",
availability: [
{
days: [1, 2, 3, 4, 5],
startTime: new Date("1970-01-01T10:00:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
date: null,
},
],
});
await createBookingScenario(scenarioData);
const result = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: "2025-06-01T00:00:00.000Z",
endTime: `${plus6DateString}T23:59:59.999Z`,
timeZone: "Asia/Kolkata",
isTeamEvent: true,
orgSlug: null,
},
});
// Expected slots based on intersection of:
// - User availability: 9 AM - 5 PM London
// - Restriction: 10 AM - 6 PM Dubai, weekdays only
const getExpectedSlotsForDate = (dateString: string) => [
`${dateString}T08:30:00.000Z`, // 9:30 AM London, 12:30 PM Dubai, 2:00 PM Kolkata
`${dateString}T09:30:00.000Z`, // 10:30 AM London, 1:30 PM Dubai, 3:00 PM Kolkata
`${dateString}T10:30:00.000Z`, // 11:30 AM London, 2:30 PM Dubai, 4:00 PM Kolkata
`${dateString}T11:30:00.000Z`, // 12:30 PM London, 3:30 PM Dubai, 5:00 PM Kolkata
`${dateString}T12:30:00.000Z`, // 1:30 PM London, 4:30 PM Dubai, 6:00 PM Kolkata
];
// Verify weekday slots
["2025-06-02", "2025-06-03", "2025-06-04", "2025-06-05", "2025-06-06"].forEach((dateString) => {
expect(result).toHaveTimeSlots(getExpectedSlotsForDate(dateString), {
dateString,
doExactMatch: false,
});
});
// Verify weekend is disabled
expect(result).toHaveDateDisabled({ dateString: "2025-06-07" });
expect(result).toHaveDateDisabled({ dateString: "2025-06-08" });
});
test("should respect recurring rule in restrictionSchedule (Europe/London, useBookerTimezone=true)", async () => {
await setupTeamAndFeatures();
vi.setSystemTime("2025-06-01T23:30:00Z");
const plus2DateString = "2025-06-02"; // Monday
const plus6DateString = "2025-06-06"; // Friday
const scenarioData: ScheduleScenario = {
...getBaseScenarioData(),
eventTypes: [
{
id: 1,
length: 60,
teamId: 1,
restrictionScheduleId: 50,
useBookerTimezone: true,
hosts: [{ userId: 101 }],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
id: 1,
name: "Test Schedule",
timeZone: "Europe/London",
availability: [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T17:00:00.000Z"),
date: null,
},
],
},
],
teams: [
{
membership: { role: "ADMIN", accepted: true },
team: { id: 1, name: "Test Team", slug: "test-team" },
},
],
},
],
};
scenarioData.users[0].schedules.push({
id: 50,
name: "Test Schedule",
timeZone: "Asia/Dubai",
availability: [
{
days: [1, 2, 3, 4, 5],
startTime: new Date("1970-01-01T10:00:00.000Z"),
endTime: new Date("1970-01-01T18:00:00.000Z"),
date: null,
},
],
});
await createBookingScenario(scenarioData);
const result = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: "2025-06-01T00:00:00.000Z",
endTime: `${plus6DateString}T23:59:59.999Z`,
timeZone: "Asia/Kolkata",
isTeamEvent: true,
orgSlug: null,
},
});
// Expected slots based on:
// - User availability: 9 AM - 5 PM London (1:30 PM - 9:30 PM Kolkata)
// - Restriction: 10 AM - 6 PM Kolkata
const getExpectedSlotsForDate = (dateString: string) => [
`${dateString}T08:30:00.000Z`, // 2:00 PM Kolkata, 9:30 AM London
`${dateString}T09:30:00.000Z`, // 3:00 PM Kolkata, 10:30 AM London
`${dateString}T10:30:00.000Z`, // 4:00 PM Kolkata, 11:30 AM London
`${dateString}T11:30:00.000Z`, // 5:00 PM Kolkata, 12:30 PM London
];
// Verify weekday slots
["2025-06-02", "2025-06-03", "2025-06-04", "2025-06-05", "2025-06-06"].forEach((dateString) => {
expect(result).toHaveTimeSlots(getExpectedSlotsForDate(dateString), {
dateString,
doExactMatch: false,
});
});
// Verify weekend is disabled
expect(result).toHaveDateDisabled({ dateString: "2025-06-07" });
expect(result).toHaveDateDisabled({ dateString: "2025-06-08" });
});
test("should return all slots when no restriction schedule is applied", async () => {
await setupTeamAndFeatures();
vi.setSystemTime("2025-06-01T23:30:00Z");
const plus2DateString = "2025-06-02"; // Monday
const plus6DateString = "2025-06-06"; // Friday
const scenarioData: ScheduleScenario = {
...getBaseScenarioData(),
eventTypes: [
{
id: 1,
length: 60,
teamId: 1,
useBookerTimezone: false,
hosts: [{ userId: 101 }],
},
],
users: [
{
...TestData.users.example,
id: 101,
schedules: [
{
id: 1,
name: "Test Schedule",
timeZone: "Europe/London",
availability: [
{
days: [0, 1, 2, 3, 4, 5, 6],
startTime: new Date("1970-01-01T09:00:00.000Z"),
endTime: new Date("1970-01-01T17:00:00.000Z"),
date: null,
},
],
},
],
teams: [
{
membership: { role: "ADMIN", accepted: true },
team: { id: 1, name: "Test Team", slug: "test-team" },
},
],
},
],
};
await createBookingScenario(scenarioData);
const result = await availableSlotsService.getAvailableSlots({
input: {
eventTypeId: 1,
eventTypeSlug: "",
startTime: "2025-06-01T00:00:00.000Z",
endTime: `${plus6DateString}T23:59:59.999Z`,
timeZone: "Asia/Kolkata",
isTeamEvent: true,
orgSlug: null,
},
});
// Expected slots based on user's availability: 9 AM - 5 PM London
const getExpectedSlotsForDate = (dateString: string) => [
`${dateString}T08:30:00.000Z`, // 9:30 AM London, 2:00 PM Kolkata
`${dateString}T09:30:00.000Z`, // 10:30 AM London, 3:00 PM Kolkata
`${dateString}T10:30:00.000Z`, // 11:30 AM London, 4:00 PM Kolkata
`${dateString}T11:30:00.000Z`, // 12:30 PM London, 5:00 PM Kolkata
`${dateString}T12:30:00.000Z`, // 1:30 PM London, 6:00 PM Kolkata
`${dateString}T13:30:00.000Z`, // 2:30 PM London, 7:00 PM Kolkata
`${dateString}T14:30:00.000Z`, // 3:30 PM London, 8:00 PM Kolkata
];
// Verify weekday slots
["2025-06-02", "2025-06-03", "2025-06-04", "2025-06-05", "2025-06-06"].forEach((dateString) => {
expect(result).toHaveTimeSlots(getExpectedSlotsForDate(dateString), {
dateString,
doExactMatch: false,
});
});
// Verify a late evening slot (8:00 PM Kolkata, 3:30 PM London) is present
const lateEveningSlot = `${plus2DateString}T14:30:00.000Z`;
expect(result.slots[plus2DateString].map((s) => s.time)).toContain(lateEveningSlot);
});
});
});