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.

103 KB
Raw Blame
/**
 * Tests for components/research.js — the "a model must be selected" guard on
 * the New Research form's submit path.
 *
 * research.js is a self-contained IIFE that wires everything up in
 * initializeResearch() on DOMContentLoaded and exports nothing. happy-dom has
 * already fired DOMContentLoaded by the time the module imports, so the
 * listener never auto-runs (see followup.test.js for the same mechanic). We
 * build the real form DOM first, dispatch DOMContentLoaded ourselves to drive
 * the real initialization (which registers the real submit handler + the real
 * FormValidator wiring), then submit the real form to exercise the guard.
 *
 * Asserting the inline error *text* — not just "no request was sent" — is
 * deliberate: it proves the model field was actually registered with the
 * validator, so the test breaks if that wiring is removed, not only if the
 * submit-time guard is.
 *
 * The fixture wraps the model field in the real .ldr-advanced-options-panel
 * because in production that field lives inside the (collapsible) Advanced
 * Options panel — the guard has to reveal it for the error to be seen.
 */

import '@js/config/urls.js'; // window.URLS (URLS.API.START_RESEARCH)
import '@js/utils/alert-helpers.js'; // window.LdrAlertHelpers (used by showSafeAlert)
import '@js/security/xss-protection.js'; // showSafeAlert, safeUpdateButton, createSafeLoadingOverlay
import '@js/utils/form-validation.js'; // FormValidator, formValidators
import '@js/components/custom_dropdown.js'; // setupCustomDropdown (used by initializeDropdowns)

const START_RESEARCH = '/api/start_research';
const CHAT_SESSIONS = '/api/chat/sessions';
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>
            <label class="ldr-mode-option"><input type="radio" name="research_mode" value="detailed"></label>
            <label class="ldr-mode-option"><input type="radio" name="research_mode" value="chat"></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>

                <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;
// Number of cache-eligible POST /available-models requests fired during init.
// Both loadModelOptions(false) call sites run synchronously inside
// initializeResearch (before any await), so a synchronous capture right after
// dispatch deterministically distinguishes "loaded once" (1) from the old
// duplicate-concurrent-load bug (2).
let initAvailableModelsCalls;

beforeAll(async () => {
    // Keep init's model/search-engine loads and any submit request happy; the
    // responses deliberately return a non-success status so neither the
    // research success handler nor the chat success handler navigates
    // (sets window.location.href) during a test.
    fetchMock = vi.fn(() =>
        Promise.resolve({
            ok: true,
            status: 200,
            json: () =>
                Promise.resolve({
                    status: 'error',
                    message: 'stubbed — no navigation',
                    providers: {},
                    provider_options: [],
                    models: [],
                    engines: [],
                }),
            text: () => Promise.resolve(''),
        })
    );
    globalThis.fetch = fetchMock;
    window.api = {
        getCsrfToken: () => 'test-csrf',
        // The real protected helper is exercised by research-api-runtime;
        // this fixture isolates the form's model-selection guard.
        fetchWithCsrfRecovery: (url, options) => fetchMock(url, options),
    };
    window.RESEARCH_STATUS = { QUEUED: 'queued', IN_PROGRESS: 'in_progress' };

    buildForm();
    await import('@js/components/research.js');
    // Drive the real initializeResearch(): caches DOM refs, wires the submit
    // handler and the query + model validators, applies the advanced-panel
    // state.
    document.dispatchEvent(new Event('DOMContentLoaded'));
    // Capture the model-list request count synchronously, before any async
    // reload path can add to it (see initAvailableModelsCalls above).
    // Count model-list calls against the shared read/refresh path.
    initAvailableModelsCalls = fetchMock.mock.calls.filter(
        ([u]) => typeof u === 'string' && u.startsWith(AVAILABLE_MODELS)
    ).length;
    // Let the fire-and-forget model/search-engine loads settle.
    await Promise.resolve();
    await Promise.resolve();
});

beforeEach(() => {
    fetchMock.mockClear();
    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;
    });
    // A non-empty query so the query guard (which runs first) always passes and
    // the model guard is the thing under test. Individual tests override this.
    document.getElementById('query').value = 'What is quantum computing?';
    document.getElementById('model').value = '';
    document.getElementById('model_hidden').value = '';
    // Reset mode back to quick (a research mode).
    document.querySelector('input[name="research_mode"][value="quick"]').checked = true;
    document.querySelector('input[name="research_mode"][value="detailed"]').checked = false;
    document.querySelector('input[name="research_mode"][value="chat"]').checked = false;
    // Reset the advanced panel to expanded and clear stale validation state.
    document.getElementById('advanced-options-panel').classList.add('ldr-expanded');
    const modelInput = document.getElementById('model');
    modelInput.classList.remove('ldr-field-invalid');
    modelInput.removeAttribute('aria-invalid');
    const btn = document.getElementById('start-research-btn');
    btn.disabled = false;
    document.querySelectorAll('.ldr-loading-overlay').forEach((o) => o.remove());
});

function submitForm() {
    document
        .getElementById('research-form')
        .dispatchEvent(new Event('submit', { cancelable: true, bubbles: true }));
}

function callsTo(url) {
    return fetchMock.mock.calls.filter(([u]) => u === url);
}

describe('research form fixture fidelity', () => {
    it('keeps one production egress-scope control in the privacy panel', () => {
        const policyScopes = document.querySelectorAll('#policy_egress_scope');

        expect(policyScopes).toHaveLength(1);

        const policyScope = document.querySelector(
            '.ldr-privacy-panel #policy_egress_scope'
        );
        expect(policyScope.name).toBe('policy_egress_scope');
        expect(policyScope.closest('#advanced-options-panel')).toBeNull();
        expect(Array.from(policyScope.options, ({ value }) => value)).toEqual([
            'adaptive',
            'public_only',
            'private_only',
            'strict',
        ]);
    });
});

describe('research form — submission ownership', () => {
    it('coalesces rapid keyboard/form submits and removes only its own overlay', async () => {
        let resolveRequest;
        const pendingRequest = new Promise(resolve => {
            resolveRequest = resolve;
        });
        fetchMock.mockImplementationOnce(() => pendingRequest);
        document.getElementById('model_hidden').value = 'qwen3:4b';

        document.getElementById('query').dispatchEvent(new KeyboardEvent(
            'keydown',
            {
                key: 'Enter',
                ctrlKey: true,
                bubbles: true,
                cancelable: true,
            },
        ));
        submitForm();

        expect(callsTo(START_RESEARCH)).toHaveLength(1);
        expect(document.querySelectorAll('.ldr-loading-overlay')).toHaveLength(1);
        expect(document.getElementById('start-research-btn').disabled).toBe(true);

        const ownedOverlay = document.querySelector('.ldr-loading-overlay');
        const unrelatedOverlay = document.createElement('div');
        unrelatedOverlay.className = 'ldr-loading-overlay';
        unrelatedOverlay.dataset.owner = 'unrelated';
        document.body.prepend(unrelatedOverlay);

        resolveRequest({
            ok: true,
            status: 200,
            json: () => Promise.resolve({
                status: 'error',
                message: 'stubbed completion',
            }),
            text: () => Promise.resolve(''),
        });

        await vi.waitFor(() => {
            expect(ownedOverlay.isConnected).toBe(false);
            expect(document.getElementById('start-research-btn').disabled)
                .toBe(false);
        });
        expect(unrelatedOverlay.isConnected).toBe(true);
        unrelatedOverlay.remove();
    });
});

describe('research form — model-required guard', () => {
    it('blocks submission, shows an inline error, and focuses the model field when no model is selected', () => {
        document.getElementById('model_hidden').value = '';

        submitForm();

        // No research request was sent.
        expect(callsTo(START_RESEARCH)).toHaveLength(0);
        // The model field is flagged invalid...
        const modelInput = document.getElementById('model');
        expect(modelInput.getAttribute('aria-invalid')).toBe('true');
        // ...with the expected inline message (proves the validator wiring)...
        const errorEl = document.getElementById('model-error');
        expect(errorEl).not.toBeNull();
        expect(errorEl.textContent).toContain('Please select or enter a model.');
        // ...and the field is focused so the user is taken straight to it.
        expect(document.activeElement).toBe(modelInput);
    });

    it('treats a whitespace-only model as empty', () => {
        document.getElementById('model_hidden').value = '   ';

        submitForm();

        expect(callsTo(START_RESEARCH)).toHaveLength(0);
        expect(document.getElementById('model').getAttribute('aria-invalid')).toBe('true');
    });

    it('expands the collapsed Advanced Options panel so the model error is visible', () => {
        // Simulate a returning user who had collapsed the panel: the model
        // field (and its error <div>) would otherwise be inside a hidden
        // subtree, giving zero feedback on submit.
        const panel = document.getElementById('advanced-options-panel');
        panel.classList.remove('ldr-expanded');
        document.getElementById('model_hidden').value = '';

        submitForm();

        expect(panel.classList.contains('ldr-expanded')).toBe(true);
        expect(document.getElementById('model-error').textContent).toContain(
            'Please select or enter a model.'
        );
        expect(callsTo(START_RESEARCH)).toHaveLength(0);
    });

    it('submits the research request once a model is present', () => {
        document.getElementById('model_hidden').value = 'qwen3:4b';

        submitForm();

        const calls = callsTo(START_RESEARCH);
        expect(calls).toHaveLength(1);
        expect(calls[0][1].method).toBe('POST');
        expect(calls[0][1].headers).toEqual({
            'Content-Type': 'application/json',
            'X-CSRFToken': 'test-csrf',
        });
        // The chosen model is included in the request payload.
        const body = JSON.parse(calls[0][1].body);
        expect(body.model).toBe('qwen3:4b');
        expect(body.query).toBe('What is quantum computing?');
    });

    it('does NOT require a model in chat mode (chat never sends the model field)', async () => {
        document.querySelector('input[name="research_mode"][value="quick"]').checked = false;
        document.querySelector('input[name="research_mode"][value="chat"]').checked = true;
        document.getElementById('model_hidden').value = ''; // no model selected

        submitForm();

        // Not blocked by the model guard: the field is not flagged...
        expect(document.getElementById('model').getAttribute('aria-invalid')).toBeNull();
        // ...and the chat path runs (hits the chat-session endpoint, not the
        // research endpoint).
        expect(callsTo(CHAT_SESSIONS)).toHaveLength(1);
        expect(callsTo(START_RESEARCH)).toHaveLength(0);
        await vi.waitFor(() => {
            expect(document.getElementById('start-research-btn').disabled).toBe(false);
            expect(document.querySelector('.ldr-loading-overlay')).toBeNull();
        });
    });

    it('blocks on an empty query even when a model is selected', () => {
        document.getElementById('query').value = '';
        document.getElementById('model_hidden').value = 'qwen3:4b';

        submitForm();

        expect(callsTo(START_RESEARCH)).toHaveLength(0);
        expect(document.getElementById('query').getAttribute('aria-invalid')).toBe('true');
    });
});

