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.1 KB
Raw Blame
/**
 * Mount-time reconciliation of the saved llm.provider against the policy.
 *
 * This is the branch in research.js's loadSettings() that runs when the
 * settings API reports an ``llm.provider`` that the egress policy has since
 * blocked — the returning-user case for #5662. It only executes during
 * initialization, so it needs its own module instance with a settings
 * response staged before DOMContentLoaded; that is why this lives in a
 * separate file from research-model-provider.test.js rather than as another
 * ``it`` block there (same reasoning that split that file off research.test.js).
 *
 * The contract: a blocked provider is never selected, nothing is silently
 * persisted in its place, and the provider's own API key field stays visible
 * so the user can still see and fix the setting that caused the block.
 */

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';

const AVAILABLE_MODELS = '/settings/api/available-models';
const SETTINGS_BASE = '/settings/api';

// OPENAI is what the user configured and what the settings API still
// reports; the policy blocks it. LMSTUDIO is the tempting enabled option
// that must NOT be chosen on the user's behalf.
const PROVIDERS = [
    {
        value: 'OPENAI',
        label: 'OpenAI ☁️ Cloud',
        disabled: true,
        disabled_reason: 'Blocked by "Require Local LLM Endpoint" (cloud-only provider)',
    },
    { value: 'LMSTUDIO', label: 'LM Studio 💻 Local', disabled: false },
];

let fetchMock;

beforeAll(async () => {
    fetchMock = vi.fn((url) => {
        if (typeof url === 'string' && url.startsWith(AVAILABLE_MODELS)) {
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () =>
                    Promise.resolve({
                        status: 'ok',
                        provider_options: PROVIDERS,
                        providers: {},
                    }),
                text: () => Promise.resolve(''),
            });
        }
        if (url === SETTINGS_BASE) {
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () =>
                    Promise.resolve({
                        status: 'ok',
                        settings: {
                            'llm.provider': { value: 'openai', editable: true },
                            'llm.model': { value: 'gpt-4o', editable: true },
                        },
                    }),
                text: () => Promise.resolve(''),
            });
        }
        return Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ status: 'ok', engines: [] }),
            text: () => Promise.resolve(''),
        });
    });
    vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock);
    window.api = { getCsrfToken: () => 'test-csrf' };
    window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };

    document.body.innerHTML = `
        <form id="research-form">
            <div id="research-alert" role="alert" style="display:none"></div>
            <div id="research-error-alert" class="ldr-settings-error-container" style="display:none"></div>
            <textarea id="query" name="query"></textarea>

            <label class="ldr-mode-option"><input type="radio" name="research_mode" value="quick" checked></label>

            <div class="ldr-advanced-options-panel ldr-expanded" id="advanced-options-panel" role="group">
                <select id="model_provider" data-initial-value="openai">
                    <option value="OPENAI" selected>OpenAI</option>
                </select>
                <div id="openai_api_key_container" style="display:none"></div>

                <input type="text" id="model">
                <input type="hidden" id="model_hidden" value="">
                <div id="model-dropdown"><div id="model-dropdown-list"></div></div>

                <input type="text" id="search_engine">
                <input type="hidden" id="search_engine_hidden" value="searxng">
                <div id="search-engine-dropdown"><div id="search-engine-dropdown-list"></div></div>

                <select id="strategy"><option value="source-based" selected>source-based</option></select>
                <input id="iterations" value="2">
                <input id="questions_per_iteration" value="3">
            </div>

            <button type="submit" id="start-research-btn"><span></span></button>
        </form>
    `;

    await import('@js/components/research.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));

    // loadSettings() is chained off the mount-time loadModelOptions, so wait
    // for the settings fetch to have been made and its handlers to drain.
    await vi.waitFor(
        () => {
            expect(
                fetchMock.mock.calls.some(([u]) => u === SETTINGS_BASE)
            ).toBe(true);
        },
        { timeout: 1000 }
    );
    await new Promise((r) => setTimeout(r, 0));
    await new Promise((r) => setTimeout(r, 0));
});

function providerSelect() {
    return document.getElementById('model_provider');
}

describe('mount-time provider reconciliation against the egress policy', () => {
    it('does not select the saved provider when the policy blocks it', async () => {
        await vi.waitFor(
            () => {
                const openai = Array.from(providerSelect().options).find(
                    (o) => o.value === 'OPENAI'
                );
                expect(openai).toBeDefined();
                expect(openai.disabled).toBe(true);
            },
            { timeout: 1000 }
        );

        expect(providerSelect().value).toBe('');
        expect(providerSelect().selectedIndex).toBe(-1);
    });

    it('does not substitute the enabled provider that happens to be available', () => {
        // LMSTUDIO is selectable, but picking it for the user would submit a
        // provider they never chose alongside the model saved for OpenAI.
        expect(providerSelect().value).not.toBe('LMSTUDIO');
    });

    it('does not persist a replacement provider behind the user\'s back', () => {
        const providerSaves = fetchMock.mock.calls.filter(
            ([u, init]) =>
                u === '/settings/api/llm.provider' && init?.method === 'PUT'
        );
        expect(providerSaves).toEqual([]);
    });

    it('keeps the blocked provider visible with its reason', () => {
        const openai = Array.from(providerSelect().options).find(
            (o) => o.value === 'OPENAI'
        );
        expect(openai.textContent).toContain('OpenAI');
        expect(openai.textContent).toContain('Require Local LLM Endpoint');
    });

    it('still shows the blocked provider\'s API key field so the user can fix it', () => {
        // activeProvider falls back to the configured provider when the
        // selection is cleared — the whole point of #5662 was that users
        // could not tell their key had been saved.
        expect(
            document.getElementById('openai_api_key_container').style.display
        ).toBe('block');
    });
});