bbf9274d37
* 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * fix: fix remaining Vitest 4.0 constructor mock patterns Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: fix more Vitest 4.0 constructor mock patterns and exclude API v2 spec files Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert more arrow function mocks to regular functions for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * fix: add React plugin, jsdom environment, and fix more constructor mocks for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert HostRepository PrismaClient mock to regular function for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: add useOrgBranding mock to React component tests for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: update TestFunction type for Vitest 4.0 compatibility Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert listBookingReports constructor mocks to regular functions for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert UserRepository constructor mock to regular function for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert OrganizationPaymentService constructor mock to regular function for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert more constructor mocks to regular functions for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: add apps/web path aliases to vitest config Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * fix: clear EventManager spy between tests for Vitest 4.0 compatibility Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: update TeamRepository mock pattern for Vitest 4.0 compatibility Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert RoutingFormResponseRepository mock to regular function for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: convert more constructor mocks to regular functions for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * fix: fix mock reset and spy clear issues for Vitest 4.0 Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * fix: add @calcom/embed-snippet alias for CI compatibility Co-Authored-By: Volnei Munhoz <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> * 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 <volnei.munhoz@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
400 lines
12 KiB
TypeScript
400 lines
12 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import "@calcom/lib/__mocks__/logger";
|
|
import { prisma } from "@calcom/prisma/__mocks__/prisma";
|
|
|
|
import { describe, it, vi, expect, beforeEach, afterEach } from "vitest";
|
|
|
|
import { WorkflowService } from "@calcom/features/ee/workflows/lib/service/WorkflowService";
|
|
import type { Workflow } from "@calcom/features/ee/workflows/lib/types";
|
|
import type { WebhookSubscriber } from "@calcom/features/webhooks/lib/dto/types";
|
|
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
|
|
import { sendGenericWebhookPayload } from "@calcom/features/webhooks/lib/sendPayload";
|
|
import {
|
|
WebhookTriggerEvents,
|
|
WorkflowTriggerEvents,
|
|
WorkflowActions,
|
|
WorkflowTemplates,
|
|
TimeUnit,
|
|
} from "@calcom/prisma/enums";
|
|
import { WebhookVersion as WebhookVersionEnum } from "@calcom/features/webhooks/lib/interface/IWebhookRepository";
|
|
|
|
import type { FormResponse, Field } from "../types/types";
|
|
import { _onFormSubmission } from "./formSubmissionUtils";
|
|
|
|
vi.mock("@calcom/prisma", () => ({
|
|
prisma,
|
|
}));
|
|
|
|
// Mock dependencies
|
|
vi.mock("@calcom/lib/getOrgIdFromMemberOrTeamId", () => ({
|
|
default: vi.fn(() => Promise.resolve(1)),
|
|
}));
|
|
vi.mock("@calcom/features/webhooks/lib/getWebhooks", () => ({
|
|
default: vi.fn(() => Promise.resolve([])),
|
|
}));
|
|
vi.mock("@calcom/features/webhooks/lib/sendPayload", () => ({
|
|
sendGenericWebhookPayload: vi.fn(() => Promise.resolve()),
|
|
}));
|
|
vi.mock("@calcom/features/tasker", () => {
|
|
const tasker = {
|
|
create: vi.fn(() => Promise.resolve()),
|
|
};
|
|
return { default: Promise.resolve(tasker) };
|
|
});
|
|
|
|
// Mock workflow dependencies
|
|
vi.mock("@calcom/features/ee/workflows/lib/service/WorkflowService", () => ({
|
|
WorkflowService: {
|
|
getAllWorkflowsFromRoutingForm: vi.fn(() => Promise.resolve([])),
|
|
scheduleFormWorkflows: vi.fn(() => Promise.resolve()),
|
|
},
|
|
}));
|
|
|
|
const mockSendEmail = vi.fn(() => Promise.resolve());
|
|
const mockResponseEmailConstructor = vi.fn();
|
|
vi.mock("../emails/templates/response-email", () => ({
|
|
default: class MockResponseEmail {
|
|
sendEmail = mockSendEmail;
|
|
constructor(...args: unknown[]) {
|
|
mockResponseEmailConstructor(...args);
|
|
}
|
|
},
|
|
}));
|
|
|
|
describe("_onFormSubmission", () => {
|
|
const mockForm = {
|
|
id: "form-1",
|
|
name: "Test Form",
|
|
disabled: false,
|
|
userId: 1,
|
|
position: 0,
|
|
description: null,
|
|
updatedById: null,
|
|
fields: [
|
|
{ id: "field-1", identifier: "email", label: "Email", type: "email", required: false },
|
|
{ id: "field-2", identifier: "name", label: "Name", type: "text", required: false },
|
|
] as Field[],
|
|
user: { id: 1, email: "test@example.com", timeFormat: 12, locale: "en" },
|
|
teamId: null,
|
|
settings: { emailOwnerOnSubmission: true },
|
|
routes: [],
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
connectedForms: [],
|
|
routers: [],
|
|
teamMembers: [],
|
|
};
|
|
|
|
const mockResponse: FormResponse = {
|
|
"field-1": { label: "Email", value: "test@response.com" },
|
|
"field-2": { label: "Name", value: "Test Name" },
|
|
};
|
|
|
|
const responseId = 123;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("Webhooks", () => {
|
|
it("should call FORM_SUBMITTED webhooks", async () => {
|
|
const mockWebhook: WebhookSubscriber = {
|
|
id: "wh-1",
|
|
secret: "secret",
|
|
subscriberUrl: "https://example.com/webhook",
|
|
payloadTemplate: null,
|
|
appId: null,
|
|
eventTriggers: [WebhookTriggerEvents.FORM_SUBMITTED],
|
|
time: null,
|
|
timeUnit: null,
|
|
version: WebhookVersionEnum.V_2021_10_20,
|
|
};
|
|
vi.mocked(getWebhooks).mockResolvedValueOnce([mockWebhook]);
|
|
|
|
await _onFormSubmission(mockForm, mockResponse, responseId);
|
|
|
|
expect(getWebhooks).toHaveBeenCalledWith({
|
|
userId: 1,
|
|
teamId: null,
|
|
orgId: 1,
|
|
triggerEvent: WebhookTriggerEvents.FORM_SUBMITTED,
|
|
});
|
|
expect(sendGenericWebhookPayload).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("should normalize identifiers with spaces to hyphens in rootData for webhook templates", async () => {
|
|
const formWithSpaces = {
|
|
...mockForm,
|
|
fields: [
|
|
{
|
|
id: "field-1",
|
|
identifier: "attendee name",
|
|
label: "Attendee Name",
|
|
type: "text",
|
|
required: false,
|
|
},
|
|
] as Field[],
|
|
};
|
|
|
|
const responseWithSpaces: FormResponse = {
|
|
"field-1": { label: "Attendee Name", value: "John Doe" },
|
|
};
|
|
|
|
const mockWebhook: WebhookSubscriber = {
|
|
id: "wh-1",
|
|
secret: "secret",
|
|
subscriberUrl: "https://example.com/webhook",
|
|
payloadTemplate: null,
|
|
appId: null,
|
|
eventTriggers: [WebhookTriggerEvents.FORM_SUBMITTED],
|
|
time: null,
|
|
timeUnit: null,
|
|
version: WebhookVersionEnum.V_2021_10_20,
|
|
};
|
|
vi.mocked(getWebhooks).mockResolvedValueOnce([mockWebhook]);
|
|
|
|
await _onFormSubmission(formWithSpaces, responseWithSpaces, responseId);
|
|
|
|
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
rootData: expect.objectContaining({
|
|
"attendee-name": "John Doe", // Spaces replaced with hyphens for template access
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Workflows", () => {
|
|
it("should call WorkflowService.scheduleFormWorkflows for FORM_SUBMITTED workflows", async () => {
|
|
const mockWorkflows: Workflow[] = [
|
|
{
|
|
id: 1,
|
|
name: "Form Submitted Workflow",
|
|
userId: 1,
|
|
teamId: null,
|
|
trigger: WorkflowTriggerEvents.FORM_SUBMITTED,
|
|
time: null,
|
|
timeUnit: null,
|
|
steps: [
|
|
{
|
|
id: 1,
|
|
action: WorkflowActions.EMAIL_ATTENDEE,
|
|
sendTo: null,
|
|
sender: null,
|
|
reminderBody: "Thank you for your submission!",
|
|
emailSubject: "Form Received",
|
|
template: WorkflowTemplates.CUSTOM,
|
|
verifiedAt: new Date(),
|
|
includeCalendarEvent: false,
|
|
numberVerificationPending: false,
|
|
numberRequired: false,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
vi.mocked(WorkflowService.getAllWorkflowsFromRoutingForm).mockResolvedValueOnce(mockWorkflows);
|
|
|
|
await _onFormSubmission(mockForm, mockResponse, responseId);
|
|
|
|
expect(WorkflowService.getAllWorkflowsFromRoutingForm).toHaveBeenCalledWith(mockForm);
|
|
expect(WorkflowService.scheduleFormWorkflows).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
workflows: mockWorkflows,
|
|
responses: {
|
|
email: {
|
|
value: "test@response.com",
|
|
response: "test@response.com",
|
|
},
|
|
name: { value: "Test Name", response: "Test Name" },
|
|
},
|
|
responseId,
|
|
routedEventTypeId: null,
|
|
form: {
|
|
...mockForm,
|
|
fields: mockForm.fields.map((field) => ({
|
|
type: field.type,
|
|
identifier: field.identifier,
|
|
})),
|
|
},
|
|
creditCheckFn: expect.any(Function),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should call WorkflowService.scheduleFormWorkflows for FORM_SUBMITTED_NO_EVENT workflows", async () => {
|
|
const mockWorkflows: Workflow[] = [
|
|
{
|
|
id: 2,
|
|
name: "Form Follow-up Workflow",
|
|
userId: 1,
|
|
teamId: null,
|
|
trigger: WorkflowTriggerEvents.FORM_SUBMITTED_NO_EVENT,
|
|
time: 30,
|
|
timeUnit: TimeUnit.MINUTE,
|
|
steps: [
|
|
{
|
|
id: 2,
|
|
action: WorkflowActions.EMAIL_ATTENDEE,
|
|
sendTo: null,
|
|
sender: null,
|
|
reminderBody: "Follow up on your form submission",
|
|
emailSubject: "Follow Up",
|
|
template: WorkflowTemplates.CUSTOM,
|
|
verifiedAt: new Date(),
|
|
includeCalendarEvent: false,
|
|
numberVerificationPending: false,
|
|
numberRequired: false,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
vi.mocked(WorkflowService.getAllWorkflowsFromRoutingForm).mockResolvedValueOnce(mockWorkflows);
|
|
|
|
await _onFormSubmission(mockForm, mockResponse, responseId);
|
|
|
|
expect(WorkflowService.getAllWorkflowsFromRoutingForm).toHaveBeenCalledWith(mockForm);
|
|
expect(WorkflowService.scheduleFormWorkflows).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
workflows: mockWorkflows,
|
|
responses: {
|
|
email: {
|
|
value: "test@response.com",
|
|
response: "test@response.com",
|
|
},
|
|
name: { value: "Test Name", response: "Test Name" },
|
|
},
|
|
routedEventTypeId: null,
|
|
responseId,
|
|
form: {
|
|
...mockForm,
|
|
fields: mockForm.fields.map((field) => ({
|
|
type: field.type,
|
|
identifier: field.identifier,
|
|
})),
|
|
},
|
|
creditCheckFn: expect.any(Function),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should pass routedEventTypeId when chosenAction is eventTypeRedirectUrl", async () => {
|
|
const mockWorkflows: Workflow[] = [
|
|
{
|
|
id: 3,
|
|
name: "Event Type Workflow",
|
|
userId: 1,
|
|
teamId: null,
|
|
trigger: WorkflowTriggerEvents.FORM_SUBMITTED,
|
|
time: null,
|
|
timeUnit: null,
|
|
steps: [
|
|
{
|
|
id: 3,
|
|
action: WorkflowActions.CAL_AI_PHONE_CALL,
|
|
sendTo: null,
|
|
sender: null,
|
|
reminderBody: null,
|
|
emailSubject: null,
|
|
template: WorkflowTemplates.CUSTOM,
|
|
verifiedAt: new Date(),
|
|
includeCalendarEvent: false,
|
|
numberVerificationPending: false,
|
|
numberRequired: false,
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
const chosenAction = {
|
|
type: "eventTypeRedirectUrl" as const,
|
|
value: "/team/test-team/test-event",
|
|
eventTypeId: 42,
|
|
};
|
|
|
|
vi.mocked(WorkflowService.getAllWorkflowsFromRoutingForm).mockResolvedValueOnce(mockWorkflows);
|
|
|
|
await _onFormSubmission(mockForm, mockResponse, responseId, chosenAction);
|
|
|
|
expect(WorkflowService.scheduleFormWorkflows).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
workflows: mockWorkflows,
|
|
responses: {
|
|
email: {
|
|
value: "test@response.com",
|
|
response: "test@response.com",
|
|
},
|
|
name: { value: "Test Name", response: "Test Name" },
|
|
},
|
|
routedEventTypeId: 42,
|
|
responseId,
|
|
form: {
|
|
...mockForm,
|
|
fields: mockForm.fields.map((field) => ({
|
|
type: field.type,
|
|
identifier: field.identifier,
|
|
})),
|
|
},
|
|
creditCheckFn: expect.any(Function),
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Response Email", () => {
|
|
it("should send response email to team members for a team form", async () => {
|
|
const teamForm = {
|
|
...mockForm,
|
|
teamId: 1,
|
|
userWithEmails: ["team-member1@example.com", "team-member2@example.com"],
|
|
user: { id: 1, email: "test@example.com", timeFormat: 12, locale: "en" },
|
|
};
|
|
|
|
await _onFormSubmission(teamForm, mockResponse, responseId);
|
|
|
|
expect(mockResponseEmailConstructor).toHaveBeenCalledWith({
|
|
form: teamForm,
|
|
toAddresses: ["team-member1@example.com", "team-member2@example.com"],
|
|
orderedResponses: [mockResponse["field-1"], mockResponse["field-2"]],
|
|
});
|
|
expect(mockSendEmail).toHaveBeenCalled();
|
|
});
|
|
|
|
it("should send response email to owner when enabled", async () => {
|
|
const ownerForm = {
|
|
...mockForm,
|
|
settings: { emailOwnerOnSubmission: true },
|
|
};
|
|
|
|
await _onFormSubmission(ownerForm, mockResponse, responseId);
|
|
|
|
expect(mockResponseEmailConstructor).toHaveBeenCalledWith({
|
|
form: ownerForm,
|
|
toAddresses: [ownerForm.user.email],
|
|
orderedResponses: [mockResponse["field-1"], mockResponse["field-2"]],
|
|
});
|
|
expect(mockSendEmail).toHaveBeenCalled();
|
|
});
|
|
|
|
it("should not send response email to owner when disabled", async () => {
|
|
const ownerForm = {
|
|
...mockForm,
|
|
settings: { emailOwnerOnSubmission: false },
|
|
};
|
|
|
|
await _onFormSubmission(ownerForm, mockResponse, responseId);
|
|
|
|
expect(mockResponseEmailConstructor).not.toHaveBeenCalled();
|
|
expect(mockSendEmail).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|