Skip to content
Closed
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
62 changes: 60 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 { RateLimitError } from '../types';
import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils';
Comment on lines +2 to +3

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

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

Expand Down Expand Up @@ -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);

Check failure on line 124 in src/lib/__tests__/apiUtils.test.ts

View workflow job for this annotation

GitHub Actions / Type Check

'error' is of type 'unknown'.

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.

}
});

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);

Check failure on line 137 in src/lib/__tests__/apiUtils.test.ts

View workflow job for this annotation

GitHub Actions / Type Check

'error' is of type 'unknown'.
}
});

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);

Check failure on line 153 in src/lib/__tests__/apiUtils.test.ts

View workflow job for this annotation

GitHub Actions / Type Check

'error' is of type 'unknown'.
}
});
});

});
Loading