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