65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
import { NextRequest } from "next/server";
|
|
|
|
const getAssistantCounts = vi.fn();
|
|
vi.mock("@/lib/server/quote-assistant-service", () => ({
|
|
getAssistantCounts: (...a: unknown[]) => getAssistantCounts(...a),
|
|
}));
|
|
|
|
const getBrowserSessionUser = vi.fn();
|
|
vi.mock("@/lib/server/browser-session", () => ({
|
|
getBrowserSessionUser: (...a: unknown[]) => getBrowserSessionUser(...a),
|
|
}));
|
|
|
|
import { GET } from "./route";
|
|
|
|
function makeReq(qs = "") {
|
|
return new NextRequest(`http://localhost/api/v1/quotes/assistant/counts${qs}`, {
|
|
headers: { authorization: "Bearer tok" },
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
getBrowserSessionUser.mockResolvedValue({ id: "u1", user_type: null, name: "Sam" });
|
|
});
|
|
|
|
describe("GET /api/v1/quotes/assistant/counts", () => {
|
|
it("401s without a valid session", async () => {
|
|
getBrowserSessionUser.mockRejectedValue(
|
|
Object.assign(new Error("Not authenticated."), { status: 401 }),
|
|
);
|
|
const res = await GET(makeReq("?ids=a"));
|
|
expect(res.status).toBe(401);
|
|
expect(getAssistantCounts).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("400s when ids is missing or empty", async () => {
|
|
expect((await GET(makeReq())).status).toBe(400);
|
|
expect((await GET(makeReq("?ids="))).status).toBe(400);
|
|
expect((await GET(makeReq("?ids=,"))).status).toBe(400);
|
|
expect(getAssistantCounts).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("400s when more than 500 ids are passed", async () => {
|
|
const ids = Array.from({ length: 501 }, (_, i) => `id${i}`).join(",");
|
|
const res = await GET(makeReq(`?ids=${ids}`));
|
|
expect(res.status).toBe(400);
|
|
expect(getAssistantCounts).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes the split, trimmed ids to the service", async () => {
|
|
getAssistantCounts.mockResolvedValue({ a: { pending: 1, total: 3 } });
|
|
const res = await GET(makeReq("?ids=a,%20b,c"));
|
|
expect(getAssistantCounts).toHaveBeenCalledWith(["a", "b", "c"]);
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({ a: { pending: 1, total: 3 } });
|
|
});
|
|
|
|
it("accepts exactly 500 ids", async () => {
|
|
getAssistantCounts.mockResolvedValue({});
|
|
const ids = Array.from({ length: 500 }, (_, i) => `id${i}`).join(",");
|
|
const res = await GET(makeReq(`?ids=${ids}`));
|
|
expect(res.status).toBe(200);
|
|
});
|
|
});
|