Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/lib/__tests__/apiUtils.test.ts
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, getAuthenticatedUser, handleRateLimit } from '../apiUtils';
import { RateLimitError } from '../types';

Check warning on line 3 in src/lib/__tests__/apiUtils.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'RateLimitError' is defined but never used

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.

P2 未使用インポート: RateLimitError

RateLimitError クラスがインポートされていますが、テスト内のアサーションでは文字列リテラル 'RateLimitError' のみを使用しており、クラス自体は一切参照されていません。このインポートは削除するか、expect.any(RateLimitError).toThrow(RateLimitError) による instanceof チェックとして活用することを推奨します。文字列比較だと name プロパティを手動で設定したあらゆるエラーが誤ってパスしてしまうため、型安全性が低くなります。

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

Comment:
**未使用インポート: `RateLimitError`**

`RateLimitError` クラスがインポートされていますが、テスト内のアサーションでは文字列リテラル `'RateLimitError'` のみを使用しており、クラス自体は一切参照されていません。このインポートは削除するか、`expect.any(RateLimitError)``.toThrow(RateLimitError)` による `instanceof` チェックとして活用することを推奨します。文字列比較だと `name` プロパティを手動で設定したあらゆるエラーが誤ってパスしてしまうため、型安全性が低くなります。

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!

import { NextResponse } from 'next/server';
import { getServerSession } from "next-auth";

Expand Down Expand Up @@ -56,6 +57,45 @@
});
});

describe('handleRateLimit', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date(1000000));
});

afterEach(() => {
vi.useRealTimers();
});

it('should throw RateLimitError with timestamp from X-RateLimit-Reset header', () => {
const res = new Response(null, {
headers: { 'X-RateLimit-Reset': '2000' }
});
expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
name: 'RateLimitError',
resetAt: new Date(2000000)
}));
Comment on lines +74 to +77

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.

P2 RateLimitError インポートを活用した instanceof チェックへの改善

現在は name: 'RateLimitError' という文字列で確認しているため、同じ name プロパティを持つ別クラスでもテストがパスしてしまいます。インポート済みの RateLimitError を使い .toThrow(RateLimitError) で型を明示的に検証することで、より堅牢なアサーションになります。

Suggested change
expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
name: 'RateLimitError',
resetAt: new Date(2000000)
}));
expect(() => handleRateLimit(res)).toThrow(RateLimitError);
expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
resetAt: new Date(2000000)
}));
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/lib/__tests__/apiUtils.test.ts
Line: 74-77

Comment:
**`RateLimitError` インポートを活用した `instanceof` チェックへの改善**

現在は `name: 'RateLimitError'` という文字列で確認しているため、同じ `name` プロパティを持つ別クラスでもテストがパスしてしまいます。インポート済みの `RateLimitError` を使い `.toThrow(RateLimitError)` で型を明示的に検証することで、より堅牢なアサーションになります。

```suggestion
      expect(() => handleRateLimit(res)).toThrow(RateLimitError);
      expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
        resetAt: new Date(2000000)
      }));
```

How can I resolve this? If you propose a fix, please make it concise.

});
Comment on lines +70 to +78

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using expect.objectContaining inside toThrowError only asserts that the thrown value is an object with matching properties (like name: 'RateLimitError'). It does not guarantee that the thrown error is actually an instance of the RateLimitError class. To ensure type safety and prevent false positives, it is more robust to assert the error class using toThrow(RateLimitError) and then catch the error to verify its custom properties. Additionally, ensure functions have explicit return types to maintain type safety.

    it('should throw RateLimitError with timestamp from X-RateLimit-Reset header', (): void => {
      const res = new Response(null, {
        headers: { 'X-RateLimit-Reset': '2000' }
      });
      expect((): void => { handleRateLimit(res); }).toThrow(RateLimitError);
      try {
        handleRateLimit(res);
      } catch (error) {
        expect((error as RateLimitError).resetAt).toEqual(new Date(2000000));
      }
    });
References
  1. Maintain explicit return types for functions in TypeScript to ensure type safety and API clarity.


it('should throw RateLimitError with fallback timestamp if header is missing', () => {
const res = new Response();
expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
name: 'RateLimitError',
resetAt: new Date(4600000)
}));
});
Comment on lines +80 to +86

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using expect.objectContaining inside toThrowError only asserts that the thrown value is an object with matching properties (like name: 'RateLimitError'). It does not guarantee that the thrown error is actually an instance of the RateLimitError class. To ensure type safety and prevent false positives, it is more robust to assert the error class using toThrow(RateLimitError) and then catch the error to verify its custom properties. Additionally, ensure functions have explicit return types to maintain type safety.

    it('should throw RateLimitError with fallback timestamp if header is missing', (): void => {
      const res = new Response();
      expect((): void => { handleRateLimit(res); }).toThrow(RateLimitError);
      try {
        handleRateLimit(res);
      } catch (error) {
        expect((error as RateLimitError).resetAt).toEqual(new Date(4600000));
      }
    });
References
  1. Maintain explicit return types for functions in TypeScript to ensure type safety and API clarity.


it('should throw RateLimitError with fallback timestamp if header is invalid', () => {
const res = new Response(null, {
headers: { 'X-RateLimit-Reset': 'invalid' }
});
expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
name: 'RateLimitError',
resetAt: new Date(4600000)
}));
});
Comment on lines +88 to +96

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using expect.objectContaining inside toThrowError only asserts that the thrown value is an object with matching properties (like name: 'RateLimitError'). It does not guarantee that the thrown error is actually an instance of the RateLimitError class. To ensure type safety and prevent false positives, it is more robust to assert the error class using toThrow(RateLimitError) and then catch the error to verify its custom properties. Additionally, ensure functions have explicit return types to maintain type safety.

    it('should throw RateLimitError with fallback timestamp if header is invalid', (): void => {
      const res = new Response(null, {
        headers: { 'X-RateLimit-Reset': 'invalid' }
      });
      expect((): void => { handleRateLimit(res); }).toThrow(RateLimitError);
      try {
        handleRateLimit(res);
      } catch (error) {
        expect((error as RateLimitError).resetAt).toEqual(new Date(4600000));
      }
    });
References
  1. Maintain explicit return types for functions in TypeScript to ensure type safety and API clarity.

});

describe('getAuthenticatedUser', () => {
it('should return user object if session is valid', async () => {
vi.mocked(getServerSession).mockResolvedValueOnce({
Expand Down
Loading