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.

17.9 KB
Raw Blame
/**
 * Saved search engines must survive the research page's real initialization,
 * refresh, dropdown selection, and submission paths without a silent swap.
 */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

import '@js/config/urls.js';
import '@js/utils/alert-helpers.js';
import '@js/security/xss-protection.js';
import '@js/utils/form-validation.js';
import '@js/components/custom_dropdown.js';
import '@js/services/api.js';

const SOURCE = readFileSync(resolve(
    __dirname,
    '../../../src/local_deep_research/web/static/js/components/research.js',
), 'utf8');
const ENGINES_URL = '/settings/api/available-search-engines';
const START_URL = '/api/start_research';
const protectedChatRequest = window.api.fetchWithCsrfRecovery;
const HINT = 'Wayback (saved; choose a configured search provider for topics)';
const ENGINES = [
    { value: 'brave', label: 'Brave' },
    { value: 'library', label: 'Search All Collections' },
    { value: 'searxng', label: 'SearXNG' },
];

let fetchMock;
let engines;
const documentListeners = [];

function response(body) {
    return Promise.resolve({ ok: true, status: 200, json: async () => body });
}

async function settle() {
    // Drain the full bootstrap/settings/save/refresh promise chains.
    await new Promise(done => setTimeout(done, 0));
}

function deferred() {
    let finish;
    const promise = new Promise(done => { finish = done; });
    return { promise, resolve: finish };
}

function settingsResponse(saved) {
    return response({ settings: {
        'search.tool': { value: saved, editable: true },
        'llm.model': { value: 'test-model', editable: true },
    } });
}

function modelsResponse() {
    return response({
        provider_options: [{ value: 'OLLAMA', label: 'Ollama', is_cloud: false }],
        providers: { ollama_models: [{ value: 'test-model', label: 'Test model' }] },
    });
}

async function mount({ initial = 'wayback', saved = initial, options = ENGINES, settingsRequest = null, modelsRequest = null, enginesRequest = null } = {}) {
    engines = options;
    document.body.innerHTML = `
        <form id="research-form">
            <div id="research-alert" role="alert" style="display:none"></div>
            <textarea id="query">A research topic</textarea>
            <label class="ldr-mode-option" data-mode="quick">
                <input name="research_mode" type="radio" value="quick" checked>
            </label>
            <label class="ldr-mode-option" data-mode="chat">
                <input name="research_mode" type="radio" value="chat">
            </label>
            <button type="button" class="ldr-advanced-options-toggle">
                <i></i><span class="sr-only"></span>
            </button>
            <div class="ldr-advanced-options-panel">
                <select id="model_provider"><option value="OLLAMA">Ollama</option></select>
                <div class="ldr-form-group">
                    <input id="model" data-initial-value="test-model">
                    <input id="model_hidden" name="llm.model" value="test-model">
                    <div id="model-dropdown"><div id="model-dropdown-list"></div></div>
                </div>
                <div class="ldr-form-group">
                    <div class="form-group">
                        <div class="ldr-custom-dropdown-with-refresh">
                            <div class="ldr-custom-dropdown" id="search-engine-dropdown">
                                <input id="search_engine" aria-describedby="engine-help" role="combobox">
                                <input id="search_engine_hidden" name="search_engine" type="hidden">
                                <div id="search-engine-dropdown-list" role="listbox"></div>
                            </div>
                            <button type="button" id="search_engine-refresh"><i></i></button>
                        </div>
                        <span id="engine-help">Choose a search engine</span>
                    </div>
                </div>
            </div>
            <button id="start-research-btn" type="submit"><span></span></button>
        </form>
    `;
    const input = document.getElementById('search_engine');
    input.setAttribute('data-initial-value', initial);
    const hidden = document.getElementById('search_engine_hidden');
    hidden.value = initial;
    const hiddenWrites = [];
    const valueProperty = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value');
    Object.defineProperty(hidden, 'value', {
        get() { return valueProperty.get.call(this); },
        set(value) {
            hiddenWrites.push(value);
            valueProperty.set.call(this, value);
        },
    });
    localStorage.setItem('advancedMenuOpen', 'false');
    window.api = {
        getCsrfToken: () => 'test-csrf',
        fetchWithCsrfRecovery: protectedChatRequest,
    };

    fetchMock = vi.fn((url, init = {}) => {
        if (String(url).startsWith(ENGINES_URL)) {
            return enginesRequest || response({ engine_options: engines });
        }
        if (String(url).startsWith('/settings/api/available-models')) {
            return modelsRequest || modelsResponse();
        }
        if (url === '/settings/api' && (!init.method || init.method === 'GET')) {
            return settingsRequest || settingsResponse(saved);
        }
        // A completed failure keeps the test on this page, without navigation.
        if (url === START_URL || url === '/api/chat/sessions') {
            return response({ success: false, message: 'Test response' });
        }
        return response({ status: 'ok' });
    });
    vi.stubGlobal('fetch', fetchMock);

    // Run the entire shipped IIFE in a fresh closure for every case. Capture
    // only its DOMContentLoaded registration so old bootstraps cannot rerun.
    let initialize;
    const addEventListener = document.addEventListener.bind(document);
    vi.spyOn(document, 'addEventListener').mockImplementation((type, listener, config) => {
        if (type === 'DOMContentLoaded') {
            initialize = listener;
        } else {
            documentListeners.push([type, listener, config]);
            addEventListener(type, listener, config);
        }
    });
    new Function(SOURCE)(); // eslint-disable-line no-new-func
    initialize();
    await settle();
    if (!modelsRequest && !enginesRequest) {
        expect(fetchMock.mock.calls.some(([url]) => url === '/settings/api')).toBe(true);
        expect(document.getElementById('model_hidden').value).toBe('test-model');
    }
    return { hiddenWrites };
}

