Skip to content

🧪 test: Add unit tests for handleRateLimit in apiUtils - #514

Closed
is0692vs wants to merge 1 commit into
mainfrom
test/api-utils-rate-limit-13734497713501379076
Closed

🧪 test: Add unit tests for handleRateLimit in apiUtils#514
is0692vs wants to merge 1 commit into
mainfrom
test/api-utils-rate-limit-13734497713501379076

Conversation

@is0692vs

@is0692vs is0692vs commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

🎯 What: Added missing unit tests for the handleRateLimit function in src/lib/apiUtils.ts.
📊 Coverage: Covered scenarios include a valid X-RateLimit-Reset header, missing header, and invalid header formats, using fake timers for deterministic testing.
Result: Improved test coverage and reliability for rate limit handling logic.


PR created automatically by Jules for task 13734497713501379076 started by @is0692vs

Greptile Summary

handleRateLimit に対し、有効なリセット時刻、ヘッダー欠落、不正値の3ケースを検証する単体テストを追加しています。

  • fake timer で現在時刻を固定し、フォールバック時刻を決定的に検証
  • RateLimitError の型と resetAt を確認
  • catch 変数の型ナローイング不足により、strict 型検査が失敗

Confidence Score: 4/5

catch 変数を適切に型ナローイングしない限り型検査が失敗するため、マージ前に修正が必要です。

追加された3つのテストはいずれも、strict モードで unknown となる catch 変数から resetAt を直接参照しており、テスト実行自体が成功しても TypeScript 型検査または Next.js ビルドを通過できません。

Files Needing Attention: src/lib/tests/apiUtils.test.ts

Important Files Changed

Filename Overview
src/lib/tests/apiUtils.test.ts handleRateLimit の主要3ケースを追加しているが、3か所で unknown 型の catch 変数に直接アクセスしており、strict 型検査に失敗する。
Prompt To Fix All With AI
### Issue 1
src/lib/__tests__/apiUtils.test.ts:124
**catch 変数の型ナローイング不足**

`strict` モードでは catch 変数が `unknown` になる一方、`toBeInstanceOf` は後続文の型ガードとして機能しないため、この行を含む3か所の `error.resetAt` 参照で TypeScript の型検査または Next.js ビルドが失敗します。`instanceof RateLimitError` でナローイングするか、既存テストと同様に明示的な型アサーションを使用してください。

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "test: Add unit tests for handleRateLimit..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Context used:

Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel

vercel Bot commented Aug 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
github-user-summary Ignored Ignored Aug 1, 2026 4:25am

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@dosubot

dosubot Bot commented Aug 1, 2026

Copy link
Copy Markdown

📄 Knowledge review

Dosu skipped reviewing this PR because your organization has used its 200 included credits for the month. Your usage will reset on 2026-09-01. To have Dosu review this PR before then, ask your organization admin to upgrade to a pro account.


Leave Feedback Ask Dosu about github-user-summary Add Dosu to your team

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@is0692vs, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 22c81001-df89-4ef8-ad3e-2951abf415ad

📥 Commits

Reviewing files that changed from the base of the PR and between e914034 and 3a68854.

📒 Files selected for processing (1)
  • src/lib/__tests__/apiUtils.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@is0692vs

is0692vs commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of the older #505, which covers the same handleRateLimit valid, missing, and invalid reset-header scenarios. We will continue the review loop on #505.

@is0692vs is0692vs closed this Aug 1, 2026
@is0692vs
is0692vs deleted the test/api-utils-rate-limit-13734497713501379076 branch August 1, 2026 04:26
@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Add unit tests for apiUtils.handleRateLimit

🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Add deterministic unit coverage for apiUtils.handleRateLimit using fake timers.
• Validate X-RateLimit-Reset parsing and fallback behavior for missing/invalid headers.
• Assert thrown RateLimitError includes the expected resetAt timestamp.
Diagram

graph TD
  A["apiUtils.test.ts (Vitest)"] --> B["handleRateLimit(res)"] --> C["throws RateLimitError"]
  B --> D["reads X-RateLimit-Reset"]
Loading
High-Level Assessment