describe('research form — egress-scope denial UX', () => {
    // Drive a server 400 that names the offending field, exactly like
    // _precheck_engine_policy does for scope-mismatch / strict-not-primary
    // denials (field: "policy_egress_scope"). The other reasons
    // (engine_unknown / unclassified / internal_error) intentionally
    // return ``field: null`` so the frontend must NOT highlight a form
    // field — those are tested in the next describe block.
    function stubEgressDenial(message, overrides = {}) {
        fetchMock.mockImplementationOnce(() =>
            Promise.resolve({
                ok: false,
                status: 400,
                json: () =>
                    Promise.resolve({
                        status: 'error',
                        message,
                        reason: 'scope_mismatch_public_only',
                        field: 'policy_egress_scope',
                        ...overrides,
                    }),
                text: () => Promise.resolve(''),
            })
        );
    }

    // The submit handler's error path runs inside fetch().then(...).then(...),
    // which needs the microtask queue to drain. A macrotask flush is the
    // reliable way to get there.
    function flush() {
        return new Promise((r) => setTimeout(r, 0));
    }

    beforeEach(() => {
        // Clear any anchored alert from a prior test.
        const err = document.getElementById('research-error-alert');
        err.innerHTML = '';
        err.style.display = 'none';
        const scope = document.getElementById('policy_egress_scope');
        scope.classList.remove('ldr-field-invalid');
        scope.removeAttribute('aria-invalid');
        const fieldErr = document.getElementById('policy_egress_scope-error');
        if (fieldErr) {
            // Reset rather than remove: showError() reuses the cached error
            // element created during addValidation(), so removing it from the
            // DOM would leave a detached node that getElementById can't find.
            fieldErr.textContent = '';
            fieldErr.style.display = 'none';
        }
        // A model must be present or the model-required guard blocks submit
        // before the fetch (and thus the egress-error UX) ever runs.
        document.getElementById('model_hidden').value = 'qwen3:4b';
    });

    it('anchors the error alert next to the submit button and flags the egress scope field', async () => {
        stubEgressDenial('Search engine searxng was blocked because your Egress Scope is set to Private only.');

        submitForm();
        await flush();

        // The anchored alert near the button is populated and visible...
        const anchored = document.getElementById('research-error-alert');
        expect(anchored.style.display).toBe('block');
        expect(anchored.textContent).toContain('Egress Scope');
        // ...and the named field gets an inline error (red border + message).
        const scope = document.getElementById('policy_egress_scope');
        expect(scope.getAttribute('aria-invalid')).toBe('true');
        const fieldErr = document.getElementById('policy_egress_scope-error');
        expect(fieldErr).not.toBeNull();
        expect(fieldErr.textContent).toContain('Egress Scope');
        // The request WAS sent (and the server refused it with a 400); the
        // point of this test is that the refusal is surfaced to the user, not
        // swallowed.
        expect(callsTo(START_RESEARCH)).toHaveLength(1);
    });

    it('does NOT auto-hide the anchored error alert (errors must persist)', async () => {
        // Regression: showAlert() used to auto-hide errors after 5s like
        // info/success. The anchored alert is shown via showFormError(), which
        // never auto-hides, so it must still be visible after the timers fire.
        vi.useFakeTimers();
        try {
            stubEgressDenial('blocked');
            submitForm();
            // Drain the fetch microtask chain (fake timers don't affect these).
            await Promise.resolve();
            await Promise.resolve();
            await Promise.resolve();
            await Promise.resolve();
            await Promise.resolve();
            // Advance well past the 5s auto-hide window.
            vi.advanceTimersByTime(6000);
            const anchored = document.getElementById('research-error-alert');
            expect(anchored.style.display).toBe('block');
            expect(anchored.textContent).toContain('blocked');
        } finally {
            vi.useRealTimers();
        }
    });

    it('clears the anchored alert and field error when the user changes the egress scope', async () => {
        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();
        expect(document.getElementById('research-error-alert').style.display).toBe('block');
        expect(document.getElementById('policy_egress_scope').getAttribute('aria-invalid')).toBe('true');

        // Changing the dropdown should clear both the anchored alert and the
        // inline field error.
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
        expect(document.getElementById('policy_egress_scope').getAttribute('aria-invalid')).toBeNull();
    });

    it('shows the anchored alert even when the server omits a field hint (network/parse failure)', async () => {
        // The catch path passes field=null; the anchored alert must still show.
        fetchMock.mockImplementationOnce(() => Promise.reject(new Error('network down')));
        submitForm();
        await flush();

        const anchored = document.getElementById('research-error-alert');
        expect(anchored.style.display).toBe('block');
        expect(anchored.textContent).toContain('An error occurred while starting research');
        // No inline field error was requested (field was null), so the
        // pre-registered error slot stays empty.
        const fieldErr = document.getElementById('policy_egress_scope-error');
        expect(fieldErr).not.toBeNull();
        expect(fieldErr.textContent).toBe('');
    });

    it('keeps visual duplicates passive so only the top alert announces the error', async () => {
        // The top #research-alert is the live ``role="alert"`` and scrolls
        // into view; the anchored and inline field errors are visual
        // duplicates only.
        const topContainer = document.getElementById('research-alert');
        const originalAppendChild = topContainer.appendChild.bind(topContainer);
        let displayAtInsertion;
        vi.spyOn(topContainer, 'appendChild').mockImplementation((child) => {
            displayAtInsertion = topContainer.style.display;
            return originalAppendChild(child);
        });

        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();

        expect(displayAtInsertion).toBe('block');
        expect(topContainer.getAttribute('role')).toBe('alert');
        const topAlert = topContainer.querySelector('.alert');
        expect(topAlert.getAttribute('role')).toBeNull();
        expect(topAlert.getAttribute('aria-atomic')).toBeNull();

        const anchored = document.getElementById('research-error-alert');
        expect(anchored.getAttribute('role')).toBeNull();
        expect(anchored.getAttribute('aria-live')).toBeNull();

        const childAlert = anchored.querySelector('.alert');
        expect(childAlert).not.toBeNull();
        expect(childAlert.getAttribute('role')).toBeNull();
        expect(childAlert.getAttribute('aria-atomic')).toBeNull();

        const inlineError = document.getElementById(
            'policy_egress_scope-error'
        );
        expect(inlineError.textContent).toContain('Egress Scope');
        expect(inlineError.getAttribute('aria-live')).toBeNull();
    });

    it('preserves an unrelated (non-egress) danger alert when clearEgressError is called', () => {
        const topAlert = document.getElementById('research-alert');
        topAlert.innerHTML =
            '<div class="alert alert-danger">An unrelated danger alert!</div>';
        topAlert.style.display = 'block';

        // Call clearEgressError directly
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        // The unrelated danger alert must survive because it does not have data-egress-alert="true"
        expect(topAlert.style.display).toBe('block');
        expect(topAlert.textContent).toContain('An unrelated danger alert!');
    });

    it('clears previous egress errors even if the subsequent submit has invalid query/model validation', async () => {
        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();
        expect(document.getElementById('research-error-alert').style.display).toBe('block');
        expect(document.getElementById('research-alert').style.display).toBe('block');

        // Make query invalid so early validation returns
        document.getElementById('query').value = '';
        submitForm();

        // Previous egress errors must be cleared
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
        expect(document.getElementById('research-alert').style.display).toBe('none');
    });

    it('also clears the top-of-form error alert when the user changes the egress scope', async () => {
        // Regression: hideFormError() used to clear only the anchored copy,
        // leaving the non-expiring top #research-alert showing a stale
        // egress error that would re-announce on the next interaction.
        // The fix (clearEgressError()) clears BOTH copies plus the inline
        // field error.
        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();
        expect(document.getElementById('research-error-alert').style.display).toBe('block');
        expect(document.getElementById('research-alert').style.display).toBe('block');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        expect(document.getElementById('research-error-alert').style.display).toBe('none');
        // Top alert was an error (alert-danger) — must also clear.
        expect(document.getElementById('research-alert').style.display).toBe('none');
    });

    it('preserves a top-of-form info banner when the user changes the egress scope (only the error clears)', async () => {
        // Counter-test: clearEgressError() must NOT wipe out a non-error
        // top alert (e.g. the "research queued" rerun hint) when the user
        // changes the scope in response to a stale error. Only the error
        // itself is stale; the other banners are still relevant.
        // Pre-populate the top alert with a non-error (success) banner.
        const topAlert = document.getElementById('research-alert');
        topAlert.innerHTML =
            '<div class="alert alert-success">Research queued — you can leave this page.</div>';
        topAlert.style.display = 'block';

        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();
        // The error overwrites the top alert (it has its own showAlert).
        expect(document.getElementById('research-alert').style.display).toBe('block');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        // An error was just shown; clearEgressError removes it. The earlier
        // success message is gone (it was replaced by the error), so the
        // top alert ends up empty/hidden — which is the correct outcome
        // for an error->submit->change flow.
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
    });
});

