Files
Udit TakkarGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f883dee084 perf: improve getTotalBookingDuration query (#27383)
* perf: improve getTotalBookingDuration query

* perf: improve getTotalBookingDuration query

* test: add unit tests for getTotalBookingDurationForUsers and yearly duration pre-fetching

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

* fix: integration tests

* fix: update tests

* fix: use vi.stubEnv instead of direct process.env modification for test isolation

Replace direct deletion of process.env variables with vi.stubEnv() to ensure
automatic restoration after tests, preventing flaky tests and side effects
in parallel execution.

Issue identified by Cubic AI (confidence: 9/10)

Co-Authored-By: unknown <>

* chore: add log

* refactor: imports

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-24 22:55:33 +05:30

62 lines
1.8 KiB
TypeScript

import type { PrismaClient } from "@calcom/prisma";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { BookingRepository } from "./BookingRepository";
describe("BookingRepository", () => {
let repository: BookingRepository;
let mockPrismaClient: {
$queryRaw: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
vi.clearAllMocks();
mockPrismaClient = {
$queryRaw: vi.fn(),
};
repository = new BookingRepository(mockPrismaClient as unknown as PrismaClient);
});
describe("getTotalBookingDuration", () => {
it("should return total minutes from the database result", async () => {
mockPrismaClient.$queryRaw.mockResolvedValue([{ totalMinutes: 120 }]);
const result = await repository.getTotalBookingDuration({
eventId: 52,
startDate: new Date("2026-01-01"),
endDate: new Date("2026-12-31"),
});
expect(result).toBe(120);
expect(mockPrismaClient.$queryRaw).toHaveBeenCalledTimes(1);
});
it("should return 0 when totalMinutes is null", async () => {
mockPrismaClient.$queryRaw.mockResolvedValue([{ totalMinutes: null }]);
const result = await repository.getTotalBookingDuration({
eventId: 52,
startDate: new Date("2026-01-01"),
endDate: new Date("2026-12-31"),
});
expect(result).toBe(0);
});
it("should call query when rescheduleUid is provided", async () => {
mockPrismaClient.$queryRaw.mockResolvedValue([{ totalMinutes: 90 }]);
const result = await repository.getTotalBookingDuration({
eventId: 52,
startDate: new Date("2026-01-01"),
endDate: new Date("2026-12-31"),
rescheduleUid: "existing-booking-uid",
});
expect(result).toBe(90);
expect(mockPrismaClient.$queryRaw).toHaveBeenCalledTimes(1);
});
});
});