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.

12.5 KB
Raw Blame
/**
 * Subscription card actions dispatch through one delegated grid listener.
 *
 * Each card carries its subscription ID as inert data (data-subscription-id),
 * and the card and its buttons name their action in data-subscription-action.
 * The buttons used to embed the ID in inline onclick JavaScript, where the
 * browser decodes escapeHtml's ' back into a quote before compiling it.
 * These tests boot the real page script with an ID built to break out of such
 * a string, then follow it from each click to the request it produces.
 */

const HOSTILE_ID = "sub\"&<');window.__subscriptionCardActionXss=true;//";
// The page percent-encodes the ID into the request path, so its quote, "&"
// and "/" characters stay inside one path segment.
const ACTION_URL = `/news/api/subscriptions/${encodeURIComponent(HOSTILE_ID)}`;
// The page makes these requests when it boots, before any click.
const BOOT_URLS = new Set([
    '/news/api/subscriptions/current',
    '/news/api/subscription/folders',
    '/news/api/scheduler/status',
]);

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>
        <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></select>
        <select id="frequency-filter"><option value="all">All</option></select>
        <main id="subscriptions-grid"></main>
    `;
}

let fetchMock;
let registeredDocumentListeners;
let registeredGridListeners;
let linkClickDefaultPrevented;

// happy-dom follows a clicked link by navigating the test window. This
// listener runs after every page listener: it records whether the page left
// the link's navigation alone, then cancels the navigation.
function cancelLinkNavigation(event) {
    if (!event.target.closest('a')) return;
    linkClickDefaultPrevented.push(event.defaultPrevented);
    event.preventDefault();
}

beforeEach(async () => {
    vi.resetModules();
    vi.useFakeTimers();
    registeredDocumentListeners = [];
    registeredGridListeners = [];
    linkClickDefaultPrevented = [];
    installDom();
    await import('@js/security/xss-protection.js');
    await import('@js/utils/alert-helpers.js');
    window.api = { getCsrfToken: vi.fn(() => 'csrf-card-actions') };
    window.RESEARCH_STATUS = { QUEUED: 'queued' };

    fetchMock = vi.fn(async input => {
        const url = String(input);
        if (url === '/news/api/subscriptions/current') {
            return jsonResponse({
                subscriptions: [{
                    id: HOSTILE_ID,
                    name: 'Hostile ID watch',
                    query: 'hostile id query',
                    is_active: true,
                    folder_id: null,
                    refresh_minutes: 60,
                    created_at: '2026-10-01T10:00:00.000Z',
                    total_runs: 0,
                    source_id: 'source-3299',
                }],
            });
        }
        if (url === '/news/api/subscription/folders') {
            return jsonResponse({ folders: [] });
        }
        if (url === '/news/api/scheduler/status') {
            return jsonResponse({ scheduler_available: false });
        }
        // Every card action gets a failure. These tests only follow the ID
        // from the click to the request it produces.
        return jsonResponse({}, 503);
    });
    vi.stubGlobal('fetch', fetchMock);
    vi.stubGlobal('confirm', vi.fn(() => true));

    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);
        });
    const grid = document.getElementById('subscriptions-grid');
    const originalGridAdd = grid.addEventListener.bind(grid);
    const gridAddSpy = vi.spyOn(grid, 'addEventListener')
        .mockImplementation((type, listener, options) => {
            registeredGridListeners.push([type, listener, options]);
            originalGridAdd(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(1);
        });
    } finally {
        documentAddSpy.mockRestore();
        gridAddSpy.mockRestore();
    }
    document.addEventListener('click', cancelLinkNavigation);
});

afterEach(() => {
    document.removeEventListener('click', cancelLinkNavigation);
    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.api;
    delete window.RESEARCH_STATUS;
    delete window.runSubscriptionNow;
    delete window.toggleSubscription;
    delete window.viewSubscriptionHistory;
    delete window.deleteSubscriptionDirect;
    delete window.showSchedulerInfo;
    delete window.formatNextUpdate;
});

function subscriptionCard() {
    return document.querySelector('.ldr-subscription-card');
}

function cardButtonIcon(action) {
    return subscriptionCard()
        .querySelector(`button[data-subscription-action="${action}"] i`);
}

// Requests made after the first `start` fetch calls, without boot requests.
function requestsSince(start) {
    return fetchMock.mock.calls.slice(start)
        .map(([url, options = {}]) => [String(url), options.method || 'GET'])
        .filter(([url]) => !BOOT_URLS.has(url));
}

/**
 * The card carries no inline handler at all.
 *
 * This used to allow exactly one: the Edit link's constant
 * onclick="event.stopPropagation();", which kept a click on the link from
 * also opening the card's history. That premise changed. The grid listener
 * now ignores any click inside a link, so the Edit link needs no handler
 * and the card has none.
 */
it('keeps every inline handler off the card', () => {
    const card = subscriptionCard();
    expect(card.dataset.subscriptionId).toBe(HOSTILE_ID);

    const inlineHandlers = [card, ...card.querySelectorAll('*')]
        .flatMap(element => [...element.attributes]
            .filter(attribute => attribute.name.startsWith('on'))
            .map(attribute => [element.tagName, attribute.name, attribute.value]));
    expect(inlineHandlers).toEqual([]);
});

it.each([
    ['the card body', '.ldr-query-text', [[`${ACTION_URL}/history`, 'GET']]],
    ['Run now', 'button[data-subscription-action="run"] i', [[`${ACTION_URL}/run`, 'POST']]],
    ['Pause', 'button[data-subscription-action="toggle"] i', [[ACTION_URL, 'PUT']]],
    ['View history', 'button[data-subscription-action="history"] i', [[`${ACTION_URL}/history`, 'GET']]],
    ['Delete', 'button[data-subscription-action="delete"] i', [[ACTION_URL, 'DELETE']]],
])('a click on %s runs exactly one action, once', async (_label, selector, expected) => {
    const start = fetchMock.mock.calls.length;

    subscriptionCard().querySelector(selector).click();

    // Each handler sends its request during the click. A second dispatch of
    // the same click (a button also opening history, or a card-body click
    // opening it twice) would add a request here.
    expect(requestsSince(start)).toEqual(expected);
    // Every action fails against the 503 stub. Its error alert shows that
    // the handler has finished, and still no other request was made.
    await vi.waitFor(() => {
        expect(document.querySelector('.alert-danger')).not.toBeNull();
    });
    expect(requestsSince(start)).toEqual(expected);
});

it.each([
    ['the Edit link', 'a[title="Edit"] i'],
    ['the View original research link', 'a.ldr-source-link'],
])('a click on %s only follows the link', (_label, selector) => {
    const start = fetchMock.mock.calls.length;

    subscriptionCard().querySelector(selector).click();

    expect(requestsSince(start)).toEqual([]);
    // The page did not cancel the navigation.
    expect(linkClickDefaultPrevented).toEqual([false]);
});

/**
 * The grid listener is the only listener that acts on a card click.
 *
 * A second card listener, such as one on document, would run again for any
 * click the grid listener lets bubble: a click on the card body would open
 * its history twice. With the grid listener removed, no click anywhere on
 * the card may make a request.
 */
it('dispatches card clicks through the grid listener alone', () => {
    const grid = document.getElementById('subscriptions-grid');
    const gridClickListeners = registeredGridListeners
        .filter(([type]) => type === 'click');
    gridClickListeners.forEach(([type, listener, options]) => {
        grid.removeEventListener(type, listener, options);
    });
    const start = fetchMock.mock.calls.length;

    const card = subscriptionCard();
    [card, ...card.querySelectorAll('*')].forEach(element => element.click());

    expect(requestsSince(start)).toEqual([]);
    // The listener removed above is the page's card dispatcher.
    expect(gridClickListeners).toHaveLength(1);
});

it('dispatches each card action with the exact subscription ID', async () => {
    cardButtonIcon('run').click();
    cardButtonIcon('toggle').click();
    cardButtonIcon('history').click();
    subscriptionCard().querySelector('.ldr-query-text').click();
    cardButtonIcon('delete').click();

    const actionRequests = () => fetchMock.mock.calls
        .map(([url, options = {}]) => [String(url), options.method || 'GET'])
        .filter(([url]) => (
            url === '/api/start_research' || url.startsWith(ACTION_URL)
        ));
    await vi.waitFor(() => {
        expect(actionRequests()).toEqual([
            [`${ACTION_URL}/run`, 'POST'],
            [ACTION_URL, 'PUT'],
            [`${ACTION_URL}/history`, 'GET'],
            [`${ACTION_URL}/history`, 'GET'],
            [ACTION_URL, 'DELETE'],
        ]);
    });
    const [, runOptions] = fetchMock.mock.calls
        .find(([url]) => url === `${ACTION_URL}/run`);
    expect(runOptions.headers['X-CSRFToken']).toBe('csrf-card-actions');
    expect(runOptions.credentials).toBe('same-origin');
    expect(runOptions.body).toBeUndefined();
    expect(confirm).toHaveBeenCalledOnce();
});

it('shows the saved-interval refusal from Run now and permits retry after repair', async () => {
    const start = fetchMock.mock.calls.length;
    const message = "Edit this subscription's refresh interval before running it";
    fetchMock.mockResolvedValueOnce(jsonResponse({ error: message }, 400));

    cardButtonIcon('run').click();

    expect(requestsSince(start)).toEqual([[`${ACTION_URL}/run`, 'POST']]);
    await vi.waitFor(() => {
        expect(document.querySelector('.alert-danger')?.textContent)
            .toContain('refresh interval before running it');
    });
    expect(document.querySelector('.alert-danger').textContent)
        .toContain('Edit this subscription');
    expect(localStorage.getItem('active_news_research')).toBeNull();

    fetchMock.mockResolvedValueOnce(jsonResponse({
        status: 'success',
        research_id: '12345678-1234-1234-1234-123456789abc',
    }));
    cardButtonIcon('run').click();

    await vi.waitFor(() => {
        expect(document.querySelector('.alert-success')?.textContent)
            .toContain('Research started!');
    });
    expect(requestsSince(start)).toEqual([
        [`${ACTION_URL}/run`, 'POST'],
        [`${ACTION_URL}/run`, 'POST'],
    ]);
    expect(JSON.parse(localStorage.getItem('active_news_research')).researchId)
        .toBe('12345678-1234-1234-1234-123456789abc');
});