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.

41.5 KB
Raw Blame
/**
 * Tests for the Model Provider dropdown on the New Research page.
 *
 * This file is intentionally a separate module from research.test.js so it
 * gets a fresh IIFE instance of research.js with an empty
 * ``policyScopeSaveQueue``. The egress-scope denial tests in research.test.js
 * dispatch many change events that queue saves in the IIFE-scoped queue;
 * running alongside them would leave our tests waiting behind tens of stale
 * queued fetches. A separate module runs them in isolation against a clean
 * queue.
 *
 * The dropdown is populated from POST /settings/api/available-models where
 * each entry carries { value, label, disabled, disabled_reason }. The
 * change handlers for the egress-scope select and the local-only checkbox
 * must save the new setting first and re-fetch afterwards — otherwise the
 * backend reads the OLD policy and the dropdown stays stale. We invalidate
 * the client-side 5-minute cache before the re-fetch so it actually
 * round-trips the server (the server rebuilds provider_options from the
 * current policy on every request, so a cache-eligible POST is enough
 * — the force_refresh=true path is reserved for cases where the
 * model *lists* themselves need to be re-discovered).
 */

import '@js/config/urls.js'; // window.URLS (URLS.SETTINGS_API.AVAILABLE_MODELS)
import '@js/utils/alert-helpers.js'; // window.LdrAlertHelpers (used by showSafeAlert)
import '@js/security/xss-protection.js';
import '@js/utils/form-validation.js'; // FormValidator, formValidators
import '@js/components/custom_dropdown.js';

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

