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

76 lines
2.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
assertMatchingSessionIdentity,
createSessionExportDownload,
REDDIT_CHAT_URL,
} from '../extensions/reddit-session-exporter/session-export.js';
it('uses the dedicated Reddit Chat host for Matrix credential capture', () => {
expect(REDDIT_CHAT_URL).toBe('https://chat.reddit.com/');
});
describe('createSessionExportDownload', () => {
it('creates a JSON data URL that Chrome can download from a service worker', () => {
const download = createSessionExportDownload([
{
name: 'reddit_session',
value: 'secret',
domain: '.reddit.com',
path: '/',
expirationDate: 1_800_000_000,
httpOnly: true,
secure: true,
sameSite: 'lax',
},
], {
homeserver: 'https://matrix.redditspace.com',
accessToken: 'matrix-secret',
userId: '@t2_bot:reddit.com',
capturedAt: 1_700_000_000_000,
});
expect(download.filename).toBe('redditbot-session.json');
expect(download.url).toMatch(/^data:application\/json;charset=utf-8,/);
expect(JSON.parse(decodeURIComponent(download.url.split(',')[1]))).toEqual({
cookies: [
{
name: 'reddit_session',
value: 'secret',
domain: '.reddit.com',
path: '/',
expires: 1_800_000_000,
httpOnly: true,
secure: true,
sameSite: 'lax',
},
],
matrix: {
homeserver: 'https://matrix.redditspace.com',
accessToken: 'matrix-secret',
userId: '@t2_bot:reddit.com',
capturedAt: 1_700_000_000_000,
},
});
});
it('refuses to create an incomplete dump without Matrix credentials', () => {
expect(() => createSessionExportDownload([], null)).toThrow(/Reddit Chat/i);
});
});
describe('assertMatchingSessionIdentity', () => {
it('accepts a Matrix user bound to the current Reddit account ID', () => {
expect(() => assertMatchingSessionIdentity(
{ username: 'bot-account', accountId: 'bot' },
'@t2_bot:reddit.com',
)).not.toThrow();
});
it('rejects a stale Matrix capture from another Reddit account', () => {
expect(() => assertMatchingSessionIdentity(
{ username: 'bot-account', accountId: 't2_bot' },
'@t2_other:reddit.com',
)).toThrow(/Reload Reddit Chat after switching accounts/);
});
});