Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

15.9 KB
Raw Blame
/** Direct browser-runtime coverage for the migrated subscriptions page. */

const NOW = new Date('2026-09-01T12:00:00.000Z');

function jsonResponse(payload, status = 200) {
    return {
        ok: status >= 200 && status < 300,
        status,
        statusText: status === 200 ? 'OK' : 'Request failed',
        json: vi.fn().mockResolvedValue(payload),
    };
}

function installDom() {
    document.body.innerHTML = `
        <header class="ldr-page-header"><h1>Subscriptions</h1></header>
        <span id="total-subscriptions">0</span>
        <span id="active-subscriptions">0</span>
        <span id="paused-subscriptions">0</span>
        <span id="updates-today">0</span>
        <span id="status-indicator"></span>
        <span id="status-text"></span>
        <span id="scheduler-details"></span>
        <div class="ldr-folder-list">
            <div class="ldr-folder-item active" data-folder="all">
                All <span class="ldr-folder-count">0</span>
            </div>
            <div class="ldr-folder-item" data-folder="uncategorized">
                Uncategorized <span class="ldr-folder-count">0</span>
            </div>
        </div>
        <button id="add-folder-btn"></button>
        <button id="create-subscription-btn"></button>
        <input id="subscription-search">
        <select id="status-filter">
            <option value="all">All</option>
            <option value="active">Active</option>
            <option value="paused">Paused</option>
        </select>
        <select id="frequency-filter">
            <option value="all">All</option>
            <option value="hourly">Hourly</option>
            <option value="daily">Daily</option>
            <option value="weekly">Weekly</option>
        </select>
        <main id="subscriptions-grid"></main>
    `;
}

let serverSubscriptions;
let serverFolders;
let fetchMock;
let modalShow;
let registeredDocumentListeners;