describe('research form — egress denial WITHOUT a form-field fix', () => {
    // The backend returns ``field: null`` for engine_unknown, unclassified,
    // and internal_error denials — those can't be fixed by toggling the
    // egress scope (or any other form field). The frontend must surface
    // the alert but must NOT highlight a form field as invalid.
    function stubNonFieldDenial(message, reason) {
        fetchMock.mockImplementationOnce(() =>
            Promise.resolve({
                ok: false,
                status: 400,
                json: () =>
                    Promise.resolve({
                        status: 'error',
                        message,
                        reason,
                        field: null,
                    }),
                text: () => Promise.resolve(''),
            })
        );
    }

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

    beforeEach(() => {
        const err = document.getElementById('research-error-alert');
        err.innerHTML = '';
        err.style.display = 'none';
        document.getElementById('model_hidden').value = 'qwen3:4b';
        // Reset the egress-scope error specifically (other fields don't
        // matter for these cases).
        const scope = document.getElementById('policy_egress_scope');
        scope.classList.remove('ldr-field-invalid');
        scope.removeAttribute('aria-invalid');
        const fieldErr = document.getElementById('policy_egress_scope-error');
        if (fieldErr) {
            fieldErr.textContent = '';
            fieldErr.style.display = 'none';
        }
    });

    it.each([
        ['engine_unknown', 'The search engine "bogus" isn\'t recognised.'],
        ['unclassified', '"bogus" couldn\'t be classified.'],
        ['internal_error', 'An internal error occurred.'],
    ])(
        'shows the alert but does NOT flag the egress scope field for %s',
        async (reason, message) => {
            stubNonFieldDenial(message, reason);
            submitForm();
            await flush();

            const anchored = document.getElementById('research-error-alert');
            expect(anchored.style.display).toBe('block');
            expect(anchored.textContent).toContain(message);

            // No form field is flagged invalid — the user can't fix the
            // underlying issue from the form (it's a config / server issue).
            expect(
                document
                    .getElementById('policy_egress_scope')
                    .getAttribute('aria-invalid')
            ).toBeNull();
            const fieldErr = document.getElementById('policy_egress_scope-error');
            expect(fieldErr.textContent).toBe('');
        }
    );
});

describe('research form — model list loading', () => {
    it('fetches the model list only once per page load (no duplicate concurrent request)', () => {
        // Regression guard: initializeResearch() and setupEventListeners() each
        // used to fire their own loadModelOptions(false), landing two concurrent
        // /available-models requests on every load and contending for a cold
        // Ollama — a primary reason the model dropdown came up empty.
        expect(initAvailableModelsCalls).toBe(1);
    });
});

describe("research form — egress lock truthfulness", () => {
    it("omits disabled operator-controlled fields from the run payload", () => {
        document.getElementById("model_hidden").value = "qwen3:4b";
        document.getElementById("policy_egress_scope").disabled = true;
        document.getElementById("llm_require_local_endpoint").disabled = true;
        document.getElementById("embeddings_require_local").disabled = true;

        submitForm();

        const body = JSON.parse(callsTo(START_RESEARCH)[0][1].body);
        expect(body).not.toHaveProperty("policy_egress_scope");
        expect(body).not.toHaveProperty("llm_require_local_endpoint");
        expect(body).not.toHaveProperty("embeddings_require_local");
    });

    it("includes explicit false locality values when controls are editable", () => {
        document.getElementById("model_hidden").value = "qwen3:4b";

        submitForm();

        const body = JSON.parse(callsTo(START_RESEARCH)[0][1].body);
        expect(body.policy_egress_scope).toBe("adaptive");
        expect(body.llm_require_local_endpoint).toBe(false);
        expect(body.embeddings_require_local).toBe(false);
    });

    it("restores an environment lock after leaving Private only", () => {
        const scope = document.getElementById("policy_egress_scope");
        const llm = document.getElementById("llm_require_local_endpoint");
        llm.dataset.envLocked = "true";
        llm.dataset.envValue = "false";
        llm.dataset.envTitle = "operator lock";
        llm.checked = false;
        llm.disabled = true;

        scope.value = "private_only";
        scope.dispatchEvent(new Event("change"));
        expect(llm.checked).toBe(true);
        expect(llm.disabled).toBe(true);

        scope.value = "adaptive";
        scope.dispatchEvent(new Event("change"));
        expect(llm.checked).toBe(false);
        expect(llm.disabled).toBe(true);
        expect(llm.title).toBe("operator lock");
    });

    it("refreshes rejected scope changes from effective server metadata", async () => {
        const scope = document.getElementById("policy_egress_scope");
        // Route mocks by URL + method rather than call order: a scope
        // change also fires the issue-#5204 search-engine re-fetch, so
        // call-ordered mockImplementationOnce entries would be consumed
        // by the wrong request. Everything that isn't the scope-setting
        // endpoint gets the same harmless stub as the beforeAll base
        // (no engine_options key, non-success status → no navigation).
        fetchMock.mockImplementation((url, options) => {
            if (url === "/settings/api/policy.egress_scope") {
                if (options?.method === "PUT") {
                    return Promise.resolve({
                        ok: false,
                        status: 400,
                        json: () => Promise.resolve({ error: "operator rejected" }),
                    });
                }
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () => Promise.resolve({
                        value: "adaptive",
                        editable: true,
                    }),
                });
            }
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () => Promise.resolve({
                    status: "error",
                    message: "stubbed — no navigation",
                    providers: {},
                    provider_options: [],
                    models: [],
                    engines: [],
                }),
                text: () => Promise.resolve(""),
            });
        });

        scope.value = "private_only";
        scope.dispatchEvent(new Event("change"));

        await vi.waitFor(() => {
            expect(scope.value).toBe("adaptive");
            expect(scope.disabled).toBe(false);
        });
        expect(scope.dataset.savedValue).toBe("adaptive");
        expect(document.querySelector(".ldr-privacy-panel").dataset.scope).toBe("adaptive");
        expect(document.body.dataset.scope).toBe("adaptive");
    });

    it("serializes rapid scope saves in selection order", async () => {
        const scope = document.getElementById("policy_egress_scope");
        let resolveFirst;
        const successfulResponse = () => ({
            ok: true,
            status: 200,
            json: () => Promise.resolve({}),
        });
        // Route by URL + method (see the previous test): only PUTs to
        // the scope-setting endpoint participate in the serialization
        // under test; the issue-#5204 engines re-fetch and any other
        // traffic resolve immediately with the base stub.
        let scopePutCount = 0;
        fetchMock.mockImplementation((url, options) => {
            if (
                url === "/settings/api/policy.egress_scope" &&
                options?.method === "PUT"
            ) {
                scopePutCount += 1;
                if (scopePutCount === 1) {
                    return new Promise(resolve => {
                        resolveFirst = resolve;
                    });
                }
                return Promise.resolve(successfulResponse());
            }
            return Promise.resolve({
                ok: true,
                status: 200,
                json: () => Promise.resolve({
                    status: "error",
                    message: "stubbed — no navigation",
                    providers: {},
                    provider_options: [],
                    models: [],
                    engines: [],
                }),
                text: () => Promise.resolve(""),
            });
        });

        scope.value = "private_only";
        scope.dispatchEvent(new Event("change"));
        scope.value = "adaptive";
        scope.dispatchEvent(new Event("change"));

        await vi.waitFor(() => {
            const writes = fetchMock.mock.calls.filter(
                ([url, options]) =>
                    url === "/settings/api/policy.egress_scope" &&
                    options?.method === "PUT"
            );
            expect(writes).toHaveLength(1);
        });

        resolveFirst(successfulResponse());
        await vi.waitFor(() => {
            const writes = fetchMock.mock.calls.filter(
                ([url, options]) =>
                    url === "/settings/api/policy.egress_scope" &&
                    options?.method === "PUT"
            );
            expect(writes).toHaveLength(2);
            expect(JSON.parse(writes[0][1].body).value).toBe("private_only");
            expect(JSON.parse(writes[1][1].body).value).toBe("adaptive");
        });
    });
});