function submit() {
    document.getElementById('research-form').dispatchEvent(
        new Event('submit', { bubbles: true, cancelable: true }),
    );
}

function startCalls() {
    return fetchMock.mock.calls.filter(([url]) => url === START_URL);
}

function hiddenValue() {
    return document.getElementById('search_engine_hidden').value;
}

function warning() {
    return document.getElementById('saved-search-engine-warning');
}

async function refresh() {
    document.getElementById('search_engine-refresh').click();
    await settle();
}

function selectEngine(value) {
    document.getElementById('search_engine').click();
    const option = document.querySelector(`#search-engine-dropdown-list [data-value="${value}"]`);
    expect(option).not.toBeNull();
    option.click();
}

beforeEach(() => {
    localStorage.clear();
    sessionStorage.clear();
});

afterEach(async () => {
    await settle();
    for (const args of documentListeners.splice(0)) document.removeEventListener(...args);
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    document.body.replaceChildren();
    delete window.api;
});

it('keeps saved Wayback visible with a persistent actionable notice through initialization and refresh', async () => {
    const { hiddenWrites } = await mount();
    expect(document.getElementById('search_engine').value).toBe(HINT);
    expect(hiddenValue()).toBe('wayback');
    expect(warning()?.textContent).toMatch(/choose a configured search provider/i);
    expect(warning()?.style.display).toBe('block');
    expect(document.querySelector('.ldr-advanced-options-panel').classList.contains('ldr-expanded')).toBe(true);
    expect(document.getElementById('search_engine').getAttribute('aria-describedby')).toContain('engine-help');
    expect(document.getElementById('search_engine').getAttribute('aria-describedby')).toContain(warning().id);
    expect(hiddenWrites.length).toBeGreaterThan(0);
    expect(new Set(hiddenWrites)).toEqual(new Set(['wayback']));
    expect(fetchMock.mock.calls.filter(([url, init]) => url === '/settings/api/search.tool' && init?.method === 'PUT')).toEqual([]);

    await refresh();
    expect(hiddenValue()).toBe('wayback');
    expect(document.getElementById('search_engine').value).toBe(HINT);
    expect(warning().style.display).toBe('block');
    expect(document.querySelectorAll('#saved-search-engine-warning')).toHaveLength(1);
    expect(new Set(hiddenWrites)).toEqual(new Set(['wayback']));
});

it('uses an unavailable saved value from the settings API even when template values were configured', async () => {
    await mount({ initial: 'searxng', saved: 'wayback' });
    expect(hiddenValue()).toBe('wayback');
    expect(document.getElementById('search_engine').value).toBe(HINT);
    expect(warning()?.style.display).toBe('block');
});

