-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Test handleRateLimit function in apiUtils #373
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,5 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { handleErrorResponse, getAuthenticatedUser } from '../apiUtils'; | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||
| import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils'; | ||
| import { NextResponse } from 'next/server'; | ||
| import { getServerSession } from "next-auth"; | ||
|
|
||
|
|
@@ -97,4 +97,47 @@ describe('apiUtils', () => { | |
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleRateLimit', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date(1600000000000)); // Timestamp: 1600000000 | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('should throw RateLimitError using the reset timestamp from the X-RateLimit-Reset header', () => { | ||
| const resetTimestamp = 1600003600; | ||
| const headers = new Headers(); | ||
| headers.set('X-RateLimit-Reset', resetTimestamp.toString()); | ||
| const res = new Response(null, { headers }); | ||
|
|
||
| expect(() => handleRateLimit(res)).toThrowError( | ||
| `GitHub API rate limit exceeded. Resets at ${new Date(resetTimestamp * 1000).toISOString()}` | ||
| ); | ||
| }); | ||
|
Comment on lines
+111
to
+120
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. 3つのテストはすべてエラーメッセージ文字列のみを照合しており、スローされるオブジェクトが Prompt To Fix With AIThis is a comment left during a code review.
Path: src/lib/__tests__/apiUtils.test.ts
Line: 111-120
Comment:
**スローされるエラー型の検証が欠如している**
3つのテストはすべてエラーメッセージ文字列のみを照合しており、スローされるオブジェクトが `RateLimitError` のインスタンスであることを確認していません。`RateLimitError` を `Error` に置き換えても同じメッセージであればテストはパスします。`expect(() => handleRateLimit(res)).toThrow(RateLimitError)` のような型チェックを追加することで、型安全性も保証できます。
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 using default time + 3600s if X-RateLimit-Reset header is missing', () => { | ||
| const headers = new Headers(); | ||
| const res = new Response(null, { headers }); | ||
| const expectedResetTimestamp = Math.floor(Date.now() / 1000) + 3600; | ||
|
|
||
| expect(() => handleRateLimit(res)).toThrowError( | ||
| `GitHub API rate limit exceeded. Resets at ${new Date(expectedResetTimestamp * 1000).toISOString()}` | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw RateLimitError using default time + 3600s if X-RateLimit-Reset header is invalid', () => { | ||
| const headers = new Headers(); | ||
| headers.set('X-RateLimit-Reset', 'invalid'); | ||
| const res = new Response(null, { headers }); | ||
| const expectedResetTimestamp = Math.floor(Date.now() / 1000) + 3600; | ||
|
|
||
| expect(() => handleRateLimit(res)).toThrowError( | ||
| `GitHub API rate limit exceeded. Resets at ${new Date(expectedResetTimestamp * 1000).toISOString()}` | ||
| ); | ||
| }); | ||
|
Comment on lines
+111
to
+141
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. Since the system time is mocked to a fixed value ( it('should throw RateLimitError using the reset timestamp from the X-RateLimit-Reset header', () => {
const resetTimestamp = 1600003600;
const headers = new Headers();
headers.set('X-RateLimit-Reset', resetTimestamp.toString());
const res = new Response(null, { headers });
expect(() => handleRateLimit(res)).toThrowError(
'GitHub API rate limit exceeded. Resets at 2020-09-13T13:26:40.000Z'
);
});
it('should throw RateLimitError using default time + 3600s if X-RateLimit-Reset header is missing', () => {
const headers = new Headers();
const res = new Response(null, { headers });
expect(() => handleRateLimit(res)).toThrowError(
'GitHub API rate limit exceeded. Resets at 2020-09-13T13:26:40.000Z'
);
});
it('should throw RateLimitError using default time + 3600s if X-RateLimit-Reset header is invalid', () => {
const headers = new Headers();
headers.set('X-RateLimit-Reset', 'invalid');
const res = new Response(null, { headers });
expect(() => handleRateLimit(res)).toThrowError(
'GitHub API rate limit exceeded. Resets at 2020-09-13T13:26:40.000Z'
);
});References
|
||
| }); | ||
| }); | ||
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.
テストで使用している
resetTimestamp = 1600003600は、フリーズされたシステム時刻 (1600000000000 ms) から計算されるフォールバック値Math.floor(1600000000000 / 1000) + 3600 = 1600003600とまったく同じ値です。そのため、実装がX-RateLimit-Resetヘッダーを完全に無視して常にフォールバックを使用するよう壊れていたとしても、このテストはパスしてしまいます。ヘッダーが正しく読まれていることを確認するには、フォールバック値と異なる値(例:1600007200)を使う必要があります。Prompt To Fix With AI