describe('research form — submit-owned alert clearing (generic + egress errors)', () => {
    // Regression: clearEgressError() used to remove only alerts tagged as
    // egress errors (`data-egress-alert="true"`). Generic submission
    // errors (network failures, 5xx, malformed bodies) were untagged and
    // therefore survived a retry or an early validation return — appearing
    // to describe the new attempt. The submit handler now tags every
    // alert it creates with `data-submit-alert="true"` and clearSubmitAlerts()
    // removes all of them, so generic AND egress errors both clear on
    // retry / setting change / early validation.

    function stubGenericError(message) {
        fetchMock.mockImplementationOnce(() =>
            Promise.resolve({
                ok: false,
                status: 500,
                json: () => Promise.resolve({ status: 'error', message }),
                text: () => Promise.resolve(''),
            })
        );
    }

    function stubNetworkError() {
        fetchMock.mockImplementationOnce(() => Promise.reject(new Error('network down')));
    }

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

    beforeEach(() => {
        // Reset both alert containers and all field-error state.
        const top = document.getElementById('research-alert');
        top.innerHTML = '';
        top.style.display = 'none';
        const anchored = document.getElementById('research-error-alert');
        anchored.innerHTML = '';
        anchored.style.display = 'none';
        // A model must be present so the model-required guard does not
        // block submit before the fetch (and the error UX) ever runs.
        document.getElementById('model_hidden').value = 'qwen3:4b';
    });

    it('tags generic submission errors as submit-owned so clearSubmitAlerts() can find them', async () => {
        stubGenericError('Search engine is unavailable');
        submitForm();
        await flush();

        const topAlertEl = document
            .getElementById('research-alert')
            .querySelector('.alert');
        const anchoredAlertEl = document
            .getElementById('research-error-alert')
            .querySelector('.alert');
        expect(topAlertEl).not.toBeNull();
        expect(anchoredAlertEl).not.toBeNull();
        expect(topAlertEl.getAttribute('data-submit-alert')).toBe('true');
        expect(anchoredAlertEl.getAttribute('data-submit-alert')).toBe('true');
    });

    it('clears generic errors on the next submit, before validation runs', async () => {
        stubGenericError('Search engine is unavailable');
        submitForm();
        await flush();
        expect(document.getElementById('research-alert').style.display).toBe('block');
        expect(document.getElementById('research-error-alert').style.display).toBe('block');

        // Second submit with a model present — fetch will be stubbed
        // again to a 200 success, but we don't care: the point is that
        // the prior generic error was wiped BEFORE the new attempt ran.
        submitForm();
        // Sync portion of clearSubmitAlerts() has run; the rest is in
        // the fetch microtask chain.
        expect(document.getElementById('research-alert').style.display).toBe('none');
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
    });

    it('clears generic errors when the user changes the egress scope', async () => {
        stubGenericError('Search engine is unavailable');
        submitForm();
        await flush();
        expect(document.getElementById('research-alert').style.display).toBe('block');
        expect(document.getElementById('research-error-alert').style.display).toBe('block');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        expect(document.getElementById('research-alert').style.display).toBe('none');
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
    });

    it('clears generic errors when the next submit fails the query-required guard', async () => {
        // First attempt: generic network error shows in both alert slots.
        stubNetworkError();
        submitForm();
        await flush();
        expect(document.getElementById('research-alert').style.display).toBe('block');
        expect(document.getElementById('research-error-alert').style.display).toBe('block');

        // Second attempt: empty query → submit handler clears alerts
        // first, then the query guard returns early with its own
        // inline error. The previous generic error must NOT survive.
        document.getElementById('query').value = '';
        submitForm();

        expect(document.getElementById('research-alert').style.display).toBe('none');
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
        // The query guard ran: the query field is now flagged invalid.
        expect(document.getElementById('query').getAttribute('aria-invalid')).toBe('true');
    });

    it('clears generic errors when the next submit fails the model-required guard', async () => {
        // First attempt: generic network error shows in both alert slots.
        stubNetworkError();
        submitForm();
        await flush();
        expect(document.getElementById('research-alert').style.display).toBe('block');
        expect(document.getElementById('research-error-alert').style.display).toBe('block');

        // Second attempt: model missing → submit handler clears alerts
        // first, then the model guard returns early.
        document.getElementById('model_hidden').value = '';
        submitForm();

        expect(document.getElementById('research-alert').style.display).toBe('none');
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
        // The model guard ran: the model field is now flagged invalid.
        expect(document.getElementById('model').getAttribute('aria-invalid')).toBe('true');
    });

    it('still preserves an unrelated non-submit danger alert when clearing', async () => {
        // Pre-populate an UNRELATED danger alert (no data-submit-alert
        // tag) into the top container, then trigger a generic error
        // (which overwrites it via showSafeAlert's inner clear), then
        // change the egress scope — the prior submit-owned error is
        // gone but if a non-submit alert were present it would survive.
        const top = document.getElementById('research-alert');
        top.innerHTML = '<div class="alert alert-danger">Unrelated</div>';
        top.style.display = 'block';

        // Simulate the state after a submit: the new error replaced
        // the old one and is tagged. Put a new unrelated alert AFTER
        // the tagged one to prove selective removal.
        stubGenericError('Search engine is unavailable');
        submitForm();
        await flush();
        // After submit, only the tagged generic error is present.
        expect(top.querySelectorAll('.alert').length).toBe(1);

        // Now inject an unrelated alert in addition to the tagged one
        // (simulating a third-party code path pushing a warning) and
        // change the scope. The tagged one clears, the untagged one
        // survives.
        const unrelated = document.createElement('div');
        unrelated.className = 'alert alert-warning';
        unrelated.textContent = 'Settings page warning';
        top.appendChild(unrelated);

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        // Tagged generic error is gone; untagged warning is preserved.
        const remaining = top.querySelectorAll('.alert');
        expect(remaining.length).toBe(1);
        expect(remaining[0].textContent).toContain('Settings page warning');
    });
});

describe('research form — malformed response resilience', () => {
    // Regression: the submit handler used to call .includes() on
    // data.reason / data.message without checking types, and the
    // button re-enable lived AFTER that classification. A malformed
    // response (e.g. data as a number, data.reason as an object) would
    // throw inside the .then() and leave the button permanently
    // disabled with the loading overlay stuck. The classifier is now
    // type-safe (typeof checks) and the button/overlay reset is in a
    // finally block so it always runs.

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

    beforeEach(() => {
        document.getElementById('model_hidden').value = 'qwen3:4b';
        document.querySelectorAll('.ldr-loading-overlay').forEach((o) => o.remove());
    });

    it.each([
        ['null', () => Promise.resolve(null)],
        ['number', () => Promise.resolve(42)],
        ['array', () => Promise.resolve(['error'])],
        ['missing status', () => Promise.resolve({ message: 'no status field' })],
        ['reason as object', () => Promise.resolve({
            status: 'error', reason: { kind: 'scope_mismatch' }, message: 'x'
        })],
        ['reason as number', () => Promise.resolve({
            status: 'error', reason: 42, message: 'x'
        })],
        ['message as object', () => Promise.resolve({
            status: 'error', message: { nested: 'thing' }
        })],
    ])('does not throw and re-enables the button for malformed body: %s', async (_label, bodyFactory) => {
        fetchMock.mockImplementationOnce(() => Promise.resolve({
            ok: false,
            status: 400,
            json: bodyFactory,
            text: () => Promise.resolve(''),
        }));
        const btn = document.getElementById('start-research-btn');
        expect(btn.disabled).toBe(false);

        submitForm();
        await flush();

        // The button must be re-enabled so the user can try again.
        expect(btn.disabled).toBe(false);
        // The loading overlay must be removed.
        expect(document.querySelector('.ldr-loading-overlay')).toBeNull();
    });

    it('does not trust a success-shaped body from a non-ok response', async () => {
        fetchMock.mockImplementationOnce(() => Promise.resolve({
            ok: false,
            status: 503,
            json: () => Promise.resolve({
                status: 'success',
                research_id: 'must-not-redirect',
            }),
            text: () => Promise.resolve(''),
        }));
        const progressPageSpy = vi.spyOn(window.URLBuilder, 'progressPage')
            .mockReturnValue(window.location.href);

        try {
            submitForm();
            await flush();

            expect(progressPageSpy).not.toHaveBeenCalled();
            expect(document.getElementById('research-alert').textContent)
                .toContain('Failed to start research');
            expect(document.getElementById('start-research-btn').disabled)
                .toBe(false);
            expect(document.querySelector('.ldr-loading-overlay')).toBeNull();
        } finally {
            progressPageSpy.mockRestore();
        }
    });

    it.each([
        ['missing', undefined],
        ['blank', '   '],
    ])('does not redirect when a successful response has a %s research ID', async (
        _label,
        researchId,
    ) => {
        fetchMock.mockImplementationOnce(() => Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({
                status: 'success',
                research_id: researchId,
            }),
            text: () => Promise.resolve(''),
        }));
        const progressPageSpy = vi.spyOn(window.URLBuilder, 'progressPage')
            .mockReturnValue(window.location.href);

        try {
            submitForm();
            await flush();

            expect(progressPageSpy).not.toHaveBeenCalled();
            expect(document.getElementById('research-alert').textContent)
                .toContain('Failed to start research');
            expect(document.getElementById('start-research-btn').disabled)
                .toBe(false);
            expect(document.querySelector('.ldr-loading-overlay')).toBeNull();
        } finally {
            progressPageSpy.mockRestore();
        }
    });

    it('surfaces a top-of-form alert with a fallback message when the body has no message field', async () => {
        fetchMock.mockImplementationOnce(() => Promise.resolve({
            ok: false,
            status: 500,
            json: () => Promise.resolve({}),
            text: () => Promise.resolve(''),
        }));
        submitForm();
        await flush();

        const top = document.getElementById('research-alert');
        expect(top.style.display).toBe('block');
        expect(top.textContent).toContain('Failed to start research');
        const btn = document.getElementById('start-research-btn');
        expect(btn.disabled).toBe(false);
    });

    it.each([
        ['empty string', ''],
        ['whitespace-only string', '   \n\t  '],
    ])('uses the fallback message when message is a %s', async (_label, message) => {
        // Regression: typeof data.message === 'string' used to accept any
        // string, including '' and '   ', so the alert rendered empty
        // (invisible to the user) — recreating the original "click submit
        // and nothing happens" symptom. Blank / whitespace-only messages
        // must be treated as absent and fall back to the generic copy.
        fetchMock.mockImplementationOnce(() => Promise.resolve({
            ok: false,
            status: 500,
            json: () => Promise.resolve({ message }),
            text: () => Promise.resolve(''),
        }));
        submitForm();
        await flush();

        const top = document.getElementById('research-alert');
        const anchored = document.getElementById('research-error-alert');
        // The fallback copy is present in BOTH alert slots — no empty
        // alert / accidental whitespace render. The trimmed blank input
        // must NOT have leaked through.
        expect(top.textContent.trim()).toContain('Failed to start research');
        expect(anchored.textContent.trim()).toContain('Failed to start research');
        // And the button is re-enabled so the user can retry.
        const btn = document.getElementById('start-research-btn');
        expect(btn.disabled).toBe(false);
    });

    it.each([
        ['empty string', ''],
        ['whitespace-only string', '   \n\t  '],
    ])('does not flag a blank field name (%s)', async (_label, field) => {
        // Counterpart test: a blank/whitespace field name must NOT be
        // passed to FormValidator.showError(); that would flag a field
        // with id="" (a no-op for querySelector, but conceptually
        // wrong). The non-blank-string check on field must match the
        // one on message.
        fetchMock.mockImplementationOnce(() => Promise.resolve({
            ok: false,
            status: 400,
            json: () => Promise.resolve({
                message: 'A normal error.',
                reason: 'scope_mismatch_public_only',
                field,
            }),
            text: () => Promise.resolve(''),
        }));
        submitForm();
        await flush();

        // No form field is flagged invalid.
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBeNull();
        expect(document.getElementById('query').getAttribute('aria-invalid')).toBeNull();
        expect(document.getElementById('model').getAttribute('aria-invalid')).toBeNull();
    });
});