it('preserves an explicit configured selection when a pending initial settings GET returns saved Wayback', async () => {
    const settings = deferred();
    await mount({ settingsRequest: settings.promise });
    selectEngine('brave');
    expect(hiddenValue()).toBe('brave');
    expect(warning().style.display).toBe('none');

    settings.resolve(settingsResponse('wayback'));
    await settle();
    expect(hiddenValue()).toBe('brave');
    expect(document.getElementById('search_engine').value).toBe('Brave');
    expect(warning().style.display).toBe('none');
    await refresh();
    expect(hiddenValue()).toBe('brave');
    submit();
    await settle();
    expect(startCalls()).toHaveLength(1);
    expect(JSON.parse(startCalls()[0][1].body).search_engine).toBe('brave');
});

it('uses configured API settings over stale template Wayback when the user has not selected an engine', async () => {
    const settings = deferred();
    await mount({ settingsRequest: settings.promise });
    expect(hiddenValue()).toBe('wayback');
    expect(warning().style.display).toBe('block');

    settings.resolve(settingsResponse('brave'));
    await settle();
    expect(hiddenValue()).toBe('brave');
    expect(document.getElementById('search_engine').value).toBe('Brave');
    expect(warning().style.display).toBe('none');
    expect(fetchMock.mock.calls.filter(([url, init]) => url === '/settings/api/search.tool' && init?.method === 'PUT')).toEqual([]);
});

it.each(['wayback', ''])('preserves a configured selection made before model/template hydration with saved API value %j', async saved => {
    const models = deferred();
    await mount({ saved, modelsRequest: models.promise });
    expect(fetchMock.mock.calls.some(([url]) => url === '/settings/api')).toBe(false);
    expect(hiddenValue()).toBe('wayback');
    selectEngine('brave');
    expect(hiddenValue()).toBe('brave');

    models.resolve(modelsResponse());
    await settle();
    expect(fetchMock.mock.calls.some(([url]) => url === '/settings/api')).toBe(true);
    expect(hiddenValue()).toBe('brave');
    expect(document.getElementById('search_engine').value).toBe('Brave');
    expect(warning().style.display).toBe('none');
    submit();
    await settle();
    expect(startCalls()).toHaveLength(1);
    expect(JSON.parse(startCalls()[0][1].body).search_engine).toBe('brave');
});

it.each(['wayback', 'brave', ''])('blocks research until the initial engine catalog is reconciled for saved value %j', async initial => {
    const catalog = deferred();
    await mount({ initial, enginesRequest: catalog.promise });
    submit();
    await settle();
    expect(startCalls()).toEqual([]);
    expect(document.getElementById('research-alert').textContent).toMatch(/search providers are still loading/i);
    expect(document.getElementById('start-research-btn').disabled).toBe(false);

    catalog.resolve(response({ engine_options: ENGINES }));
    await settle();
    expect(document.getElementById('research-alert').textContent.trim()).toBe('');
    if (initial === 'wayback') {
        expect(hiddenValue()).toBe('wayback');
        submit();
        expect(startCalls()).toEqual([]);
        selectEngine('brave');
    } else {
        expect(hiddenValue()).toBe(initial || 'searxng');
    }
    submit();
    await settle();
    expect(startCalls()).toHaveLength(1);
    expect(JSON.parse(startCalls()[0][1].body).search_engine).toBe(initial === 'wayback' ? 'brave' : initial || 'searxng');
});

it('clears the loading message after a failed catalog load while retaining the saved-unavailable submit guard', async () => {
    const catalog = deferred();
    await mount({ enginesRequest: catalog.promise });
    submit();
    expect(startCalls()).toEqual([]);
    catalog.resolve({ ok: false, status: 503 });
    await settle();
    expect(document.getElementById('research-alert').textContent.trim()).toBe('');
    expect(hiddenValue()).toBe('wayback');
    expect(warning()?.style.display).toBe('block');
    submit();
    expect(startCalls()).toEqual([]);
});

it('allows chat while the initial engine catalog is still loading', async () => {
    const catalog = deferred();
    await mount({ enginesRequest: catalog.promise });
    document.querySelector('input[value="chat"]').checked = true;
    submit();
    await settle();
    expect(fetchMock.mock.calls.filter(([url]) => url === '/api/chat/sessions')).toHaveLength(1);
    expect(startCalls()).toEqual([]);
    catalog.resolve(response({ engine_options: ENGINES }));
    await settle();
});

