Files
calendar/packages/lib/server/defaultResponder.test.ts
T
1ff5d57ded feat: distributed tracing - 2 (#24861)
* feat: distributed tracing 2

* feat: distributed tracing 2

* refactor: feedback

* refactor: feedback

* fix: type error

* fix: trpc error

---------

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
2025-11-13 11:51:54 +00:00

57 lines
1.9 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from "next";
import { describe, expect, it, vi } from "vitest";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { TRPCError } from "@trpc/server";
import { defaultResponder } from "./defaultResponder";
describe("defaultResponder", () => {
it("should call res.json when response is still writable and result is not null", async () => {
const f = vi.fn().mockResolvedValue({});
const req = {} as NextApiRequest;
const res = {
json: vi.fn(),
writableEnded: false,
setHeader: vi.fn(),
} as unknown as NextApiResponse;
await defaultResponder(f)(req, res);
expect(res.json).toHaveBeenCalled();
});
it("should not call res.json when response is not writable", async () => {
const f = vi.fn().mockResolvedValue({});
const req = {} as NextApiRequest;
const res = {
json: vi.fn(),
writableEnded: true,
setHeader: vi.fn(),
} as unknown as NextApiResponse;
await defaultResponder(f)(req, res);
expect(res.json).not.toHaveBeenCalled();
});
it("should respond with status code 409 for NoAvailableUsersFound", async () => {
const f = vi.fn().mockRejectedValue(new Error(ErrorCode.NoAvailableUsersFound));
const req = {} as NextApiRequest;
const res = {
status: vi.fn().mockReturnThis(),
json: vi.fn(),
setHeader: vi.fn(),
} as unknown as NextApiResponse;
await defaultResponder(f)(req, res);
expect(res.status).toHaveBeenCalledWith(409);
});
it("Rate limit should respond with a 429 status code", async () => {
const f = vi.fn().mockRejectedValue(new TRPCError({ code: "TOO_MANY_REQUESTS" }));
const req = {} as NextApiRequest;
const res = {
status: vi.fn().mockReturnThis(),
json: vi.fn(),
setHeader: vi.fn(),
} as unknown as NextApiResponse;
await defaultResponder(f)(req, res);
expect(res.status).toHaveBeenCalledWith(429);
});
});