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.

10.5 KB
Raw Blame
/**
 * Settings page model selection (#7398): when the stored model's casing
 * drifts from the listing, the provider-filter pass must canonicalize to the
 * case-insensitive match instead of silently replacing the user's model with
 * the first listed one. A model that genuinely is not offered for the
 * provider must still fall back to the first entry.
 *
 * `filterModelOptionsForProvider` has two code paths — the in-place
 * `updateDropdownOptions` path and the `setupCustomDropdown` re-init
 * fallback — and both are exercised here.
 */

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';
import '@js/components/custom_dropdown.js';

// Captured at import time so each boot can restore the shared helper after a
// test that removes it.
const sharedFindDropdownOption = window.findDropdownOption;

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

const LISTED_MODELS = [
    { value: 'gpt-4o-mini', label: 'GPT-4o mini' },
    { value: 'gpt-4o', label: 'GPT-4o' },
];

/**
 * Boot the settings page with `llm.provider=openai` and a stored
 * `llm.model`, then let the post-init provider filter run.
 *
 * @param {object} opts
 * @param {string} opts.storedModel - value of the stored `llm.model` setting
 * @param {Array<{value: string, label: string}>} [opts.models] - models the
 *     provider lists (defaults to LISTED_MODELS)
 * @param {boolean} [opts.withoutSharedHelper] - true: drop `window.findDropdownOption`
 *     to exercise settings.js's inline fallback lookup
 * @param {boolean} opts.inPlaceUpdate - true: `updateDropdownOptions` is
 *     available (in-place path); false: only `setupCustomDropdown` (re-init path)
 */
async function bootSettings({
    storedModel,
    inPlaceUpdate,
    withoutSharedHelper = false,
    models = LISTED_MODELS,
}) {
    vi.useFakeTimers();
    vi.resetModules();
    document.head.insertAdjacentHTML(
        'beforeend',
        '<meta name="csrf-token" content="csrf-model-case">',
    );
    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: 'openai',
            options: [
                { value: 'openai', label: 'OpenAI API' },
                { value: 'ollama', label: 'Ollama (Local)' },
            ],
        }),
        'llm.model': setting({
            category: 'llm_general',
            name: 'Model',
            type: 'LLM',
            ui_element: 'select',
            value: storedModel,
        }),
    };

    vi.stubGlobal('fetch', 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: { openai_models: models },
                provider_options: [{ 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}`);
    }));
    window.ui = { showMessage: vi.fn() };
    window.matchMedia = vi.fn(() => ({ matches: false }));

    // Stubs replace the real dropdown widget so only settings.js logic runs.
    // `findDropdownOption` (from custom_dropdown.js) is intentionally kept.
    const setValue = vi.fn();
    window.setupCustomDropdown = vi.fn(() => ({ setValue }));
    if (inPlaceUpdate) {
        window.updateDropdownOptions = vi.fn();
    } else {
        delete window.updateDropdownOptions;
    }

    if (withoutSharedHelper) delete window.findDropdownOption;
    else window.findDropdownOption = sharedFindDropdownOption;

    await import('@js/components/settings.js');
    if (document.readyState === 'loading') {
        document.dispatchEvent(new Event('DOMContentLoaded'));
    }
    await flushPromises();
    // Past the 100ms post-init provider filter.
    await vi.advanceTimersByTimeAsync(400);

    return {
        setValue,
        hidden: document.getElementById('llm.model_hidden'),
        visible: document.getElementById('llm.model'),
    };
}

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.head.querySelector('meta[name="csrf-token"]')?.remove();
    document.body.replaceChildren();
});

describe('in-place provider filter (updateDropdownOptions path)', () => {
    it('canonicalizes a drifted stored model instead of swapping to the first one', async () => {
        const { hidden, visible } = await bootSettings({
            storedModel: 'GPT-4O',
            inPlaceUpdate: true,
        });

        expect(hidden).not.toBeNull();
        expect(hidden.value).toBe('gpt-4o');
        expect(hidden.value).not.toBe('gpt-4o-mini');
        expect(visible.value).toBe('GPT-4o');
    });

    it('keeps an exactly matching stored model untouched', async () => {
        const { hidden } = await bootSettings({
            storedModel: 'gpt-4o',
            inPlaceUpdate: true,
        });
        expect(hidden.value).toBe('gpt-4o');
    });

    it('still falls back to the first model when the stored one is not offered', async () => {
        const { hidden, visible } = await bootSettings({
            storedModel: 'not-a-real-model',
            inPlaceUpdate: true,
        });
        expect(hidden.value).toBe('gpt-4o-mini');
        expect(visible.value).toBe('GPT-4o mini');
    });
});

describe('re-init provider filter (setupCustomDropdown fallback path)', () => {
    it('canonicalizes a drifted stored model instead of swapping to the first one', async () => {
        const { setValue } = await bootSettings({
            storedModel: 'GPT-4O',
            inPlaceUpdate: false,
        });

        const values = setValue.mock.calls.map(([value]) => value);
        expect(values).toContain('gpt-4o');
        expect(values).not.toContain('gpt-4o-mini');
    });

    it('still falls back to the first model when the stored one is not offered', async () => {
        const { setValue } = await bootSettings({
            storedModel: 'not-a-real-model',
            inPlaceUpdate: false,
        });
        expect(setValue).toHaveBeenCalledWith('gpt-4o-mini', false);
    });
});

describe('inline lookup when window.findDropdownOption is unavailable', () => {
    it('still canonicalizes a drifted model and warns that the helper is missing', async () => {
        const warn = vi.spyOn(SafeLogger, 'warn');
        const { hidden, visible } = await bootSettings({
            storedModel: 'GPT-4O',
            inPlaceUpdate: true,
            withoutSharedHelper: true,
        });

        expect(hidden.value).toBe('gpt-4o');
        expect(visible.value).toBe('GPT-4o');
        expect(warn).toHaveBeenCalledWith(
            expect.stringContaining('findDropdownOption unavailable'),
        );
    });

    it('does not warn when the shared helper is present', async () => {
        const warn = vi.spyOn(SafeLogger, 'warn');
        await bootSettings({ storedModel: 'GPT-4O', inPlaceUpdate: true });
        expect(warn).not.toHaveBeenCalledWith(
            expect.stringContaining('findDropdownOption unavailable'),
        );
    });

    it('still falls back to the first model when the stored one is not offered', async () => {
        const { hidden } = await bootSettings({
            storedModel: 'not-a-real-model',
            inPlaceUpdate: true,
            withoutSharedHelper: true,
        });
        expect(hidden.value).toBe('gpt-4o-mini');
    });

    // The shared helper's ordering is pinned in custom-dropdown.test.js; these
    // pin the inline copy, which runs only when the helper is missing. A
    // single pass accepting "exact OR case-variant" would pick whichever comes
    // first, so both listing orders are covered.
    it.each([
        ['case-variant listed before the exact match', [
            { value: 'GPT-4O', label: 'Variant first' },
            { value: 'gpt-4o', label: 'Exact second' },
        ]],
        ['exact match listed before the case-variant', [
            { value: 'gpt-4o', label: 'Exact first' },
            { value: 'GPT-4O', label: 'Variant second' },
        ]],
    ])('prefers the exact match over a case-variant (%s)', async (_name, models) => {
        const { hidden } = await bootSettings({
            storedModel: 'gpt-4o',
            inPlaceUpdate: true,
            withoutSharedHelper: true,
            models,
        });

        expect(hidden.value).toBe('gpt-4o');
    });
});