describe('research form — egress-scope change preserves unrelated field errors', () => {
    // Regression: the egress-scope change listener used to call
    // clearSubmitAlerts(), which called researchValidator.clearErrors()
    // for every registered field. That meant changing only Egress Scope
    // would silently hide an unresolved query-required / model-required
    // error, which is unrelated and must still be visible. The change
    // listener now wipes only the egress-scope inline error via
    // FormValidator.clearFieldError(); submit-owned alerts are still
    // dismissed, but other field errors are untouched.

    function stubEgressDenial(message) {
        fetchMock.mockImplementationOnce(() =>
            Promise.resolve({
                ok: false,
                status: 400,
                json: () => Promise.resolve({
                    status: 'error',
                    message,
                    reason: 'scope_mismatch_public_only',
                    field: 'policy_egress_scope',
                }),
                text: () => Promise.resolve(''),
            })
        );
    }

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

    function flagEgressScopeDirectly() {
        // Simulate the field being flagged outside of a submit (e.g.
        // the user-acknowledged retry flow where the server didn't get
        // to flag it themselves, or a stale state carried over from a
        // prior page load). Mirrors the showError() path used by
        // showFormError().
        const scope = document.getElementById('policy_egress_scope');
        scope.classList.add('ldr-field-invalid');
        scope.setAttribute('aria-invalid', 'true');
        const fieldErr = document.getElementById('policy_egress_scope-error');
        if (fieldErr) {
            fieldErr.textContent = 'Egress scope blocked';
            fieldErr.style.display = 'block';
        }
    }

    function flagQueryMissing() {
        // Mirror the query validator's behaviour when the field is empty.
        const q = document.getElementById('query');
        q.classList.add('ldr-field-invalid');
        q.setAttribute('aria-invalid', 'true');
        const fieldErr = document.getElementById('query-error');
        if (fieldErr) {
            fieldErr.textContent = 'Please enter a research query.';
            fieldErr.style.display = 'block';
        }
    }

    function flagModelMissing() {
        const m = document.getElementById('model');
        m.classList.add('ldr-field-invalid');
        m.setAttribute('aria-invalid', 'true');
        const fieldErr = document.getElementById('model-error');
        if (fieldErr) {
            fieldErr.textContent = 'Please select or enter a model.';
            fieldErr.style.display = 'block';
        }
    }

    beforeEach(() => {
        const top = document.getElementById('research-alert');
        top.innerHTML = '';
        top.style.display = 'none';
        const anchored = document.getElementById('research-error-alert');
        anchored.innerHTML = '';
        anchored.style.display = 'none';
        document.getElementById('model_hidden').value = 'qwen3:4b';
        // Reset every error field so each test starts from a clean slate.
        for (const id of ['query', 'model', 'policy_egress_scope']) {
            const el = document.getElementById(id);
            el.classList.remove('ldr-field-invalid');
            el.removeAttribute('aria-invalid');
            const fieldErr = document.getElementById(`${id}-error`);
            if (fieldErr) {
                fieldErr.textContent = '';
                fieldErr.style.display = 'none';
            }
        }
    });

    it('clears ONLY the egress-scope inline error when the egress scope changes', async () => {
        // Set up an egress-scope error via the real submit path.
        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();
        flagEgressScopeDirectly();
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBe('true');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        // The egress-scope inline error is wiped...
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBeNull();
        const egressFieldErr = document.getElementById('policy_egress_scope-error');
        expect(egressFieldErr.textContent).toBe('');
        // ...and the submit-owned alert toasts are gone.
        expect(document.getElementById('research-alert').style.display).toBe('none');
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
    });

    it('preserves an unrelated query-required error when the egress scope changes', async () => {
        flagEgressScopeDirectly();
        flagQueryMissing();
        // Sanity: both errors are showing before the change.
        expect(document.getElementById('query').getAttribute('aria-invalid')).toBe('true');
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBe('true');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        // The egress error is cleared...
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBeNull();
        const egressFieldErr = document.getElementById('policy_egress_scope-error');
        expect(egressFieldErr.textContent).toBe('');
        // ...but the query error is STILL visible — changing the scope
        // is not a user-acknowledged retry for query-required.
        expect(document.getElementById('query').getAttribute('aria-invalid')).toBe('true');
        expect(document.getElementById('query-error').textContent).toContain(
            'Please enter a research query.'
        );
    });

    it('preserves an unrelated model-required error when the egress scope changes', async () => {
        flagEgressScopeDirectly();
        flagModelMissing();
        expect(document.getElementById('model').getAttribute('aria-invalid')).toBe('true');
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBe('true');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        // Egress error is cleared...
        expect(
            document.getElementById('policy_egress_scope').getAttribute('aria-invalid')
        ).toBeNull();
        // ...model error is preserved.
        expect(document.getElementById('model').getAttribute('aria-invalid')).toBe('true');
        expect(document.getElementById('model-error').textContent).toContain(
            'Please select or enter a model.'
        );
    });

    it('still wipes submit-owned alert toasts when the egress scope changes', async () => {
        // The change listener should still dismiss the submit-owned toast
        // alerts so a stale message stops being re-announced on the next
        // interaction. This is the part of the prior behaviour that did NOT
        // regress; we keep it explicit so a future refactor can't silently
        // re-introduce the regression from the other direction.
        stubEgressDenial('Search engine blocked by Egress Scope.');
        submitForm();
        await flush();
        expect(document.getElementById('research-alert').style.display).toBe('block');
        expect(document.getElementById('research-error-alert').style.display).toBe('block');

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));

        expect(document.getElementById('research-alert').style.display).toBe('none');
        expect(document.getElementById('research-error-alert').style.display).toBe('none');
    });
});

