diff --git a/api/src/index.ts b/api/src/index.ts index 49142fc..5777d87 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -50,7 +50,8 @@ async function api(req: Request, env: Env, path: string[]): Promise { const route = `${req.method} ${path.join("/")}`; if (route === "GET challenges") { - const limit = Math.min(100, Math.max(1, Number(new URL(req.url).searchParams.get("limit")) || 20)); + // SQLite refuses a fractional LIMIT, so take the whole part after clamping. + const limit = Math.floor(Math.min(100, Math.max(1, Number(new URL(req.url).searchParams.get("limit")) || 20))); return json({ challenges: await recent(env.DB, limit) }); } if (req.method === "GET" && path[0] === "challenges" && path.length === 2) { diff --git a/api/test/api.test.ts b/api/test/api.test.ts index e047574..d9ab339 100644 --- a/api/test/api.test.ts +++ b/api/test/api.test.ts @@ -49,6 +49,15 @@ describe("challenges", () => { expect(jobs?.n).toBe(1); }); + it("lists the whole part of a fractional limit", async () => { + for (let i = 0; i < 3; i++) expect((await challenge(uuid())).status).toBe(201); + for (const [limit, count] of [["2.5", 2], ["1.5", 1]] as const) { + const r = await get(`challenges?limit=${limit}`); + expect(r.status).toBe(200); + expect((await r.json<{ challenges: unknown[] }>()).challenges).toHaveLength(count); + } + }); + it("holds 10 challenges per key per hour", async () => { const key = "one-busy-holder-key"; for (let i = 0; i < 10; i++) expect((await challenge(uuid(), key)).status).toBe(201);