it('blocks research until a configured engine is explicitly selected, then clears the warning and submits that engine', async () => {
    await mount();
    submit();
    await settle();
    expect(startCalls()).toEqual([]);
    expect(document.getElementById('search_engine')).toBe(document.activeElement);
    expect(document.getElementById('start-research-btn').disabled).toBe(false);
    expect(warning()?.textContent).toMatch(/choose a configured search provider/i);

    // Typing is a filter, not a configured selection.
    document.getElementById('search_engine').value = 'SearXNG';
    document.getElementById('search_engine').dispatchEvent(new Event('input'));
    submit();
    expect(startCalls()).toEqual([]);
    expect(hiddenValue()).toBe('wayback');

    selectEngine('searxng');
    expect(warning().style.display).toBe('none');
    expect(document.getElementById('research-alert').textContent.trim()).toBe('');
    await settle();
    await refresh();
    expect(hiddenValue()).toBe('searxng');
    expect(warning().style.display).toBe('none');
    expect(document.getElementById('search_engine').getAttribute('aria-describedby')).not.toContain(warning().id);

    submit();
    await settle();
    expect(startCalls()).toHaveLength(1);
    expect(JSON.parse(startCalls()[0][1].body).search_engine).toBe('searxng');
});

it('clears the notice when a refresh makes the saved engine configured', async () => {
    await mount({ initial: 'temporarily-missing' });
    expect(hiddenValue()).toBe('temporarily-missing');
    expect(warning()?.style.display).toBe('block');
    engines = [...ENGINES, { value: 'temporarily-missing', label: 'Configured engine' }];
    await refresh();
    expect(hiddenValue()).toBe('temporarily-missing');
    expect(document.getElementById('search_engine').value).toBe('Configured engine');
    expect(warning().style.display).toBe('none');
    submit();
    await settle();
    expect(startCalls()).toHaveLength(1);
});

it('keeps a configured saved choice instead of the preferred default', async () => {
    await mount({ initial: 'brave' });
    expect(hiddenValue()).toBe('brave');
    expect(document.getElementById('search_engine').value).toBe('Brave');
    expect(warning()).toBeNull();
    submit();
    await settle();
    expect(startCalls()).toHaveLength(1);
    expect(JSON.parse(startCalls()[0][1].body).search_engine).toBe('brave');
});

it('still chooses the normal preferred default when no choice was saved', async () => {
    await mount({ initial: '', saved: '' });
    expect(hiddenValue()).toBe('searxng');
    expect(document.getElementById('search_engine').value).toBe('SearXNG');
    expect(warning()).toBeNull();
});

it('retains existing fallback behavior for an engine listed but disabled by egress policy', async () => {
    await mount({
        initial: 'brave',
        options: ENGINES.map(engine => (engine.value === 'brave'
            ? { ...engine, egress: { allowed: false, reason: 'scope_mismatch_private_only' } }
            : engine)),
    });
    expect(hiddenValue()).toBe('searxng');
    expect(warning()).toBeNull();
});

it('does not dismiss the saved-engine notice when a disabled option is clicked', async () => {
    await mount({ options: ENGINES.map(engine => ({
        ...engine,
        egress: { allowed: false, reason: 'scope_mismatch_private_only' },
    })) });
    selectEngine('searxng');
    expect(hiddenValue()).toBe('wayback');
    expect(warning()?.style.display).toBe('block');
    submit();
    expect(startCalls()).toEqual([]);
});

it('retains the existing clear-selection behavior when the saved configured engine and all alternatives are disabled', async () => {
    await mount({ initial: 'brave', options: ENGINES.map(engine => ({
        ...engine,
        egress: { allowed: false, reason: 'scope_mismatch_private_only' },
    })) });
    expect(hiddenValue()).toBe('');
    expect(document.getElementById('search_engine').value).toBe('');
    expect(warning()).toBeNull();
});

it('does not block chat, which does not submit a search engine', async () => {
    await mount();
    document.querySelector('input[value="chat"]').checked = true;
    submit();
    await settle();
    expect(fetchMock.mock.calls.filter(([url]) => url === '/api/chat/sessions')).toHaveLength(1);
    expect(startCalls()).toEqual([]);
});