describe('research form — scope-aware search engine dropdown (issue #5204)', () => {
    // The Search Engine dropdown must re-evaluate which options are
    // selectable whenever the active Egress Scope or the saved
    // primary changes. Disabled entries get aria-disabled="true" and
    // a one-line reason label; the user's current selection is
    // preserved (the precheck is the second backstop if it becomes
    // disabled). The backend precheck + the PR #5126 inline-error
    // path are unchanged — this is the UX layer.

    const ENGINES_URL = '/settings/api/available-search-engines';
    const FULL_OPTIONS = [
        { value: 'arxiv', label: 'ArXiv', category: 'Scientific', requires_api_key: false, is_favorite: false,
          group: 'scientific', group_label: 'Scientific', group_order: 3,
          base_group: 'scientific', base_group_label: 'Scientific', base_group_order: 3 },
        { value: 'github', label: 'GitHub', category: 'Code', requires_api_key: false, is_favorite: false,
          group: 'code', group_label: 'Code', group_order: 5,
          base_group: 'code', base_group_label: 'Code', base_group_order: 5 },
        { value: 'library', label: 'Library', category: 'Local RAG', requires_api_key: false, is_favorite: false,
          group: 'local', group_label: 'Local RAG', group_order: 1,
          base_group: 'local', base_group_label: 'Local RAG', base_group_order: 1 },
        { value: 'collection_disabled', label: 'Indian History (Collection)', category: 'Local RAG', requires_api_key: false, is_favorite: false,
          group: 'local', group_label: 'Local RAG', group_order: 1,
          base_group: 'local', base_group_label: 'Local RAG', base_group_order: 1,
          agent_enabled: false },
        { value: 'collection_enabled', label: 'Sci Papers (Collection)', category: 'Local RAG', requires_api_key: false, is_favorite: false,
          group: 'local', group_label: 'Local RAG', group_order: 1,
          base_group: 'local', base_group_label: 'Local RAG', base_group_order: 1,
          agent_enabled: true },
    ];

    function stubEngines(url) {
        // The handler can match either an unfiltered request (no
        // query params) or an egress-aware one (?egress_scope=&primary=).
        // The URL produced by the app may be relative or fully
        // qualified depending on the env (happy-dom returns
        // ``http://localhost:3000/...`` because window.location.origin
        // is set); match both.
        const isEnginesUrl = (u) =>
            !!u &&
            (u === ENGINES_URL ||
                u.startsWith(ENGINES_URL + '?') ||
                u.includes(ENGINES_URL + '?') ||
                u.endsWith(ENGINES_URL));
        if (!isEnginesUrl(url)) return null;
        // Detect the egress-scope query string so the test can drive
        // the backend contract end-to-end.
        const qIdx = (url || '').indexOf('?');
        const qs = qIdx >= 0 ? (url || '').slice(qIdx + 1) : '';
        const hasEgress = qs.includes('egress_scope=');
        let body = { engine_options: FULL_OPTIONS, engines: {}, favorites: [] };
        if (hasEgress) {
            const params = new URLSearchParams(qs);
            const scope = params.get('egress_scope') || '';
            const primary = params.get('primary') || '';
            body = {
                engine_options: FULL_OPTIONS.map((opt) => decorateForScope(opt, scope, primary)),
                engines: {},
                favorites: [],
            };
        }
        return {
            ok: true,
            status: 200,
            json: () => Promise.resolve(body),
            text: () => Promise.resolve(''),
        };
    }

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

    function getEnginesCalls() {
        // The URL is fully qualified in happy-dom
        // (http://localhost:3000/settings/api/...); match the path
        // suffix so both the relative form and the absolute form
        // produced by the URL builder count.
        return fetchMock.mock.calls.filter(([u]) => {
            if (!u) return false;
            return (
                u === ENGINES_URL ||
                u.startsWith(ENGINES_URL + '?') ||
                u.endsWith(ENGINES_URL) ||
                u.includes(ENGINES_URL + '?')
            );
        });
    }

    function openDropdownAndReadOptions() {
        // Drive the custom dropdown's open path: focus the input,
        // then read the rendered option list. Mirrors how a real user
        // would open the dropdown.
        const input = document.getElementById('search_engine');
        input.dispatchEvent(new Event('focus'));
        const list = document.getElementById('search-engine-dropdown-list');
        return Array.from(list.querySelectorAll('.ldr-custom-dropdown-item'));
    }

    async function waitForOptionDisabled(value, expectedDisabled) {
        let option;
        await vi.waitFor(() => {
            option = openDropdownAndReadOptions().find(
                element => element.getAttribute('data-value') === value
            );
            expect(option).toBeDefined();
            expect(option.classList.contains('ldr-custom-dropdown-item--disabled'))
                .toBe(expectedDisabled);
        });
        return option;
    }

    function decorateForScope(opt, scope, primary) {
        if (scope === 'private_only') {
            if (opt.value === 'library' || opt.category === 'Local RAG') {
                return { ...opt, egress: { allowed: true, reason: 'allowed' } };
            }
            return { ...opt, egress: { allowed: false, reason: 'scope_mismatch_private_only' } };
        }
        if (scope === 'public_only') {
            if (opt.category === 'Local RAG') {
                return { ...opt, egress: { allowed: false, reason: 'scope_mismatch_public_only' } };
            }
            return { ...opt, egress: { allowed: true, reason: 'allowed' } };
        }
        if (scope === 'strict') {
            if (primary && opt.value === primary) {
                return { ...opt, egress: { allowed: true, reason: 'primary_carve_out' } };
            }
            return { ...opt, egress: { allowed: false, reason: 'strict_not_primary' } };
        }
        // adaptive / unknown / no-scope => no egress annotation
        return { ...opt };
    }

    beforeEach(() => {
        // Reset the egress scope + search engine back to defaults so
        // each test starts from a known state. The base beforeEach
        // already clears the form, so we just touch the two fields.
        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'adaptive';
        const hidden = document.getElementById('search_engine_hidden');
        hidden.value = 'arxiv';
        const input = document.getElementById('search_engine');
        input.value = '';
        document.getElementById('strategy').value = 'source-based';
        fetchMock.mockClear();
    });

    it('initial load includes the egress_scope query param when a scope is set', async () => {
        document.getElementById('policy_egress_scope').value = 'private_only';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        // Dispatch the scope change so the reapplier fires.
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const calls = getEnginesCalls();
        expect(calls.length).toBeGreaterThan(0);
        // The reapplier (or initial load, depending on test order)
        // asked for the egress-aware shape.
        const hasEgressCall = calls.some(([u]) => u.includes('egress_scope=private_only'));
        expect(hasEgressCall).toBe(true);
    });

    it('marks options disabled with aria-disabled and a one-line reason', async () => {
        // Set the primary to library (a local engine) so a PUBLIC
        // engine (arxiv/github) is the one that gets denied under
        // private_only — otherwise the primary carve-out keeps the
        // public primary allowed and there'd be nothing to disable.
        document.getElementById('search_engine_hidden').value = 'library';
        document.getElementById('policy_egress_scope').value = 'private_only';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const rendered = openDropdownAndReadOptions();
        // Every option the backend returned is rendered.
        expect(rendered.length).toBe(FULL_OPTIONS.length);
        // Arxiv (public) is denied under private_only; library (the
        // local primary) is allowed by carve-out.
        const arxiv = await waitForOptionDisabled('arxiv', true);
        const library = await waitForOptionDisabled('library', false);
        expect(arxiv.getAttribute('aria-disabled')).toBe('true');
        const reason = arxiv.querySelector('.ldr-dropdown-item-disabled-reason');
        expect(reason).not.toBeNull();
        expect(reason.textContent).toMatch(/not a local source under Private only/i);
        // The local primary is allowed (carve-out) and not disabled.
        expect(library.getAttribute('aria-disabled')).toBeNull();
    });

    it('re-applies when the user switches to a different scope', async () => {
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        // 1. Switch to public_only.
        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'public_only';
        scope.dispatchEvent(new Event('change'));
        await flush();
        await flush();

        // The local library entry is now disabled (local source under
        // public-only is denied).
        await waitForOptionDisabled('library', true);

        // 2. Switch to private_only.
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));
        await flush();
        await flush();

        // The local library entry is now allowed.
        await waitForOptionDisabled('library', false);
    });

    it('disables primary if it violates the active egress scope', async () => {
        // The user's saved search.tool (arxiv) is the primary.
        document.getElementById('search_engine_hidden').value = 'arxiv';
        document.getElementById('policy_egress_scope').value = 'private_only';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        // arxiv is a public engine so under private_only it must be disabled
        await waitForOptionDisabled('arxiv', true);
    });

    it('does not request egress filtering under "Adaptive" scope so all primary options remain selectable', async () => {
        document.getElementById('policy_egress_scope').value = 'adaptive';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const calls = getEnginesCalls();
        // Under Adaptive, options are unfiltered (no ?egress_scope=) so user can pick any primary
        const hasEgressCall = calls.some(([u]) => u.includes('egress_scope='));
        expect(hasEgressCall).toBe(false);
    });

    it('does not request egress filtering under "Primary only" (strict) scope', async () => {
        document.getElementById('policy_egress_scope').value = 'strict';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const calls = getEnginesCalls();
        // Under strict scope, options are unfiltered so user can choose any single engine as primary
        const hasEgressCall = calls.some(([u]) => u.includes('egress_scope='));
        expect(hasEgressCall).toBe(false);
    });

    it('does not request egress filtering under "Unprotected" (escape hatch)', async () => {
        document.getElementById('policy_egress_scope').value = 'unprotected';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const calls = getEnginesCalls();
        const hasEgressCall = calls.some(([u]) => u.includes('egress_scope='));
        expect(hasEgressCall).toBe(false);
    });

    it('switches the visible selection to a pre-configured favorite when the current engine becomes hidden', async () => {
        // The user picked github (public, allowed under public_only).
        // They then switch to private_only. github becomes disabled.
        // The reconcile step should switch the visible selection to
        // the next pre-configured favorite — SearXNG first, then
        // library ("Search All Collections") — so the form is never
        // submitted with a hidden value by default.
        document.getElementById('search_engine_hidden').value = 'github';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engine_options: [], engines: [] }),
            text: () => Promise.resolve(''),
        }));

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

        // github was hidden under private_only — the reconcile should
        // have moved the selection to the library fallback.
        expect(document.getElementById('search_engine_hidden').value).toBe('library');
        const input = document.getElementById('search_engine');
        expect(input.value).toBe('Library');
    });

    it('reconciles selection from library to a public engine when switching to public_only', async () => {
        document.getElementById('search_engine_hidden').value = 'library';
        document.getElementById('search_engine').value = 'Library';
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

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

        // library is disabled under public_only, so selection reconciles to arxiv/searxng (allowed public engine)
        expect(document.getElementById('search_engine_hidden').value).toBe('arxiv');
        expect(document.getElementById('search_engine').value).toBe('ArXiv');
    });

    it('prefers SearXNG over library when picking the default', async () => {
        // With both SearXNG and library in the option list under
        // public_only, a hidden selection must reconcile to SearXNG
        // (the preferred default).
        const options = [
            { value: 'arxiv', label: 'ArXiv', category: 'Scientific', requires_api_key: false, is_favorite: false,
              group: 'scientific', group_label: 'Scientific', group_order: 3,
              base_group: 'scientific', base_group_label: 'Scientific', base_group_order: 3 },
            { value: 'searxng', label: 'SearXNG', category: 'Web Search', requires_api_key: false, is_favorite: false,
              group: 'web', group_label: 'Web Search', group_order: 2,
              base_group: 'web', base_group_label: 'Web Search', base_group_order: 2 },
            { value: 'library', label: 'Library', category: 'Local RAG', requires_api_key: false, is_favorite: false,
              group: 'local', group_label: 'Local RAG', group_order: 1,
              base_group: 'local', base_group_label: 'Local RAG', base_group_order: 1 },
        ];
        fetchMock.mockImplementation(() => Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({
                engine_options: options.map((o) => (
                    o.value === 'searxng'
                        ? { ...o, egress: { allowed: true, reason: 'allowed' } }
                        : { ...o, egress: { allowed: false, reason: 'scope_mismatch_private_only' } }
                )),
                engines: {},
                favorites: [],
            }),
            text: () => Promise.resolve(''),
        }));

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

        // arxiv is hidden; reconcile must prefer SearXNG over library.
        expect(document.getElementById('search_engine_hidden').value).toBe('searxng');
        expect(document.getElementById('search_engine').value).toBe('SearXNG');
    });

    it('keeps the user\'s current selection when it is still visible after an update', async () => {
        // When the current selection survives the options refresh (still allowed),
        // the reconcile must NOT touch it.
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

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

        // Select library through the real dropdown callback so both the DOM
        // fields and the module-private selectedSearchEngineValue agree.
        const library = openDropdownAndReadOptions().find(
            option => option.getAttribute('data-value') === 'library'
        );
        expect(library).toBeDefined();
        library.click();
        await flush();
        await flush();

        // A subsequent options refresh must preserve that live selection.
        scope.dispatchEvent(new Event('change'));
        await flush();
        await flush();

        // library is allowed under adaptive scope so it must stay selected
        expect(document.getElementById('search_engine_hidden').value).toBe('library');
    });

    it('re-applies when the user picks a different primary (saved engine changes)', async () => {
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        document.getElementById('policy_egress_scope').value = 'private_only';
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        fetchMock.mockClear();

        // Now change the saved primary: arxiv -> library. This must
        // trigger a re-fetch with primary=library, which reshapes
        // the carve-out and the disabled set.
        const hidden = document.getElementById('search_engine_hidden');
        hidden.value = 'library';
        hidden.dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const calls = getEnginesCalls();
        // The reapplier fired with the new primary in the URL.
        const hasPrimary = calls.some(([u]) => u.includes('primary=library'));
        expect(hasPrimary).toBe(true);
    });

    it('uses the submitted hidden primary when cached dropdown state diverges', async () => {
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        // First select library through the real dropdown so the module cache
        // owns a different value from the form field used below.
        document.getElementById('policy_egress_scope').value = 'adaptive';
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();
        const library = openDropdownAndReadOptions().find(
            option => option.getAttribute('data-value') === 'library'
        );
        expect(library).toBeDefined();
        library.click();
        await flush();
        await flush();

        // A form hydration/reconciliation path updates the submitted value.
        // Scope classification must use that value, not the stale cache.
        const hidden = document.getElementById('search_engine_hidden');
        hidden.value = 'arxiv';
        fetchMock.mockClear();
        const scope = document.getElementById('policy_egress_scope');
        scope.value = 'private_only';
        scope.dispatchEvent(new Event('change'));
        await flush();
        await flush();

        const privateOnlyCall = getEnginesCalls().find(([url]) => (
            url.includes('egress_scope=private_only')
        ));
        expect(privateOnlyCall).toBeDefined();
        expect(privateOnlyCall[0]).toContain('primary=arxiv');
        expect(privateOnlyCall[0]).not.toContain('primary=library');
    });

    it('reclassifies using the just-clicked primary, not the previous one (issue #5204 follow-up)', async () => {
        // Regression: the hidden input's change listener fires before
        // the dropdown's onSelect callback (custom_dropdown.js), which
        // means the re-fetch used to carry the STALE in-memory
        // primary. The newly selected engine could then briefly be
        // marked unavailable because the carve-out was still on the
        // old primary.
        //
        // Drive the real click path — open the dropdown, click an
        // option — and assert the fetch URL carries the NEW primary
        // and the dropdown re-renders with the right disabled set.
        fetchMock.mockImplementation((url) => Promise.resolve(stubEngines(url) || {
            ok: true, status: 200,
            json: () => Promise.resolve({ engines: [], engine_options: [] }),
            text: () => Promise.resolve(''),
        }));

        // Saved primary = arxiv, scope = private_only.
        document.getElementById('search_engine_hidden').value = 'arxiv';
        document.getElementById('policy_egress_scope').value = 'private_only';
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await flush();
        await flush();

        // Under private_only, arxiv (public) is disabled.
        await waitForOptionDisabled('arxiv', true);

        // User clicks library. library is a local engine, so it is
        // selectable under private_only.
        fetchMock.mockClear();
        const list = document.getElementById('search-engine-dropdown-list');
        const libraryItem = Array.from(list.querySelectorAll('.ldr-custom-dropdown-item'))
            .find((el) => el.getAttribute('data-value') === 'library');
        expect(libraryItem).toBeDefined();
        libraryItem.click();
        await flush();
        await flush();

        // The re-fetch must carry the NEWLY-CLICKED primary (library),
        // not the previous one (arxiv). The bug would emit primary=arxiv.
        const calls = getEnginesCalls();
        expect(calls.length).toBeGreaterThan(0);
        const postClickCall = calls.find(([u]) => u.includes('primary='));
        expect(postClickCall).toBeDefined();
        expect(postClickCall[0]).toContain('primary=library');
        expect(postClickCall[0]).not.toContain('primary=arxiv');

        // The hidden input reflects the new selection.
        expect(document.getElementById('search_engine_hidden').value).toBe('library');

        // The dropdown has re-rendered with the new disabled set:
        // arxiv loses its primary carve-out and is now denied under
        // private_only (because the re-fetch used primary=library).
        await waitForOptionDisabled('arxiv', true);
        await waitForOptionDisabled('library', false);
    });

    it('ignores a superseded (older) response when a newer fetch is in flight (issue #5204 follow-up)', async () => {
        // Regression for the rapid scope/primary-change race: every
        // scope/primary change fires a new fetch, and without
        // cancellation the last response to arrive wins. If the older
        // fetch completes last, the dropdown ends up showing
        // classifications for a scope/primary that is no longer
        // selected. The reapplier must abort the older request (or
        // drop its response via a generation token).
        const scopeSelect = document.getElementById('policy_egress_scope');
        const hidden = document.getElementById('search_engine_hidden');
        hidden.value = 'arxiv';

        // Build a controllable fetch: each call returns a Promise we
        // can resolve manually. The first response is for the OLD
        // scope (private_only); the second is for the NEW scope
        // (public_only). We resolve the second first, then the first,
        // and assert the dropdown reflects the NEW scope, not the old.
        // Only the engines endpoint is captured — other endpoints
        // (settings save, etc.) are stubbed but ignored.
        const responders = [];
        const isEnginesUrl = (u) =>
            typeof u === 'string' && (
                u === ENGINES_URL ||
                u.startsWith(ENGINES_URL + '?') ||
                u.endsWith(ENGINES_URL) ||
                u.includes(ENGINES_URL + '?')
            );
        fetchMock.mockImplementation((url, _opts) => {
            // Pass through non-engine endpoints immediately.
            if (!isEnginesUrl(url)) {
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () => Promise.resolve({}),
                    text: () => Promise.resolve(''),
                });
            }
            const qIdx = (url || '').indexOf('?');
            const qs = qIdx >= 0 ? (url || '').slice(qIdx + 1) : '';
            const params = new URLSearchParams(qs);
            const scope = params.get('egress_scope') || '';
            const body = {
                engine_options: FULL_OPTIONS.map((opt) => decorateForScope(opt, scope, params.get('primary') || '')),
                engines: {},
                favorites: [],
            };
            let resolveFn;
            const promise = new Promise((resolve) => {
                resolveFn = resolve;
            });
            responders.push({ url, resolve: () => resolveFn({
                ok: true,
                status: 200,
                json: () => Promise.resolve(body),
                text: () => Promise.resolve(''),
            }) });
            return promise;
        });

        // Fire the FIRST fetch (private_only).
        scopeSelect.value = 'private_only';
        scopeSelect.dispatchEvent(new Event('change'));
        await flush();
        expect(responders.length).toBe(1);

        // Before the first fetch resolves, fire a SECOND fetch
        // (public_only). This must cancel / supersede the first.
        scopeSelect.value = 'public_only';
        scopeSelect.dispatchEvent(new Event('change'));
        await flush();
        expect(responders.length).toBe(2);

        // Resolve the SECOND (newer) request first.
        responders[1].resolve();
        await vi.waitFor(() => {
            const github = openDropdownAndReadOptions().find(
                (el) => el.getAttribute('data-value') === 'github'
            );
            expect(github).toBeDefined();
            expect(
                github.classList.contains('ldr-custom-dropdown-item--disabled')
            ).toBe(false);
        });

        // Then resolve the FIRST (older, now superseded) request.
        responders[0].resolve();
        await flush();
        await flush();

        // The dropdown must reflect the NEW scope (public_only), not the OLD
