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.

8.8 KB
Raw Blame
/** Direct-import coverage for research_form.js warning rendering and refresh recovery. */

import '@js/config/urls.js';
import '@js/security/xss-protection.js';

async function flushPromises(turns = 8) {
    for (let turn = 0; turn < turns; turn += 1) {
        await Promise.resolve();
    }
}

async function loadWarningRuntime() {
    vi.resetModules();
    vi.stubGlobal('URLS', window.URLS);
    await import('@js/research_form.js');
}

beforeEach(() => {
    document.body.innerHTML = '<div id="research-alert"></div>';
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    delete window.api;
    delete window.refetchSettingsAndUpdateWarnings;
    delete window.displayWarnings;
    delete window.clearAllWarnings;
    delete window.checkAndDisplayWarnings;
    delete window.dismissWarning;
    document.body.replaceChildren();
});

it('renders only safe warning actions, escapes fields, and clears the surface', async () => {
    await loadWarningRuntime();
    const hostile = '<img src=x onerror="window.__warningXss=true">';

    window.displayWarnings([
        {
            type: 'searxng_recommendation',
            icon: hostile,
            title: `Use ${hostile}`,
            message: `Configure ${hostile}`,
            actionUrl: '/settings/?tab=search&engine=<local>',
            actionLabel: `Open ${hostile}`,
            dismissKey: 'app.warnings.dismiss_search_hint',
        },
        {
            type: 'configuration',
            icon: '!',
            title: 'External action rejected',
            message: 'Keep navigation inside the application',
            actionUrl: '//attacker.example/leave',
            actionLabel: 'Leave',
            dismissKey: null,
        },
    ]);

    const alert = document.getElementById('research-alert');
    const banners = alert.querySelectorAll('.warning-banner');
    expect(banners).toHaveLength(2);
    expect(banners[0].classList.contains('ldr-alert-info')).toBe(true);
    expect(banners[1].classList.contains('ldr-alert-warning')).toBe(true);
    expect(alert.querySelector('img')).toBeNull();
    expect(window.__warningXss).toBeUndefined();

    const actions = alert.querySelectorAll('.ldr-alert-action');
    expect(actions).toHaveLength(1);
    expect(actions[0].getAttribute('href'))
        .toBe('/settings/?tab=search&engine=<local>');
    expect(actions[0].textContent).toContain(`Open ${hostile}`);

    const dismissButtons = alert.querySelectorAll('button');
    expect(dismissButtons).toHaveLength(1);
    expect(dismissButtons[0].getAttribute('onclick')).toBeNull();
    expect(dismissButtons[0].dataset.dismissKey)
        .toBe('app.warnings.dismiss_search_hint');
    expect(alert.style.display).toBe('block');

    window.clearAllWarnings();

    expect(alert.style.display).toBe('none');
    expect(alert.childElementCount).toBe(0);
});

it.each([
    '/\\outside.example/leave',
    '/\t/outside.example/leave',
    '/\n/outside.example/leave',
    '/\r/outside.example/leave',
])('omits warning actions that the browser resolves off-origin: %j', async actionUrl => {
    await loadWarningRuntime();
    expect(new URL(actionUrl, window.location.href).origin)
        .not.toBe(window.location.origin);

    window.displayWarnings([{
        type: 'configuration',
        icon: '!',
        title: 'Review settings',
        message: 'Open the settings page',
        actionUrl,
        actionLabel: 'Review',
    }]);

    expect(document.querySelector('.ldr-alert-action')).toBeNull();
});

