-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add tests for handleRateLimit #451
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { handleErrorResponse, handleRateLimit, getAuthenticatedUser } from '../apiUtils'; | ||||||||||||||||||||||||||
| import { RateLimitError } from '@/lib/types'; | ||||||||||||||||||||||||||
| import { NextResponse } from 'next/server'; | ||||||||||||||||||||||||||
| import { getServerSession } from "next-auth"; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -56,6 +57,74 @@ describe('apiUtils', () => { | |||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| describe('handleRateLimit', () => { | ||||||||||||||||||||||||||
| let originalDateNow: () => number; | ||||||||||||||||||||||||||
| const mockNow = 1700000000000; // 2023-11-14T22:13:20.000Z | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||
| originalDateNow = Date.now; | ||||||||||||||||||||||||||
| Date.now = vi.fn(() => mockNow); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||||
| Date.now = originalDateNow; | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it('should throw RateLimitError with X-RateLimit-Reset header timestamp', () => { | ||||||||||||||||||||||||||
| const resetTimestamp = 1700003600; | ||||||||||||||||||||||||||
| const res = new Response(null, { | ||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||
| 'X-RateLimit-Reset': resetTimestamp.toString(), | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| expect(() => handleRateLimit(res)).toThrow(RateLimitError); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| handleRateLimit(res); | ||||||||||||||||||||||||||
| } // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||||||||||||
| catch (error: any) { | ||||||||||||||||||||||||||
| expect(error).toBeInstanceOf(RateLimitError); | ||||||||||||||||||||||||||
| expect(error.resetAt.getTime()).toBe(resetTimestamp * 1000); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+84
to
+90
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.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/lib/__tests__/apiUtils.test.ts
Line: 84-89
Comment:
TypeScript のストリクトモードでは `catch` ブロックの変数は `unknown` 型になります。`error.resetAt` や `error.resetAt.getTime()` へのアクセスはコンパイルエラーとなります(`TS18046: 'error' is of type 'unknown'`)。同パターンが3テスト全てに存在します。
```suggestion
try {
handleRateLimit(res);
} catch (error) {
expect(error).toBeInstanceOf(RateLimitError);
expect((error as RateLimitError).resetAt.getTime()).toBe(resetTimestamp * 1000);
}
```
How can I resolve this? If you propose a fix, please make it concise.
Comment on lines
+82
to
+90
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: 82-89
Comment:
**catch ブロックのアサーションがサイレントスキップされる可能性**
各テストで `handleRateLimit(res)` を2回呼び出しています(`expect().toThrow()` で1回、`try/catch` でもう1回)。`try` ブロックが何らかの理由でスローしない場合、catch 内の `expect(error.resetAt.getTime())...` は完全に実行されず、テストが誤ってグリーンになります。`resetAt` の値検証はこのパターンが唯一担っているため、`expect.assertions(N)` でアサーション実行数を保証することを検討してください。同パターンが3テスト全てに存在します。
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it('should throw RateLimitError with default timestamp (1 hour from now) if header is missing', () => { | ||||||||||||||||||||||||||
| const res = new Response(null); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| expect(() => handleRateLimit(res)).toThrow(RateLimitError); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| handleRateLimit(res); | ||||||||||||||||||||||||||
| } // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||||||||||||
| catch (error: any) { | ||||||||||||||||||||||||||
| expect(error).toBeInstanceOf(RateLimitError); | ||||||||||||||||||||||||||
| const expectedResetTimestamp = Math.floor(mockNow / 1000) + 3600; | ||||||||||||||||||||||||||
| expect(error.resetAt.getTime()).toBe(expectedResetTimestamp * 1000); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+96
to
+105
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. Calling let thrownError: unknown;
try {
handleRateLimit(res);
} catch (error) {
thrownError = error;
}
expect(thrownError).toBeInstanceOf(RateLimitError);
const expectedResetTimestamp = Math.floor(mockNow / 1000) + 3600;
expect((thrownError as RateLimitError).resetAt.getTime()).toBe(expectedResetTimestamp * 1000); |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| it('should throw RateLimitError with default timestamp if header is invalid', () => { | ||||||||||||||||||||||||||
| const res = new Response(null, { | ||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||
| 'X-RateLimit-Reset': 'invalid-timestamp', | ||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| expect(() => handleRateLimit(res)).toThrow(RateLimitError); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| handleRateLimit(res); | ||||||||||||||||||||||||||
| } // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||||||||||||||||||||||||||
| catch (error: any) { | ||||||||||||||||||||||||||
| expect(error).toBeInstanceOf(RateLimitError); | ||||||||||||||||||||||||||
| const expectedResetTimestamp = Math.floor(mockNow / 1000) + 3600; | ||||||||||||||||||||||||||
| expect(error.resetAt.getTime()).toBe(expectedResetTimestamp * 1000); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+115
to
+124
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. Calling let thrownError: unknown;
try {
handleRateLimit(res);
} catch (error) {
thrownError = error;
}
expect(thrownError).toBeInstanceOf(RateLimitError);
const expectedResetTimestamp = Math.floor(mockNow / 1000) + 3600;
expect((thrownError as RateLimitError).resetAt.getTime()).toBe(expectedResetTimestamp * 1000); |
||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| describe('getAuthenticatedUser', () => { | ||||||||||||||||||||||||||
| it('should return user object if session is valid', async () => { | ||||||||||||||||||||||||||
| vi.mocked(getServerSession).mockResolvedValueOnce({ | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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.
Calling
handleRateLimittwice is redundant. Additionally, in TypeScript strict mode, the caughterroris typed asunknown, so accessingerror.resetAtdirectly will cause a compilation error. Capturing the thrown error and asserting on it is cleaner and type-safe.