Skip to content

test(scripts): add unit tests for retry/rate-limit utils + fix Jest config under ESM#9561

Open
ash-balla wants to merge 1 commit into
kirodotdev:mainfrom
ash-balla:test/add-utils-unit-tests
Open

test(scripts): add unit tests for retry/rate-limit utils + fix Jest config under ESM#9561
ash-balla wants to merge 1 commit into
kirodotdev:mainfrom
ash-balla:test/add-utils-unit-tests

Conversation

@ash-balla

Copy link
Copy Markdown

Summary

Adds unit tests for two previously-untested utility modules in scripts/, and fixes the Jest configuration so the test runner actually works.

While adding tests I found that the existing Jest setup could not run at all: jest.config.js used CommonJS module.exports, but scripts/package.json declares "type": "module", so Node parsed the config as ESM and threw:

ReferenceError: module is not defined in ES module scope

This prevented every Jest test (including the existing test/data_models.test.ts) from executing — consistent with the "Unit Tests (To be implemented)" note in scripts/README.md. Renaming the config to jest.config.cjs resolves it (Jest auto-discovers .cjs).

Changes

  • Rename scripts/jest.config.jsscripts/jest.config.cjs (same contents) so the config loads as CommonJS under "type": "module".
  • Add scripts/test/retry_utils.test.ts — 13 tests for retryWithBackoff:
    • returns immediately on first-attempt success
    • retries on a retryable error then resolves
    • throws immediately on non-retryable errors (no retry)
    • exhausts retries (default 3 → 4 attempts; custom maxRetries; returns the last error)
    • detects retryable errors via error.code, error.status, error.name, and message text
    • honors a custom retryableErrors list (and overrides defaults)
    • verifies exponential backoff with maxDelay capping (delays [100, 200, 400, 500])
  • Add scripts/test/rate_limit_utils.test.ts — 8 tests:
    • processBatch: preserves order across batches, handles empty input, handles an oversized batch size, and runs items within a batch in parallel
    • checkRateLimit: queries the API once per 10 calls, pauses when remaining is below threshold, does not pause when above, and tolerates a failing rate-limit lookup without throwing

Test Results

All suites pass:

Test Suites: 3 passed, 3 total
Tests:       34 passed, 34 total

Jest exit code 0.

Notes

  • Tests follow the existing test/data_models.test.ts conventions (TypeScript, describe/it, imports without .js extension).
  • No production/runtime code was changed — this PR only adds tests and fixes the test-runner config.

@ash-balla ash-balla requested review from a team as code owners June 18, 2026 16:41
…t config under ESM

- Add test/retry_utils.test.ts covering retryWithBackoff: success, retry-then-succeed,
  non-retryable errors, retry exhaustion, retryable detection (code/status/name/message),
  custom retryableErrors, and exponential backoff capping.
- Add test/rate_limit_utils.test.ts covering processBatch (ordering, empty input,
  oversized batch, in-batch parallelism) and checkRateLimit (10-call cadence, pause
  below threshold, no pause above, error tolerance).
- Rename jest.config.js to jest.config.cjs so the config loads correctly given
  package.json "type": "module" (previously threw "module is not defined in ES module
  scope", which prevented any jest tests from running).

All 3 suites pass: 34 tests, jest exit code 0.
@ash-balla ash-balla force-pushed the test/add-utils-unit-tests branch from dd9a608 to f61dc06 Compare June 18, 2026 16:46
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.

1 participant