Files
RedditBot/tests/reddit-session-importer.test.ts
T

46 lines
1.8 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { Cookie } from 'playwright';
import { RedditSessionImporter } from '../src/services/reddit-session-importer.js';
const cookies: Cookie[] = [{
name: 'reddit_session', value: 'secret', domain: '.reddit.com', path: '/', expires: -1,
httpOnly: true, secure: true, sameSite: 'Lax',
}];
const credentials = {
homeserver: 'https://matrix.redditspace.com',
accessToken: 'matrix-secret',
userId: '@t2_bot:reddit.com',
capturedAt: 1_700_000_000_000,
};
describe('RedditSessionImporter', () => {
it('verifies both parts of the dump before activating the account', async () => {
const browser = { importRedditCookies: vi.fn(), isSessionValid: vi.fn(async () => true) };
const matrix = { whoami: vi.fn(async () => credentials.userId) };
const chat = { setMatrixClient: vi.fn() };
const save = vi.fn();
const importer = new RedditSessionImporter(browser, chat as never, vi.fn(() => matrix as never), save);
await importer.import(cookies, credentials);
expect(matrix.whoami).toHaveBeenCalledOnce();
expect(browser.importRedditCookies).toHaveBeenCalledWith(cookies);
expect(chat.setMatrixClient).toHaveBeenCalledWith(matrix);
expect(save).toHaveBeenCalledWith(credentials);
});
it('rejects non-Reddit Matrix homeservers without making a request', async () => {
const createMatrix = vi.fn();
const importer = new RedditSessionImporter(
{ importRedditCookies: vi.fn(), isSessionValid: vi.fn() },
{ setMatrixClient: vi.fn() } as never,
createMatrix,
vi.fn(),
);
await expect(importer.import(cookies, { ...credentials, homeserver: 'https://attacker.example' }))
.rejects.toThrow(/homeserver/i);
expect(createMatrix).not.toHaveBeenCalled();
});
});