Respect Reddit rate limit delays
This commit is contained in:
1 parent
7bcdbe236a
commit
50de1c180d
5 files changed
+77
-10
No files matched your search
+27
-1
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { MonitorService } from '../src/services/monitor.js';
|
||||
import type { RedditApiClient, RedditPost } from '../src/reddit/api.js';
|
||||
import { RedditRateLimitError, type RedditApiClient, type RedditPost } from '../src/reddit/api.js';
|
||||
import type { PostsRepository, MessagesRepository } from '../src/database/repositories.js';
|
||||
import { createMockLogger } from '../tests/mockLogger.js';
|
||||
import { getTestConfig } from '../tests/testUtils.js';
|
||||
@@ -246,6 +246,32 @@ describe('MonitorService', () => {
|
||||
expect(logger.logs.some(l => l.level === 'error' && l.message.includes('Failed to fetch'))).toBe(true);
|
||||
});
|
||||
|
||||
it('stops scanning and propagates Reddit rate limits without logging the error object', async () => {
|
||||
const redditApi = createMockRedditApi([]);
|
||||
vi.mocked(redditApi.getNewPosts).mockRejectedValue(new RedditRateLimitError(57_000));
|
||||
const config = getTestConfig({
|
||||
reddit: {
|
||||
subreddits: [
|
||||
{ name: 'sub1', type: 'image' },
|
||||
{ name: 'sub2', type: 'image' },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
monitor = new MonitorService(
|
||||
redditApi,
|
||||
createMockPostsRepo(),
|
||||
createMockMessagesRepo(),
|
||||
config,
|
||||
logger,
|
||||
0,
|
||||
);
|
||||
|
||||
await expect(monitor.getUnprocessedPosts()).rejects.toMatchObject({ retryAfterMs: 57_000 });
|
||||
expect(redditApi.getNewPosts).toHaveBeenCalledTimes(1);
|
||||
expect(logger.logs.some((entry) => entry.level === 'error')).toBe(false);
|
||||
});
|
||||
|
||||
it('returns only one post per call', async () => {
|
||||
const posts: RedditPost[] = Array.from({ length: 10 }, (_, i) => ({
|
||||
id: `t3_${i}`, subreddit: 'sub1', author: `user${i}`, title: `Post ${i}`,
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import axios from 'axios';
|
||||
import type { AxiosInstance } from 'axios';
|
||||
import type { BrowserContext } from 'playwright';
|
||||
import { RedditApiClient } from '../src/reddit/api.js';
|
||||
import { RedditApiClient, RedditRateLimitError } from '../src/reddit/api.js';
|
||||
import type { RedditPost } from '../src/reddit/api.js';
|
||||
import { createMockLogger } from '../tests/mockLogger.js';
|
||||
import { getTestConfig } from '../tests/testUtils.js';
|
||||
@@ -455,13 +455,22 @@ describe('RedditApiClient', () => {
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('throws on 429 rate limit', async () => {
|
||||
const error = { isAxiosError: true, response: { status: 429 } };
|
||||
it('throws a compact error with the Reddit rate-limit reset delay on 429', async () => {
|
||||
const error = {
|
||||
isAxiosError: true,
|
||||
response: { status: 429, headers: { 'x-ratelimit-reset': '57' } },
|
||||
};
|
||||
const mockAxios = { get: vi.fn().mockRejectedValue(error) };
|
||||
vi.mocked(axios.create).mockReturnValue(mockAxios as any);
|
||||
|
||||
const client2 = new RedditApiClient(getTestConfig(), logger);
|
||||
await expect(client2.getNewPosts('test')).rejects.toThrow();
|
||||
const result = client2.getNewPosts('test');
|
||||
|
||||
await expect(result).rejects.toBeInstanceOf(RedditRateLimitError);
|
||||
await expect(result).rejects.toMatchObject({ retryAfterMs: 57_000 });
|
||||
expect(logger.logs.some((entry) =>
|
||||
entry.level === 'warn' && entry.message.includes('retry after 57s'),
|
||||
)).toBe(true);
|
||||
});
|
||||
|
||||
it('parses valid image posts from response', async () => {
|
||||
|
||||
Reference in new issue
Block a user