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.

16.2 KB
Raw Blame
/**
 * Tab scoping under an empty `llm.provider` (#6586).
 *
 * `organizeSettings`' filter callback used to `return true` early for an
 * empty/absent provider BEFORE the tab-scoping block, so that state
 * rendered the complete settings list in every tab and the tab filter was
 * dead code. The emptiness check is now a conjunct of the provider
 * condition instead: with no provider selected nothing is provider-scoped
 * away (the safe direction) and tab scoping still applies.
 *
 * Lives in its own file: the settings component captures its DOM element
 * references at import time, so it must be imported against a fresh DOM
 * (vitest isolates modules per file, not per test). Reset the module cache
 * before every test so each import binds to that test's fixture, including
 * when shuffle runs the absent-provider case first (#7217).
 */

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,
    };
}

beforeEach(() => {
    vi.resetModules();
    // Keep delayed component work within this test's DOM lifetime.
    vi.useFakeTimers();
    vi.spyOn(document, 'readyState', 'get').mockReturnValue('complete');
});

afterEach(() => {
    vi.clearAllTimers();
    vi.useRealTimers();
    vi.restoreAllMocks();
    vi.unstubAllGlobals();
    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('keeps tab scoping when llm.provider is empty and hides nothing provider-scoped (#6586)', async () => {
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-empty-provider">',
    );
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search">
            <button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
            <button type="button" class="ldr-settings-tab" data-tab="search">Search</button>
            <button type="button" class="ldr-settings-tab" data-tab="llm">LLM</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 = {
        'app.nickname': setting({
            name: 'Nickname', type: 'APP', ui_element: 'text', value: 'nick',
        }),
        'search.tool': setting({
            category: 'search_general', name: 'Search Tool', type: 'SEARCH',
            ui_element: 'select', value: 'searxng',
        }),
        'llm.provider': setting({
            category: 'llm_general', name: 'Provider', type: 'LLM',
            ui_element: 'select',
            // The hardening state pinned by #6586: no provider selected.
            value: '',
            options: [
                { value: 'openai', label: 'OpenAI API' },
                { value: 'ollama', label: 'Ollama (Local)' },
            ],
        }),
        'llm.ollama.url': setting({
            category: 'llm_ollama', name: 'Ollama URL', type: 'LLM',
            ui_element: 'text', value: 'http://localhost:11434',
        }),
        'llm.openai.api_key': setting({
            category: 'llm_openai', name: 'OpenAI API Key', type: 'LLM',
            ui_element: 'password', value: '',
        }),
        // A stray legacy duplicate: migration 0004 renames app.max_tokens
        // to llm.max_tokens, but an unmigrated/leftover row can still hand
        // organizeSettings both keys at once. 'max_tokens' is listed under
        // the LLM tab's tabSpecificSettings (not app's), so the All-tab
        // de-dup loop must hide this app-prefixed duplicate while still
        // showing the canonical llm.max_tokens entry.
        'app.max_tokens': setting({
            name: 'Max Tokens (legacy)', type: 'APP',
            ui_element: 'number', value: 4096,
        }),
        'llm.max_tokens': setting({
            category: 'llm_general', name: 'Max Tokens', type: 'LLM',
            ui_element: 'number', value: 4096,
        }),
    };

    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: [], openai_models: [] },
                provider_options: [],
            }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
            return Promise.resolve(jsonResponse({ engine_options: [] }));
        }
        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();

    // Safe direction with an empty provider: nothing is provider-scoped
    // away, so both providers' settings render on the All tab.
    await vi.waitFor(() => {
        expect(document.getElementById('setting-llm-ollama-url')).not.toBeNull();
    });
    expect(document.getElementById('setting-llm-openai-api_key')).not.toBeNull();
    expect(document.getElementById('setting-app-nickname')).not.toBeNull();
    expect(document.getElementById('search.tool')).not.toBeNull();

    // All-tab de-duplication must still apply while the provider is empty.
    // 'max_tokens' is tab-specific to LLM (tabSpecificSettings.llm), so the
    // stray app.max_tokens duplicate is excluded from the All tab even
    // though nothing is provider-scoped away, while the canonical
    // llm.max_tokens entry still renders. Under the reverted early return
    // (`if (!selectedProvider) return true;` ahead of this de-dup loop)
    // every visible setting short-circuits to `true`, so the stray
    // duplicate would wrongly appear here too.
    expect(document.getElementById('setting-llm-max_tokens')).not.toBeNull();
    expect(document.getElementById('setting-app-max_tokens')).toBeNull();

    // Tab scoping must still apply while the provider is empty: the Search
    // tab renders search settings only. The old `if (!selectedProvider)
    // return true;` early return sat BEFORE the tab-scoping block, so the
    // empty-provider state rendered the complete list in every tab (#6586).
    document.querySelector('.ldr-settings-tab[data-tab="search"]').click();
    await flushPromises();
    expect(document.getElementById('search.tool')).not.toBeNull();
    expect(document.getElementById('setting-app-nickname')).toBeNull();
    expect(document.getElementById('setting-llm-ollama-url')).toBeNull();
    expect(document.getElementById('setting-llm-openai-api_key')).toBeNull();

    // ...and switching back to All restores the complete list.
    document.querySelector('.ldr-settings-tab[data-tab="all"]').click();
    await flushPromises();
    expect(document.getElementById('setting-app-nickname')).not.toBeNull();
    expect(document.getElementById('setting-llm-ollama-url')).not.toBeNull();

    // The provider selector itself (llm.provider, rendered via the custom
    // dropdown with input_id 'llm.provider') must still render on the LLM
    // tab when no provider is selected: it is the control the empty state
    // needs to let a user pick one, so an empty value can never be a
    // reason to hide it while tab scoping is applied.
    document.querySelector('.ldr-settings-tab[data-tab="llm"]').click();
    await flushPromises();
    expect(document.getElementById('llm.provider')).not.toBeNull();
    expect(document.getElementById('setting-llm-max_tokens')).not.toBeNull();
    expect(document.getElementById('setting-app-nickname')).toBeNull();
    expect(document.getElementById('search.tool')).toBeNull();
});

it('keeps tab scoping when the llm.provider row is absent from the fixture entirely (#6586)', async () => {
    // A stricter variant of the empty-value case above: here `llm.provider`
    // is not just an empty string, it is MISSING from the settings the
    // server returns. `organizeSettings` looks it up with `.find(...)`,
    // which yields `undefined` rather than a setting object, so
    // `providerSetting?.value` (and `selectedProvider`) must still resolve
    // to the same falsy '' the empty-value case exercises. The reverted
    // early return (`if (!selectedProvider) return true;` ahead of the
    // de-dup/tab-scoping logic) would have been just as dead here as for an
    // empty value, so this pins the same safe-direction behavior for the
    // other shape a "no provider selected" fixture can take.
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-absent-provider">',
    );
    document.body.innerHTML = `
        <form id="settings-form">
            <input id="settings-search">
            <button type="button" class="ldr-settings-tab active" data-tab="all">All</button>
            <button type="button" class="ldr-settings-tab" data-tab="search">Search</button>
            <button type="button" class="ldr-settings-tab" data-tab="llm">LLM</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 = {
        'app.nickname': setting({
            name: 'Nickname', type: 'APP', ui_element: 'text', value: 'nick',
        }),
        'search.tool': setting({
            category: 'search_general', name: 'Search Tool', type: 'SEARCH',
            ui_element: 'select', value: 'searxng',
        }),
        // No 'llm.provider' entry at all: `settings.find(...)` (and the
        // `allSettings.find(...)` fallback) both yield `undefined` here,
        // unlike the sibling test above where the key exists with `value: ''`.
        'llm.ollama.url': setting({
            category: 'llm_ollama', name: 'Ollama URL', type: 'LLM',
            ui_element: 'text', value: 'http://localhost:11434',
        }),
        'llm.openai.api_key': setting({
            category: 'llm_openai', name: 'OpenAI API Key', type: 'LLM',
            ui_element: 'password', value: '',
        }),
        // Same stray legacy duplicate as the empty-value case: the All-tab
        // de-dup loop must still hide this app-prefixed duplicate while the
        // canonical llm.max_tokens entry still renders.
        'app.max_tokens': setting({
            name: 'Max Tokens (legacy)', type: 'APP',
            ui_element: 'number', value: 4096,
        }),
        'llm.max_tokens': setting({
            category: 'llm_general', name: 'Max Tokens', type: 'LLM',
            ui_element: 'number', value: 4096,
        }),
    };

    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: [], openai_models: [] },
                provider_options: [],
            }));
        }
        if (url === URLS.SETTINGS_API.AVAILABLE_SEARCH_ENGINES) {
            return Promise.resolve(jsonResponse({ engine_options: [] }));
        }
        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();

    // Safe direction with an absent provider row: nothing is
    // provider-scoped away, so both providers' settings render on the All
    // tab, same as the empty-value case.
    await vi.waitFor(() => {
        expect(document.getElementById('setting-llm-ollama-url')).not.toBeNull();
    });
    expect(document.getElementById('setting-llm-openai-api_key')).not.toBeNull();
    expect(document.getElementById('setting-app-nickname')).not.toBeNull();
    expect(document.getElementById('search.tool')).not.toBeNull();

    // All-tab de-duplication must still apply with the provider row absent:
    // the stray app.max_tokens duplicate is excluded while the canonical
    // llm.max_tokens entry still renders.
    expect(document.getElementById('setting-llm-max_tokens')).not.toBeNull();
    expect(document.getElementById('setting-app-max_tokens')).toBeNull();

    // Tab scoping must still apply with the provider row absent: the Search
    // tab renders search settings only.
    document.querySelector('.ldr-settings-tab[data-tab="search"]').click();
    await flushPromises();
    expect(document.getElementById('search.tool')).not.toBeNull();
    expect(document.getElementById('setting-app-nickname')).toBeNull();
    expect(document.getElementById('setting-llm-ollama-url')).toBeNull();
    expect(document.getElementById('setting-llm-openai-api_key')).toBeNull();

    // ...and switching back to All restores the complete list.
    document.querySelector('.ldr-settings-tab[data-tab="all"]').click();
    await flushPromises();
    expect(document.getElementById('setting-app-nickname')).not.toBeNull();
    expect(document.getElementById('setting-llm-ollama-url')).not.toBeNull();

    // The LLM tab still scopes to LLM-tab-specific settings only (no
    // llm.provider element check here: with the row absent there is no
    // setting for the component to render a control for).
    document.querySelector('.ldr-settings-tab[data-tab="llm"]').click();
    await flushPromises();
    expect(document.getElementById('setting-llm-max_tokens')).not.toBeNull();
    expect(document.getElementById('setting-app-nickname')).toBeNull();
    expect(document.getElementById('search.tool')).toBeNull();
});