// one (private_only). github is allowed under public_only but denied
// under private_only (public source under private), so the disabled
// state of github is the visible signal. With primary=library, the
// library entry is the carve-out under both scopes, so we deliberately
        // check github instead.
        const rendered = openDropdownAndReadOptions();
        const github = rendered.find((el) => el.getAttribute('data-value') === 'github');
        expect(github).toBeDefined();
        expect(github.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(false);
    });

    it('settles outer Promise and retains wrapper loading state on aborted request until newest request settles', async () => {
        const fetchPromises = [];

        fetchMock.mockImplementation((url) => {
            const urlStr = typeof url === 'string' ? url : (url && url.toString ? url.toString() : '');
            if (!urlStr.includes('available-search-engines')) {
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () => Promise.resolve({}),
                    text: () => Promise.resolve(''),
                });
            }
            let resFn;
            const p = new Promise((res) => {
                resFn = res;
            });
            fetchPromises.push({ resolve: resFn });
            return p;
        });

        const searchEngineInput = document.getElementById('search_engine');

        // Trigger refresh button click (starts first load)
        const refreshBtn = document.getElementById('search_engine-refresh');
        refreshBtn.click();
        await flush();

        expect(refreshBtn.classList.contains('ldr-loading')).toBe(true);
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(true);
        expect(fetchPromises.length).toBe(1);

        // Before first fetch completes, fire a scope change (starts second load, aborting/superseding the first)
        const scopeSelect = document.getElementById('policy_egress_scope');
        scopeSelect.value = 'private_only';
        scopeSelect.dispatchEvent(new Event('change'));
        await flush();

        expect(fetchPromises.length).toBe(2);
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(true);

        // Simulate AbortError on the first request
        const abortErr = new Error('The user aborted a request.');
        abortErr.name = 'AbortError';
        fetchPromises[0].resolve(Promise.reject(abortErr));
        await flush();
        await flush();

        // The refresh button loading indicator must be cleared (proving the superseded Promise settled),
        // but the dropdown wrapper must STILL be loading because the second request is in flight.
        expect(refreshBtn.classList.contains('ldr-loading')).toBe(false);
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(true);

        // Now resolve second request (newest request)
        fetchPromises[1].resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ engine_options: FULL_OPTIONS, engines: {}, favorites: [] }),
            text: () => Promise.resolve(''),
        });
        await flush();
        await flush();

        // Settlement of the newest request clears the wrapper loading indicator
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(false);
    });

    it('retains wrapper loading state on non-aborting stale response until newest request settles', async () => {
        const fetchPromises = [];

        fetchMock.mockImplementation((url) => {
            const urlStr = typeof url === 'string' ? url : (url && url.toString ? url.toString() : '');
            if (!urlStr.includes('available-search-engines')) {
                return Promise.resolve({
                    ok: true,
                    status: 200,
                    json: () => Promise.resolve({}),
                    text: () => Promise.resolve(''),
                });
            }
            let resFn;
            const p = new Promise((res) => {
                resFn = res;
            });
            fetchPromises.push({ resolve: resFn });
            return p;
        });

        const searchEngineInput = document.getElementById('search_engine');

        // Trigger refresh button click (starts first load)
        const refreshBtn = document.getElementById('search_engine-refresh');
        refreshBtn.click();
        await flush();

        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(true);
        expect(fetchPromises.length).toBe(1);

        // Before first fetch completes, fire a scope change (starts second load)
        const scopeSelect = document.getElementById('policy_egress_scope');
        scopeSelect.value = 'private_only';
        scopeSelect.dispatchEvent(new Event('change'));
        await flush();

        expect(fetchPromises.length).toBe(2);
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(true);

        // Resolve the FIRST (stale generation, non-aborting) response
        fetchPromises[0].resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ engine_options: FULL_OPTIONS, engines: {}, favorites: [] }),
            text: () => Promise.resolve(''),
        });
        await flush();
        await flush();

        // Older request settling must NOT clear the newer request's wrapper loading indicator
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(true);

        // Resolve the SECOND (newest) request
        fetchPromises[1].resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ engine_options: FULL_OPTIONS, engines: {}, favorites: [] }),
            text: () => Promise.resolve(''),
        });
        await flush();
        await flush();

        // Settlement of the newest request clears the wrapper loading indicator
        expect(searchEngineInput.parentNode.classList.contains('ldr-loading')).toBe(false);
    });
});

describe('research form — explicit search-engine selection', () => {
    const FULL_OPTIONS = [
        { value: 'collection_disabled', label: 'Indian History (Collection)',
          agent_enabled: false, requires_api_key: false, has_api_key: false },
        { value: 'openlibrary', label: 'Open Library',
          agent_enabled: false, requires_api_key: false, has_api_key: false },
        { value: 'gutenberg', label: 'Project Gutenberg',
          agent_enabled: false, requires_api_key: false, has_api_key: false },
        { value: 'github', label: 'GitHub',
          agent_enabled: false, requires_api_key: true, has_api_key: true },
        { value: 'brave', label: 'Brave',
          agent_enabled: false, requires_api_key: true, has_api_key: false },
        { value: 'arxiv', label: 'ArXiv', requires_api_key: false },
        { value: 'egress_denied', label: 'Egress Denied Engine',
          agent_enabled: false, requires_api_key: false,
          egress: { allowed: false, reason: 'scope_mismatch_private_only' } },
    ];

    function getOption(value) {
        document.getElementById('search_engine').dispatchEvent(new Event('focus'));
        return Array.from(document.querySelectorAll('#search-engine-dropdown-list .ldr-custom-dropdown-item'))
            .find(el => el.getAttribute('data-value') === value);
    }

    function setStrategy(value) {
        const select = document.getElementById('strategy');
        select.value = value;
        select.dispatchEvent(new Event('change'));
    }

    async function loadOptions() {
        const wrapper = document.getElementById('search_engine').parentNode;
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        // The preceding test may already have rendered these options with
        // different credential state. Wait for this refresh to finish.
        await vi.waitFor(() => {
            expect(wrapper.classList.contains('ldr-loading')).toBe(false);
            expect(getOption('openlibrary')).toBeDefined();
        });
    }

    beforeEach(() => {
        setStrategy('langgraph-agent');
        document.getElementById('search_engine_hidden').value = 'arxiv';
        fetchMock.mockImplementation(() => Promise.resolve({
            ok: true,
            status: 200,
            json: () => Promise.resolve({ engine_options: FULL_OPTIONS, engines: {}, favorites: [] }),
            text: () => Promise.resolve(''),
        }));
    });

    it.each(['collection_disabled', 'openlibrary', 'gutenberg', 'github'])(
        'selects and submits %s even when excluded from the default agent tools', async value => {
            await loadOptions();
            const item = getOption(value);
            expect(item.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(false);
            expect(item.getAttribute('aria-disabled')).toBeNull();
            expect(item.querySelector('.ldr-dropdown-item-disabled-reason')).toBeNull();

            item.click();
            expect(document.getElementById('search_engine_hidden').value).toBe(value);
            document.getElementById('model_hidden').value = 'test-model';
            submitForm();
            await vi.waitFor(() => expect(callsTo(START_RESEARCH)).toHaveLength(1));
            const payload = JSON.parse(callsTo(START_RESEARCH)[0][1].body);
            expect(payload.search_engine).toBe(value);
            expect(payload.strategy).toBe('langgraph-agent');
            // Let the submit's error response release submission ownership.
            await new Promise(resolve => setTimeout(resolve, 0));
        }
    );

    it('preserves an explicitly chosen engine across strategy changes', async () => {
        setStrategy('source-based');
        await loadOptions();
        getOption('openlibrary').click();

        for (const strategy of ['langgraph-agent', 'source-based']) {
            setStrategy(strategy);
            expect(getOption('openlibrary').classList.contains('ldr-custom-dropdown-item--disabled')).toBe(false);
            expect(document.getElementById('search_engine_hidden').value).toBe('openlibrary');
        }
    });

    it('blocks a missing required API key with the correct explanation for every strategy', async () => {
        await loadOptions();
        for (const strategy of ['langgraph-agent', 'source-based']) {
            setStrategy(strategy);
            const item = getOption('brave');
            expect(item.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(true);
            expect(item.getAttribute('aria-disabled')).toBe('true');
            const reason = item.querySelector('.ldr-dropdown-item-disabled-reason').textContent;
            expect(reason).toMatch(/api key.*settings/i);
            expect(reason).not.toMatch(/collection|langgraph/i);
            const previousSelection = document.getElementById('search_engine_hidden').value;
            item.click();
            expect(document.getElementById('search_engine_hidden').value).toBe(previousSelection);
        }
    });

    it('keeps policy-denied engines blocked across strategy changes', async () => {
        await loadOptions();
        for (const strategy of ['langgraph-agent', 'source-based']) {
            setStrategy(strategy);
            const item = getOption('egress_denied');
            expect(item.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(true);
            expect(item.querySelector('.ldr-dropdown-item-disabled-reason').textContent).toMatch(/Private only/);
            const previousSelection = document.getElementById('search_engine_hidden').value;
            item.click();
            expect(document.getElementById('search_engine_hidden').value).toBe(previousSelection);
        }
    });

    it('refreshes credential availability after a required key is configured', async () => {
        await loadOptions();
        expect(getOption('brave').getAttribute('aria-disabled')).toBe('true');
        fetchMock.mockImplementation(() => Promise.resolve({
            ok: true,
            json: () => Promise.resolve({
                engine_options: FULL_OPTIONS.map(option => (option.value === 'brave'
                    ? { ...option, has_api_key: true } : option)),
            }),
        }));
        document.getElementById('policy_egress_scope').dispatchEvent(new Event('change'));
        await vi.waitFor(() => expect(getOption('brave').getAttribute('aria-disabled')).toBeNull());
        getOption('brave').click();
        expect(document.getElementById('search_engine_hidden').value).toBe('brave');
    });
});


it('submits the remembered provider when policy clears the visible selection', async () => {
    // A pending settings PUT can leave the DB on a previous provider.
    // The request must carry the latest choice so server policy checks
    // that provider instead of falling back to the stale saved default.
    const provider = document.getElementById('model_provider');
    const previousValue = provider.value;
    const previousInitial = provider.getAttribute('data-initial-value');
    try {
        provider.setAttribute('data-initial-value', 'DEEPSEEK');
        provider.value = '';
        document.getElementById('model_hidden').value = 'deepseek-chat';

        submitForm();

        const calls = callsTo(START_RESEARCH);
        expect(calls).toHaveLength(1);
        const payload = JSON.parse(calls[0][1].body);
        expect(payload.model_provider).toBe('DEEPSEEK');
        expect(payload.model).toBe('deepseek-chat');
        await Promise.resolve();
        await Promise.resolve();
    } finally {
        provider.value = previousValue;
        if (previousInitial === null) provider.removeAttribute('data-initial-value');
        else provider.setAttribute('data-initial-value', previousInitial);
    }
});