The PR’s approach is appropriate: it adds deterministic, scenario-based unit tests (valid header / missing header / invalid header) without changing runtime behavior.

Files changed (1) +60 / -2

Tests (1) +60 / -2
apiUtils.test.tsAdd handleRateLimit unit tests with fake timers +60/-2

Add handleRateLimit unit tests with fake timers

• Imports handleRateLimit and RateLimitError, and adds a new describe block covering valid, missing, and invalid X-RateLimit-Reset header scenarios. Uses Vitest fake timers + fixed system time to make resetAt assertions deterministic.

src/lib/tests/apiUtils.test.ts

@google-labs-jules

Copy link
Copy Markdown
Contributor

Closing as a duplicate of the older #505, which covers the same handleRateLimit valid, missing, and invalid reset-header scenarios. We will continue the review loop on #505.

Understood. Acknowledging that this work is a duplicate of #505 and stopping work on this task.

handleRateLimit(res);
} catch (error) {
expect(error).toBeInstanceOf(RateLimitError);
expect(error.resetAt.getTime()).toBe(1700003600000);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 catch 変数の型ナローイング不足

strict モードでは catch 変数が unknown になる一方、toBeInstanceOf は後続文の型ガードとして機能しないため、この行を含む3か所の error.resetAt 参照で TypeScript の型検査または Next.js ビルドが失敗します。instanceof RateLimitError でナローイングするか、既存テストと同様に明示的な型アサーションを使用してください。

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/lib/__tests__/apiUtils.test.ts
Line: 124

Comment:
**catch 変数の型ナローイング不足**

`strict` モードでは catch 変数が `unknown` になる一方、`toBeInstanceOf` は後続文の型ガードとして機能しないため、この行を含む3か所の `error.resetAt` 参照で TypeScript の型検査または Next.js ビルドが失敗します。`instanceof RateLimitError` でナローイングするか、既存テストと同様に明示的な型アサーションを使用してください。

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (1) 📜 Skill insights (0)

Context used
✅ Compliance rules (platform): 30 rules

Grey Divider


Remediation recommended

1. Relative imports in apiUtils.test.ts 📘 Rule violation ✧ Quality
Description
The test uses relative imports (../types, ../apiUtils) for modules under src/ instead of the
required @/ path alias, which breaks the project’s import convention and can cause brittle pathing
as files move.
Code

src/lib/tests/apiUtils.test.ts[R2-3]

+import { RateLimitError } from '../types';
+import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils';
Evidence
PR Compliance ID 226103 requires imports targeting code under src/ to use the @/ alias. The
changed imports in src/lib/__tests__/apiUtils.test.ts use ../types and ../apiUtils, both of
which resolve to src/lib/* and therefore violate the rule.

Rule 226103: Use @/ path alias for src imports in frontend code
src/lib/tests/apiUtils.test.ts[1-3]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
Tests under `src/` are importing other `src/` modules via relative paths (e.g., `../apiUtils`, `../types`) instead of the required `@/` alias.

## Issue Context
`tsconfig.json` defines the `@/*` path mapping to `./src/*`, so `@/` imports are supported and should be used for `src`-internal imports.

## Fix Focus Areas
- src/lib/__tests__/apiUtils.test.ts[1-3]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

To customize comments, go to the Qodo configuration screen, or learn more in the docs.

Qodo Logo

Comment on lines +2 to +3
import { RateLimitError } from '../types';
import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

1. Relative imports in apiutils.test.ts 📘 Rule violation ✧ Quality

The test uses relative imports (../types, ../apiUtils) for modules under src/ instead of the
required @/ path alias, which breaks the project’s import convention and can cause brittle pathing
as files move.
Agent Prompt
## Issue description
Tests under `src/` are importing other `src/` modules via relative paths (e.g., `../apiUtils`, `../types`) instead of the required `@/` alias.

## Issue Context
`tsconfig.json` defines the `@/*` path mapping to `./src/*`, so `@/` imports are supported and should be used for `src`-internal imports.

## Fix Focus Areas
- src/lib/__tests__/apiUtils.test.ts[1-3]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

@codecov

codecov Bot commented Aug 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant