Files
calendar/apps/web/test/lib/getSchedule/restrictionSchedule.test.ts
T
Volnei MunhozGitHubVolnei MunhozDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
bbf9274d37 chore: upgrade Vitest to 4.0.16 and Vite to 6.4.1 (#26351)
* chore: upgrade Vitest to 4.0.16 and Vite to 6.4.1

- Update vitest from 2.1.9 to 4.0.16
- Update @vitest/ui from 2.1.9 to 4.0.16
- Update vitest-fetch-mock from 0.3.0 to 0.4.5
- Update vitest-mock-extended from 2.0.2 to 3.1.0
- Update vite from 4.5.14/5.4.21 to 6.4.1 across all packages
- Update @vitejs/plugin-react to 5.1.2
- Update @vitejs/plugin-react-swc to 4.2.2
- Update @vitejs/plugin-basic-ssl to 2.1.0
- Update vite-plugin-dts to 4.5.4
- Rename vitest.config.ts to vitest.config.mts for ESM compatibility
- Add globals: true to vitest config

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: address Vitest 4.0 and Vite 6 breaking changes

- Convert arrow function mockImplementation patterns to regular functions
  (Vitest 4.0 breaking change: arrow functions can't be constructor mocks)
- Fix CSS imports with ?inline suffix for Vite 6 compatibility
- Add biome override to disable useArrowFunction rule for test files
- Fix syntax errors in test files introduced by regex replacements

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix remaining Vitest 4.0 constructor mock patterns

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix more Vitest 4.0 constructor mock patterns and exclude API v2 spec files

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more arrow function mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more arrow function mocks to regular functions for Vitest 4.0

- Fix CrmService.integration.test.ts jsforce.Connection mock
- Fix RetellSDKClient.test.ts Retell mock
- Fix RetellAIService.test.ts CreditService mocks
- Fix GoogleCalendarSubscriptionAdapter.test.ts CalendarAuth mock

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert Google Calendar and OAuthManager arrow function mocks for Vitest 4.0

- Fix googleapis.ts Calendar, OAuth2Client, and JWT mocks
- Fix utils.ts JWT mock
- Fix OAuthManager.ts defaultMockOAuthManager mock

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add React plugin, jsdom environment, and fix more constructor mocks for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert HostRepository PrismaClient mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add useOrgBranding mock to React component tests for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: update TestFunction type for Vitest 4.0 compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert listBookingReports constructor mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert UserRepository constructor mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert OrganizationPaymentService constructor mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more constructor mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add apps/web path aliases to vitest config

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix test issues for Vitest 4.0 compatibility

- Fix Response constructor 204 status code issue in testUtils.ts
- Fix FeaturesRepository mock persistence in handleNotificationWhenNoSlots.test.ts
- Add @vitest-environment node directive to formSubmissionUtils.test.ts
- Fix document.querySelector mock in embed.test.ts

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: clear EventManager spy between tests for Vitest 4.0 compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: update TeamRepository mock pattern for Vitest 4.0 compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert RoutingFormResponseRepository mock to regular function for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: convert more constructor mocks to regular functions for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix mock reset and spy clear issues for Vitest 4.0

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: fix remaining test failures for Vitest 4.0 upgrade

- Fix booking-validations.test.ts: convert UserRepository mock to regular function
- Fix route.test.ts: update 500 error test to mock ImageResponse instead of fetch
- Fix users-public-view.test.tsx: add missing mocks for getOrgFullOrigin and useRouterQuery
- Add @calcom/web path alias to vitest config

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add vitest-mocks for generated files that don't exist in CI

- Add svg-hashes.json mock for route.test.ts
- Add tailwind.generated.css mock for embed.test.ts
- Update vitest config to use mock files

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: update vitest config aliases for CI compatibility

- Use array format for aliases to ensure proper ordering
- Add @calcom/platform-constants alias to resolve from source
- Add @calcom/embed-react alias to resolve from source
- Ensure svg-hashes.json mock alias is matched before @calcom/web

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: add @calcom/embed-snippet alias for CI compatibility

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* Fix wrong test

* fix: migrate from CLI flags to VITEST_MODE env var for Vitest 4.0

Vitest 4.0 no longer allows custom CLI flags like --packaged-embed-tests-only.
This change migrates to using VITEST_MODE environment variable instead:
- VITEST_MODE=packaged-embed for packaged embed tests
- VITEST_MODE=integration for integration tests
- VITEST_MODE=timezone for timezone-dependent tests

Updated vitest.config.mts to handle mode-based include/exclude patterns.
Updated CI workflows and package scripts to use the new env var approach.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: return default include pattern instead of undefined in vitest config

The getTestInclude() function was returning undefined for the default case,
but Vitest 4.0 expects an array. This caused 'resolved.include is not iterable'
error in CI.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: always set INTEGRATION_TEST_MODE for jsdom environment

The getBookingFields.ts file checks for INTEGRATION_TEST_MODE to allow
server-side imports in the jsdom environment. Without this, tests fail
with 'getBookingFields must not be imported on the client side' error.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

* fix: support legacy CLI flags for backwards compatibility with main workflow

The CI runs workflows from main branch, which uses the old CLI flag approach
(yarn test -- --integrationTestsOnly). This commit adds backwards compatibility
by checking both VITEST_MODE env var and process.argv for the legacy flags.

Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-01-01 18:16:10 -03:00

477 lines
15 KiB
TypeScript

import prismock from "../../../../../tests/libs/__mocks__/prisma";
import {
createBookingScenario,
Timezones,
TestData,
type ScenarioData,
} from "../../utils/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);
});
});
});