* init * wiring up * fix type * feat: implement DI pattern for webhook producer in API v2 - Export IWebhookProducerService and getWebhookProducer from platform-libraries - Add WEBHOOK_PRODUCER token and useFactory provider in RegularBookingModule - Inject webhookProducer in RegularBookingService and pass to base class This follows the composition root pattern where only the NestJS module knows about getWebhookProducer(), and all consumers depend only on the IWebhookProducerService interface via constructor injection. Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com> * test: migrate BOOKING_REQUESTED tests to new webhook architecture - Remove failing BOOKING_REQUESTED tests from fresh-booking.test.ts (4 tests) - Remove failing BOOKING_REQUESTED tests from reschedule.test.ts (2 tests) - Remove failing BOOKING_REQUESTED test from collective-scheduling.test.ts (1 test) - Replace WebhookTaskConsumer.test.ts with placeholder (constructor changed) - Create new webhook architecture test suite: - producer/WebhookTaskerProducerService.test.ts (14 tests) - consumer/WebhookTaskConsumer.test.ts (8 tests) - consumer/triggers/booking-requested.test.ts (8 tests) The new test suite is organized by trigger type for extensibility as more triggers are migrated to the producer/consumer pattern. Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com> * test: remove paid events BOOKING_REQUESTED test (moved to new architecture) Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com> * wrap webhook in own try-catch * wire datafetcher * fix * fix v2 * fix circular dependency * -- * merge-conflict-resolve * mreg-conflict-resolve * remove early return * test: add integration tests for BOOKING_REQUESTED webhook producer invocation Cover all 8 scenarios verifying the booking flow correctly invokes the webhook producer for BOOKING_REQUESTED: 1. Basic confirmation → queueBookingRequestedWebhook called 2. Booker-is-organizer + confirmation → still called 3. Confirmation threshold NOT met → not called (BOOKING_CREATED instead) 4. Confirmation threshold IS met → called 5. Paid event + confirmation → called after payment succeeds 6. Reschedule + confirmation (non-organizer) → called (not BOOKING_RESCHEDULED) 7. Reschedule + confirmation (organizer) → not called (BOOKING_RESCHEDULED instead) 8. Collective scheduling + confirmation → called Adds reusable MockWebhookProducer helper in @calcom/testing for extendable use as more webhook triggers migrate to the new architecture. Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com> * fix bug * fix conditional check * remove unnecessary comment * add missing expect * remove empty test * clean up * tasker config * -- * fix missing metadata * remove faulty if else * test: add payload content verification tests for BOOKING_REQUESTED webhook Co-Authored-By: ali@cal.com <alishahbaz7@gmail.com> * remove unnecessary tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Hariom Balhara <1780212+hariombalhara@users.noreply.github.com>
235 lines
7.3 KiB
TypeScript
235 lines
7.3 KiB
TypeScript
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import type { ILogger } from "../../interface/infrastructure";
|
|
import type { WebhookTasker } from "../../tasker/WebhookTasker";
|
|
import { WebhookTaskerProducerService } from "../../service/WebhookTaskerProducerService";
|
|
|
|
/**
|
|
* Unit Tests for WebhookTaskerProducerService
|
|
*
|
|
* Tests the lightweight Producer service for queueing webhook delivery tasks.
|
|
* The producer has minimal dependencies (only Tasker and Logger) and queues
|
|
* tasks to be processed by WebhookTaskConsumer.
|
|
*/
|
|
describe("WebhookTaskerProducerService", () => {
|
|
let producer: WebhookTaskerProducerService;
|
|
let mockWebhookTasker: WebhookTasker;
|
|
let mockLogger: ILogger;
|
|
|
|
beforeEach(() => {
|
|
mockWebhookTasker = {
|
|
deliverWebhook: vi.fn().mockResolvedValue({ taskId: "mock-task-id" }),
|
|
} as unknown as WebhookTasker;
|
|
|
|
mockLogger = {
|
|
debug: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
getSubLogger: vi.fn().mockReturnThis(),
|
|
} as unknown as ILogger;
|
|
|
|
producer = new WebhookTaskerProducerService({
|
|
webhookTasker: mockWebhookTasker,
|
|
logger: mockLogger,
|
|
});
|
|
});
|
|
|
|
describe("Constructor & Dependencies", () => {
|
|
it("should be instantiable with Tasker and Logger", () => {
|
|
expect(producer).toBeInstanceOf(WebhookTaskerProducerService);
|
|
});
|
|
|
|
it("should create sub-logger with prefix", () => {
|
|
expect(mockLogger.getSubLogger).toHaveBeenCalledWith({
|
|
prefix: ["[WebhookTaskerProducerService]"],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("queueBookingRequestedWebhook", () => {
|
|
it("should queue a BOOKING_REQUESTED webhook task with required params", async () => {
|
|
const params = {
|
|
bookingUid: "booking-123",
|
|
eventTypeId: 456,
|
|
userId: 789,
|
|
};
|
|
|
|
await producer.queueBookingRequestedWebhook(params);
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_REQUESTED,
|
|
bookingUid: "booking-123",
|
|
eventTypeId: 456,
|
|
userId: 789,
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should queue a BOOKING_REQUESTED webhook task with all optional params", async () => {
|
|
const params = {
|
|
bookingUid: "booking-123",
|
|
eventTypeId: 456,
|
|
userId: 789,
|
|
teamId: 111,
|
|
orgId: 222,
|
|
oAuthClientId: "oauth-client-123",
|
|
operationId: "custom-op-id",
|
|
metadata: { customKey: "customValue" },
|
|
};
|
|
|
|
await producer.queueBookingRequestedWebhook(params);
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
operationId: "custom-op-id",
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_REQUESTED,
|
|
bookingUid: "booking-123",
|
|
eventTypeId: 456,
|
|
userId: 789,
|
|
teamId: 111,
|
|
orgId: 222,
|
|
oAuthClientId: "oauth-client-123",
|
|
metadata: { customKey: "customValue" },
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should generate operationId if not provided", async () => {
|
|
const params = {
|
|
bookingUid: "booking-123",
|
|
};
|
|
|
|
await producer.queueBookingRequestedWebhook(params);
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
operationId: expect.any(String),
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_REQUESTED,
|
|
bookingUid: "booking-123",
|
|
timestamp: expect.any(String),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should log debug message when queueing", async () => {
|
|
const params = {
|
|
bookingUid: "booking-123",
|
|
eventTypeId: 456,
|
|
};
|
|
|
|
await producer.queueBookingRequestedWebhook(params);
|
|
|
|
expect(mockLogger.debug).toHaveBeenCalledWith(
|
|
"Queueing booking webhook task",
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_REQUESTED,
|
|
bookingUid: "booking-123",
|
|
eventTypeId: 456,
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should log and rethrow error if tasker fails", async () => {
|
|
const error = new Error("Tasker error");
|
|
vi.mocked(mockWebhookTasker.deliverWebhook).mockRejectedValueOnce(error);
|
|
|
|
const params = {
|
|
bookingUid: "booking-123",
|
|
};
|
|
|
|
await expect(producer.queueBookingRequestedWebhook(params)).rejects.toThrow("Tasker error");
|
|
|
|
expect(mockLogger.error).toHaveBeenCalledWith(
|
|
"Failed to queue webhook delivery task",
|
|
expect.objectContaining({
|
|
error: "Tasker error",
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Other booking webhook methods", () => {
|
|
it("should queue BOOKING_CREATED webhook", async () => {
|
|
await producer.queueBookingCreatedWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_CREATED,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should queue BOOKING_CANCELLED webhook", async () => {
|
|
await producer.queueBookingCancelledWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_CANCELLED,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should queue BOOKING_RESCHEDULED webhook", async () => {
|
|
await producer.queueBookingRescheduledWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_RESCHEDULED,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should queue BOOKING_REJECTED webhook", async () => {
|
|
await producer.queueBookingRejectedWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_REJECTED,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should queue BOOKING_NO_SHOW_UPDATED webhook", async () => {
|
|
await producer.queueBookingNoShowUpdatedWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_NO_SHOW_UPDATED,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Payment webhook methods", () => {
|
|
it("should queue BOOKING_PAYMENT_INITIATED webhook", async () => {
|
|
await producer.queueBookingPaymentInitiatedWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_PAYMENT_INITIATED,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("should queue BOOKING_PAID webhook", async () => {
|
|
await producer.queueBookingPaidWebhook({ bookingUid: "booking-123" });
|
|
|
|
expect(mockWebhookTasker.deliverWebhook).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_PAID,
|
|
bookingUid: "booking-123",
|
|
})
|
|
);
|
|
});
|
|
});
|
|
});
|