feat: harden analytics dashboard
This commit is contained in:
44
app/api/logs/route.test.ts
Normal file
44
app/api/logs/route.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user