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

52 lines
1.6 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { createSessionExportDownload } from '../extensions/reddit-session-exporter/session-export.js';
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);
});
});