Files
calendar/packages/lib/server/getServerErrorFromUnknown.test.ts
T
Carina WollendorferGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>CarinaWolli
45698db0b4 fix: validate Round Robin host availability in handleNewBooking (#22253)
* fix: validate Round Robin host availability in handleNewBooking

- Add validation to ensure Round Robin events have at least one available non-fixed host
- Throw NoAvailableUsersFound error when no Round Robin hosts are available
- Add unit test to verify the fix works correctly
- Fixes issue where Round Robin events with fixed hosts could be booked without Round Robin hosts

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* test: fix host configuration in Round Robin test

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* test: improve Round Robin test to only make RR host busy

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* fix: correct Round Robin validation to only check when RR hosts assigned

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* fix: refine Round Robin validation to only apply when both fixed and RR hosts present

- Add fixedUserPool.length > 0 condition to validation
- Ensures validation only triggers for specific bug scenario: events with fixed hosts + RR hosts but no available RR hosts
- Prevents blocking normal Round Robin events that only have RR hosts
- Updates test description to reflect more precise validation logic

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* fix: simplify Round Robin validation to resolve E2E test failures

Remove overly restrictive fixedUserPool.length > 0 condition that was
blocking valid Round Robin booking scenarios in E2E tests. The validation
now only checks if Round Robin hosts are assigned but none are available,
which is the intended behavior for the bug fix.

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* fix: restore fixedUserPool condition to Round Robin validation

- Add back fixedUserPool.length > 0 condition to make validation specific to bug scenario
- Only triggers when Round Robin events have both fixed hosts AND Round Robin hosts but no available Round Robin hosts
- Prevents blocking normal Round Robin events that only have Round Robin hosts (like in organization settings E2E test)
- Updates test description to reflect more precise validation logic

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

Co-Authored-By: carina@cal.com <c.wollendorfer@me.com>

* throw error if no RR user is available

* add tests

* fix comments

* revert change

* remove comments

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: CarinaWolli <wollencarina@gmail.com>
2025-07-11 16:47:21 +01:00

139 lines
4.6 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { ErrorWithCode } from "@calcom/lib/errors";
import { HttpError } from "../http-error";
import { getServerErrorFromUnknown } from "./getServerErrorFromUnknown";
const test400Codes = [
ErrorCode.RequestBodyWithouEnd,
ErrorCode.MissingPaymentCredential,
ErrorCode.MissingPaymentAppId,
ErrorCode.AvailabilityNotFoundInSchedule,
ErrorCode.CancelledBookingsCannotBeRescheduled,
ErrorCode.BookingTimeOutOfBounds,
ErrorCode.BookingNotAllowedByRestrictionSchedule,
ErrorCode.BookerLimitExceeded,
ErrorCode.BookerLimitExceededReschedule,
];
const test404Codes = [
ErrorCode.EventTypeNotFound,
ErrorCode.BookingNotFound,
ErrorCode.RestrictionScheduleNotFound,
];
const test409Codes = [
ErrorCode.NoAvailableUsersFound,
ErrorCode.FixedHostsUnavailableForBooking,
ErrorCode.RoundRobinHostsUnavailableForBooking,
ErrorCode.AlreadySignedUpForBooking,
ErrorCode.BookingSeatsFull,
ErrorCode.NotEnoughAvailableSeats,
ErrorCode.BookingConflict,
ErrorCode.PaymentCreationFailure,
ErrorCode.ChargeCardFailure,
];
describe("getServerErrorFromUnknown", () => {
test("should handle a StripeInvalidRequestError", () => {
const stripeError = {
name: "StripeInvalidRequestError",
type: "StripeInvalidRequestError",
rawType: "invalid_request_error",
message: "No such customer: cus_12345; a valid customer ID is required.",
code: "resource_missing",
doc_url: "https://stripe.com/docs/error-codes/resource-missing",
statusCode: 400,
raw: {
error: { type: "invalid_request_error", message: "No such customer" },
},
headers: {
"content-type": "application/json",
"request-id": "req_abc123xyz",
},
requestId: "req_abc123xyz",
param: "customer",
};
Object.setPrototypeOf(stripeError, Error.prototype);
const result = getServerErrorFromUnknown(stripeError);
expect(result).toBeInstanceOf(HttpError);
expect(result.statusCode).toBe(400);
expect(result.message).toBe(stripeError.message);
expect(result.cause).toEqual(stripeError);
expect(result.name).toBe("HttpError");
});
test("ErrorWithCode should preserve data property", () => {
const rescheduleData = {
rescheduleUid: "abc123",
startTime: new Date("2024-01-15T10:00:00Z"),
attendees: [{ name: "John Doe", email: "john@example.com" }],
};
const error = new ErrorWithCode(
ErrorCode.BookerLimitExceededReschedule,
"Maximum booking limit reached, please reschedule an existing booking",
rescheduleData
);
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(400);
expect(result.message).toBe("Maximum booking limit reached, please reschedule an existing booking");
expect(result.data).toEqual(rescheduleData);
});
test("Plain Error with ErrorCode message should work the same as ErrorWithCode", () => {
const error = new Error(ErrorCode.NoAvailableUsersFound);
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(409);
expect(result.message).toBe(ErrorCode.NoAvailableUsersFound);
});
test("Generic Error without ErrorCode should return 500", () => {
const error = new Error("Some unexpected server error");
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(500);
expect(result.message).toBe("Some unexpected server error");
});
test("Server-side ErrorCode should return 500", () => {
const error = new ErrorWithCode(ErrorCode.CreatingOauthClientError, "OAuth client creation failed");
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(500);
expect(result.message).toBe("OAuth client creation failed");
});
});
test400Codes.forEach((errorCode) => {
test(`${errorCode} should return 400 Bad Request`, () => {
const error = new ErrorWithCode(errorCode, `Test message for ${errorCode}`);
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(400);
});
});
test404Codes.forEach((errorCode) => {
test(`${errorCode} should return 404 Not Found`, () => {
const error = new ErrorWithCode(errorCode, `Test message for ${errorCode}`);
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(404);
});
});
test409Codes.forEach((errorCode) => {
test(`${errorCode} should return 409 Conflict`, () => {
const error = new ErrorWithCode(errorCode, `Test message for ${errorCode}`);
const result = getServerErrorFromUnknown(error);
expect(result.statusCode).toBe(409);
});
});