Skip to content
Open
Show file tree
Hide file tree
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: 44 additions & 0 deletions __tests__/lib/rate-limit-upstash.mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { upstashLimit } from '@/lib/rate-limit-upstash';

const originalUrl = process.env.UPSTASH_REDIS_REST_URL;
const originalToken = process.env.UPSTASH_REDIS_REST_TOKEN;
// Ensure env vars are set for the adapter to initialize (mocked services will be used)
process.env.UPSTASH_REDIS_REST_URL = process.env.UPSTASH_REDIS_REST_URL || 'http://localhost';
process.env.UPSTASH_REDIS_REST_TOKEN = process.env.UPSTASH_REDIS_REST_TOKEN || 'test-token';

jest.mock('@upstash/redis', () => ({
Redis: jest.fn().mockImplementation(() => ({})),
}), { virtual: true });

jest.mock('@upstash/ratelimit', () => {
const slidingWindow = jest.fn().mockImplementation((_max: number, _win: string) => ({}));

Check warning on line 14 in __tests__/lib/rate-limit-upstash.mock.test.ts

View workflow job for this annotation

GitHub Actions / build

'_win' is defined but never used

Check warning on line 14 in __tests__/lib/rate-limit-upstash.mock.test.ts

View workflow job for this annotation

GitHub Actions / build

'_max' is defined but never used
const Ratelimit = jest.fn().mockImplementation(() => ({
limit: async (_id: string) => ({ success: true, remaining: 2, resetAfter: 10000 }),

Check warning on line 16 in __tests__/lib/rate-limit-upstash.mock.test.ts

View workflow job for this annotation

GitHub Actions / build

'_id' is defined but never used
}));
// Attach static helper
(Ratelimit as any).slidingWindow = slidingWindow;

Check failure on line 19 in __tests__/lib/rate-limit-upstash.mock.test.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return { Ratelimit };
}, { virtual: true });

describe('Upstash adapter (mocked)', () => {
afterAll(() => {
if (originalUrl === undefined) {
delete process.env.UPSTASH_REDIS_REST_URL;
} else {
process.env.UPSTASH_REDIS_REST_URL = originalUrl;
}

if (originalToken === undefined) {
delete process.env.UPSTASH_REDIS_REST_TOKEN;
} else {
process.env.UPSTASH_REDIS_REST_TOKEN = originalToken;
}
});

it('returns mapped rate limit result', async () => {
const res = await upstashLimit('test-id', { maxRequests: 5, windowMs: 60 * 60 * 1000 });
expect(res.success).toBe(true);
expect(typeof res.remaining).toBe('number');
expect(typeof res.reset).toBe('number');
});
});
32 changes: 32 additions & 0 deletions __tests__/lib/rate-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { checkRateLimit as checkLimitInmem, RateLimitConfig } from '@/lib/rate-limit';

describe('In-memory rate limiter (sanity checks)', () => {
const originalRateLimitMode = process.env.RATE_LIMIT_MODE;

afterAll(() => {
if (originalRateLimitMode === undefined) {
delete process.env.RATE_LIMIT_MODE;
} else {
process.env.RATE_LIMIT_MODE = originalRateLimitMode;
}
});

it('should allow requests up to the limit and then block', async () => {
process.env.RATE_LIMIT_MODE = 'INMEM';

const key = `test:${Date.now()}:${Math.random()}`;
const config: RateLimitConfig = { maxRequests: 3, windowMs: 1000 * 60 };

const r1 = await checkLimitInmem(key, config);
expect(r1.success).toBe(true);

const r2 = await checkLimitInmem(key, config);
expect(r2.success).toBe(true);

const r3 = await checkLimitInmem(key, config);
expect(r3.success).toBe(true);

const r4 = await checkLimitInmem(key, config);
expect(r4.success).toBe(false);
});
});
Loading
Loading