[codex] Clamp popular tag limits#382
Conversation
Greptile SummaryThis PR refactors the popular-tags limit parsing to use the shared
Confidence Score: 4/5The route change itself is correct and safe to merge; the only concern is in the new test file. The malformed-limit test uses a mock dataset too small to distinguish the old partial-parse result (7) from the new default (50). This means the test passes even if the old buggy path is restored, leaving the 'malformed numeric strings fall back to default' guarantee unverified. Everything else — the helper delegation, zero clamping, and zero-limit test — is solid. src/app/api/tags/popular/route.test.ts — the malformed-limit test needs a larger mock dataset to be meaningful. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[GET /api/tags/popular] --> B[searchParams.get limit]
B --> C{parsePaginationParam}
C -- null or empty --> D[default = 50]
C -- NaN / non-finite --> D
C -- valid number --> E[Math.trunc, clamp 1..200]
D --> F[limit = 50]
E --> G[limit = clamped value]
F --> H[Fetch active gigs]
G --> H
H --> I[Build gigCountMap]
I --> J[Fetch tag_follows]
J --> K[Build followerCountMap]
K --> L[Merge allTags Set]
L --> M[Sort by follower_count desc, gig_count desc]
M --> N[slice 0 .. limit]
N --> O[NextResponse.json tags]
Reviews (1): Last reviewed commit: "Clamp popular tag limits" | Re-trigger Greptile |
| expect(json.tags).toHaveLength(1); | ||
| expect(json.tags[0].tag).toBe("API"); | ||
| }); | ||
|
|
||
| it("uses the default limit for malformed numeric strings", async () => { | ||
| mockPopularTagsData(); | ||
|
|
||
| const response = await GET(makeRequest({ limit: "7px" })); |
There was a problem hiding this comment.
Malformed-limit test doesn't distinguish old from new behavior
The mock supplies only 4 distinct tags, but the key difference between the old code (parseInt("7px", 10) → 7) and the new code (Number("7px") → NaN → default 50) only becomes observable when there are more than 7 tags. With 4 tags, slice(0, 7) and slice(0, 50) both return the same 4 elements, so expect(json.tags).toHaveLength(4) passes regardless of which implementation is active. Reverting parsePaginationParam to the old ad-hoc expression would leave this test green while silently reintroducing the bug. The mock data needs at least 8 unique tags to make the assertion meaningful.
Fixes #360.
Summary:
parsePaginationParamhelperlimit=0clamp to the documented minimum of 1 instead of expanding to the defaultValidation: