-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 test: Add unit tests for handleRateLimit in apiUtils #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { handleErrorResponse, getAuthenticatedUser } from '../apiUtils'; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { RateLimitError } from '../types'; | ||
| import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils'; | ||
| import { NextResponse } from 'next/server'; | ||
| import { getServerSession } from "next-auth"; | ||
|
|
||
|
|
@@ -97,4 +98,61 @@ | |
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleRateLimit', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date(1700000000000)); // Nov 14 2023 22:13:20 GMT | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('should throw RateLimitError with exact timestamp if header is present and valid', () => { | ||
| const res = new Response(null, { | ||
| headers: { | ||
| 'X-RateLimit-Reset': '1700003600' | ||
| } | ||
| }); | ||
|
|
||
| expect(() => handleRateLimit(res)).toThrow(RateLimitError); | ||
| try { | ||
| handleRateLimit(res); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(RateLimitError); | ||
| expect(error.resetAt.getTime()).toBe(1700003600000); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis 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. |
||
| } | ||
| }); | ||
|
|
||
| it('should throw RateLimitError with default timestamp (+1 hour) if header is missing', () => { | ||
| const res = new Response(null); | ||
|
|
||
| expect(() => handleRateLimit(res)).toThrow(RateLimitError); | ||
| try { | ||
| handleRateLimit(res); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(RateLimitError); | ||
| // Current time is 1700000000, so +1 hour (3600 seconds) is 1700003600 | ||
| expect(error.resetAt.getTime()).toBe(1700003600000); | ||
| } | ||
| }); | ||
|
|
||
| it('should throw RateLimitError with default timestamp if header is invalid (NaN)', () => { | ||
| const res = new Response(null, { | ||
| headers: { | ||
| 'X-RateLimit-Reset': 'invalid-date' | ||
| } | ||
| }); | ||
|
|
||
| expect(() => handleRateLimit(res)).toThrow(RateLimitError); | ||
| try { | ||
| handleRateLimit(res); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(RateLimitError); | ||
| expect(error.resetAt.getTime()).toBe(1700003600000); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Relative imports in apiutils.test.ts
📘 Rule violation✧ QualityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools