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.

7.2 KB
Raw Blame
/**
 * Cross-provider settings search (#6587): while `llm.provider` selects one
 * provider, the settings search must still surface OTHER providers'
 * settings — the provider scoping that hides them from the browsed list
 * must not swallow explicit search queries. Before the fix, the search
 * path routed through `organizeSettings`, whose provider filter
 * unconditionally dropped other providers' rows, so credentials for a
 * non-selected provider were unreachable without switching AND saving
 * the provider first.
 */

import '@js/config/urls.js';
import '@js/services/api.js';
import '@js/utils/alert-helpers.js';
import '@js/utils/provider-options.js';
import '@js/utils/value-helpers.js';

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

function jsonResponse(payload, status = 200) {
    return new Response(JSON.stringify(payload), {
        status,
        headers: { 'Content-Type': 'application/json' },
    });
}

function setting(overrides) {
    return {
        category: 'general',
        description: '',
        editable: true,
        max_value: null,
        min_value: null,
        options: null,
        step: null,
        visible: true,
        ...overrides,
    };
}

afterEach(() => {
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    vi.useRealTimers();
    delete window.ui;
    delete window.modelProvidersRequestInProgress;
    delete window.searchEnginesRequestInProgress;
    delete window.modelDropdownsInitialized;
    delete window.searchEngineDropdownInitialized;
    delete window.setupCustomDropdown;
    delete window.updateDropdownOptions;
    document.body.replaceChildren();
});

it('searches into non-selected providers while provider scoping stays on for browsing (#6587)', async () => {
    vi.useFakeTimers();
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-cross-provider-search">',
    );
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search">
            <button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
            <div id="settings-alert"></div>
            <div id="settings-content"></div>
            <button id="reset-to-defaults-button" type="button">Reset defaults</button>
            <button id="toggle-raw-config" type="button"><span id="toggle-text"></span></button>
            <section id="raw-config" style="display: none">
                <textarea id="raw_config_editor"></textarea>
            </section>
            <div id="llm.model-empty-warning" style="display: none"></div>
        </form>
    `;

    const settings = {
        'llm.provider': setting({
            category: 'llm_general',
            name: 'Provider',
            type: 'LLM',
            ui_element: 'select',
            value: 'ollama',
            options: [
                { value: 'openai', label: 'OpenAI API' },
                { value: 'anthropic', label: 'Anthropic API' },
                { value: 'ollama', label: 'Ollama (Local)' },
            ],
        }),
        'llm.ollama.api_key': setting({
            category: 'llm_ollama',
            name: 'Ollama API Key',
            type: 'LLM',
            ui_element: 'password',
            value: '',
        }),
        'llm.openai.api_key': setting({
            category: 'llm_openai',
            name: 'OpenAI API Key',
            type: 'LLM',
            ui_element: 'password',
            value: '',
        }),
        'llm.anthropic.api_key': setting({
            category: 'llm_anthropic',
            name: 'Anthropic API Key',
            type: 'LLM',
            ui_element: 'password',
            value: '',
        }),
        'llm.model': setting({
            category: 'llm_general',
            name: 'Model',
            type: 'LLM',
            ui_element: 'select',
            value: 'llama3',
        }),
    };

    const fetchMock = vi.fn((url) => {
        if (url === URLS.SETTINGS_API.BASE) {
            return Promise.resolve(jsonResponse({ status: 'success', settings }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_MODELS) {
            return Promise.resolve(jsonResponse({
                providers: {
                    ollama_models: [{ value: 'llama3', label: 'Llama 3' }],
                    openai_models: [{ value: 'gpt-4o', label: 'GPT-4o' }],
                },
                provider_options: [
                    { value: 'ollama', label: 'Ollama' },
                    { value: 'openai', label: 'OpenAI' },
                ],
            }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
            return Promise.resolve(jsonResponse({
                engine_options: [{ value: 'searxng', label: 'SearXNG' }],
            }));
        }
        if (url === '/settings/api/data-location') {
            return Promise.resolve(jsonResponse({
                data_directory: '/tmp/ldr',
                security_notice: { encrypted: false },
            }));
        }
        if (url === URLS.SETTINGS_API.BACKUP_STATUS) {
            return Promise.resolve(jsonResponse({ enabled: false, count: 0, backups: [] }));
        }
        throw new Error(`Unexpected request: ${url}`);
    });
    vi.stubGlobal('fetch', fetchMock);
    window.ui = { showMessage: vi.fn() };
    window.matchMedia = vi.fn(() => ({ matches: false }));
    window.setupCustomDropdown = vi.fn(() => ({ setValue: vi.fn() }));
    window.updateDropdownOptions = vi.fn();

    await import('@js/components/settings.js');
    if (document.readyState === 'loading') {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
    await flushPromises();
    await vi.advanceTimersByTimeAsync(301);

    // Browsed "all" list stays provider-scoped: ollama visible, the
    // other two providers hidden (existing behavior, unchanged).
    expect(document.getElementById('setting-llm-ollama-api_key')).not.toBeNull();
    expect(document.getElementById('setting-llm-openai-api_key')).toBeNull();
    expect(document.getElementById('setting-llm-anthropic-api_key')).toBeNull();

    // Searching for another provider's settings must surface them
    // WITHOUT switching (and saving) the provider first (#6587).
    const settingsSearch = document.getElementById('settings-search');
    settingsSearch.value = 'openai';
    settingsSearch.dispatchEvent(new Event('input', { bubbles: true }));
    await vi.advanceTimersByTimeAsync(251);

    expect(document.getElementById('setting-llm-openai-api_key')).not.toBeNull();
    // Search still text-matches: anthropic does not match "openai".
    expect(document.getElementById('setting-llm-anthropic-api_key')).toBeNull();
    // The selected provider's own key does not match "openai" either.
    expect(document.getElementById('setting-llm-ollama-api_key')).toBeNull();

    // Clearing the search restores provider scoping for browsing.
    settingsSearch.value = '';
    settingsSearch.dispatchEvent(new Event('input', { bubbles: true }));
    await vi.advanceTimersByTimeAsync(251);
    expect(document.getElementById('setting-llm-ollama-api_key')).not.toBeNull();
    expect(document.getElementById('setting-llm-openai-api_key')).toBeNull();
});