GPT 6 PROMPT
Review pagination against a contract
Find correctness defects in a pagination helper before changing it.
Complete prompt
Task
Find correctness defects in a pagination helper before changing it.
Instructions
Review [PAGINATION HELPER] against this contract: [INPUT SHAPE], [FILTER RULE], [PAGE INDEX RULE], and [OUTPUT SHAPE]. Reproduce each user-visible mismatch with a small input and expected versus actual result. Check validation, filter/slice order, total and totalPages, empty matches, and pages past the end. Cite the relevant code line. Then propose the smallest fix and tests; keep claims tied to runs you actually performed.
Expected output
Located findings, tiny reproductions, focused patch, and observed test results.
Checks
- A filtered first page is filled from all matching items.
- total and totalPages count matches, not the source array.
- Invalid page or pageSize values follow the stated contract.Expected output and checks
Expected output
Located findings, tiny reproductions, focused patch, and observed test results.
Checks
- A filtered first page is filled from all matching items.
- total and totalPages count matches, not the source array.
- Invalid page or pageSize values follow the stated contract.
Worked example
Worked example: Review pagination against a contract
Original input
Review the attached original.js against this contract: an array of {id,status} records and {page=1,pageSize=10,status} options, status, when supplied, matches item.status exactly; filter before slicing, page and pageSize are positive integers; invalid values throw RangeError, and {items,page,pageSize,total,totalPages}; counts describe matching records. Reproduce each user-visible mismatch with a small input and expected versus actual result. Check validation, filter/slice order, total and totalPages, empty matches, and pages past the end. Cite the relevant code line. Then propose the smallest fix and tests; keep claims tied to runs you actually performed.
Attached original.js:
```js
// Synthetic review fixture. Intentionally defective; do not use in production.
function paginate(items, { page = 1, pageSize = 10, status } = {}) {
const start = (page - 1) * pageSize;
const pageItems = items.slice(start, start + pageSize);
const visibleItems = status
? pageItems.filter((item) => item.status === status)
: pageItems;
return {
items: visibleItems,
page,
pageSize,
total: items.length,
totalPages: Math.ceil(items.length / pageSize),
};
}
module.exports = { paginate };
```
Additional contract: page beyond the end returns an empty array; do not mutate input.What the checks found
- Located four correctness defects with concrete values and source lines.
- Used direct observed values and the valid 1/6 versus 6/6 test run.
- No unrelated code was changed.
Original output
## Findings
1. `original.js:3-4` accepts `page: 0`. Observed with five records and `pageSize: 2`: `{items: [], page: 0, total: 5, totalPages: 3}`. The contract requires `RangeError`; `corrected.js:2-4` validates before slicing.
2. `original.js:3,13` accepts `pageSize: 0`; the raw `totalPages` is `Infinity` (`JSON.stringify` displays it as `null`). `corrected.js:5-7` rejects it with `RangeError`.
3. `original.js:4-7` slices before filtering. With statuses `open, closed, open, closed, open`, page 1 of size 2 for `open` returns only ID 1. Filtering before slicing returns IDs 1 and 3.
4. `original.js:12-13` reports unfiltered counts: `total: 5`, `totalPages: 3` instead of `3` and `2` for that query. `status ?` also ignores a supplied empty string. `corrected.js:9-20` uses `status === undefined`, filters first, and derives both counts from matches.
## Patch and checks
The focused patch is `corrected.js`, with boundary tests in `pagination.test.js`. Direct calls are recorded in `review-observations.txt`. On the valid Node v26.3.0 test run, the original exited 1 (1 pass, 5 fail); the correction exited 0 (6 pass, 0 fail). The tests also cover no matches, a page beyond the end, and unchanged input.Files from this run