Files
cal.diy/packages/features/calendar-subscription/lib/__tests__/CalendarSubscriptionService.test.ts
T
e6b2116a2b feat: Calendar Cache and Sync (#23876)
* feat: calendar cache and sync - wip

* Add env.example

* refactor on CalendarCacheEventService

* remove test console.log

* Fix type checks errors

* chore: remove pt comment

* add route.ts

* chore: fix tests

* Improve cache impl

* chore: update recurring event id

* chore: small improvements

* calendar cache improvements

* Fix remove dynamic imports

* Add cleanup stale cache

* Fix tests

* add event update

* type fixes

* feat: add comprehensive tests for new calendar subscription API routes

- Add tests for /api/cron/calendar-subscriptions-cleanup route (9 tests)
- Add tests for /api/cron/calendar-subscriptions route (10 tests)
- Add tests for /api/webhooks/calendar-subscription/[provider] route (11 tests)
- Add missing feature flags for calendar-subscription-cache and calendar-subscription-sync
- All 30 tests pass with comprehensive coverage of authentication, feature flags, error handling, and service instantiation

Tests cover:
- Authentication scenarios (API key validation, Bearer tokens, query parameters)
- Feature flag combinations (cache/sync enabled/disabled states)
- Success and error handling (including non-Error exceptions)
- Service instantiation with proper dependency injection
- Provider validation for webhook endpoints

Co-Authored-By: Volnei Munhoz <[email protected]>

* feat: add comprehensive tests for calendar subscription services, repositories, and adapters

- Add unit tests for CalendarSubscriptionService with subscription, webhook, and event processing
- Add unit tests for CalendarCacheEventService with cache operations and cleanup
- Add unit tests for CalendarSyncService with Cal.com event filtering and booking operations
- Add unit tests for CalendarCacheEventRepository with CRUD operations
- Add unit tests for SelectedCalendarRepository with calendar selection management
- Add unit tests for GoogleCalendarSubscriptionAdapter with subscription and event fetching
- Add unit tests for Office365CalendarSubscriptionAdapter with placeholder implementation
- Add unit tests for AdaptersFactory with provider management and adapter creation
- Fix lint issues by removing explicit 'any' type casting and unused variables
- All tests follow Cal.com conventions using Vitest framework with proper mocking

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix: improve calendar-subscriptions-cleanup test performance by adding missing mocks

- Add comprehensive mocks for defaultResponderForAppDir, logger, performance monitoring, and Sentry
- Fix slow test execution (933ms -> <100ms) caused by missing dependency mocks
- Ensure consistent test performance across different environments

Co-Authored-By: Volnei Munhoz <[email protected]>

* Fix tests

* Fix tests

* type fix

* Fix coderabbit comments

* Fix types

* Fix test

* Update apps/web/app/api/cron/calendar-subscriptions/route.ts

Co-authored-by: Alex van Andel <[email protected]>

* Fixes by first review

* feat: add database migrations for calendar cache and sync fields

- Add CalendarCacheEventStatus enum with confirmed, tentative, cancelled values
- Add new fields to SelectedCalendar: channelId, channelKind, channelResourceId, channelResourceUri, channelExpiration, syncSubscribedAt, syncToken, syncedAt, syncErrorAt, syncErrorCount
- Create CalendarCacheEvent table with foreign key to SelectedCalendar
- Add necessary indexes and constraints for performance and data integrity

Fixes database schema issues causing e2e test failures with 'column does not exist' errors.

Co-Authored-By: Volnei Munhoz <[email protected]>

* only google-calendar for now

* docs: add Calendar Cache and Sync feature documentation

- Add comprehensive feature overview and motivation
- Document feature flags with SQL examples
- Include SQL examples for enabling features for users and teams
- Reference technical documentation files

Addresses PR #23876 documentation requirements

Co-Authored-By: Volnei Munhoz <[email protected]>

* docs: update calendar subscription README with comprehensive documentation

- Undo incorrect changes to main README.md
- Update packages/features/calendar-subscription/README.md with:
  - Feature overview and motivation
  - Environment variables section
  - Complete feature flags documentation with SQL examples
  - SQL examples for enabling features for users and teams
  - Detailed architecture documentation

Addresses PR #23876 documentation requirements

Co-Authored-By: Volnei Munhoz <[email protected]>

* fix docs

* Fix test to available calendars

* Fix test to available calendars

* add migration and sync boilerplate

* fix typo

* remove double log

* sync boilerplate

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
2025-09-29 14:26:14 +00:00

421 lines
15 KiB
TypeScript

import "../__mocks__/delegationCredential";
import { describe, test, expect, vi, beforeEach } from "vitest";
import type { AdapterFactory } from "@calcom/features/calendar-subscription/adapters/AdaptersFactory";
import type { CalendarCacheEventService } from "@calcom/features/calendar-subscription/lib/cache/CalendarCacheEventService";
import type { CalendarSyncService } from "@calcom/features/calendar-subscription/lib/sync/CalendarSyncService";
import type { FeaturesRepository } from "@calcom/features/flags/features.repository";
import type { ISelectedCalendarRepository } from "@calcom/lib/server/repository/SelectedCalendarRepository.interface";
import type { SelectedCalendar } from "@calcom/prisma/client";
import { CalendarSubscriptionService } from "../CalendarSubscriptionService";
const mockSelectedCalendar: SelectedCalendar = {
id: "test-calendar-id",
userId: 1,
credentialId: 1,
integration: "google_calendar",
externalId: "[email protected]",
eventTypeId: null,
delegationCredentialId: null,
domainWideDelegationCredentialId: null,
googleChannelId: null,
googleChannelKind: null,
googleChannelResourceId: null,
googleChannelResourceUri: null,
googleChannelExpiration: null,
error: null,
lastErrorAt: null,
watchAttempts: 0,
maxAttempts: 3,
unwatchAttempts: 0,
createdAt: new Date(),
updatedAt: new Date(),
channelId: "test-channel-id",
channelKind: "web_hook",
channelResourceId: "test-resource-id",
channelResourceUri: "test-resource-uri",
channelExpiration: new Date(Date.now() + 86400000),
syncSubscribedAt: new Date(),
syncToken: "test-sync-token",
syncedAt: new Date(),
syncErrorAt: null,
syncErrorCount: 0,
};
const mockCredential = {
id: 1,
key: { access_token: "test-token" },
user: { email: "[email protected]" },
delegatedTo: null,
};
const mockSubscriptionResult = {
provider: "google_calendar" as const,
id: "test-channel-id",
resourceId: "test-resource-id",
resourceUri: "test-resource-uri",
expiration: new Date(Date.now() + 86400000),
};
const mockEvents = {
provider: "google_calendar" as const,
syncToken: "new-sync-token",
items: [
{
id: "event-1",
iCalUID: "[email protected]",
start: new Date(),
end: new Date(Date.now() + 3600000),
busy: true,
summary: "Test Event",
description: "Test Description",
location: "Test Location",
status: "confirmed",
isAllDay: false,
timeZone: "UTC",
recurringEventId: null,
originalStartDate: null,
createdAt: new Date(),
updatedAt: new Date(),
etag: "test-etag",
kind: "calendar#event",
},
],
};
describe("CalendarSubscriptionService", () => {
let service: CalendarSubscriptionService;
let mockAdapterFactory: AdapterFactory;
let mockSelectedCalendarRepository: ISelectedCalendarRepository;
let mockFeaturesRepository: FeaturesRepository;
let mockCalendarCacheEventService: CalendarCacheEventService;
let mockCalendarSyncService: CalendarSyncService;
let mockAdapter: {
subscribe: ReturnType<typeof vi.fn>;
unsubscribe: ReturnType<typeof vi.fn>;
validate: ReturnType<typeof vi.fn>;
extractChannelId: ReturnType<typeof vi.fn>;
fetchEvents: ReturnType<typeof vi.fn>;
};
beforeEach(async () => {
mockAdapter = {
subscribe: vi.fn().mockResolvedValue(mockSubscriptionResult),
unsubscribe: vi.fn().mockResolvedValue(undefined),
validate: vi.fn().mockResolvedValue(true),
extractChannelId: vi.fn().mockResolvedValue("test-channel-id"),
fetchEvents: vi.fn().mockResolvedValue(mockEvents),
};
mockAdapterFactory = {
get: vi.fn().mockReturnValue(mockAdapter),
getProviders: vi.fn().mockReturnValue(["google_calendar", "office365_calendar"]),
};
mockSelectedCalendarRepository = {
findByIdWithCredentials: vi.fn().mockResolvedValue(mockSelectedCalendar),
findByChannelId: vi.fn().mockResolvedValue(mockSelectedCalendar),
findNextSubscriptionBatch: vi.fn().mockResolvedValue([mockSelectedCalendar]),
updateSyncStatus: vi.fn().mockResolvedValue(mockSelectedCalendar),
updateSubscription: vi.fn().mockResolvedValue(mockSelectedCalendar),
};
mockFeaturesRepository = {
checkIfFeatureIsEnabledGlobally: vi.fn().mockResolvedValue(true),
checkIfUserHasFeature: vi.fn().mockResolvedValue(true),
};
mockCalendarCacheEventService = {
handleEvents: vi.fn().mockResolvedValue(undefined),
cleanupCache: vi.fn().mockResolvedValue(undefined),
cleanupStaleCache: vi.fn().mockResolvedValue(undefined),
};
mockCalendarSyncService = {
handleEvents: vi.fn().mockResolvedValue(undefined),
};
service = new CalendarSubscriptionService({
adapterFactory: mockAdapterFactory,
selectedCalendarRepository: mockSelectedCalendarRepository,
featuresRepository: mockFeaturesRepository,
calendarCacheEventService: mockCalendarCacheEventService,
calendarSyncService: mockCalendarSyncService,
});
const { getCredentialForCalendarCache } = await import("../__mocks__/delegationCredential");
getCredentialForCalendarCache.mockResolvedValue(mockCredential);
});
describe("subscribe", () => {
test("should successfully subscribe to a calendar", async () => {
await service.subscribe("test-calendar-id");
expect(mockSelectedCalendarRepository.findByIdWithCredentials).toHaveBeenCalledWith("test-calendar-id");
expect(mockAdapterFactory.get).toHaveBeenCalledWith("google_calendar");
expect(mockAdapter.subscribe).toHaveBeenCalledWith(mockSelectedCalendar, mockCredential);
expect(mockSelectedCalendarRepository.updateSubscription).toHaveBeenCalledWith("test-calendar-id", {
channelId: "test-channel-id",
channelResourceId: "test-resource-id",
channelResourceUri: "test-resource-uri",
channelKind: "google_calendar",
channelExpiration: mockSubscriptionResult.expiration,
syncSubscribedAt: expect.any(Date),
});
});
test("should return early if selected calendar not found", async () => {
mockSelectedCalendarRepository.findByIdWithCredentials.mockResolvedValue(null);
await service.subscribe("non-existent-id");
expect(mockAdapter.subscribe).not.toHaveBeenCalled();
expect(mockSelectedCalendarRepository.updateSubscription).not.toHaveBeenCalled();
});
test("should return early if selected calendar has no credentialId", async () => {
mockSelectedCalendarRepository.findByIdWithCredentials.mockResolvedValue({
...mockSelectedCalendar,
credentialId: null,
});
await service.subscribe("test-calendar-id");
expect(mockAdapter.subscribe).not.toHaveBeenCalled();
expect(mockSelectedCalendarRepository.updateSubscription).not.toHaveBeenCalled();
});
});
describe("unsubscribe", () => {
test("should successfully unsubscribe from a calendar", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally.mockResolvedValue(true);
await service.unsubscribe("test-calendar-id");
expect(mockSelectedCalendarRepository.findByIdWithCredentials).toHaveBeenCalledWith("test-calendar-id");
expect(mockAdapter.unsubscribe).toHaveBeenCalledWith(mockSelectedCalendar, mockCredential);
expect(mockSelectedCalendarRepository.updateSubscription).toHaveBeenCalledWith("test-calendar-id", {
syncSubscribedAt: null,
});
expect(mockCalendarCacheEventService.cleanupCache).toHaveBeenCalledWith(mockSelectedCalendar);
});
test("should not cleanup cache if cache is disabled", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally.mockResolvedValue(false);
await service.unsubscribe("test-calendar-id");
expect(mockCalendarCacheEventService.cleanupCache).not.toHaveBeenCalled();
});
test("should return early if selected calendar not found", async () => {
mockSelectedCalendarRepository.findByIdWithCredentials.mockResolvedValue(null);
await service.unsubscribe("non-existent-id");
expect(mockAdapter.unsubscribe).not.toHaveBeenCalled();
});
});
describe("processWebhook", () => {
test("should successfully process a valid webhook", async () => {
const mockRequest = new Request("http://example.com");
await service.processWebhook("google_calendar", mockRequest);
expect(mockAdapter.validate).toHaveBeenCalledWith(mockRequest);
expect(mockAdapter.extractChannelId).toHaveBeenCalledWith(mockRequest);
expect(mockSelectedCalendarRepository.findByChannelId).toHaveBeenCalledWith("test-channel-id");
});
test("should throw error for invalid webhook", async () => {
const mockRequest = new Request("http://example.com");
mockAdapter.validate.mockResolvedValue(false);
await expect(service.processWebhook("google_calendar", mockRequest)).rejects.toThrow(
"Invalid webhook request"
);
});
test("should throw error for missing channel ID", async () => {
const mockRequest = new Request("http://example.com");
mockAdapter.extractChannelId.mockResolvedValue(null);
await expect(service.processWebhook("google_calendar", mockRequest)).rejects.toThrow(
"Missing channel ID in webhook"
);
});
test("should return null for old subscription", async () => {
const mockRequest = new Request("http://example.com");
mockSelectedCalendarRepository.findByChannelId.mockResolvedValue(null);
const _result = await service.processWebhook("google_calendar", mockRequest);
expect(_result).toBeNull();
});
});
describe("processEvents", () => {
test("should process events when both cache and sync are enabled", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true);
mockFeaturesRepository.checkIfUserHasFeature.mockResolvedValue(true);
await service.processEvents(mockSelectedCalendar);
expect(mockAdapter.fetchEvents).toHaveBeenCalledWith(mockSelectedCalendar, mockCredential);
expect(mockSelectedCalendarRepository.updateSyncStatus).toHaveBeenCalledWith(mockSelectedCalendar.id, {
syncToken: "new-sync-token",
syncedAt: expect.any(Date),
syncErrorAt: null,
syncErrorCount: 0,
});
expect(mockCalendarCacheEventService.handleEvents).toHaveBeenCalledWith(
mockSelectedCalendar,
mockEvents.items
);
expect(mockCalendarSyncService.handleEvents).toHaveBeenCalledWith(
mockSelectedCalendar,
mockEvents.items
);
});
test("should not process cache when cache is disabled globally", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);
mockFeaturesRepository.checkIfUserHasFeature.mockResolvedValue(true);
await service.processEvents(mockSelectedCalendar);
expect(mockCalendarCacheEventService.handleEvents).not.toHaveBeenCalled();
expect(mockCalendarSyncService.handleEvents).toHaveBeenCalled();
});
test("should not process cache when cache is disabled for user", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true);
mockFeaturesRepository.checkIfUserHasFeature.mockResolvedValue(false);
await service.processEvents(mockSelectedCalendar);
expect(mockCalendarCacheEventService.handleEvents).not.toHaveBeenCalled();
expect(mockCalendarSyncService.handleEvents).toHaveBeenCalled();
});
test("should return early when both cache and sync are disabled", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(false);
await service.processEvents(mockSelectedCalendar);
expect(mockAdapter.fetchEvents).not.toHaveBeenCalled();
expect(mockCalendarCacheEventService.handleEvents).not.toHaveBeenCalled();
expect(mockCalendarSyncService.handleEvents).not.toHaveBeenCalled();
});
test("should handle API errors and update sync status", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true);
mockFeaturesRepository.checkIfUserHasFeature.mockResolvedValue(true);
const apiError = new Error("API Error");
mockAdapter.fetchEvents.mockRejectedValue(apiError);
await expect(service.processEvents(mockSelectedCalendar)).rejects.toThrow("API Error");
expect(mockSelectedCalendarRepository.updateSyncStatus).toHaveBeenCalledWith(mockSelectedCalendar.id, {
syncErrorAt: expect.any(Date),
syncErrorCount: {
increment: 1,
},
});
});
test("should return early when no events are fetched", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally
.mockResolvedValueOnce(true)
.mockResolvedValueOnce(true);
mockFeaturesRepository.checkIfUserHasFeature.mockResolvedValue(true);
mockAdapter.fetchEvents.mockResolvedValue({
...mockEvents,
items: [],
});
await service.processEvents(mockSelectedCalendar);
expect(mockCalendarCacheEventService.handleEvents).not.toHaveBeenCalled();
expect(mockCalendarSyncService.handleEvents).not.toHaveBeenCalled();
});
test("should return early when selected calendar has no credentialId", async () => {
const calendarWithoutCredential = {
...mockSelectedCalendar,
credentialId: null,
};
await service.processEvents(calendarWithoutCredential);
expect(mockAdapter.fetchEvents).not.toHaveBeenCalled();
});
});
describe("checkForNewSubscriptions", () => {
test("should process new subscriptions", async () => {
const subscribeSpy = vi.spyOn(service, "subscribe").mockResolvedValue(undefined);
await service.checkForNewSubscriptions();
expect(mockSelectedCalendarRepository.findNextSubscriptionBatch).toHaveBeenCalledWith({
take: 100,
integrations: ["google_calendar", "office365_calendar"],
});
expect(subscribeSpy).toHaveBeenCalledWith(mockSelectedCalendar.id);
});
});
describe("feature flag methods", () => {
test("isCacheEnabled should check global cache feature", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally.mockResolvedValue(true);
const result = await service.isCacheEnabled();
expect(result).toBe(true);
expect(mockFeaturesRepository.checkIfFeatureIsEnabledGlobally).toHaveBeenCalledWith(
"calendar-subscription-cache"
);
});
test("isCacheEnabledForUser should check user cache feature", async () => {
mockFeaturesRepository.checkIfUserHasFeature.mockResolvedValue(true);
const result = await service.isCacheEnabledForUser(1);
expect(result).toBe(true);
expect(mockFeaturesRepository.checkIfUserHasFeature).toHaveBeenCalledWith(
1,
"calendar-subscription-cache"
);
});
test("isSyncEnabled should check global sync feature", async () => {
mockFeaturesRepository.checkIfFeatureIsEnabledGlobally.mockResolvedValue(true);
const result = await service.isSyncEnabled();
expect(result).toBe(true);
expect(mockFeaturesRepository.checkIfFeatureIsEnabledGlobally).toHaveBeenCalledWith(
"calendar-subscription-sync"
);
});
});
});