Files
new-api-analytics/app/api/logs/route.test.ts

45 lines
1.3 KiB
TypeScript

import { describe, expect, mock, test } from "bun:test";
import type { NextRequest } from "next/server";
const getLogsMock = mock(async () => ({ logs: [], total: 0, page: 1, page_size: 100 }));
const getUserRankingMock = mock(async () => []);
const getModelRankingMock = mock(async () => []);
const getChannelRankingMock = mock(async () => []);
mock.module("@/lib/queries", () => ({
getLogs: getLogsMock,
getUserRanking: getUserRankingMock,
getModelRanking: getModelRankingMock,
getChannelRanking: getChannelRankingMock,
}));
const { GET } = await import("./route");
function request(path: string) {
return { nextUrl: new URL(`http://localhost${path}`) } as NextRequest;
}
describe("/api/logs", () => {
test("rejects reversed date ranges", async () => {
const res = await GET(request("/api/logs?start=200&end=100"));
expect(res.status).toBe(400);
expect(await res.json()).toEqual({
error: "Invalid query parameter",
field: "range",
});
});
test("clamps page size before querying", async () => {
getLogsMock.mockClear();
const res = await GET(request("/api/logs?page=2&page_size=10000"));
expect(res.status).toBe(200);
expect(getLogsMock.mock.calls[0][0]).toMatchObject({
page: 2,
pageSize: 200,
});
});
});