Files
calendar/packages/lib/server/repository/selectedCalendar.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

494 lines
16 KiB
TypeScript

import prismock from "@calcom/testing/lib/__mocks__/prisma";
import { describe, expect, it, beforeEach } from "vitest";
import type { FeatureId } from "@calcom/features/flags/config";
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
import prisma from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { SelectedCalendarRepository } from "./selectedCalendar";
describe("SelectedCalendarRepository", () => {
beforeEach(() => {
prismock.selectedCalendar.deleteMany();
});
describe("getNextBatchToWatch", () => {
it("excludes calendars when calendar-cache feature is disabled on a team", async () => {
const user = await prisma.user.create({
data: {
email: "calendar-cache-disabled@example.com",
username: "calendar-cache-disabled",
},
});
const team = await prisma.team.create({
data: {
name: "Calendar Cache Disabled Team",
slug: "calendar-cache-disabled-team",
},
});
await prisma.membership.create({
data: {
userId: user.id,
teamId: team.id,
role: MembershipRole.ADMIN,
accepted: true,
},
});
const featuresRepository = new FeaturesRepository(prismock);
await featuresRepository.setTeamFeatureState({
teamId: team.id,
featureId: "calendar-cache" as FeatureId,
state: "disabled",
assignedBy: "test",
});
await prisma.selectedCalendar.create({
data: {
userId: user.id,
integration: "google_calendar",
externalId: "disabled@example.com",
credentialId: 1,
},
});
const nextBatch = await SelectedCalendarRepository.getNextBatchToWatch();
expect(nextBatch).toEqual([]);
});
});
describe("create", () => {
it("should create a selected calendar", async () => {
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
};
const result = await SelectedCalendarRepository.create(data);
expect(result).toEqual(expect.objectContaining(data));
});
it("should throw error if we try to create a user-level calendar with same userId_integration_externalId as an existing user-level calendar", async () => {
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: null,
};
await SelectedCalendarRepository.create(data);
await expect(
SelectedCalendarRepository.create({
...data,
credentialId: 2,
})
).rejects.toThrow("Selected calendar already exists");
});
it("should allow creating a user-level calendar with same userId_integration_externalId as an existing event-type level calendar", async () => {
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: 1,
};
await SelectedCalendarRepository.create(data);
const userLevelCalendarData = {
...data,
eventTypeId: null,
};
const created = await SelectedCalendarRepository.create(userLevelCalendarData);
expect(created).toEqual(expect.objectContaining(userLevelCalendarData));
});
});
describe("update", () => {
it("should update a selected calendar and return it", async () => {
const calendarToUpdate = await SelectedCalendarRepository.create({
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
});
const updatedCalendar = await SelectedCalendarRepository.update({
where: { userId: 1, externalId: "test@gmail.com" },
data: { integration: "office365_calendar" },
});
expect(updatedCalendar.id).toBe(calendarToUpdate.id);
expect(updatedCalendar.integration).toBe("office365_calendar");
});
it("should throw error when trying to update multiple calendars", async () => {
await SelectedCalendarRepository.create({
userId: 1,
integration: "google_calendar",
externalId: "test1@gmail.com",
credentialId: 1,
});
await SelectedCalendarRepository.create({
userId: 1,
integration: "google_calendar2",
externalId: "test1@gmail.com",
credentialId: 2,
});
await expect(
SelectedCalendarRepository.update({
where: { userId: 1, externalId: "test1@gmail.com" },
data: { integration: "office365_calendar" },
})
).rejects.toThrow(
"Multiple SelectedCalendar records found to update. updateMany should be used instead"
);
});
});
describe("delete", () => {
it("should delete a selected calendar and return it", async () => {
const calendar = await SelectedCalendarRepository.create({
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
});
const deleted = await SelectedCalendarRepository.delete({
where: {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
},
});
expect(deleted).toEqual(calendar);
const result = await SelectedCalendarRepository.findFirst({
where: { id: calendar.id },
});
expect(result).toBeNull();
});
it("should throw error when trying to delete non-existent calendar", async () => {
await expect(
SelectedCalendarRepository.delete({
where: {
userId: 999,
integration: "google_calendar",
externalId: "nonexistent@gmail.com",
credentialId: 999,
},
})
).rejects.toThrow("SelectedCalendar not found");
});
});
describe("findUserLevelUniqueOrThrow", () => {
it("should find user level calendar", async () => {
const calendar = await SelectedCalendarRepository.create({
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: null, // User level calendar
});
const result = await SelectedCalendarRepository.findUserLevelUniqueOrThrow({
where: { userId: 1, externalId: "test@gmail.com" },
});
expect(result).toEqual(
expect.objectContaining({
userId: calendar.userId,
externalId: calendar.externalId,
})
);
});
it("should not find event type level calendar", async () => {
await SelectedCalendarRepository.create({
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: 1, // Event type level calendar
});
await expect(
SelectedCalendarRepository.findUserLevelUniqueOrThrow({
where: { userId: 1, externalId: "test@gmail.com" },
})
).rejects.toThrow("SelectedCalendar not found");
});
});
describe("upsert", () => {
describe("User Level Calendar", () => {
const eventTypeId = null;
it("should update existing calendar as long as a record with same userId_integration_externalId is present for eventTypeId=null", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const updatedData = {
...initialData,
credentialId: 2,
eventTypeId,
};
const result = await SelectedCalendarRepository.upsert(updatedData);
expect(result.credentialId).toBe(2);
expect(existingCalendar.id).toBe(result.id);
});
it("should create new calendar if no record with same userId_integration_externalId is present when eventTypeId=null", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const updatedData = {
...initialData,
externalId: "test2@gmail.com",
credentialId: 2,
eventTypeId,
};
const result = await SelectedCalendarRepository.upsert(updatedData);
expect(await prisma.selectedCalendar.count()).toBe(2);
expect(result).toEqual(expect.objectContaining(updatedData));
expect(existingCalendar.id).not.toBe(result.id);
});
});
describe("Event Type Level Calendar", () => {
const eventTypeId = 101;
it("should update existing calendar as long as record with same userId_integration_externalId_eventTypeId is present", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const updatedData = {
...initialData,
credentialId: 2,
eventTypeId,
};
const result = await SelectedCalendarRepository.upsert(updatedData);
expect(result.credentialId).toBe(2);
expect(existingCalendar.id).toBe(result.id);
});
it("should create new calendar if no record with same userId_integration_externalId_eventTypeId is present", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const updatedData = {
...initialData,
credentialId: 2,
externalId: "test2@gmail.com",
eventTypeId,
};
const result = await SelectedCalendarRepository.upsert(updatedData);
expect(await prisma.selectedCalendar.count()).toBe(2);
expect(result).toEqual(expect.objectContaining(updatedData));
expect(existingCalendar.id).not.toBe(result.id);
});
});
});
describe("Delegation Credential", () => {
it("should create a selected calendar with delegationCredentialId", async () => {
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
delegationCredentialId: "delegationCredential-123",
};
const result = await SelectedCalendarRepository.create(data);
expect(result).toEqual(expect.objectContaining(data));
});
describe("upsert", () => {
describe("updation", () => {
it("should update existing record with delegationCredentialId if credentialId is -1", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: null,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: -1,
delegationCredentialId: "delegationCredential-123",
};
const result = await SelectedCalendarRepository.upsert(data);
expect(result.id).not.toBe(null);
expect(result.id).toBe(existingCalendar.id);
expect(result.credentialId).toBe(null);
expect(result.delegationCredentialId).toBe(data.delegationCredentialId);
});
it("should update existing record with credentialId if credentialId is valid(>0) even if delegationCredentialId is set", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: null,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 2,
delegationCredentialId: "delegationCredential-123",
};
const beforeDelegationCredentialId = data.delegationCredentialId;
const result = await SelectedCalendarRepository.upsert(data);
expect(result.id).not.toBe(null);
expect(result.id).toBe(existingCalendar.id);
expect(result.credentialId).toBe(data.credentialId);
expect(result.delegationCredentialId).toBe(beforeDelegationCredentialId);
});
});
describe("creation", () => {
it("should create a new record with delegationCredentialId if credentialId is -1", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
eventTypeId: null,
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const data = {
userId: 1,
integration: "google_calendar",
externalId: "anotheremail@gmail.com",
credentialId: -1,
delegationCredentialId: "delegationCredential-123",
};
// It will create a new record because of unique constraint violation
const result = await SelectedCalendarRepository.upsert(data);
expect(result.id).not.toBe(null);
expect(result.id).not.toBe(existingCalendar.id);
expect(result.credentialId).toBe(null);
expect(result.delegationCredentialId).toBe(data.delegationCredentialId);
});
});
it("shouldnt update existing delegationCredentialId if upsert data doesn't have it", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
eventTypeId: null,
delegationCredentialId: "delegationCredential-123",
credentialId: 1,
};
const beforeDelegationCredentialId = initialData.delegationCredentialId;
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
credentialId: 1,
};
const result = await SelectedCalendarRepository.upsert(data);
expect(result.id).toBe(existingCalendar.id);
expect(result.credentialId).toBe(existingCalendar.credentialId);
expect(result.delegationCredentialId).toBe(beforeDelegationCredentialId);
});
it("shouldnt update delegationCredentialId if it is undefined", async () => {
const initialData = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
eventTypeId: null,
delegationCredentialId: "delegationCredential-123",
};
const existingCalendar = await SelectedCalendarRepository.create(initialData);
const data = {
userId: 1,
integration: "google_calendar",
externalId: "test@gmail.com",
};
const result = await SelectedCalendarRepository.upsert(data);
expect(result.id).toBe(existingCalendar.id);
expect(result.credentialId).toBe(existingCalendar.credentialId);
expect(result.delegationCredentialId).toBe(existingCalendar.delegationCredentialId);
});
});
});
});