Files
cal.diy/packages/features/booking-audit/lib/service/__tests__/BookingAuditViewerService.test.ts
T
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 <[email protected]>

* 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 <[email protected]>

* fix: fix remaining Vitest 4.0 constructor mock patterns

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

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

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

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

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

* 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 <[email protected]>

* 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 <[email protected]>

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

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

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

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

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

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

* fix: update TestFunction type for Vitest 4.0 compatibility

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

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

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

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

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

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

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

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

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

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

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

* 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 <[email protected]>

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

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

* fix: update TeamRepository mock pattern for Vitest 4.0 compatibility

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

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

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

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

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

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

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

* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

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

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

* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

---------

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

893 lines
32 KiB
TypeScript

import { describe, it, expect, beforeEach, vi, type Mock } from "vitest";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
import type { IAttendeeRepository } from "@calcom/features/bookings/repositories/IAttendeeRepository";
import type { ISimpleLogger } from "@calcom/features/di/shared/services/logger.service";
import { BookingAuditViewerService } from "../BookingAuditViewerService";
import { BookingAuditPermissionError, BookingAuditErrorCode } from "../BookingAuditAccessService";
import type { IBookingAuditRepository, BookingAuditWithActor, BookingAuditAction, BookingAuditType } from "../../repository/IBookingAuditRepository";
import type { AuditActorType } from "../../repository/IAuditActorRepository";
vi.mock("@calcom/features/pbac/services/permission-check.service");
vi.mock("@calcom/features/users/repositories/UserRepository");
vi.mock("@calcom/features/bookings/repositories/BookingRepository");
vi.mock("@calcom/features/membership/repositories/MembershipRepository");
const createMockTeamBooking = (overrides?: {
userId?: number;
teamId?: number | null;
parentTeamId?: number;
}) => ({
userId: overrides?.userId ?? 456,
user: {
id: overrides?.userId ?? 456,
email: "[email protected]",
},
eventType: {
teamId: (overrides && "teamId" in overrides ? overrides.teamId : overrides?.teamId ?? 100) ?? null,
parent: (overrides?.parentTeamId ? { teamId: overrides.parentTeamId } : undefined) ?? null,
hosts: [],
users: [],
},
attendees: [],
});
const createMockAuditLog = (
overrides?: Partial<{
id: string;
bookingUid: string;
action: BookingAuditAction;
type: BookingAuditType;
timestamp: Date;
createdAt: Date;
updatedAt: Date;
data: unknown;
actorId: string;
actorType: AuditActorType;
actorUserUuid: string | null;
actorName: string | null;
}>
): BookingAuditWithActor => ({
id: overrides?.id ?? "audit-log-1",
bookingUid: overrides?.bookingUid ?? "booking-uid-123",
action: overrides?.action ?? "CREATED",
type: overrides?.type ?? "RECORD_CREATED",
timestamp: overrides?.timestamp ?? new Date("2024-01-15T10:00:00Z"),
createdAt: overrides?.createdAt ?? new Date("2024-01-15T10:00:00Z"),
updatedAt: overrides?.updatedAt ?? new Date("2024-01-15T10:00:00Z"),
actorId: overrides?.actorId ?? "actor-1",
data: overrides?.data ?? { version: 1, fields: { startTime: 1705315200000, endTime: 1705318800000, status: "ACCEPTED" } },
source: "WEBAPP" as const,
operationId: "operation-id-123",
actor: {
id: overrides?.actorId ?? "actor-1",
type: overrides?.actorType ?? "USER" as const,
userUuid: (overrides && "actorUserUuid" in overrides ? overrides.actorUserUuid : "user-uuid-123") as string | null,
attendeeId: null,
name: (overrides && "actorName" in overrides ? overrides.actorName : "John Doe") as string | null,
createdAt: new Date("2024-01-01T00:00:00Z"),
},
});
const createMockUser = (overrides?: Partial<{ id: number; name: string | null; email: string; avatarUrl: string | null }>) => ({
id: overrides?.id ?? 123,
name: (overrides && "name" in overrides ? overrides.name : "John Doe") as string | null,
email: overrides?.email ?? "[email protected]",
avatarUrl: (overrides && "avatarUrl" in overrides ? overrides.avatarUrl : null) as string | null,
});
describe("BookingAuditViewerService - Integration Tests", () => {
let service: BookingAuditViewerService;
let mockBookingRepository: {
findByUidIncludeEventType: Mock<BookingRepository["findByUidIncludeEventType"]>;
getFromRescheduleUid: Mock<BookingRepository["getFromRescheduleUid"]>;
};
let mockUserRepository: {
getUserOrganizationAndTeams: Mock<UserRepository["getUserOrganizationAndTeams"]>;
findByUuid: Mock<UserRepository["findByUuid"]>;
};
let mockBookingAuditRepository: {
create: Mock<IBookingAuditRepository["create"]>;
findAllForBooking: Mock<IBookingAuditRepository["findAllForBooking"]>;
findRescheduledLogsOfBooking: Mock<IBookingAuditRepository["findRescheduledLogsOfBooking"]>;
};
let mockMembershipRepository: {
hasMembership: Mock<MembershipRepository["hasMembership"]>;
};
let mockPermissionCheckService: {
checkPermission: Mock<PermissionCheckService["checkPermission"]>;
};
let mockAttendeeRepository: {
findById: Mock;
};
let mockLog: {
error: Mock;
};
beforeEach(() => {
vi.clearAllMocks();
mockBookingRepository = {
findByUidIncludeEventType: vi.fn(),
getFromRescheduleUid: vi.fn(),
};
mockUserRepository = {
getUserOrganizationAndTeams: vi.fn(),
findByUuid: vi.fn(),
};
mockBookingAuditRepository = {
create: vi.fn(),
findAllForBooking: vi.fn().mockResolvedValue([]),
findRescheduledLogsOfBooking: vi.fn(),
};
mockMembershipRepository = {
hasMembership: vi.fn(),
};
mockPermissionCheckService = {
checkPermission: vi.fn(),
};
mockAttendeeRepository = {
findById: vi.fn(),
};
mockLog = {
error: vi.fn(),
};
vi.mocked(BookingRepository).mockImplementation(function() { return mockBookingRepository as unknown as BookingRepository; });
vi.mocked(UserRepository).mockImplementation(function() { return mockUserRepository as unknown as UserRepository; });
vi.mocked(MembershipRepository).mockImplementation(function() { return mockMembershipRepository as unknown as MembershipRepository; });
vi.mocked(PermissionCheckService).mockImplementation(function() { return mockPermissionCheckService as unknown as PermissionCheckService; });
service = new BookingAuditViewerService({
bookingAuditRepository: mockBookingAuditRepository as unknown as IBookingAuditRepository,
userRepository: mockUserRepository as unknown as UserRepository,
bookingRepository: mockBookingRepository as unknown as BookingRepository,
membershipRepository: mockMembershipRepository as unknown as MembershipRepository,
attendeeRepository: mockAttendeeRepository as unknown as IAttendeeRepository,
log: mockLog as unknown as ISimpleLogger,
});
});
describe("getAuditLogsForBooking", () => {
describe("when user has permission to view audit logs", () => {
beforeEach(() => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
createMockTeamBooking({ teamId: 100 })
);
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
});
it("should return enriched audit logs with actor information", async () => {
const mockLog = createMockAuditLog();
const mockUser = createMockUser();
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(mockUser);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.bookingUid).toBe("booking-uid-123");
expect(result.auditLogs).toHaveLength(1);
expect(result.auditLogs[0]).toMatchObject({
id: "audit-log-1",
bookingUid: "booking-uid-123",
action: "CREATED",
type: "RECORD_CREATED",
actor: expect.objectContaining({
type: "USER",
displayName: "John Doe",
displayEmail: "[email protected]",
displayAvatar: null,
}),
});
});
it("should format timestamps as ISO strings", async () => {
const mockLog = createMockAuditLog({
timestamp: new Date("2024-01-15T10:00:00Z"),
createdAt: new Date("2024-01-15T10:00:00Z"),
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.auditLogs[0].timestamp).toBe("2024-01-15T10:00:00.000Z");
expect(result.auditLogs[0].createdAt).toBe("2024-01-15T10:00:00.000Z");
});
it("should include action display title with translation key", async () => {
const mockLog = createMockAuditLog({ action: "CREATED" });
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.auditLogs[0].actionDisplayTitle).toEqual({
key: "booking_audit_action.created",
});
});
it("should handle multiple audit logs in correct order", async () => {
const logs = [
createMockAuditLog({
id: "log-1",
action: "CREATED",
timestamp: new Date("2024-01-15T10:00:00Z"),
data: { version: 1, fields: { startTime: 1705315200000, endTime: 1705318800000, status: "ACCEPTED" } }
}),
createMockAuditLog({
id: "log-2",
action: "ACCEPTED",
timestamp: new Date("2024-01-15T11:00:00Z"),
data: { version: 1, fields: { status: { old: "PENDING", new: "ACCEPTED" } } }
}),
createMockAuditLog({
id: "log-3",
action: "CANCELLED",
timestamp: new Date("2024-01-15T12:00:00Z"),
data: {
version: 1,
fields: {
status: { old: "ACCEPTED", new: "CANCELLED" },
cancellationReason: { old: null, new: "User requested" },
cancelledBy: { old: null, new: "user-123" },
}
}
}),
];
mockBookingAuditRepository.findAllForBooking.mockResolvedValue(logs);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(3);
expect(result.auditLogs[0].id).toBe("log-1");
expect(result.auditLogs[1].id).toBe("log-2");
expect(result.auditLogs[2].id).toBe("log-3");
});
it("should return empty array when no audit logs exist", async () => {
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([]);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.bookingUid).toBe("booking-uid-123");
expect(result.auditLogs).toEqual([]);
});
});
describe("when enriching actor information", () => {
beforeEach(() => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
createMockTeamBooking({ teamId: 100 })
);
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
});
it("should enrich USER actor with user details from repository", async () => {
const mockLog = createMockAuditLog({
actorType: "USER",
actorUserUuid: "user-uuid-456",
});
const mockUser = createMockUser({
name: "Jane Smith",
email: "[email protected]",
avatarUrl: "https://example.com/avatar.jpg",
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(mockUser);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(mockUserRepository.findByUuid).toHaveBeenCalledWith({ uuid: "user-uuid-456" });
expect(result.auditLogs[0].actor).toMatchObject({
displayName: "Jane Smith",
displayEmail: "[email protected]",
displayAvatar: "https://example.com/avatar.jpg",
});
});
it("should use email as display name when user name is null", async () => {
const mockLog = createMockAuditLog({
actorType: "USER",
actorName: null
});
const mockUser = createMockUser({ name: null, email: "[email protected]" });
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(mockUser);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs[0].actor.displayName).toBe("[email protected]");
});
it("should show 'Deleted User' when user not found in repository", async () => {
const mockLog = createMockAuditLog({ actorType: "USER" });
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(null);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs[0].actor).toMatchObject({
displayName: "Deleted User",
displayEmail: null,
displayAvatar: null,
});
});
it("should show 'Cal.com' for SYSTEM actor", async () => {
const mockLog = createMockAuditLog({
actorType: "SYSTEM",
actorUserUuid: null,
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs[0].actor).toMatchObject({
type: "SYSTEM",
displayName: "Cal.com",
displayEmail: null,
displayAvatar: null,
});
expect(mockUserRepository.findByUuid).not.toHaveBeenCalled();
});
it("should show 'Guest' for GUEST actor without name", async () => {
const mockLog = createMockAuditLog({
actorType: "GUEST",
actorUserUuid: null,
actorName: null,
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs[0].actor).toMatchObject({
type: "GUEST",
displayName: "Guest",
displayEmail: null,
displayAvatar: null,
});
});
it("should use provided name for GUEST actor", async () => {
const mockLog = createMockAuditLog({
actorType: "GUEST",
actorUserUuid: null,
actorName: "External Guest",
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs[0].actor.displayName).toBe("External Guest");
});
it("should show 'Attendee' for ATTENDEE actor without name", async () => {
const mockLog = createMockAuditLog({
actorType: "ATTENDEE",
actorUserUuid: null,
actorName: null,
});
// Update the mock to include attendeeId in the actor
const logWithAttendeeId = {
...mockLog,
actor: {
...mockLog.actor,
attendeeId: 999,
},
};
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([logWithAttendeeId]);
mockAttendeeRepository.findById.mockResolvedValue({
id: 999,
name: null,
email: "[email protected]",
});
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(mockAttendeeRepository.findById).toHaveBeenCalledWith(999);
expect(result.auditLogs[0].actor).toMatchObject({
type: "ATTENDEE",
displayName: "[email protected]",
displayEmail: "[email protected]",
displayAvatar: null,
});
});
it("should use provided name for ATTENDEE actor", async () => {
const mockLog = createMockAuditLog({
actorType: "ATTENDEE",
actorUserUuid: null,
actorName: "Meeting Participant",
});
// Update the mock to include attendeeId in the actor
const logWithAttendeeId = {
...mockLog,
actor: {
...mockLog.actor,
attendeeId: 888,
},
};
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([logWithAttendeeId]);
mockAttendeeRepository.findById.mockResolvedValue({
id: 888,
name: "Meeting Participant",
email: "[email protected]",
});
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(mockAttendeeRepository.findById).toHaveBeenCalledWith(888);
expect(result.auditLogs[0].actor.displayName).toBe("Meeting Participant");
expect(result.auditLogs[0].actor.displayEmail).toBe("[email protected]");
});
it("should show 'Deleted Attendee' when attendee not found in repository", async () => {
const mockLog = createMockAuditLog({
actorType: "ATTENDEE",
actorUserUuid: null,
actorName: null,
});
// Update the mock to include attendeeId in the actor
const logWithAttendeeId = {
...mockLog,
actor: {
...mockLog.actor,
attendeeId: 777,
},
};
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([logWithAttendeeId]);
mockAttendeeRepository.findById.mockResolvedValue(null);
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(mockAttendeeRepository.findById).toHaveBeenCalledWith(777);
expect(result.auditLogs[0].actor).toMatchObject({
type: "ATTENDEE",
displayName: "Deleted Attendee",
displayEmail: null,
displayAvatar: null,
});
});
});
describe("when handling rescheduled bookings", () => {
beforeEach(() => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
createMockTeamBooking({ teamId: 100 })
);
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
});
it("should include rescheduled from log when booking was created from reschedule", async () => {
const currentBookingLog = createMockAuditLog({
id: "current-log",
bookingUid: "new-booking-uid",
action: "CREATED",
});
const rescheduledLog = createMockAuditLog({
id: "rescheduled-log",
bookingUid: "old-booking-uid",
action: "RESCHEDULED",
data: {
version: 1,
fields: {
startTime: { old: "2024-01-15T10:00:00Z", new: "2024-01-16T10:00:00Z" },
endTime: { old: "2024-01-15T11:00:00Z", new: "2024-01-16T11:00:00Z" },
rescheduledToUid: { old: null, new: "new-booking-uid" },
},
},
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([currentBookingLog]);
mockBookingRepository.getFromRescheduleUid.mockResolvedValue("old-booking-uid");
mockBookingAuditRepository.findRescheduledLogsOfBooking.mockResolvedValue([rescheduledLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "new-booking-uid",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(2);
expect(result.auditLogs[0].id).toBe("rescheduled-log");
expect(result.auditLogs[0].bookingUid).toBe("new-booking-uid");
expect(result.auditLogs[0].actionDisplayTitle.key).toBe("booking_audit_action.rescheduled_from");
expect(result.auditLogs[0].displayJson).toHaveProperty("rescheduledFromUid", "old-booking-uid");
expect(result.auditLogs[1].id).toBe("current-log");
});
it("should not include rescheduled from log when booking was not rescheduled", async () => {
const mockLog = createMockAuditLog();
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockBookingRepository.getFromRescheduleUid.mockResolvedValue(null);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(1);
expect(mockBookingAuditRepository.findRescheduledLogsOfBooking).not.toHaveBeenCalled();
});
it("should continue without rescheduled from log when matching log not found", async () => {
const currentBookingLog = createMockAuditLog({
bookingUid: "new-booking-uid",
});
const rescheduledLog = createMockAuditLog({
id: "rescheduled-log",
bookingUid: "old-booking-uid",
action: "RESCHEDULED",
data: {
version: 1,
fields: {
startTime: { old: "2024-01-15T10:00:00Z", new: "2024-01-16T10:00:00Z" },
endTime: { old: "2024-01-15T11:00:00Z", new: "2024-01-16T11:00:00Z" },
rescheduledToUid: { old: null, new: "different-booking-uid" },
},
},
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([currentBookingLog]);
mockBookingRepository.getFromRescheduleUid.mockResolvedValue("old-booking-uid");
mockBookingAuditRepository.findRescheduledLogsOfBooking.mockResolvedValue([rescheduledLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "new-booking-uid",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(1);
expect(result.auditLogs[0].id).toBe("audit-log-1");
expect(mockLog.error).toHaveBeenCalledWith(
"No rescheduled log found for booking old-booking-uid -> new-booking-uid"
);
});
});
describe("when permission check fails", () => {
it("should throw error when user lacks permission", async () => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
createMockTeamBooking({ teamId: 100 })
);
mockPermissionCheckService.checkPermission.mockResolvedValue(false);
mockMembershipRepository.hasMembership.mockResolvedValue(true);
await expect(
service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
})
).rejects.toThrow(BookingAuditPermissionError);
expect(mockBookingAuditRepository.findAllForBooking).not.toHaveBeenCalled();
});
it("should throw error when organization ID is null", async () => {
await expect(
service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: null,
})
).rejects.toThrow(BookingAuditPermissionError);
expect(mockBookingAuditRepository.findAllForBooking).not.toHaveBeenCalled();
});
it("should throw error when booking not found", async () => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(null);
await expect(
service.getAuditLogsForBooking({
bookingUid: "non-existent-booking",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
})
).rejects.toThrow(BookingAuditErrorCode.BOOKING_NOT_FOUND_OR_PERMISSION_DENIED);
expect(mockBookingAuditRepository.findAllForBooking).not.toHaveBeenCalled();
});
});
describe("when handling different timezones", () => {
beforeEach(() => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
createMockTeamBooking({ teamId: 100 })
);
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
});
it("should pass user timezone to action service for display formatting", async () => {
const mockLog = createMockAuditLog({
action: "RESCHEDULED",
data: {
version: 1,
fields: {
startTime: { old: "2024-01-15T10:00:00Z", new: "2024-01-16T10:00:00Z" },
endTime: { old: "2024-01-15T11:00:00Z", new: "2024-01-16T11:00:00Z" },
rescheduledToUid: { old: null, new: "new-booking-uid" },
},
},
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/Los_Angeles",
organizationId: 200,
});
expect(result.auditLogs[0].actionDisplayTitle).toHaveProperty("params");
expect(result.auditLogs[0].displayJson).toBeDefined();
});
});
describe("when handling seat audit actions", () => {
beforeEach(() => {
mockBookingRepository.findByUidIncludeEventType.mockResolvedValue(
createMockTeamBooking({ teamId: 100 })
);
mockPermissionCheckService.checkPermission.mockResolvedValue(true);
});
it("should handle SEAT_BOOKED action with seat information", async () => {
const mockLog = createMockAuditLog({
id: "seat-booked-log",
action: "SEAT_BOOKED",
data: {
version: 1,
fields: {
seatReferenceUid: "seat-ref-123",
attendeeEmail: "[email protected]",
attendeeName: "Jane Attendee",
startTime: 1705315200000,
endTime: 1705318800000,
},
},
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(1);
expect(result.auditLogs[0].action).toBe("SEAT_BOOKED");
expect(result.auditLogs[0].actionDisplayTitle.key).toBe("booking_audit_action.seat_booked");
expect(result.auditLogs[0].displayJson).toBeDefined();
});
it("should handle SEAT_RESCHEDULED action with time changes", async () => {
const mockLog = createMockAuditLog({
id: "seat-rescheduled-log",
action: "SEAT_RESCHEDULED",
data: {
version: 1,
fields: {
seatReferenceUid: "seat-ref-123",
attendeeEmail: "[email protected]",
startTime: {
old: "2024-01-15T10:00:00Z",
new: "2024-01-16T10:00:00Z",
},
endTime: {
old: "2024-01-15T11:00:00Z",
new: "2024-01-16T11:00:00Z",
},
rescheduledToBookingUid: {
old: null,
new: "new-booking-uid",
},
},
},
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "America/New_York",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(1);
expect(result.auditLogs[0].action).toBe("SEAT_RESCHEDULED");
expect(result.auditLogs[0].actionDisplayTitle.key).toBe("booking_audit_action.seat_rescheduled");
expect(result.auditLogs[0].actionDisplayTitle.params).toBeDefined();
expect(result.auditLogs[0].actionDisplayTitle.components).toBeDefined();
expect(result.auditLogs[0].displayJson).toBeDefined();
});
it("should handle SEAT_RESCHEDULED action without moving to different booking", async () => {
const mockLog = createMockAuditLog({
id: "seat-rescheduled-same-booking",
action: "SEAT_RESCHEDULED",
data: {
version: 1,
fields: {
seatReferenceUid: "seat-ref-123",
attendeeEmail: "[email protected]",
startTime: {
old: "2024-01-15T10:00:00Z",
new: "2024-01-15T11:00:00Z",
},
endTime: {
old: "2024-01-15T11:00:00Z",
new: "2024-01-15T12:00:00Z",
},
rescheduledToBookingUid: {
old: null,
new: null,
},
},
},
});
mockBookingAuditRepository.findAllForBooking.mockResolvedValue([mockLog]);
mockUserRepository.findByUuid.mockResolvedValue(createMockUser());
const result = await service.getAuditLogsForBooking({
bookingUid: "booking-uid-123",
userId: 123,
userEmail: "[email protected]",
userTimeZone: "UTC",
organizationId: 200,
});
expect(result.auditLogs).toHaveLength(1);
expect(result.auditLogs[0].action).toBe("SEAT_RESCHEDULED");
expect(result.auditLogs[0].actionDisplayTitle.components).toBeUndefined();
});
});
});
});