Files
calendar/setupVitest.ts
T
Keith WilliamsGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
baaa04151d perf: optimize payment app imports to avoid loading entire app store (#23408)
* perf: optimize payment app imports to avoid loading entire app store

- Add PaymentServiceMap generation to app-store-cli build process
- Generate payment.services.generated.ts with lazy imports for 6 payment services
- Update handlePayment.ts, deletePayment.ts, handlePaymentRefund.ts to use PaymentServiceMap
- Update getConnectedApps.ts and tRPC payment routers to use PaymentServiceMap
- Follow same pattern as analytics optimization in PR #23372
- Reduces bundle size by avoiding import of 100+ apps when only payment functionality needed

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

* Update build.ts

* fix: update payment service test mocking to work with PaymentServiceMap

- Remove obsolete appStoreMock line from bookingScenario.ts since handlePayment now uses PaymentServiceMap
- Update setupVitest.ts to import prismaMock from correct PrismockClient instance
- Add PaymentServiceMap mock following PR #22450 pattern for calendar services
- Ensure MockPaymentService uses consistent externalId across test files
- Fix webhook handler to return 200 status by ensuring payment records are found correctly

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

* fix: revert prismaMock import to avoid interfering with other tests' vi.spyOn() calls

- Remove global prismaMock import from setupVitest.ts that was causing 'is not a spy' errors
- Update MockPaymentService to import prismaMock locally to maintain payment test functionality
- Fixes organization and outOfOffice tests while preserving payment service optimization

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

* fix: remove E2E conditional check from payment services map generation

- Payment services map now always includes all payment apps regardless of E2E environment
- Ensures payment functionality is consistently available across all environments
- Addresses CI failures caused by conditional payment service loading

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

* refactor: use direct PaymentService imports instead of .lib structure

- Update app-store-cli to import directly from lib/PaymentService.ts files
- Modify all payment handlers to access PaymentService directly
- Update test mocks to match new direct import structure
- Remove .lib property access pattern across payment system
- Maintain backward compatibility while improving import efficiency

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

* fix: revert chargeCard booking.id parameter additions

- Remove booking.id parameter from chargeCard calls in chargeCard.handler.ts and payments.tsx
- Addresses GitHub feedback to investigate chargeCard signature changes in separate PR
- Keeps all other direct PaymentService import refactor changes intact

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-08-28 10:05:36 -03:00

163 lines
4.1 KiB
TypeScript

import matchers from "@testing-library/jest-dom/matchers";
import ResizeObserver from "resize-observer-polyfill";
import { vi, expect } from "vitest";
import createFetchMock from "vitest-fetch-mock";
import type { CalendarService } from "@calcom/types/Calendar";
global.ResizeObserver = ResizeObserver;
const fetchMocker = createFetchMock(vi);
// sets globalThis.fetch and globalThis.fetchMock to our mocked version
fetchMocker.enableMocks();
expect.extend(matchers);
class MockExchangeCalendarService implements CalendarService {
constructor() {}
async createEvent() {
return {
uid: "mock",
id: "mock",
password: "",
type: "",
url: "",
additionalInfo: {},
};
}
async updateEvent() {
return {
uid: "mock",
id: "mock",
password: "",
type: "",
url: "",
additionalInfo: {},
};
}
async deleteEvent() {}
async getAvailability() {
return [];
}
async listCalendars() {
return [];
}
}
vi.mock("@calcom/exchangecalendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
vi.mock("@calcom/exchange2013calendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
vi.mock("@calcom/exchange2016calendar/lib/CalendarService", () => ({
default: MockExchangeCalendarService,
}));
const MOCK_PAYMENT_UID = "MOCK_PAYMENT_UID";
class MockPaymentService {
constructor(credentials?: any) {
this.credentials = credentials;
}
private credentials: any;
async create(
payment: any,
bookingId: number,
userId: number,
username: string | null,
bookerName: string | null,
paymentOption: any,
bookerEmail: string,
bookerPhoneNumber?: string | null,
selectedEventTypeTitle?: string,
eventTitle?: string
) {
const { default: prismaMock } = await import("./tests/libs/__mocks__/prisma");
const externalId = "mock_payment_external_id";
const paymentCreateData = {
uid: MOCK_PAYMENT_UID,
appId: null,
bookingId,
fee: 10,
success: false,
refunded: false,
data: {},
externalId,
paymentOption,
amount: payment.amount,
currency: payment.currency,
};
const createdPayment = await prismaMock.payment.create({
data: paymentCreateData,
});
return createdPayment;
}
async collectCard() {
return { success: true };
}
async chargeCard() {
return { success: true };
}
async refund() {
return { success: true };
}
async deletePayment() {
return { success: true };
}
async afterPayment(event: any, booking: any, paymentData: any) {
const { sendAwaitingPaymentEmailAndSMS } = await import("@calcom/emails");
await sendAwaitingPaymentEmailAndSMS({
...event,
paymentInfo: {
link: "http://mock-payment.example.com/",
paymentOption: paymentData.paymentOption || "ON_BOOKING",
amount: paymentData.amount,
currency: paymentData.currency,
},
});
return { success: true };
}
}
vi.mock("@calcom/app-store/stripepayment/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/paypal/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/alby/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/hitpay/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/btcpayserver/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/mock-payment-app/index", () => ({
PaymentService: MockPaymentService,
}));
vi.mock("@calcom/app-store/payment.services.generated", () => ({
PaymentServiceMap: {
stripepayment: Promise.resolve({ PaymentService: MockPaymentService }),
paypal: Promise.resolve({ PaymentService: MockPaymentService }),
alby: Promise.resolve({ PaymentService: MockPaymentService }),
hitpay: Promise.resolve({ PaymentService: MockPaymentService }),
btcpayserver: Promise.resolve({ PaymentService: MockPaymentService }),
"mock-payment-app": Promise.resolve({ PaymentService: MockPaymentService }),
},
}));