Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ async function api(req: Request, env: Env, path: string[]): Promise<Response> {
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) {
Expand Down
9 changes: 9 additions & 0 deletions api/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down