beforeEach(async () => {
    vi.resetModules();
    vi.useFakeTimers();
    vi.setSystemTime(NOW);
    registeredDocumentListeners = [];
    localStorage.clear();
    installDom();

    const purify = {
        isSupported: true,
        addHook: vi.fn(),
        sanitize: vi.fn(dirty => {
            const template = document.createElement('template');
            // eslint-disable-next-line no-unsanitized/property -- test-only sanitizer stand-in for repo-owned fixtures.
            template.innerHTML = String(dirty);
            template.content.querySelectorAll('script, iframe, object')
                .forEach(node => node.remove());
            template.content.querySelectorAll('*').forEach(node => {
                for (const attr of [...node.attributes]) {
                    if (attr.name.startsWith('on')) node.removeAttribute(attr.name);
                }
            });
            return template.innerHTML;
        }),
    };
    globalThis.DOMPurify = purify;
    window.DOMPurify = purify;
    await import('@js/security/xss-protection.js');
    await import('@js/utils/alert-helpers.js');

    window.api = { getCsrfToken: vi.fn(() => 'csrf-subscriptions-direct') };
    window.RESEARCH_STATUS = { QUEUED: 'queued' };
    modalShow = vi.fn();
    const modalInstances = new WeakMap();
    class Modal {
        constructor(element) {
            this.element = element;
            modalInstances.set(element, this);
        }

        show() {
            modalShow(this.element);
        }

        hide() {}

        static getInstance(element) {
            return modalInstances.get(element) || null;
        }
    }
    globalThis.bootstrap = { Modal };
    window.bootstrap = globalThis.bootstrap;

    serverSubscriptions = [{
        id: 'sub-active',
        name: '<img src=x onerror="window.__subscriptionXss=true"> Migration watch',
        query: 'FastAPI migration updates',
        is_active: true,
        folder_id: 'folder-security',
        refresh_minutes: 60,
        refresh_interval: 'hourly',
        last_refreshed: NOW.toISOString(),
        next_refresh: '2026-09-02T12:00:00.000Z',
        created_at: '2026-08-01T10:00:00.000Z',
        total_runs: 4,
        source_id: 'source/3299?view=full',
    }, {
        id: 'sub-paused',
        name: 'Economy daily',
        query: 'Economy and markets',
        is_active: false,
        folder_id: null,
        refresh_minutes: 1440,
        refresh_interval: 'daily',
        last_refreshed: '2026-08-30T10:00:00.000Z',
        created_at: '2026-08-02T10:00:00.000Z',
        total_runs: 0,
    }, {
        id: 'sub-weekly',
        query: 'Security bulletin',
        is_active: true,
        folder_id: null,
        refresh_minutes: 10080,
        refresh_interval: 'weekly',
        created_at: '2026-08-03T10:00:00.000Z',
    }];
    serverFolders = [{
        id: 'folder-security',
        name: '<img src=x onerror="window.__folderXss=true"> Security',
    }];

    fetchMock = vi.fn(async (input, options = {}) => {
        const url = String(input);
        if (url === '/news/api/subscriptions/current') {
            return jsonResponse({
                subscriptions: serverSubscriptions.map(item => ({ ...item })),
            });
        }
        if (url === '/news/api/subscription/folders' && !options.method) {
            return jsonResponse({
                folders: serverFolders.map(folder => ({ ...folder })),
            });
        }
        if (url === '/news/api/scheduler/status') {
            return jsonResponse({
                scheduler_available: true,
                is_running: true,
                active_users: 0,
                scheduled_jobs: 3,
            });
        }
        if (url === '/news/api/subscriptions/sub-active/run') {
            return jsonResponse({
                status: 'queued',
                research_id: 'research-3299<script>',
            });
        }
        if (url === '/news/api/subscriptions/sub-active' && options.method === 'PUT') {
            serverSubscriptions.find(item => item.id === 'sub-active').is_active = false;
            return jsonResponse({ status: 'success' });
        }
        if (url === '/news/api/subscriptions/sub-active/history') {
            return jsonResponse({
                total_runs: 1,
                history: [{
                    research_id: 'history/id?unsafe=true',
                    headline: '<img src=x onerror="window.__historyXss=true"> Result',
                    status: 'completed" onclick="window.__historyXss=true',
                    created_at: NOW.toISOString(),
                    duration_seconds: 12,
                    topics: ['<script>window.__historyXss=true</script>', 'FastAPI'],
                }],
            });
        }
        if (url === '/news/api/subscriptions/sub-paused' && options.method === 'DELETE') {
            serverSubscriptions = serverSubscriptions.filter(item => item.id !== 'sub-paused');
            return jsonResponse({}, 204);
        }
        if (url === '/news/api/subscription/folders' && options.method === 'POST') {
            const folder = { id: 'folder-new', name: JSON.parse(options.body).name };
            serverFolders.push(folder);
            return jsonResponse(folder);
        }
        throw new Error(`Unexpected request: ${url} ${options.method || 'GET'}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('confirm', vi.fn(() => true));
    vi.stubGlobal('prompt', vi.fn(() => '  Migration Team  '));

    const originalDocumentAdd = document.addEventListener.bind(document);
    const documentAddSpy = vi.spyOn(document, 'addEventListener')
        .mockImplementation((type, listener, options) => {
            registeredDocumentListeners.push([type, listener, options]);
            originalDocumentAdd(type, listener, options);
        });
    try {
        await import('@js/pages/subscriptions.js');
        document.dispatchEvent(new Event('DOMContentLoaded'));
        await vi.waitFor(() => {
            expect(document.querySelectorAll('.ldr-subscription-card')).toHaveLength(3);
            expect(document.querySelector('[data-folder="folder-security"]')).not.toBeNull();
            expect(document.getElementById('status-text').textContent)
                .toBe('Not Tracking Your Session');
        });
    } finally {
        documentAddSpy.mockRestore();
    }
});

afterEach(() => {
    registeredDocumentListeners.forEach(([type, listener, options]) => {
        document.removeEventListener(type, listener, options);
    });
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    localStorage.clear();
    document.body.replaceChildren();
    delete window.__subscriptionXss;
    delete window.__folderXss;
    delete window.__historyXss;
    delete window.api;
    delete window.bootstrap;
    delete window.DOMPurify;
    delete window.runSubscriptionNow;
    delete window.toggleSubscription;
    delete window.viewSubscriptionHistory;
    delete window.deleteSubscriptionDirect;
    delete window.showSchedulerInfo;
    delete window.formatNextUpdate;
});

it('boots against the migrated envelopes and renders safe cards, folders, stats, and scheduler state', () => {
    expect(fetchMock).toHaveBeenCalledWith('/news/api/subscriptions/current', {
        credentials: 'same-origin',
    });
    expect(fetchMock).toHaveBeenCalledWith('/news/api/subscription/folders', {
        credentials: 'same-origin',
    });
    expect(fetchMock).toHaveBeenCalledWith('/news/api/scheduler/status', {
        credentials: 'same-origin',
    });
    expect(document.getElementById('total-subscriptions').textContent).toBe('3');
    expect(document.getElementById('active-subscriptions').textContent).toBe('2');
    expect(document.getElementById('paused-subscriptions').textContent).toBe('1');
    expect(document.getElementById('updates-today').textContent).toBe('1');
    expect(document.querySelector('[data-folder="all"] .ldr-folder-count').textContent)
        .toBe('3');
    expect(document.querySelector('[data-folder="uncategorized"] .ldr-folder-count').textContent)
        .toBe('2');
    expect(document.querySelector('img, script')).toBeNull();
    expect(window.__subscriptionXss).toBeUndefined();
    expect(window.__folderXss).toBeUndefined();
    expect(document.querySelector('[data-subscription-id="sub-active"] .ldr-source-link')
        .getAttribute('href')).toBe('/progress/source%2F3299%3Fview%3Dfull');
    expect(document.getElementById('scheduler-details').textContent)
        .toContain('Log out and log back in');
});

it('combines search, status, frequency, and folder filters through real DOM listeners', () => {
    const visibleIds = () => [...document.querySelectorAll('.ldr-subscription-card')]
        .map(card => card.dataset.subscriptionId);
    const status = document.getElementById('status-filter');
    status.value = 'paused';
    status.dispatchEvent(new Event('change'));
    expect(visibleIds()).toEqual(['sub-paused']);

    status.value = 'all';
    status.dispatchEvent(new Event('change'));
    const frequency = document.getElementById('frequency-filter');
    frequency.value = 'weekly';
    frequency.dispatchEvent(new Event('change'));
    expect(visibleIds()).toEqual(['sub-weekly']);

    frequency.value = 'all';
    frequency.dispatchEvent(new Event('change'));
    const search = document.getElementById('subscription-search');
    search.value = 'markets';
    search.dispatchEvent(new Event('input'));
    expect(visibleIds()).toEqual(['sub-paused']);

    search.value = '';
    search.dispatchEvent(new Event('input'));
    document.querySelector('[data-folder="folder-security"]')
        .dispatchEvent(new MouseEvent('click', { bubbles: true }));
    expect(visibleIds()).toEqual(['sub-active']);

    document.querySelector('[data-folder="all"]')
        .dispatchEvent(new MouseEvent('click', { bubbles: true }));
    expect(visibleIds()).toEqual(['sub-active', 'sub-paused', 'sub-weekly']);
});

it('runs research through the real page handler with CSRF and a safe progress link', async () => {
    expect(window.runSubscriptionNow).toEqual(expect.any(Function));

    await window.runSubscriptionNow('sub-active');

    const [, options] = fetchMock.mock.calls.find(([url]) => (
        url === '/news/api/subscriptions/sub-active/run'
    ));
    expect(options).toMatchObject({
        method: 'POST',
        credentials: 'same-origin',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': 'csrf-subscriptions-direct',
        },
    });
    expect(options.body).toBeUndefined();
    expect(fetchMock.mock.calls.some(([url]) => url === '/api/start_research'))
        .toBe(false);
    expect(JSON.parse(localStorage.getItem('active_news_research')))
        .toMatchObject({
            researchId: 'research-3299<script>',
            query: 'FastAPI migration updates',
        });
    const successAlert = [...document.querySelectorAll('.alert-success')]
        .find(alert => alert.textContent.includes('Research started!'));
    expect(successAlert).toBeDefined();
    expect(successAlert.querySelector('a').getAttribute('href'))
        .toBe('/progress/research-3299script');
    expect(successAlert.querySelector('script')).toBeNull();
});

it('renders subscription history as inert content with encoded report IDs', async () => {
    await window.viewSubscriptionHistory('sub-active');

    expect(fetchMock).toHaveBeenCalledWith(
        '/news/api/subscriptions/sub-active/history',
        { credentials: 'same-origin' },
    );
    expect(modalShow).toHaveBeenCalledWith(document.getElementById('historyModal'));
    const modal = document.getElementById('historyModal');
    expect(modal.querySelector('img, script')).toBeNull();
    expect(window.__historyXss).toBeUndefined();
    expect(modal.querySelector('.ldr-history-item-header a').getAttribute('href'))
        .toBe('/progress/history%2Fid%3Funsafe%3Dtrue');
    expect(modal.querySelector('.ldr-status-badge').hasAttribute('onclick'))
        .toBe(false);
    modal.dispatchEvent(new Event('hidden.bs.modal'));
    expect(document.getElementById('historyModal')).toBeNull();
});

it('updates and deletes cards through explicit global inline-action contracts', async () => {
    await window.toggleSubscription('sub-active');
    const toggleCall = fetchMock.mock.calls.find(([, options]) => options.method === 'PUT');
    expect(toggleCall[0]).toBe('/news/api/subscriptions/sub-active');
    expect(JSON.parse(toggleCall[1].body)).toEqual({ is_active: false });
    expect(toggleCall[1].headers['X-CSRFToken']).toBe('csrf-subscriptions-direct');
    expect(document.getElementById('active-subscriptions').textContent).toBe('1');

    await window.deleteSubscriptionDirect('sub-paused');
    const deleteCall = fetchMock.mock.calls.find(([, options]) => options.method === 'DELETE');
    expect(deleteCall[0]).toBe('/news/api/subscriptions/sub-paused');
    expect(deleteCall[1].headers['X-CSRFToken']).toBe('csrf-subscriptions-direct');
    expect(document.querySelector('[data-subscription-id="sub-paused"]')).toBeNull();
    expect(document.getElementById('total-subscriptions').textContent).toBe('2');
});

it('creates a trimmed folder through the bound control and safely re-renders it', async () => {
    document.getElementById('add-folder-btn').click();
    await vi.waitFor(() => {
        expect(document.querySelector('[data-folder="folder-new"]')).not.toBeNull();
    });

    const createCall = fetchMock.mock.calls.find(([url, options]) => (
        url === '/news/api/subscription/folders' && options.method === 'POST'
    ));
    expect(createCall[0]).toBe('/news/api/subscription/folders');
    expect(createCall[1].headers['X-CSRFToken']).toBe('csrf-subscriptions-direct');
    expect(JSON.parse(createCall[1].body)).toEqual({ name: 'Migration Team' });
    expect(document.querySelector('[data-folder="folder-new"]').textContent)
        .toContain('Migration Team');

    window.showSchedulerInfo();
    const alert = [...document.querySelectorAll('.alert-info')].find(element => (
        element.textContent.includes('About the Subscription Scheduler')
    ));
    expect(alert.textContent).toContain('About the Subscription Scheduler');
});