Respect Reddit rate limit delays

This commit is contained in:
jjbrunton committed 2026-08-08 18:34:37 +01:00
1 parent 7bcdbe236a
commit 50de1c180d
5 files changed
+77 -10

No files matched your search

+13 -4
View File
@@ -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 () => {