function buildForm() {
    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>
            <div class="ldr-privacy-panel" data-scope="adaptive">
                <i id="ldr-privacy-panel-icon"></i>
                <select id="policy_egress_scope" name="policy_egress_scope">
                    <option value="adaptive" selected>Adaptive</option>
                    <option value="public_only">Public only</option>
                    <option value="private_only">Private only</option>
                    <option value="strict">Primary only</option>
                </select>
                <input type="checkbox" id="llm_require_local_endpoint">
                <input type="checkbox" id="embeddings_require_local">
            </div>

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

            <button type="button" class="ldr-advanced-options-toggle ldr-open" aria-expanded="true">
                <i class="fas fa-chevron-up"></i><span class="sr-only"></span>
            </button>
            <div class="ldr-advanced-options-panel ldr-expanded" id="advanced-options-panel" role="group">
                <select id="model_provider"><option value="OLLAMA" selected>Ollama</option></select>

                <!-- Each loader writes 'ldr-loading' onto its own input's
                     parentNode, so #model and #search_engine need separate
                     wrappers here (as research.test.js:61-73 has them). Left
                     as direct siblings they share one parent and the model
                     loader's spinner becomes indistinguishable from the
                     search-engine loader's. -->
                <div class="ldr-model-control">
                    <input type="text" id="model">
                    <input type="hidden" id="model_hidden" value="">
                    <div id="model-dropdown"><div id="model-dropdown-list"></div></div>
                    <button type="button" id="model-refresh"></button>
                </div>

                <div class="ldr-search-engine-control">
                    <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>
                    <button type="button" id="search_engine-refresh"></button>
                </div>

                <select id="strategy"><option value="source-based" selected>source-based</option><option value="langgraph-agent">LangGraph Agent</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>
    `;
}

let fetchMock;
let initAvailableModelsUrl;
let initAvailableModelsOptions;

beforeAll(async () => {
    fetchMock = vi.fn(() =>
        Promise.resolve({
            ok: true,
            status: 200,
            json: () =>
                Promise.resolve({
                    status: 'ok',
                    provider_options: [],
                    providers: {},
                    engines: [],
                }),
            text: () => Promise.resolve(''),
        })
    );
    // Use vi.spyOn instead of direct reassignment so this file doesn't
    // clobber the fetchMock installed by research.test.js's beforeAll
    // when both files run in the same vitest worker. (Direct
    // ``globalThis.fetch = fetchMock`` would leave research.test.js's
    // fetchMock empty and break its regression test.)
    vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock);
    window.api = { getCsrfToken: () => 'test-csrf' };
    window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };

    buildForm();
    await import('@js/components/research.js');
    document.dispatchEvent(new Event('DOMContentLoaded'));
    // Capture the URL of the synchronous-init /available-models request
    // so we can assert the initial read uses the model-list URL. beforeEach calls
    // mockClear() which would wipe this otherwise.
    [initAvailableModelsUrl, initAvailableModelsOptions] =
        fetchMock.mock.calls.find(
            ([u]) => typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
        ) ?? [null, null];
    await Promise.resolve();
    await Promise.resolve();
});

const SAMPLE_PROVIDERS = [
    { value: 'OLLAMA', label: 'Ollama 💻 Local' },
    { value: 'LMSTUDIO', label: 'LM Studio 💻 Local' },
    {
        value: 'DEEPSEEK',
        label: 'DeepSeek ☁️ Cloud',
        disabled: true,
        disabled_reason: 'Blocked by "Require Local LLM Endpoint"',
    },
    {
        value: 'OPENAI',
        label: 'OpenAI ☁️ Cloud',
        disabled: true,
        disabled_reason: 'Blocked by "Require Local LLM Endpoint"',
    },
];

function stubModelsResponse(providerOptions) {
    fetchMock.mockImplementation((url) => {
        if (typeof url !== 'string') {
            return Promise.reject(new Error('unexpected fetch'));
        }
        if (url.startsWith(AVAILABLE_MODELS)) {
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () =>
                    Promise.resolve({
                        status: 'ok',
                        provider_options: providerOptions,
                        providers: {},
                    }),
                text: () => Promise.resolve(''),
            });
        }
        return Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ status: 'ok' }),
            text: () => Promise.resolve(''),
        });
    });
}

function getProviderOption(value) {
    return Array.from(
        document.getElementById('model_provider').options
    ).find((o) => o.value === value);
}

function flush() {
    return new Promise((r) => setTimeout(r, 0));
}

beforeEach(() => {
    // Reset the model_provider select so each test starts from the
    // fixture's lone <option value="OLLAMA">.
    const sel = document.getElementById('model_provider');
    sel.innerHTML = '<option value="OLLAMA" selected>Ollama</option>';
    // Tests that stage a specific persisted provider set this; clear it here
    // so a failing assertion can't leak the attribute into the next test.
    sel.removeAttribute('data-initial-value');
    // Reset privacy controls.
    const policyScope = document.getElementById('policy_egress_scope');
    policyScope.value = 'adaptive';
    policyScope.disabled = false;
    policyScope.dataset.savedValue = 'adaptive';
    ['llm_require_local_endpoint', 'embeddings_require_local'].forEach((id) => {
        const control = document.getElementById(id);
        control.checked = false;
        control.disabled = false;
        control.title = '';
        delete control.dataset.envLocked;
        delete control.dataset.envValue;
        delete control.dataset.envTitle;
        delete control.dataset.userChecked;
        delete control.dataset.userCheckedSaved;
    });
    // Reset fetchMock to the default (the beforeAll stub). Tests that
    // want a specific response install their own implementation.
    fetchMock.mockImplementation((url) => {
        if (typeof url !== 'string') {
            return Promise.reject(new Error('unexpected fetch'));
        }
        if (url.startsWith(AVAILABLE_MODELS)) {
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () =>
                    Promise.resolve({
                        status: 'ok',
                        provider_options: [],
                        providers: {},
                    }),
                text: () => Promise.resolve(''),
            });
        }
        return Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ status: 'ok' }),
            text: () => Promise.resolve(''),
        });
    });
    fetchMock.mockClear();
});

describe('research form — model provider dropdown', () => {
    it('mounts WITHOUT force_refresh so the page load stays fast (server builds provider_options per request anyway)', () => {
        // initAvailableModelsUrl is captured synchronously right after
        // dispatching DOMContentLoaded (see beforeAll), before any
        // beforeEach clears the mock — so we can assert the exact URL
        // the mount-time loadModelOptions used. Using the cached
        // endpoint is fine because the server rebuilds provider_options
        // from the current policy on every request; force_refresh=true
        // would re-discover every provider's models (~1s+ wall clock)
        // for no benefit on mount.
        expect(initAvailableModelsUrl).not.toBeNull();
        expect(initAvailableModelsUrl.startsWith(AVAILABLE_MODELS)).toBe(true);
        expect(initAvailableModelsUrl).toBe(AVAILABLE_MODELS);
        expect(initAvailableModelsOptions).toEqual({
            method: 'POST', headers: { 'X-CSRFToken': 'test-csrf' },
        });
    });

    it('renders disabled <option> with the policy reason appended to the label', async () => {
        stubModelsResponse(SAMPLE_PROVIDERS);

        // Trigger a force-refresh by toggling the egress scope — the
        // change listener saves the new value and then force-refreshes
        // the providers list.
        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const deepseek = getProviderOption('DEEPSEEK');
                expect(deepseek).toBeDefined();
            },
            { timeout: 1000 }
        );

        const deepseek = getProviderOption('DEEPSEEK');
        expect(deepseek.disabled).toBe(true);
        expect(deepseek.textContent).toContain('DeepSeek');
        expect(deepseek.textContent).toContain('Blocked by');

        const openai = getProviderOption('OPENAI');
        expect(openai).toBeDefined();
        expect(openai.disabled).toBe(true);
        expect(openai.textContent).toContain('Blocked by');

        const ollama = getProviderOption('OLLAMA');
        expect(ollama).toBeDefined();
        expect(ollama.disabled).toBe(false);
        expect(ollama.textContent).not.toContain('Blocked by');
    });

    it('egress-scope change saves before re-fetching so the backend sees the new policy', async () => {
        stubModelsResponse(SAMPLE_PROVIDERS);
        fetchMock.mockClear();

        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));

        // Wait for the PUT to land.
        await vi.waitFor(
            () => {
                const saves = fetchMock.mock.calls.filter(
                    ([u, init]) =>
                        u === '/settings/api/policy.egress_scope' &&
                        init?.method === 'PUT'
                );
                expect(saves.length).toBeGreaterThanOrEqual(1);
            },
            { timeout: 1000 }
        );

        // And a re-fetch of the providers list (cache miss because we
        // invalidated after the PUT). The URL is the bare cached
        // endpoint — the server rebuilds provider_options from the
        // current policy every request, so force_refresh=true is
        // unnecessary here.
        await vi.waitFor(
            () => {
                const refreshes = fetchMock.mock.calls.filter(
                    ([u]) =>
                        typeof u === 'string' &&
                        u.startsWith(AVAILABLE_MODELS)
                );
                expect(refreshes.length).toBeGreaterThanOrEqual(1);
            },
            { timeout: 1000 }
        );

        // Ordering: the setting PUT must finish before model discovery POST.
        const allCalls = fetchMock.mock.calls;
        const putIndex = allCalls.findIndex(
            ([u, init]) =>
                u === '/settings/api/policy.egress_scope' &&
                init?.method === 'PUT'
        );
        const getIndex = allCalls.findIndex(
            ([u]) =>
                typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
        );
        expect(putIndex).toBeGreaterThanOrEqual(0);
        expect(getIndex).toBeGreaterThanOrEqual(0);
        expect(putIndex).toBeLessThan(getIndex);
        expect(allCalls[getIndex]).toEqual([
            AVAILABLE_MODELS,
            { method: 'POST', headers: { 'X-CSRFToken': 'test-csrf' } },
        ]);

        // And specifically NOT the slow forced-refresh path —
        // its ~1s+ wall-clock cost is what motivated this regression.
        const forceRefreshCalls = allCalls.filter(
            ([u, init]) =>
                typeof u === 'string' &&
                u === `${AVAILABLE_MODELS}?force_refresh=true` &&
                init?.method === 'POST'
        );
        expect(forceRefreshCalls).toHaveLength(0);
    });

    it('local-only checkbox change saves before re-fetching', async () => {
        stubModelsResponse(SAMPLE_PROVIDERS);
        fetchMock.mockClear();

        const cb = document.getElementById('llm_require_local_endpoint');
        cb.checked = true;
        cb.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const saves = fetchMock.mock.calls.filter(
                    ([u, init]) =>
                        u === '/settings/api/llm.require_local_endpoint' &&
                        init?.method === 'PUT'
                );
                expect(saves.length).toBeGreaterThanOrEqual(1);
            },
            { timeout: 1000 }
        );

        await vi.waitFor(
            () => {
                const refreshes = fetchMock.mock.calls.filter(
                    ([u]) =>
                        typeof u === 'string' &&
                        u.startsWith(AVAILABLE_MODELS)
                );
                expect(refreshes.length).toBeGreaterThanOrEqual(1);
            },
            { timeout: 1000 }
        );

        const allCalls = fetchMock.mock.calls;
        const putIndex = allCalls.findIndex(
            ([u, init]) =>
                u === '/settings/api/llm.require_local_endpoint' &&
                init?.method === 'PUT'
        );
        const getIndex = allCalls.findIndex(
            ([u]) =>
                typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
        );
        expect(putIndex).toBeGreaterThanOrEqual(0);
        expect(getIndex).toBeGreaterThanOrEqual(0);
        expect(putIndex).toBeLessThan(getIndex);
        expect(allCalls[getIndex]).toEqual([
            AVAILABLE_MODELS,
            { method: 'POST', headers: { 'X-CSRFToken': 'test-csrf' } },
        ]);

        const forceRefreshCalls = allCalls.filter(
            ([u, init]) =>
                typeof u === 'string' &&
                u === `${AVAILABLE_MODELS}?force_refresh=true` &&
                init?.method === 'POST'
        );
        expect(forceRefreshCalls).toHaveLength(0);
    });

    it('a rejected save leaves the queue quiet and the page usable', async () => {
        // Note on what this can and cannot prove: saveSearchSetting swallows
        // its own errors and always resolves, so a non-ok PUT does not by
        // itself produce a rejection to catch. What this guards is the
        // shape of the chain — the save queue is a dangling promise between
        // toggles, so anything that DID reject in it (a throwing refresh, a
        // future non-swallowing save) would escape as an unhandledRejection
        // with no handler attached. The terminal .catch on the queue is what
        // keeps that impossible.
        fetchMock.mockImplementation((url, init) => {
            if (
                url === '/settings/api/policy.egress_scope' &&
                init?.method === 'PUT'
            ) {
                return Promise.resolve({
                    ok: false,
                    status: 500,
                    json: () =>
                        Promise.resolve({
                            status: 'error',
                            error: 'simulated save failure',
                        }),
                    text: () => Promise.resolve('simulated save failure'),
                });
            }
            if (typeof url === 'string' && url.startsWith(AVAILABLE_MODELS)) {
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () =>
                        Promise.resolve({
                            status: 'ok',
                            provider_options: SAMPLE_PROVIDERS,
                            providers: {},
                        }),
                    text: () => Promise.resolve(''),
                });
            }
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () => Promise.resolve({ status: 'ok' }),
                text: () => Promise.resolve(''),
            });
        });
        fetchMock.mockClear();

        // Track unhandled rejections on the global so a swallowed failure
        // can still be observed if the chain ever lets one through.
        const rejections = [];
        const handler = (e) => {
            rejections.push(e?.reason?.message || e?.reason || String(e));
        };
        process.on('unhandledRejection', handler);

        try {
            const scope = document.getElementById('policy_egress_scope');
            scope.value = 'private_only';
            scope.dispatchEvent(new Event('change'));

            await vi.waitFor(
                () => {
                    const saves = fetchMock.mock.calls.filter(
                        ([u, init]) =>
                            u === '/settings/api/policy.egress_scope' &&
                            init?.method === 'PUT'
                    );
                    expect(saves.length).toBeGreaterThanOrEqual(1);
                },
                { timeout: 1000 }
            );

            await flush();
            await flush();

            expect(rejections).toEqual([]);
        } finally {
            process.off('unhandledRejection', handler);
        }
    });

    it('clears stale <option>s before re-rendering, so a provider that disappeared between fetches is gone', async () => {
        let call = 0;
        fetchMock.mockImplementation((url) => {
            if (typeof url !== 'string') {
                return Promise.reject(new Error('unexpected'));
            }
            if (url.startsWith(AVAILABLE_MODELS)) {
                call += 1;
                const provider_options =
                    call === 1 ? SAMPLE_PROVIDERS : [SAMPLE_PROVIDERS[0]];
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () =>
                        Promise.resolve({
                            status: 'ok',
                            provider_options,
                            providers: {},
                        }),
                    text: () => Promise.resolve(''),
                });
            }
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () => Promise.resolve({ status: 'ok' }),
                text: () => Promise.resolve(''),
            });
        });

        const scope = document.getElementById('policy_egress_scope');

        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));
        await vi.waitFor(
            () => {
                expect(getProviderOption('DEEPSEEK')).toBeDefined();
                expect(getProviderOption('OPENAI')).toBeDefined();
            },
            { timeout: 1000 }
        );

        scope.value = 'adaptive';
        scope.dispatchEvent(new Event('change'));
        await vi.waitFor(
            () => {
                const sel = document.getElementById('model_provider');
                const values = Array.from(sel.options).map((o) => o.value);
                expect(values).toEqual(['OLLAMA']);
            },
            { timeout: 1000 }
        );
    });

    it('remembers a newer provider through policy blocking and re-enabling', async () => {
        // First load with DEEPSEEK enabled
        const initialProviders = [
            { value: 'OLLAMA', label: 'Ollama 💻 Local', disabled: false },
            { value: 'DEEPSEEK', label: 'DeepSeek ☁️ Cloud', disabled: false },
            { value: 'OPENAI', label: 'OpenAI ☁️ Cloud', disabled: false },
        ];
        stubModelsResponse(initialProviders);

        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'adaptive';
        scope.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const deepseek = getProviderOption('DEEPSEEK');
                expect(deepseek).toBeDefined();
                expect(deepseek.disabled).toBe(false);
            },
            { timeout: 1000 }
        );

        // The user changes the provider after the page started with OLLAMA.
        // Reverting the remembered selection would silently restore OLLAMA
        // after the policy toggle, while the database still says DEEPSEEK.
        const sel = document.getElementById('model_provider');
        sel.setAttribute('data-initial-value', 'OLLAMA');
        sel.value = 'DEEPSEEK';
        sel.dispatchEvent(new Event('change'));
        await flush();
        expect(sel.value).toBe('DEEPSEEK');

        // Now toggle policy to private_only where DEEPSEEK is disabled
        stubModelsResponse(SAMPLE_PROVIDERS);
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const deepseek = getProviderOption('DEEPSEEK');
                expect(deepseek).toBeDefined();
                expect(deepseek.disabled).toBe(true);
            },
            { timeout: 1000 }
        );

        expect(sel.value).toBe('');
        expect(sel.selectedIndex).toBe(-1);
        expect(sel.getAttribute('data-initial-value')).toBe('DEEPSEEK');
        const providerSaves = fetchMock.mock.calls.filter(
            ([url, init]) => url === '/settings/api/llm.provider' && init?.method === 'PUT'
        );
        expect(providerSaves).toHaveLength(1);
        expect(JSON.parse(providerSaves[0][1].body).value).toBe('deepseek');

        stubModelsResponse(initialProviders);
        scope.value = 'public_only';
        scope.dispatchEvent(new Event('change'));
        await vi.waitFor(() => expect(sel.value).toBe('DEEPSEEK'));
    });

    it('clears the selection instead of substituting when the configured provider is blocked', async () => {
        // The regression this guards: substituting some other enabled
        // provider is a programmatic assignment, so the change listener
        // never fires and llm.provider is never saved. The form would then
        // post a provider the user never chose, paired with the model saved
        // for the blocked one, while the settings DB still said otherwise.
        // Clearing instead makes the backend fall back to the saved
        // llm.provider and fail with a clean PolicyDeniedError.
        const sel = document.getElementById('model_provider');
        sel.setAttribute('data-initial-value', 'OPENAI');
        sel.value = 'OPENAI';

        // OPENAI (both current and initial) is blocked; LMSTUDIO is a
        // perfectly good enabled option that we must NOT silently jump to.
        const providers = [
            { value: 'OPENAI', label: 'OpenAI ☁️ Cloud', disabled: true, disabled_reason: 'Blocked by policy' },
            { value: 'LMSTUDIO', label: 'LM Studio 💻 Local', disabled: false },
            { value: 'OLLAMA', label: 'Ollama 💻 Local', disabled: false },
        ];
        stubModelsResponse(providers);

        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const openai = getProviderOption('OPENAI');
                expect(openai).toBeDefined();
                expect(openai.disabled).toBe(true);
            },
            { timeout: 1000 }
        );

        expect(sel.value).toBe('');
        expect(sel.selectedIndex).toBe(-1);

        // And nothing was persisted on the user's behalf.
        const providerSaves = fetchMock.mock.calls.filter(
            ([u, init]) => u === '/settings/api/llm.provider' && init?.method === 'PUT'
        );
        expect(providerSaves).toEqual([]);

        // Clean up data-initial-value
        sel.removeAttribute('data-initial-value');
    });

    it('never selects a disabled option when every provider is blocked', async () => {
        // `select.value = x` happily selects a disabled <option>, so the
        // all-blocked case has to clear rather than fall through to
        // "keep whatever we had".
        const sel = document.getElementById('model_provider');
        sel.setAttribute('data-initial-value', 'OPENAI');
        sel.value = 'OPENAI';

        const providers = [
            { value: 'OPENAI', label: 'OpenAI ☁️ Cloud', disabled: true, disabled_reason: 'Blocked by policy' },
            { value: 'DEEPSEEK', label: 'DeepSeek ☁️ Cloud', disabled: true, disabled_reason: 'Blocked by policy' },
        ];
        stubModelsResponse(providers);

        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const deepseek = getProviderOption('DEEPSEEK');
                expect(deepseek).toBeDefined();
                expect(deepseek.disabled).toBe(true);
            },
            { timeout: 1000 }
        );

        expect(sel.value).toBe('');
        expect(sel.selectedIndex).toBe(-1);
        // Both blocked providers stay visible with their reason, so the
        // user can still see that the key they configured was read.
        expect(getProviderOption('OPENAI').textContent).toContain('Blocked by policy');

        sel.removeAttribute('data-initial-value');
    });

    it('applies only the newest policy toggle when two land back to back', async () => {
        // Both the scope select and the local-only checkbox run on the same
        // save queue with a shared generation counter. Without the guard,
        // two quick toggles fire two independent save->refresh chains and
        // whichever response arrives last wins — which may be the older
        // policy's disabled set.
        const blocked = [
            { value: 'OLLAMA', label: 'Ollama 💻 Local', disabled: false },
            { value: 'DEEPSEEK', label: 'DeepSeek ☁️ Cloud', disabled: true, disabled_reason: 'Blocked by policy' },
        ];
        const allowed = [
            { value: 'OLLAMA', label: 'Ollama 💻 Local', disabled: false },
            { value: 'DEEPSEEK', label: 'DeepSeek ☁️ Cloud', disabled: false },
        ];

        // Model the backend: provider_options is derived from whatever
        // policy is currently saved, so the response depends on the PUTs
        // that landed before it. The refresh that survives must therefore
        // render the policy in effect after BOTH toggles, not after one.
        let blockedByPolicy = false;
        fetchMock.mockImplementation((url, init) => {
            if (typeof url === 'string' && url.startsWith(AVAILABLE_MODELS)) {
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () =>
                        Promise.resolve({
                            status: 'ok',
                            provider_options: blockedByPolicy ? blocked : allowed,
                            providers: {},
                        }),
                    text: () => Promise.resolve(''),
                });
            }
            if (init?.method === 'PUT' && typeof url === 'string') {
                const value = JSON.parse(init.body).value;
                if (url === '/settings/api/policy.egress_scope') {
                    blockedByPolicy = value === 'private_only';
                } else if (url === '/settings/api/llm.require_local_endpoint') {
                    blockedByPolicy = value === true;
                }
            }
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () => Promise.resolve({ status: 'ok' }),
                text: () => Promise.resolve(''),
            });
        });
        fetchMock.mockClear();

        const scope = document.getElementById('policy_egress_scope');
        const checkbox = document.getElementById('llm_require_local_endpoint');

        // Public-only leaves the local-inference checkbox editable.
        scope.value = 'public_only';
        scope.dispatchEvent(new Event('change'));
        checkbox.checked = true;
        checkbox.dispatchEvent(new Event('change'));

        await vi.waitFor(
            () => {
                const saves = fetchMock.mock.calls.filter(
                    ([u, init]) =>
                        u === '/settings/api/llm.require_local_endpoint' &&
                        init?.method === 'PUT'
                );
                expect(saves.length).toBe(1);
            },
            { timeout: 1000 }
        );
        await flush();
        await flush();

        // Superseded refresh was dropped: one models fetch, not two.
        const modelFetches = fetchMock.mock.calls.filter(
            ([u]) => typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
        );
        expect(modelFetches.length).toBe(1);
        // The later checkbox save requires local inference even with a
        // public search scope, so cloud providers must now be disabled.
        expect(getProviderOption('DEEPSEEK').disabled).toBe(true);
    });
});


it('ignores an older manual model response after a policy refresh completes', async () => {
    let resolveManual;
    const manual = new Promise(resolve => { resolveManual = resolve; });
    let requestedManual = false;
    const payload = disabled => ({
        ok: true,
        json: async () => ({ providers: {}, provider_options: [
            { value: 'OPENAI', label: 'OpenAI', disabled },
            { value: 'OLLAMA', label: 'Ollama', disabled: false },
        ] }),
    });
    fetchMock.mockImplementation((url, options) => {
        if (url.startsWith(AVAILABLE_MODELS)) {
            expect(options).toEqual({ method: 'POST', headers: { 'X-CSRFToken': 'test-csrf' } });
            if (url === `${AVAILABLE_MODELS}?force_refresh=true` && !requestedManual) {
                requestedManual = true;
                return manual;
            }
            return Promise.resolve(payload(true));
        }
        return Promise.resolve({ ok: true, json: async () => ({ status: 'ok' }), text: async () => '' });
    });
    const provider = document.getElementById('model_provider');
    provider.innerHTML = '<option value="OPENAI" selected>OpenAI</option>';
    document.getElementById('model-refresh').click();
    await vi.waitFor(() => expect(requestedManual).toBe(true));
    const local = document.getElementById('llm_require_local_endpoint');
    local.checked = true;
    local.dispatchEvent(new Event('change'));
    await vi.waitFor(() => expect(getProviderOption('OPENAI').disabled).toBe(true));
    expect(provider.value).toBe('');
    resolveManual(payload(false));
    await flush();
    await flush();
    expect(getProviderOption('OPENAI').disabled).toBe(true);
    expect(provider.value).toBe('');
});

it('clears the model loading state when a superseded request rejects', async () => {
    // The sibling test above drives the .then arm of loadModelOptions' stale
    // guard; this one drives the .catch arm, which has the same two jobs on the
    // way out. A superseded response must undo the loading class it added and
    // leave the models cache holding an array: updateModelOptionsForProvider()
    // reloads whenever the cache is null, so returning early with it still null
    // strands the spinner AND issues another request - and because every entry
    // bumps the generation, two concurrent chains keep each other stale forever.
    const modelLoading = () => document
        .getElementById('model')
        .parentNode.classList.contains('ldr-loading');
    const modelFetches = () => fetchMock.mock.calls.filter(
        ([u]) => typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
    ).length;
    const providerPayload = (providerOptions) => ({
        ok: true,
        status: 200,
        json: async () => ({
            status: 'ok',
            providers: {},
            provider_options: providerOptions,
        }),
        text: async () => '',
    });
    let rejectManual;
    const manual = new Promise((_resolve, reject) => { rejectManual = reject; });
    // The newer request is deferred, not abandoned. While it is unsettled,
    // nothing but the rejection below can clear the loading state - that is
    // this test's whole signal - but it is released before the test returns.
    // research.js chains the policy refresh onto policyScopeSaveQueue, which
    // is scoped to the setupEventListeners() IIFE and so is unreachable from
    // beforeEach: a request abandoned in flight would park that queue for
    // every later test in this file, not just for this one.
    let resolveNewer;
    const newer = new Promise((resolve) => { resolveNewer = resolve; });
    let requestedManual = false;
    fetchMock.mockImplementation((url, options) => {
        if (typeof url === 'string' && url.startsWith(AVAILABLE_MODELS)) {
            expect(options).toEqual({ method: 'POST', headers: { 'X-CSRFToken': 'test-csrf' } });
            if (url === `${AVAILABLE_MODELS}?force_refresh=true` && !requestedManual) {
                requestedManual = true;
                return manual;
            }
            return newer;
        }
        return Promise.resolve({
            ok: true,
            status: 200,
            json: async () => ({ status: 'ok' }),
            text: async () => '',
        });
    });

    document.getElementById('model-refresh').click();
    await vi.waitFor(() => expect(requestedManual).toBe(true));
    // A policy toggle supersedes the manual request: it invalidates the models
    // cache and starts a newer load, which has not answered yet.
    const local = document.getElementById('llm_require_local_endpoint');
    local.checked = true;
    local.dispatchEvent(new Event('change'));
    await vi.waitFor(() => expect(modelFetches()).toBe(2));
    expect(modelLoading()).toBe(true);

    const beforeReject = modelFetches();
    rejectManual(new Error('models endpoint unreachable'));
    await flush();
    await flush();
    await flush();

    try {
        expect(modelLoading()).toBe(false);
        expect(modelFetches()).toBe(beforeReject);
    } finally {
        // Released in a finally: a failing assertion above must not leave the
        // queue parked for the tests that follow.
        resolveNewer(providerPayload([
            { value: 'OLLAMA', label: 'Ollama' },
            { value: 'NEWEST_ONLY', label: 'Newest only' },
        ]));
    }
    // The refresh thunk settles only once this response has been rendered, and
    // the queue's trailing .catch runs on the microtasks right after it.
    await vi.waitFor(() => expect(getProviderOption('NEWEST_ONLY')).toBeDefined());
    await flush();
});

it('keeps the loader bounded when a superseded request succeeds', async () => {
    // Sibling of the test above, on the other arm. That one rejects, so it can
    // only reach loadModelOptions' .catch handler; a superseded response that
    // *succeeds* reaches the .then handler instead, and that is the arm whose
    // unwind is load-bearing. Reverting only it (back to
    // `resolve(getCachedData(CACHE_KEYS.MODELS) || [])`) leaves the cache unset
    // and the loading class on, so the refresh button's own .then re-enters
    // updateModelOptionsForProvider() -> loadModelOptions(), which bumps the
    // generation and supersedes the request that is still in flight. Measured
    // against that single-arm revert: a third request instead of two, and the
    // spinner stuck on. The fetch-count and loading assertions are what carry
    // that signal. The dropdown assertions are a positive control that the
    // newest payload is the one that renders - they hold under the revert too,
    // because the mock hands the same shared promise to every request after
    // the first, so the extra request the revert provokes answers with the
    // newest payload as well.
    const modelLoading = () => document
        .getElementById('model')
        .parentNode.classList.contains('ldr-loading');
    const modelFetches = () => fetchMock.mock.calls.filter(
        ([u]) => typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
    ).length;
    const providerPayload = (providerOptions) => ({
        ok: true,
        status: 200,
        json: async () => ({
            status: 'ok',
            providers: {},
            provider_options: providerOptions,
        }),
        text: async () => '',
    });
    let resolveManual;
    const manual = new Promise((resolve) => { resolveManual = resolve; });
    let resolveNewer;
    const newer = new Promise((resolve) => { resolveNewer = resolve; });
    let requestedManual = false;
    fetchMock.mockImplementation((url, options) => {
        if (typeof url === 'string' && url.startsWith(AVAILABLE_MODELS)) {
            expect(options).toEqual({ method: 'POST', headers: { 'X-CSRFToken': 'test-csrf' } });
            if (url === `${AVAILABLE_MODELS}?force_refresh=true` && !requestedManual) {
                requestedManual = true;
                return manual;
            }
            return newer;
        }
        return Promise.resolve({
            ok: true,
            status: 200,
            json: async () => ({ status: 'ok' }),
            text: async () => '',
        });
    });

    document.getElementById('model-refresh').click();
    await vi.waitFor(() => expect(requestedManual).toBe(true));
    // A policy toggle supersedes the manual request: it invalidates the models
    // cache and starts a newer load, which has not answered yet. The cache
    // being unset is what makes the .then unwind matter.
    const local = document.getElementById('llm_require_local_endpoint');
    local.checked = true;
    local.dispatchEvent(new Event('change'));
    await vi.waitFor(() => expect(modelFetches()).toBe(2));
    expect(modelLoading()).toBe(true);

    // The superseded request now SUCCEEDS with a well-formed payload.
    resolveManual(providerPayload([
        { value: 'STALE_ONLY', label: 'Stale only' },
    ]));
    await flush();
    await flush();
    await flush();

    try {
        // No third request, no stranded spinner, and the stale payload never
        // reaches the dropdown.
        expect(modelFetches()).toBe(2);
        expect(modelLoading()).toBe(false);
        expect(getProviderOption('STALE_ONLY')).toBeUndefined();
    } finally {
        // The still-in-flight request is the newest one, so its data is what
        // renders when it lands. Released in a finally for the same reason as
        // the sibling test above: policyScopeSaveQueue is waiting on this
        // request, so a failing assertion must not leave it in flight.
        resolveNewer(providerPayload([
            { value: 'OLLAMA', label: 'Ollama' },
            {
                value: 'OPENAI',
                label: 'OpenAI',
                disabled: true,
                disabled_reason: 'Blocked by "Require Local LLM Endpoint"',
            },
        ]));
    }
    await vi.waitFor(() => expect(getProviderOption('OPENAI')).toBeDefined());
    expect(getProviderOption('OPENAI').disabled).toBe(true);
    expect(getProviderOption('STALE_ONLY')).toBeUndefined();
    expect(modelFetches()).toBe(2);
    expect(modelLoading()).toBe(false);
});