Skip to content

fix(paginate): break loop on empty items with non-null nextToken#48

Open
aldorizona10-glitch wants to merge 2 commits into
TestSprite:mainfrom
aldorizona10-glitch:fix/paginate-empty-items-infinite-loop
Open

fix(paginate): break loop on empty items with non-null nextToken#48
aldorizona10-glitch wants to merge 2 commits into
TestSprite:mainfrom
aldorizona10-glitch:fix/paginate-empty-items-infinite-loop

Conversation

@aldorizona10-glitch

Copy link
Copy Markdown

Summary

Fixes an infinite loop in paginate() (issue #35) where the CLI hangs indefinitely when a filtered list endpoint returns { items: [], nextToken: 'cursor' }.

Problem

paginate() in src/lib/pagination.ts checked nextToken !== null to decide whether to keep looping, but never checked whether items was actually empty. Without --max-items set, the loop had no way to stop for this edge case, causing testsprite test list --project <id> with a zero-result filter to hang forever.

Fix

Added an early-exit guard after processing each page:

// Guard against infinite loop when API returns empty items with non-null nextToken.
if (page.items.length === 0) break;

This breaks the loop when the API returns zero items, regardless of what nextToken says. The nextToken from the server is still returned to the caller for potential resume.

Changes

  • src/lib/pagination.ts: Added empty-items guard in paginate() loop
  • test/pagination.test.ts: New file with 5 unit tests:
    • Collects all items across pages until nextToken is null
    • Breaks out of loop on empty items with non-null nextToken (the bug repro)
    • Respects maxItems cap
    • Handles single page with null nextToken
    • Handles consecutive empty pages then data

Testing

  • npm run typecheck — clean
  • npm run lint — clean
  • npm run format:check — all files pass Prettier
  • npm test — all existing tests + 5 new tests pass

Closes #35

Signed-off-by: Aldo Rizona aldorizona10@gmail.com

paginate() in pagination.ts entered an infinite loop when the API
returned { items: [], nextToken: 'cursor' }. The loop only checked
nextToken !== null, never whether items was empty. Any filtered list
command returning zero results would hang indefinitely.

Add an early-exit guard: if the page contains zero items, break
regardless of nextToken. Includes 5 unit tests covering the bug
reproduction, maxItems cap, single-page, and consecutive empty pages.

Closes TestSprite#35

Signed-off-by: Aldo Rizona <aldorizona10@gmail.com>
@aldorizona10-glitch

Copy link
Copy Markdown
Author

Hi maintainers 👋

First-time contributor here — CI runs are awaiting approval ().

Quick summary of this PR:

Coverage impact: this PR adds tests to src/lib/pagination.ts (previously had 14 tests, now 19). The 2 lines added are fully covered. Global coverage should remain ≥80%.

Happy to make any changes if needed. Thanks for reviewing!

…ck gate

Pure mechanical prettier reflow of two vi.fn(async () => ({...})) callbacks in
test/pagination.test.ts. No logic change — only line wrapping to satisfy the
format:check CI gate (npm run format:check). All other gates already pass.
Coverage remains >=80% on all 4 metrics (lines/statements/functions/branches).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Infinite loop in paginate() when API returns empty items with non-null nextToken

1 participant