Files
RedditBot/tests/reddit-api.test.ts
T

583 lines
20 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import axios from 'axios';
import type { AxiosInstance } from 'axios';
import type { BrowserContext } from 'playwright';
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';
vi.mock('axios', async () => {
const actual = await vi.importActual<typeof import('axios')>('axios');
return {
default: {
...actual.default,
create: vi.fn(),
isAxiosError: (e: unknown) => Boolean(e && typeof e === 'object' && (e as { isAxiosError?: boolean }).isAxiosError),
},
};
});
describe('RedditApiClient', () => {
let client: RedditApiClient;
let logger: ReturnType<typeof createMockLogger>;
beforeEach(() => {
logger = createMockLogger();
const config = getTestConfig();
client = new RedditApiClient(config, logger);
vi.clearAllMocks();
});
describe('hasImage', () => {
const validPostFactory = (overrides: Partial<RedditPost> = {}): RedditPost => ({
id: 't3_test',
name: 't3_test',
subreddit: 'test',
author: 'testuser',
title: 'Test',
url: 'http://example.com',
permalink: '/r/test/comments/abc/test',
thumbnail: '',
is_self: false,
created_utc: Date.now() / 1000,
over_18: false,
spoiler: false,
...overrides,
});
it('accepts posts with post_hint=image', () => {
const post = validPostFactory({ post_hint: 'image' });
// We can't test private method directly, but we can test via getImageUrl
const url = client.getImageUrl(post);
// Since url doesn't match image patterns, it falls through to thumbnail
// which is empty, so returns null. The hasImage check is internal.
// Let's test via getNewPosts filtering instead.
});
it('accepts posts with preview images', () => {
const post = validPostFactory({
preview: {
images: [{
source: { url: 'http://i.redd.it/image.jpg', width: 100, height: 100 },
resolutions: [],
}],
},
});
const url = client.getImageUrl(post);
expect(url).toBe('http://i.redd.it/image.jpg');
});
it('accepts posts with i.redd.it URLs', () => {
const post = validPostFactory({ url: 'https://i.redd.it/abc123.jpg' });
const url = client.getImageUrl(post);
expect(url).toBe('https://i.redd.it/abc123.jpg');
});
it('accepts posts with i.imgur.com URLs', () => {
const post = validPostFactory({ url: 'https://i.imgur.com/abc123.jpg' });
const url = client.getImageUrl(post);
expect(url).toBe('https://i.imgur.com/abc123.jpg');
});
it('extracts imgur direct URL from imgur page URL', () => {
const post = validPostFactory({ url: 'https://imgur.com/abc123' });
const url = client.getImageUrl(post);
expect(url).toBe('https://i.imgur.com/abc123.jpg');
});
it('extracts imgur direct URL from imgur page URL with extension', () => {
const post = validPostFactory({ url: 'https://imgur.com/abc123.jpg' });
const url = client.getImageUrl(post);
expect(url).toBe('https://i.imgur.com/abc123.jpg');
});
it('accepts posts with image file extensions in URL', () => {
const post = validPostFactory({ url: 'http://example.com/photo.png' });
const url = client.getImageUrl(post);
expect(url).toBe('http://example.com/photo.png');
});
it('accepts .gif URLs', () => {
const post = validPostFactory({ url: 'http://example.com/animation.gif' });
const url = client.getImageUrl(post);
expect(url).toBe('http://example.com/animation.gif');
});
it('accepts .webp URLs', () => {
const post = validPostFactory({ url: 'http://example.com/image.webp' });
const url = client.getImageUrl(post);
expect(url).toBe('http://example.com/image.webp');
});
it('falls back to thumbnail when no other image source', () => {
const post = validPostFactory({ thumbnail: 'http://example.com/thumb.jpg' });
const url = client.getImageUrl(post);
expect(url).toBe('http://example.com/thumb.jpg');
});
it('returns null when no image source available', () => {
const post = validPostFactory({ thumbnail: '' });
const url = client.getImageUrl(post);
expect(url).toBeNull();
});
it('returns null for empty thumbnail', () => {
const post = validPostFactory({ thumbnail: '' });
const url = client.getImageUrl(post);
expect(url).toBeNull();
});
it('skips non-image thumbnail URLs', () => {
const post = validPostFactory({ thumbnail: 'about:blank' });
const url = client.getImageUrl(post);
expect(url).toBeNull();
});
});
describe('decodeRedditUrl', () => {
it('decodes HTML entities in preview URLs', () => {
const post: RedditPost = {
id: 't3_test',
name: 't3_test',
subreddit: 'test',
author: 'user',
title: 'Test',
url: 'http://example.com',
permalink: '/r/test/comments/abc/test',
thumbnail: '',
is_self: false,
created_utc: Date.now() / 1000,
over_18: false,
spoiler: false,
preview: {
images: [{
source: { url: 'http://example.com/&amp;test.jpg', width: 100, height: 100 },
resolutions: [],
}],
},
};
const url = client.getImageUrl(post);
expect(url).toBe('http://example.com/&test.jpg');
});
});
describe('getPostUrl', () => {
it('constructs correct URL from permalink', () => {
const post: RedditPost = {
id: 't3_abc',
name: 't3_abc',
subreddit: 'test',
author: 'user',
title: 'Test',
url: 'http://example.com',
permalink: '/r/test/comments/abc/test',
thumbnail: '',
is_self: false,
created_utc: Date.now() / 1000,
over_18: false,
spoiler: false,
};
expect(client.getPostUrl(post)).toBe('https://www.reddit.com/r/test/comments/abc/test');
});
});
describe('isValidPost (via getNewPosts filtering)', () => {
it('skips self posts', () => {
const textPost: RedditPost = {
id: 't3_text',
name: 't3_text',
subreddit: 'test',
author: 'user',
title: 'Text post',
url: 'https://www.reddit.com/r/test/comments/abc/',
permalink: '/r/test/comments/abc/text',
thumbnail: 'self',
is_self: true,
created_utc: Date.now() / 1000,
over_18: false,
spoiler: false,
};
// We can't easily test the private isValidPost, but we can verify
// the behavior through the public API by checking that image posts
// pass through and text posts don't.
});
it('skips deleted authors', () => {
const post: RedditPost = {
id: 't3_del',
name: 't3_del',
subreddit: 'test',
author: '[deleted]',
title: 'Deleted user',
url: 'http://i.redd.it/img.jpg',
permalink: '/r/test/comments/abc/del',
thumbnail: 'http://i.redd.it/img.jpg',
is_self: false,
created_utc: Date.now() / 1000,
over_18: false,
spoiler: false,
};
// isValidPost should return false for [deleted]
});
it('skips AutoModerator posts', () => {
const post: RedditPost = {
id: 't3_mod',
name: 't3_mod',
subreddit: 'test',
author: 'AutoModerator',
title: 'Mod message',
url: 'http://i.redd.it/img.jpg',
permalink: '/r/test/comments/abc/mod',
thumbnail: 'http://i.redd.it/img.jpg',
is_self: false,
created_utc: Date.now() / 1000,
over_18: false,
spoiler: false,
};
// isValidPost should return false for AutoModerator
});
});
describe('isSellerProfile', () => {
it('returns true when seller keyword matches', async () => {
const mockResponse = {
data: {
data: {
subreddit: {
public_description: 'Check out my OnlyFans page!',
title: 'My Profile',
display_name_prefixed: 'u/testuser',
},
},
},
};
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockResolvedValue(mockResponse),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', ['onlyfans']);
expect(result).toBe(true);
});
it('returns false when no keywords match', async () => {
const mockResponse = {
data: {
data: {
subreddit: {
public_description: 'Just a regular user',
title: 'My Profile',
display_name_prefixed: 'u/testuser',
},
},
},
};
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockResolvedValue(mockResponse),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', ['onlyfans', 'fansly']);
expect(result).toBe(false);
});
it('returns false for empty seller keywords', async () => {
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', []);
expect(result).toBe(false);
});
it('returns false when user profile not found (404)', async () => {
const error = { isAxiosError: true, response: { status: 404 } };
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockRejectedValue(error),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('nonexistent', ['onlyfans']);
expect(result).toBe(false);
});
it('is case-insensitive for keyword matching', async () => {
const mockResponse = {
data: {
data: {
subreddit: {
public_description: 'Check out my ONLYFANS page!',
title: 'My Profile',
display_name_prefixed: 'u/testuser',
},
},
},
};
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockResolvedValue(mockResponse),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', ['onlyfans']);
expect(result).toBe(true);
});
it('returns false on non-404 errors', async () => {
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockRejectedValue(new Error('Network error')),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', ['onlyfans']);
expect(result).toBe(false);
});
it('returns true when a domain in the bio matches sellerDomains', async () => {
const mockResponse = {
data: {
data: {
subreddit: {
public_description: 'find me here https://my.linktr.ee/cutiepie',
title: 'My Profile',
display_name_prefixed: 'u/testuser',
},
},
},
};
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockResolvedValue(mockResponse),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', [], ['linktr.ee']);
expect(result).toBe(true);
});
it('matches bare domains without a scheme', async () => {
const mockResponse = {
data: {
data: {
subreddit: {
public_description: 'dm for fansly.com/me',
title: '',
display_name_prefixed: 'u/testuser',
},
},
},
};
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockResolvedValue(mockResponse),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', [], ['fansly.com']);
expect(result).toBe(true);
});
it('does not match unrelated domains that share a suffix', async () => {
const mockResponse = {
data: {
data: {
subreddit: {
public_description: 'see notlinktr.ee for my art',
title: '',
display_name_prefixed: 'u/testuser',
},
},
},
};
vi.mocked(axios.create).mockReturnValue({
get: vi.fn().mockResolvedValue(mockResponse),
} as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.isSellerProfile('testuser', [], ['linktr.ee']);
expect(result).toBe(false);
});
});
describe('resolveUsernameToT2', () => {
it('uses the imported Reddit session and normalizes the account ID', async () => {
const mockAxios = {
get: vi.fn().mockResolvedValue({ data: { data: { id: 'abc123' } } }),
};
vi.mocked(axios.create).mockReturnValue(mockAxios as unknown as AxiosInstance);
const client2 = new RedditApiClient(getTestConfig(), logger);
const cookies = vi.fn().mockResolvedValue([
{ name: 'reddit_session', value: 'session-value' },
]);
client2.setBrowserContext({ cookies } as unknown as BrowserContext);
await expect(client2.resolveUsernameToT2('alice')).resolves.toBe('t2_abc123');
expect(mockAxios.get).toHaveBeenCalledWith('/user/alice/about.json', {
params: { raw_json: 1 },
headers: { Cookie: 'reddit_session=session-value' },
});
});
it('returns null only when Reddit confirms the user is missing', async () => {
const error = { isAxiosError: true, response: { status: 404 } };
const mockAxios = { get: vi.fn().mockRejectedValue(error) };
vi.mocked(axios.create).mockReturnValue(mockAxios as unknown as AxiosInstance);
const client2 = new RedditApiClient(getTestConfig(), logger);
await expect(client2.resolveUsernameToT2('missing')).resolves.toBeNull();
});
it('does not misclassify a 403 as an unknown user', async () => {
const error = { isAxiosError: true, response: { status: 403 } };
const mockAxios = { get: vi.fn().mockRejectedValue(error) };
vi.mocked(axios.create).mockReturnValue(mockAxios as unknown as AxiosInstance);
const client2 = new RedditApiClient(getTestConfig(), logger);
await expect(client2.resolveUsernameToT2('alice')).rejects.toBe(error);
});
});
describe('getNewPosts', () => {
it('returns empty array for 404 subreddit', async () => {
const error = { isAxiosError: true, response: { status: 404 } };
const mockAxios = { get: vi.fn().mockRejectedValue(error) };
vi.mocked(axios.create).mockReturnValue(mockAxios as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.getNewPosts('nonexistent');
expect(result).toEqual([]);
});
it('returns empty array for 403 private subreddit', async () => {
const error = { isAxiosError: true, response: { status: 403 } };
const mockAxios = { get: vi.fn().mockRejectedValue(error) };
vi.mocked(axios.create).mockReturnValue(mockAxios as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.getNewPosts('private');
expect(result).toEqual([]);
});
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);
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 () => {
const mockResponse = {
data: { data: { children: [
{
data: {
id: 't3_1',
name: 't3_1',
subreddit: 'test',
author: 'user1',
title: 'Image post 1',
url: 'https://i.redd.it/abc123.jpg',
permalink: '/r/test/comments/abc/1',
thumbnail: 'https://i.redd.it/abc123.jpg',
is_self: false,
post_hint: 'image',
created_utc: 1000000,
over_18: false,
spoiler: false,
},
},
{
data: {
id: 't3_2',
name: 't3_2',
subreddit: 'test',
author: 'user2',
title: 'Text post',
url: 'https://www.reddit.com/r/test/comments/abc/2',
permalink: '/r/test/comments/abc/2',
thumbnail: 'self',
is_self: true,
created_utc: 1000001,
over_18: false,
spoiler: false,
},
},
] } },
};
const mockAxios = { get: vi.fn().mockResolvedValue(mockResponse) };
vi.mocked(axios.create).mockReturnValue(mockAxios as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.getNewPosts('test');
expect(result.length).toBe(1);
expect(result[0].id).toBe('t3_1');
expect(result[0].title).toBe('Image post 1');
});
it('filters out deleted authors', async () => {
const mockResponse = {
data: { data: { children: [
{
data: {
id: 't3_del',
name: 't3_del',
subreddit: 'test',
author: '[deleted]',
title: 'Deleted',
url: 'https://i.redd.it/img.jpg',
permalink: '/r/test/comments/abc/del',
thumbnail: 'https://i.redd.it/img.jpg',
is_self: false,
created_utc: 1000000,
over_18: false,
spoiler: false,
},
},
] } },
};
const mockAxios = { get: vi.fn().mockResolvedValue(mockResponse) };
vi.mocked(axios.create).mockReturnValue(mockAxios as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.getNewPosts('test');
expect(result.length).toBe(0);
});
it('filters out AutoModerator posts', async () => {
const mockResponse = {
data: { data: { children: [
{
data: {
id: 't3_mod',
name: 't3_mod',
subreddit: 'test',
author: 'AutoModerator',
title: 'Mod message',
url: 'https://i.redd.it/img.jpg',
permalink: '/r/test/comments/abc/mod',
thumbnail: 'https://i.redd.it/img.jpg',
is_self: false,
created_utc: 1000000,
over_18: false,
spoiler: false,
},
},
] } },
};
const mockAxios = { get: vi.fn().mockResolvedValue(mockResponse) };
vi.mocked(axios.create).mockReturnValue(mockAxios as any);
const client2 = new RedditApiClient(getTestConfig(), logger);
const result = await client2.getNewPosts('test');
expect(result.length).toBe(0);
});
});
});