it('dismisses through the migrated settings contract and refreshes warnings', async () => {
    window.api = { getCsrfToken: vi.fn(() => 'csrf-warning-3299') };
    const fetchMock = vi.fn((url, options = {}) => {
        if (
            url === '/settings/save_all_settings'
            && options.method === 'POST'
        ) {
            return Promise.resolve(new Response(JSON.stringify({
                status: 'success',
            }), { status: 200 }));
        }
        if (url === '/settings/api/warnings') {
            return Promise.resolve(new Response(JSON.stringify({
                warnings: [],
            }), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    await loadWarningRuntime();
    window.displayWarnings([{
        type: 'configuration',
        icon: '!',
        title: 'Dismiss me',
        message: 'Persist this choice',
        dismissKey: 'app.warnings.dismiss_research_hint',
    }]);

    document.querySelector('.ldr-warning-dismiss').click();

    await vi.waitFor(() => {
        expect(fetchMock).toHaveBeenCalledWith('/settings/api/warnings');
    });
    expect(fetchMock).toHaveBeenCalledWith(
        '/settings/save_all_settings',
        {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': 'csrf-warning-3299',
            },
            body: JSON.stringify({
                'app.warnings.dismiss_research_hint': true,
            }),
        },
    );
    expect(document.getElementById('research-alert').style.display)
        .toBe('none');
});

it('keeps the warning and skips refresh when dismissal is rejected', async () => {
    window.api = { getCsrfToken: vi.fn(() => 'csrf-warning-3299') };
    const fetchMock = vi.fn(() => Promise.resolve(new Response(JSON.stringify({
        status: 'error',
        detail: 'Dismissal was rejected',
    }), { status: 409 })));
    vi.stubGlobal('fetch', fetchMock);
    await loadWarningRuntime();
    window.displayWarnings([{
        type: 'configuration',
        icon: '!',
        title: 'Still relevant',
        message: 'Do not hide this warning',
        dismissKey: 'app.warnings.dismiss_research_hint',
    }]);

    const logger = vi.spyOn(SafeLogger, 'error');
    document.querySelector('.ldr-warning-dismiss').click();

    await vi.waitFor(() => {
        expect(logger).toHaveBeenCalledWith(
            'Failed to dismiss warning:',
            expect.objectContaining({ message: 'Dismissal was rejected' }),
        );
    });
    expect(fetchMock).toHaveBeenCalledOnce();
    expect(document.getElementById('research-alert').textContent)
        .toContain('Still relevant');
});

it('rechecks warnings after both successful and failed settings refreshes', async () => {
    vi.useFakeTimers();
    await loadWarningRuntime();
    let settingsCalls = 0;
    let warningCalls = 0;
    const fetchMock = vi.fn(url => {
        if (url === '/settings/api') {
            settingsCalls += 1;
            if (settingsCalls === 1) {
                return Promise.resolve(new Response(JSON.stringify({
                    status: 'success',
                    settings: {
                        'search.iterations': { value: 3 },
                    },
                }), { status: 200 }));
            }
            return Promise.reject(new Error('settings temporarily offline'));
        }
        if (url === '/settings/api/warnings') {
            warningCalls += 1;
            return Promise.resolve(new Response(JSON.stringify(
                warningCalls === 1
                    ? {
                        warnings: [{
                            type: 'configuration',
                            icon: '!',
                            title: 'Current warning',
                            message: 'Rendered after settings refresh',
                        }],
                    }
                    : { warnings: [] },
            ), { status: 200 }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);

    window.refetchSettingsAndUpdateWarnings();
    await flushPromises();
    expect(settingsCalls).toBe(1);
    expect(vi.getTimerCount()).toBe(1);

    await vi.advanceTimersByTimeAsync(100);
    await flushPromises();
    expect(warningCalls).toBe(1);
    expect(document.getElementById('research-alert').textContent)
        .toContain('Rendered after settings refresh');

    window.refetchSettingsAndUpdateWarnings();
    await flushPromises();
    expect(settingsCalls).toBe(2);
    expect(vi.getTimerCount()).toBe(1);

    await vi.advanceTimersByTimeAsync(100);
    await flushPromises();
    expect(warningCalls).toBe(2);
    expect(document.getElementById('research-alert').style.display)
        .toBe('none');

    window.displayWarnings([{
        type: 'configuration',
        icon: '!',
        title: 'Temporary warning',
        message: 'Must clear after a valid empty refresh',
    }]);
    fetchMock.mockResolvedValueOnce(new Response(JSON.stringify({ warnings: [] }), { status: 200 }));

    await window.checkAndDisplayWarnings();

    expect(document.getElementById('research-alert').style.display)
        .toBe('none');
    expect(document.getElementById('research-alert').textContent).toBe('');
});