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.

58.7 KB
Raw Blame
/**
 * Tests for components/custom_dropdown.js
 *
 * Tests the custom dropdown component's core behavior:
 * setup, filtering, selection, keyboard navigation, and cleanup.
 */

import '@js/security/xss-protection.js';
import '@js/components/custom_dropdown.js';

const setupCustomDropdown = window.setupCustomDropdown;
const updateDropdownOptions = window.updateDropdownOptions;

describe('setupCustomDropdown', () => {
    let input, hiddenInput, dropdownList, onSelect, options;

    beforeEach(() => {
        input = document.createElement('input');
        input.type = 'text';
        input.id = 'test-dropdown-input';

        hiddenInput = document.createElement('input');
        hiddenInput.type = 'hidden';
        hiddenInput.id = 'test-dropdown-input_hidden';

        dropdownList = document.createElement('div');
        dropdownList.id = 'test-dropdown-list';

        const wrapper = document.createElement('div');
        wrapper.appendChild(input);
        wrapper.appendChild(hiddenInput);
        wrapper.appendChild(dropdownList);
        document.body.appendChild(wrapper);

        onSelect = vi.fn();

        options = [
            { value: 'gpt4', label: 'GPT-4' },
            { value: 'claude', label: 'Claude' },
            { value: 'llama', label: 'Llama 3' },
            { value: 'mistral', label: 'Mistral' },
        ];
    });

    afterEach(() => {
        const wrapper = input.closest('div');
        if (wrapper && wrapper.parentNode) {
            wrapper.remove();
        }
        // Clean up any detached dropdown lists
        const detached = document.getElementById('test-dropdown-list');
        if (detached) detached.remove();
    });

    it('returns control functions', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        expect(dd.updateDropdown).toBeTypeOf('function');
        expect(dd.showDropdown).toBeTypeOf('function');
        expect(dd.hideDropdown).toBeTypeOf('function');
        expect(dd.destroy).toBeTypeOf('function');
        expect(dd.setValue).toBeTypeOf('function');
        dd.destroy();
    });

    it('initially hides the dropdown', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        expect(dropdownList.style.display).toBe('none');
        dd.destroy();
    });

    it('shows dropdown on input click', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        expect(dropdownList.style.display).toBe('block');
        dd.destroy();
    });

    it('populates dropdown with all options on click', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        expect(items.length).toBe(4);
        dd.destroy();
    });

    it('filters options when typing', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.value = 'Cl';
        input.dispatchEvent(new Event('input'));
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        expect(items.length).toBe(1);
        expect(items[0].getAttribute('data-value')).toBe('claude');
        dd.destroy();
    });

    it('shows "no results" when nothing matches', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.value = 'zzzzz';
        input.dispatchEvent(new Event('input'));
        expect(dropdownList.querySelector('.ldr-custom-dropdown-no-results')).not.toBeNull();
        dd.destroy();
    });

    it('calls onSelect when an option is clicked', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items[1].click(); // Click "Claude"
        expect(onSelect).toHaveBeenCalledWith('claude', options[1]);
        dd.destroy();
    });

    it('invokes onSelect BEFORE dispatching the hidden input change event (issue #5204 follow-up)', () => {
        // The order matters: any change listener that reclassifies
        // the dropdown (research.js: applyEgressScopeToEngines) needs
        // the in-memory selection to be authoritative when it runs,
        // otherwise the re-fetch carries the previous primary and the
        // newly selected engine is briefly marked unavailable.
        //
        // Use a mock onSelect that flips a sentinel flag the change
        // listener can observe — the change listener must see the flag
        // already set (proving onSelect ran first), not still false
        // (which would mean the change event fired before onSelect).
        let onSelectRan = false;
        const trackingOnSelect = vi.fn((_value, _item) => {
            onSelectRan = true;
        });
        let onSelectStateAtChange = null;
        hiddenInput.addEventListener('change', () => {
            onSelectStateAtChange = onSelectRan;
        });

        const dd = setupCustomDropdown(input, dropdownList, () => options, trackingOnSelect);

        input.click();
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items[1].click(); // Click "Claude"

        // 1. onSelect fired with the clicked value.
        expect(trackingOnSelect).toHaveBeenCalledWith('claude', options[1]);
        // 2. By the time the change event fires, onSelect has already
        //    run. The bug would dispatch the change event first; the
        //    change listener would observe onSelectStateAtChange=false.
        expect(onSelectStateAtChange).toBe(true);
        // 3. The hidden input reflects the new value.
        expect(hiddenInput.value).toBe('claude');
        dd.destroy();
    });

    it('Enter on a highlighted item invokes onSelect before dispatching the hidden input change event', () => {
        let onSelectRan = false;
        const trackingOnSelect = vi.fn((_value, _item) => {
            onSelectRan = true;
        });
        let onSelectStateAtChange = null;
        hiddenInput.addEventListener('change', () => {
            onSelectStateAtChange = onSelectRan;
        });

        const dd = setupCustomDropdown(input, dropdownList, () => options, trackingOnSelect);

        input.dispatchEvent(new Event('focus'));
        // First ArrowDown highlights the first item.
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
        // Enter selects the highlighted item.
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));

        expect(trackingOnSelect).toHaveBeenCalledWith('gpt4', options[0]);
        expect(onSelectStateAtChange).toBe(true);
        expect(hiddenInput.value).toBe('gpt4');
        dd.destroy();
    });

    it('updates input value on selection', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items[0].click(); // Click "GPT-4"
        expect(input.value).toBe('GPT-4');
        dd.destroy();
    });

    it('updates hidden input on selection', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items[0].click();
        expect(hiddenInput.value).toBe('gpt4');
        dd.destroy();
    });

    it('hides dropdown after selection', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items[0].click();
        expect(dropdownList.style.display).toBe('none');
        dd.destroy();
    });

    it('hides dropdown on Escape key', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        input.click();
        expect(dropdownList.style.display).toBe('block');
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
        expect(dropdownList.style.display).toBe('none');
        dd.destroy();
    });

    it('sets ARIA attributes for accessibility', () => {
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
        // Initially closed
        expect(input.getAttribute('aria-expanded')).toBe('false');
        // Open
        input.click();
        expect(input.getAttribute('aria-expanded')).toBe('true');

        // Items have role="option"
        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items.forEach(item => {
            expect(item.getAttribute('role')).toBe('option');
        });
        dd.destroy();
    });

    describe('setValue', () => {
        it('sets value by matching option value', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            dd.setValue('mistral');
            expect(input.value).toBe('Mistral');
            expect(hiddenInput.value).toBe('mistral');
            dd.destroy();
        });

        it('calls onSelect callback', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            dd.setValue('claude');
            expect(onSelect).toHaveBeenCalledWith('claude', options[1]);
            dd.destroy();
        });

        it('clears input for unknown value when custom values disallowed', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, false);
            dd.setValue('unknown-model');
            expect(input.value).toBe('');
            dd.destroy();
        });

        it('sets raw value when custom values are allowed', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            dd.setValue('custom-model');
            expect(input.value).toBe('custom-model');
            expect(hiddenInput.value).toBe('custom-model');

            // Same contract with triggerChange = false (the settings page's
            // init path): the hidden field still gets the raw value, so
            // callers need no separate hidden-input write.
            hiddenInput.value = '';
            dd.setValue('custom-model', false);
            expect(input.value).toBe('custom-model');
            expect(hiddenInput.value).toBe('custom-model');
            dd.destroy();
        });

        describe('case-insensitive matching (#7398)', () => {
            const caseVariants = [
                { value: 'org/model-a', label: 'Wrong case' },
                { value: 'Org/Model-A', label: 'Right case' },
            ];

            it('prefers an exact match over an earlier case-variant', () => {
                const dd = setupCustomDropdown(input, dropdownList, () => caseVariants, onSelect);
                dd.setValue('Org/Model-A');
                // Displayed model must be the one actually submitted.
                expect(input.value).toBe('Right case');
                expect(hiddenInput.value).toBe('Org/Model-A');
                expect(onSelect).toHaveBeenCalledWith('Org/Model-A', caseVariants[1]);
                dd.destroy();
            });

            it('still prefers the exact match when it comes first', () => {
                const reversed = [caseVariants[1], caseVariants[0]];
                const dd = setupCustomDropdown(input, dropdownList, () => reversed, onSelect);
                dd.setValue('org/model-a');
                expect(input.value).toBe('Wrong case');
                expect(hiddenInput.value).toBe('org/model-a');
                dd.destroy();
            });

            it('canonicalizes a drifted stored value to the listed casing', () => {
                const listed = [{ value: 'Org/Model-A', label: 'Model A' }];
                const dd = setupCustomDropdown(input, dropdownList, () => listed, onSelect);
                dd.setValue('org/model-a');
                expect(input.value).toBe('Model A');
                expect(hiddenInput.value).toBe('Org/Model-A');
                expect(onSelect).toHaveBeenCalledWith('Org/Model-A', listed[0]);
                dd.destroy();
            });

            it('canonicalizes the hidden field even when change events are suppressed', () => {
                const listed = [{ value: 'Org/Model-A', label: 'Model A' }];
                const dd = setupCustomDropdown(input, dropdownList, () => listed, onSelect);
                dd.setValue('org/model-a', false);
                expect(input.value).toBe('Model A');
                expect(hiddenInput.value).toBe('Org/Model-A');
                expect(onSelect).not.toHaveBeenCalled();
                dd.destroy();
            });

            it('keeps an unmatched custom value as typed', () => {
                const dd = setupCustomDropdown(input, dropdownList, () => caseVariants, onSelect, true);
                dd.setValue('Some/Other-Model', false);
                expect(input.value).toBe('Some/Other-Model');
                expect(hiddenInput.value).toBe('Some/Other-Model');
                dd.destroy();
            });
        });
    });

    describe('findDropdownOption helper (#7398)', () => {
        const find = window.findDropdownOption;
        const list = [
            { value: 'org/model-a', label: 'lower' },
            { value: 'Org/Model-A', label: 'canonical' },
            { value: 'other', label: 'other' },
        ];

        it('is exposed for the settings page', () => {
            expect(find).toBeTypeOf('function');
        });

        it('returns the exact match before a case-insensitive one', () => {
            expect(find(list, 'Org/Model-A').label).toBe('canonical');
            expect(find(list, 'org/model-a').label).toBe('lower');
        });

        it('falls back to a case-insensitive match', () => {
            expect(find(list, 'OTHER').label).toBe('other');
            expect(find([list[1]], 'org/model-a').label).toBe('canonical');
        });

        it('returns undefined for no match, empty or missing input', () => {
            expect(find(list, 'missing')).toBeUndefined();
            expect(find(list, '')).toBeUndefined();
            expect(find(list, null)).toBeUndefined();
            expect(find(list, undefined)).toBeUndefined();
            expect(find(null, 'x')).toBeUndefined();
            expect(find([], 'x')).toBeUndefined();
        });

        it('tolerates malformed options', () => {
            expect(find([null, {}, { value: 5 }, { value: 'x', label: 'X' }], 'X').label).toBe('X');
        });
    });

    describe('destroy', () => {
        it('cleans up event listeners and registry', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            dd.destroy();
            // After destroy, input events should not trigger dropdown
            input.click();
            // Dropdown should stay hidden (no listener to open it)
            expect(dropdownList.style.display).toBe('none');
        });
    });

    describe('keyboard navigation', () => {
        it('ArrowDown opens dropdown and selects first item', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown', bubbles: true }));
            expect(dropdownList.style.display).toBe('block');
            const active = dropdownList.querySelector('.active');
            expect(active).not.toBeNull();
            dd.destroy();
        });

        it('Enter uses unhighlighted custom text instead of the first filtered option', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt';
            input.dispatchEvent(new Event('input'));
            expect(dropdownList.querySelector('.ldr-custom-dropdown-item').getAttribute('data-value')).toBe('gpt4');
            expect(dropdownList.querySelector('.active')).toBeNull();

            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('gpt', null);
            expect(hiddenInput.value).toBe('gpt');
            expect(onSelect).not.toHaveBeenCalledWith('gpt4', options[0]);
            expect(dropdownList.style.display).toBe('none');
            dd.destroy();
        });

        it('Enter selects a highlighted option before custom text', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown', bubbles: true }));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it('Enter selects the first filtered option when custom values are disabled', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            input.value = 'gpt';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it('Enter selects the first option for blank custom input', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = '';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it('Enter selects the first option for whitespace custom input', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = '   ';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it('Enter trims custom input before selecting it', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = '  custom-model  ';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('custom-model', null);
            expect(hiddenInput.value).toBe('custom-model');
            dd.destroy();
        });

        it('Enter selects the exact-matching option instead of treating typed text as custom', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt4';
            input.dispatchEvent(new Event('input'));
            // 'gpt4' matches an option value and nothing is highlighted.
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            // Should select the real option object, not pass (value, null).
            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(onSelect).not.toHaveBeenCalledWith('gpt4', null);
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it('Enter preserves a selected model ID when the field shows its label', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            dd.setValue('gpt4', false);
            expect(input.value).toBe('GPT-4');
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it.each([' gpt4 ', 'GPT-4'])('Enter resolves known text %s to its option object', (text) => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = text;
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalledWith('gpt4', options[0]);
            expect(input.value).toBe('GPT-4');
            expect(hiddenInput.value).toBe('gpt4');
            dd.destroy();
        });

        it('Enter keeps the current identity when option labels are duplicated', () => {
            options[0].label = 'Shared model';
            options[1].label = 'Shared model';
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            dd.setValue('claude', false);
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalledWith('claude', options[1]);
            expect(hiddenInput.value).toBe('claude');
            dd.destroy();
        });

        it('Enter keeps the selected ID when its label is another option value', () => {
            options = [
                { value: 'first-id', label: 'second-id' },
                { value: 'second-id', label: 'Other model' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            dd.setValue('first-id', false);
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalledWith('first-id', options[0]);
            expect(hiddenInput.value).toBe('first-id');
            dd.destroy();
        });

        it('Enter does not guess an option for an ambiguous newly typed label', () => {
            options[0].label = 'Shared model';
            options[1].label = 'Shared model';
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'Shared model';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalledWith('Shared model', null);
            expect(hiddenInput.value).toBe('Shared model');
            dd.destroy();
        });

        it('Enter cannot select a disabled known value hidden by whitespace filtering', () => {
            options[0].disabled = true;
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = ' gpt4 ';
            input.dispatchEvent(new Event('input'));
            expect(dropdownList.querySelectorAll('.ldr-custom-dropdown-item')).toHaveLength(0);
            expect(input.getAttribute('aria-expanded')).toBe('true');
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).not.toHaveBeenCalled();
            expect(hiddenInput.value).toBe('');
            // This Enter takes the same exactItem?.disabled early return as
            // "a disabled exact match on Enter closes the dropdown" below;
            // it must leave the dropdown closed here too, not just no-op.
            expect(input.getAttribute('aria-expanded')).toBe('false');
            expect(dropdownList.style.display).toBe('none');
            dd.destroy();
        });

        it('Enter during IME composition does not commit a selection', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt';
            input.dispatchEvent(new Event('input'));
            // Simulate an IME composition-confirming Enter (CJK input methods).
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, isComposing: true }));

            // The composition-confirming Enter must not fire onSelect.
            expect(onSelect).not.toHaveBeenCalled();
            dd.destroy();
        });

        it('Enter selects highlighted item', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            // Open and select first
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown', bubbles: true }));
            // Select it
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalled();
            dd.destroy();
        });

        it('Enter resolves a typed case-variant LABEL to the option (case-insensitive label match)', () => {
            // Fails on the unedited head: labelMatches compared with a
            // strict `===` (the pre-fix label comparator), so a
            // lower-cased label never matched and the raw typed text was
            // persisted as the model ID instead of resolving to the option.
            options = [
                { value: 'anthropic/claude-3.5-sonnet', label: 'Claude 3.5 Sonnet' },
                { value: 'openai/gpt-4o', label: 'GPT-4o' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'claude 3.5 sonnet';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('anthropic/claude-3.5-sonnet', options[0]);
            expect(hiddenInput.value).toBe('anthropic/claude-3.5-sonnet');
            dd.destroy();
        });

        it('Enter resolves a typed case-variant VALUE to the option (case-insensitive value fallback)', () => {
            // Fails on the unedited head: the value fallback also compared
            // with a strict `===` (the pre-fix value comparator), so
            // typing the option's own ID in a different case matched
            // neither the label nor the value and was kept as free-text
            // custom input instead of resolving to the option.
            options = [
                { value: 'anthropic/claude-3.5-sonnet', label: 'Claude 3.5 Sonnet' },
                { value: 'openai/gpt-4o', label: 'GPT-4o' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'OPENAI/GPT-4O';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('openai/gpt-4o', options[1]);
            expect(hiddenInput.value).toBe('openai/gpt-4o');
            dd.destroy();
        });

        it('Enter prefers an exact label match over a case-fold value match on a different option', () => {
            // Fails on the unedited head: the case-insensitive value
            // match ran before the unique-label match, so Array.find()
            // returns the first option whose VALUE merely case-folds to
            // the typed text -- 'mistral-7b' -- even though the typed
            // text is an exact match for a DIFFERENT option's LABEL
            // ('Mistral-7B', the Fireworks-hosted model). On the unedited
            // head this resolves to 'mistral-7b' and rewrites the field
            // to "Mistral 7B Instruct" instead.
            options = [
                { value: 'accounts/fireworks/models/mistral-7b', label: 'Mistral-7B' },
                { value: 'mistral-7b', label: 'Mistral 7B Instruct' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'Mistral-7B';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('accounts/fireworks/models/mistral-7b', options[0]);
            expect(onSelect).not.toHaveBeenCalledWith('mistral-7b', options[1]);
            expect(input.value).toBe('Mistral-7B');
            expect(hiddenInput.value).toBe('accounts/fireworks/models/mistral-7b');
            dd.destroy();
        });

        it('Enter prefers an exact-case label over its lower/upper-case sibling, regardless of typed order', () => {
            // Fails on the unedited head: the label comparator lower-cases
            // both sides before comparing, so "GPT-4o" and "gpt-4o" fold
            // into one ambiguity class of two labelMatches with no hidden
            // value to disambiguate them. On the unedited head that
            // resolves to neither option: the typed LABEL TEXT ITSELF is
            // persisted as the model ID instead.
            options = [
                { value: 'engine-a', label: 'GPT-4o' },
                { value: 'engine-b', label: 'gpt-4o' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt-4o';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('engine-b', options[1]);
            expect(onSelect).not.toHaveBeenCalledWith('gpt-4o', null);
            expect(hiddenInput.value).toBe('engine-b');
            dd.destroy();
        });

        it('Enter still preserves typed text when the exact tier is itself ambiguous', () => {
            // This does NOT fail on the unedited head -- it passes there
            // too, since two options sharing one identical label were
            // already ambiguous under the pre-fix case-insensitive tier
            // with no hidden value to break the tie. It is included as a
            // precedence guard on the new exact tier added above: two
            // options with the IDENTICAL label and no hidden match must
            // stay ambiguous at the exact level too and fall through to
            // the same custom-text behaviour, not start guessing now that
            // an exact tier runs first.
            options = [
                { value: 'engine-a', label: 'Shared name' },
                { value: 'engine-b', label: 'Shared name' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'Shared name';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('Shared name', null);
            expect(hiddenInput.value).toBe('Shared name');
            dd.destroy();
        });

        it.each(['Foo', 'foo'])('Enter resolves an exact value before a same-named label, independent of typed case (%s)', (text) => {
            // Fails if the exact tier is reordered back to label-first
            // (unique exact label tried before exact value): typing
            // "Foo" would then match options[0]'s LABEL directly and
            // commit 'x-1', while typing "foo" still reaches options[1]
            // through the case-insensitive value fallback and commits
            // 'Foo' -- two different resolutions for the same text typed
            // in different cases. With the value check running first,
            // both spellings resolve to options[1] ('Foo'), whose value
            // the exact tier finds directly for "Foo" and the
            // case-insensitive tier finds for "foo".
            options = [
                { value: 'x-1', label: 'Foo' },
                { value: 'Foo', label: 'Bar' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = text;
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('Foo', options[1]);
            expect(input.value).toBe('Bar');
            expect(hiddenInput.value).toBe('Foo');
            dd.destroy();
        });

        it('Enter keeps the hidden-value tie-break winner among duplicate labels instead of an unrelated option whose value matches the typed text', () => {
            // Fails if the exact tier's hidden tie-break is deleted
            // (exactCurrentItem forced to null): with two options sharing
            // a label, the tie-break is the only thing that resolves the
            // exact-label tier by the currently selected option's own
            // value. Without it, the exact value check that now runs
            // next matches a third, differently-labelled option whose
            // value happens to equal the typed text, and Enter switches
            // to that unrelated option instead of reconfirming the one
            // that was already selected.
            options = [
                { value: 'model-a', label: 'Same Name' },
                { value: 'model-b', label: 'Same Name' },
                { value: 'Same Name', label: 'Third Model' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            dd.setValue('model-b', false);
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('model-b', options[1]);
            expect(onSelect).not.toHaveBeenCalledWith('Same Name', options[2]);
            expect(hiddenInput.value).toBe('model-b');
            dd.destroy();
        });

        it('Enter resolves an ambiguous label to the option whose value is the empty string, pinning the typeof guard', () => {
            // Fails if the six `typeof o.value === 'string'` / `typeof
            // o.label === 'string'` guards are reverted to the old
            // truthy check: value '' is falsy, so a truthy guard drops
            // this option from the hidden tie-break, and with a second
            // option sharing the same label the unique-label fallback
            // cannot rescue it either (two labelMatches, not one).
            // Resolution then falls all the way through to persisting
            // the typed label text as unmatched custom input instead.
            // The `typeof` guard admits value '' as a legitimate string,
            // so this pins what the shipped code actually does with it
            // -- not a claim that a real provider ever emits it.
            options = [
                { value: '', label: 'Shared' },
                { value: 'v2', label: 'Shared' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'Shared';
            input.dispatchEvent(new Event('input'));
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('', options[0]);
            expect(onSelect).not.toHaveBeenCalledWith('Shared', null);
            expect(hiddenInput.value).toBe('');
            dd.destroy();
        });

        it('setValue() with a case-variant stored value resolves on a bare Enter (duplicate labels)', () => {
            // The hidden input can hold a case variant the user never typed
            // (before #7398, setValue() wrote the RAW value into it; it now
            // canonicalizes, so the variant is planted directly below to keep
            // guarding the Enter tie-break). With two options sharing a
            // label, the pre-fix tie-break
            // (`o.value === hiddenInput.value`) failed that case-variant
            // comparison, the labelMatches.length===1 fallback did not
            // apply either (two options match the label), and Enter fell
            // through to persisting the raw label as custom text instead
            // of the already-selected option's ID.
            options = [
                { value: 'openai/gpt-4o', label: 'Shared display name' },
                { value: 'anthropic/claude-3.5-sonnet', label: 'Shared display name' },
            ];
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            dd.setValue('OpenAI/GPT-4O', false); // case-variant of options[0].value
            expect(input.value).toBe('Shared display name');
            // setValue() canonicalizes to the listed casing (#7398)...
            expect(hiddenInput.value).toBe('openai/gpt-4o');
            // ...so plant the case variant directly to keep the tie-break covered.
            hiddenInput.value = 'OpenAI/GPT-4O';

            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).toHaveBeenCalledWith('openai/gpt-4o', options[0]);
            expect(hiddenInput.value).toBe('openai/gpt-4o');
            dd.destroy();
        });

        it('a disabled exact match on Enter closes the dropdown instead of leaving it open', () => {
            // Fails on the unedited head: the pre-fix
            // `exactItem?.disabled === true` early return returned
            // without calling hideDropdown(), leaving aria-expanded="true"
            // and the list rendered even though Enter is a no-op.
            options[0].disabled = true; // gpt4
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt4';
            input.dispatchEvent(new Event('input'));
            expect(input.getAttribute('aria-expanded')).toBe('true');

            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));

            expect(onSelect).not.toHaveBeenCalled();
            expect(input.getAttribute('aria-expanded')).toBe('false');
            expect(dropdownList.style.display).toBe('none');
            dd.destroy();
        });

        it('Enter with keyCode 229 (IME) still prevents the default action but commits nothing', () => {
            // Fails on the unedited head: the IME guard sat above
            // e.preventDefault() (custom_dropdown.js:587-589 pre-fix), so a
            // keyCode===229 Enter returned before preventDefault() was ever
            // called, leaving the keystroke free to fall through to the
            // surrounding form's submit handler.
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect, true);
            input.value = 'gpt';
            input.dispatchEvent(new Event('input'));
            const event = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true, keyCode: 229 });

            input.dispatchEvent(event);

            expect(event.defaultPrevented).toBe(true);
            expect(onSelect).not.toHaveBeenCalled();
            dd.destroy();
        });
    });

    describe('group headers', () => {
        const grouped = [
            { value: 'fav1', label: 'Pinned One', group_label: 'Favorites' },
            { value: 'arxiv', label: 'ArXiv', group_label: 'Academic' },
            { value: 'pubmed', label: 'PubMed', group_label: 'Academic' },
            { value: 'tavily', label: 'Tavily', group_label: 'API key' },
        ];

        it('renders one non-selectable header per band, in order', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            input.click();
            const headers = dropdownList.querySelectorAll('.ldr-custom-dropdown-group-header');
            expect(Array.from(headers).map(h => h.textContent)).toEqual([
                'Favorites', 'Academic', 'API key',
            ]);
            headers.forEach(h =>
                expect(h.classList.contains('ldr-custom-dropdown-item')).toBe(false)
            );
            dd.destroy();
        });

        it('keeps the selectable option count equal to items (headers excluded)', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            input.click();
            const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
            expect(items.length).toBe(4);
            dd.destroy();
        });

        it('shows a band header once even when it has multiple items', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            input.click();
            const academic = Array.from(
                dropdownList.querySelectorAll('.ldr-custom-dropdown-group-header')
            ).filter(h => h.textContent === 'Academic');
            expect(academic.length).toBe(1);
            dd.destroy();
        });

        it('hides a band header when filtering removes all its items', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            input.value = 'arxiv';
            input.dispatchEvent(new Event('input'));
            const headerTexts = Array.from(
                dropdownList.querySelectorAll('.ldr-custom-dropdown-group-header')
            ).map(h => h.textContent);
            expect(headerTexts).toEqual(['Academic']);
            dd.destroy();
        });

        it('renders no headers when options have no group_label', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);
            input.click();
            const headers = dropdownList.querySelectorAll('.ldr-custom-dropdown-group-header');
            expect(headers.length).toBe(0);
            dd.destroy();
        });

        it('marks headers as presentational so assistive tech skips them', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            input.click();
            const header = dropdownList.querySelector('.ldr-custom-dropdown-group-header');
            expect(header.getAttribute('role')).toBe('presentation');
            expect(header.getAttribute('aria-hidden')).toBe('true');
            dd.destroy();
        });

        it('keyboard navigation lands on an item, never a header', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            // First ArrowDown must select the first ITEM (fav1), not the
            // 'Favorites' header that precedes it.
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown', bubbles: true }));
            const active = dropdownList.querySelector('.active');
            expect(active.classList.contains('ldr-custom-dropdown-item')).toBe(true);
            input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }));
            expect(onSelect).toHaveBeenCalledWith('fav1', expect.objectContaining({ value: 'fav1' }));
            dd.destroy();
        });

        it('renders both favorite stars and band headers when both apply', () => {
            const onFavoriteToggle = vi.fn();
            const dd = setupCustomDropdown(
                input, dropdownList, () => grouped, onSelect, false, 'No results found.', onFavoriteToggle
            );
            input.click();
            expect(dropdownList.querySelectorAll('.ldr-custom-dropdown-group-header').length).toBe(3);
            expect(dropdownList.querySelectorAll('.ldr-dropdown-favorite-star').length).toBe(4);
            dd.destroy();
        });

        it('updateDropdownOptions re-renders band headers for the open list', () => {
            const dd = setupCustomDropdown(input, dropdownList, () => grouped, onSelect);
            input.click();
            const newOptions = [
                { value: 'wiki', label: 'Wikipedia', group_label: 'No API key' },
                { value: 'serper', label: 'Serper', group_label: 'API key' },
            ];
            updateDropdownOptions(input, newOptions);
            const headerTexts = Array.from(
                dropdownList.querySelectorAll('.ldr-custom-dropdown-group-header')
            ).map(h => h.textContent);
            expect(headerTexts).toEqual(['No API key', 'API key']);
            expect(dropdownList.querySelectorAll('.ldr-custom-dropdown-item').length).toBe(2);
            dd.destroy();
        });
    });
});

describe('updateDropdownOptions', () => {
    it('does nothing for null input', () => {
        expect(() => updateDropdownOptions(null, [])).not.toThrow();
    });

    it('invokes onSelect BEFORE dispatching the hidden input change event when selecting after re-render', () => {
        const input = document.createElement('input');
        input.type = 'text';
        input.id = 'update-test-input';

        const hiddenInput = document.createElement('input');
        hiddenInput.type = 'hidden';
        hiddenInput.id = 'update-test-input_hidden';

        const dropdownList = document.createElement('div');
        dropdownList.id = 'update-test-list';

        const wrapper = document.createElement('div');
        wrapper.appendChild(input);
        wrapper.appendChild(hiddenInput);
        wrapper.appendChild(dropdownList);
        document.body.appendChild(wrapper);

        let onSelectRan = false;
        const trackingOnSelect = vi.fn((_value, _item) => {
            onSelectRan = true;
        });
        let onSelectStateAtChange = null;
        hiddenInput.addEventListener('change', () => {
            onSelectStateAtChange = onSelectRan;
        });

        const initialOptions = [
            { value: 'gpt4', label: 'GPT-4' },
        ];
        const dd = setupCustomDropdown(input, dropdownList, () => initialOptions, trackingOnSelect);
        input.click();

        const newOptions = [
            { value: 'gpt4', label: 'GPT-4' },
            { value: 'claude', label: 'Claude' },
        ];
        updateDropdownOptions(input, newOptions);

        const items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
        items[1].click(); // Click "Claude"

        expect(trackingOnSelect).toHaveBeenCalledWith('claude', newOptions[1]);
        expect(onSelectStateAtChange).toBe(true);
        expect(hiddenInput.value).toBe('claude');
        dd.destroy();

        wrapper.remove();
        if (document.getElementById('update-test-list')) {
            document.getElementById('update-test-list').remove();
        }
    });
});

describe('disabled options (issue #5204)', () => {
    // The custom dropdown is reused by the scope-aware search engine
    // picker. Disabled entries must render with the right visual +
    // semantic contract so screen readers and keyboard users get the
    // same affordance as a native <option disabled>.
    let input, hiddenInput, dropdownList, onSelect;

    beforeEach(() => {
        input = document.createElement('input');
        input.type = 'text';
        input.id = 'test-disabled-input';
        hiddenInput = document.createElement('input');
        hiddenInput.type = 'hidden';
        hiddenInput.id = 'test-disabled-input_hidden';
        dropdownList = document.createElement('div');
        dropdownList.id = 'test-disabled-list';
        const wrapper = document.createElement('div');
        wrapper.appendChild(input);
        wrapper.appendChild(hiddenInput);
        wrapper.appendChild(dropdownList);
        document.body.appendChild(wrapper);
        onSelect = vi.fn();
    });

    afterEach(() => {
        const wrapper = input.closest('div');
        if (wrapper && wrapper.parentNode) wrapper.remove();
        const detached = document.getElementById('test-disabled-list');
        if (detached) detached.remove();
    });

    function openList() {
        // Drive the focus path the user takes.
        input.dispatchEvent(new Event('focus'));
        return Array.from(
            dropdownList.querySelectorAll('.ldr-custom-dropdown-item')
        );
    }

    it('renders disabled entries with aria-disabled, the disabled class, and a reason label', () => {
        const options = [
            { value: 'arxiv', label: 'ArXiv' },
            {
                value: 'library',
                label: 'Library',
                disabled: true,
                disabled_reason: 'Blocked: not a local source under Private only',
            },
        ];
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);

        const rendered = openList();
        const lib = rendered.find((el) => el.getAttribute('data-value') === 'library');
        expect(lib.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(true);
        expect(lib.getAttribute('aria-disabled')).toBe('true');
        const reason = lib.querySelector('.ldr-dropdown-item-disabled-reason');
        expect(reason).not.toBeNull();
        expect(reason.textContent).toMatch(/not a local source under Private only/);
        // The reason is also exposed to assistive tech via
        // aria-describedby pointing at the reason span's id.
        const describedById = lib.getAttribute('aria-describedby');
        expect(describedById).toBe(reason.id);
        dd.destroy();
    });

    it('does not call onSelect when a disabled entry is clicked', () => {
        const options = [
            {
                value: 'library',
                label: 'Library',
                disabled: true,
                disabled_reason: 'Blocked: local source under Public only',
            },
        ];
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);

        const rendered = openList();
        const lib = rendered[0];
        lib.click();

        expect(onSelect).not.toHaveBeenCalled();
        // Hidden input stays empty — no value was selected.
        expect(hiddenInput.value).toBe('');
        dd.destroy();
    });

    it('skips disabled entries in arrow-key navigation', () => {
        const options = [
            { value: 'a', label: 'A' },
            { value: 'b', label: 'B', disabled: true, disabled_reason: 'blocked' },
            { value: 'c', label: 'C' },
        ];
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);

        // Open + focus first entry.
        input.dispatchEvent(new Event('focus'));
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
        // The first ArrowDown lands on the first ENABLED entry
        // (A), NOT on the disabled B.
        let active = dropdownList.querySelector('.ldr-custom-dropdown-item.active');
        expect(active).not.toBeNull();
        expect(active.getAttribute('data-value')).toBe('a');

        // Next ArrowDown skips B and lands on C.
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
        active = dropdownList.querySelector('.ldr-custom-dropdown-item.active');
        expect(active.getAttribute('data-value')).toBe('c');

        // Next ArrowDown wraps back to A (still skips B).
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowDown' }));
        active = dropdownList.querySelector('.ldr-custom-dropdown-item.active');
        expect(active.getAttribute('data-value')).toBe('a');

        // ArrowUp from A (index 0) wraps to C (skipping disabled B).
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowUp' }));
        active = dropdownList.querySelector('.ldr-custom-dropdown-item.active');
        expect(active.getAttribute('data-value')).toBe('c');

        dd.destroy();
    });

    it('Enter on a highlighted disabled entry is a no-op', () => {
        // Direct: the only option is disabled. After the open, Enter
        // would otherwise auto-select the first item; the disabled
        // gate must short-circuit it.
        const options = [
            { value: 'b', label: 'B', disabled: true, disabled_reason: 'blocked' },
        ];
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);

        input.dispatchEvent(new Event('focus'));
        input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));

        expect(onSelect).not.toHaveBeenCalled();
        expect(hiddenInput.value).toBe('');
        dd.destroy();
    });

    it('updateDropdownOptions also respects the disabled contract', () => {
        // The research.js reapplier calls updateDropdownOptions (not
        // setupCustomDropdown) when re-stamping the egress-aware list,
        // so this path is just as important.
        const dd = setupCustomDropdown(
            input,
            dropdownList,
            () => [],
            onSelect
        );
        // Open so the in-place re-render kicks in.
        input.dispatchEvent(new Event('focus'));

        const newOptions = [
            {
                value: 'arxiv',
                label: 'ArXiv',
                disabled: true,
                disabled_reason: 'Blocked: not a local source under Private only',
            },
            { value: 'library', label: 'Library' },
        ];
        updateDropdownOptions(input, newOptions);

        const rendered = Array.from(
            dropdownList.querySelectorAll('.ldr-custom-dropdown-item')
        );
        const arxiv = rendered.find((el) => el.getAttribute('data-value') === 'arxiv');
        expect(arxiv.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(true);
        expect(arxiv.getAttribute('aria-disabled')).toBe('true');
        const reason = arxiv.querySelector('.ldr-dropdown-item-disabled-reason');
        expect(reason.textContent).toMatch(/not a local source under Private only/);
        dd.destroy();
    });

    it('keeps the label measurable when a long disabled_reason is present (#7359)', () => {
        // The pre-fix .ldr-custom-dropdown-item was a flex row; the
        // reason was a sibling flex item in the same row, so a long
        // reason (~127 chars for the agent-disabled message) competed
        // for horizontal space and squeezed the label to 0 width on a
        // 460px-wide dropdown. The fix in custom_dropdown.css wraps
        // the item (`flex-wrap: wrap`) and forces the reason onto its
        // own row (`flex: 0 0 100%` on the reason selector). happy-dom
        // does not resolve `flex-wrap` / `flex-basis` from the
        // external stylesheet, so this test pins the DOM contract that
        // the CSS rule assumes: the label and the reason are siblings
        // under the same item, the reason has the class
        // `ldr-dropdown-item-disabled-reason` (the CSS selector's
        // target), and the label is `ldr-dropdown-item-label` (its
        // sibling under the flex container). The CSS wrap + basis is
        // a real-browser visual fix; a layout-level regression here
        // would not be caught in jsdom but is verifiable in
        // Playwright (and the project already pins the visible
        // behavior in the accessibility suites).
        const longReason =
            "Hidden from the LangGraph research agent's tool list " +
            "(check “Available to the research agent” on the collection " +
            "page to re-enable).";
        const options = [
            {
                value: 'library',
                label: 'Library',
                disabled: true,
                disabled_reason: longReason,
            },
        ];
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);

        const [lib] = openList();
        const label = lib.querySelector('.ldr-dropdown-item-label');
        const reason = lib.querySelector('.ldr-dropdown-item-disabled-reason');

        // The label and reason must be present and siblings under
        // the same flex container — that is the contract the
        // custom_dropdown.css wrap rule assumes.
        expect(label).not.toBeNull();
        expect(reason).not.toBeNull();
        expect(label.parentElement).toBe(reason.parentElement);
        // The reason selector must match — the CSS rule in
        // custom_dropdown.css targets exactly this class.
        expect(reason.classList.contains('ldr-dropdown-item-disabled-reason')).toBe(true);
        // The label is rendered with the class the layout CSS targets
        // for the flex: 1 / min-width: 0 sizing.
        expect(label.classList.contains('ldr-dropdown-item-label')).toBe(true);
        // The full reason text reaches the DOM (the original bug
        // pinned it was "DOM still has the text but the label is
        // squeezed" — we want both the DOM and the layout to be
        // correct, not just one).
        expect(reason.textContent).toBe(longReason);
        dd.destroy();
    });

    it('keeps the label measurable on a star-less row (#7359 round 2)', () => {
        // Round 2 of the same issue: the round-1 fix added
        // `flex: 0 0 100%` to the reason, which on a star-less row
        // (no onFavoriteToggle) where the label's flex basis is 0
        // made the label shrink to 0 px (the row never wrapped).
        // The round-2 fix adds `min-width: 1px` to the label in the
        // disabled-row selector, forcing a wrap regardless of whether
        // a favorite star is present. This test pins the DOM
        // contract for the new CSS rule.
        const longReason =
            "Hidden from the LangGraph research agent's tool list " +
            "(check the collection page to re-enable).";
        const options = [
            {
                value: 'provider',
                label: 'OpenAI',
                disabled: true,
                disabled_reason: longReason,
            },
        ];
        // No onFavoriteToggle → no star in the rendered item.
        const dd = setupCustomDropdown(input, dropdownList, () => options, onSelect);

        const [row] = openList();
        // No star present in the rendered row.
        expect(row.querySelector('.ldr-dropdown-favorite-star')).toBeNull();
        const label = row.querySelector('.ldr-dropdown-item-label');
        const reason = row.querySelector('.ldr-dropdown-item-disabled-reason');
        expect(label).not.toBeNull();
        expect(reason).not.toBeNull();
        // The label and reason are still siblings under the same
        // flex container, so the round-1 wrap assumption holds.
        expect(label.parentElement).toBe(reason.parentElement);
        // The reason carries the class the round-1 CSS targets.
        expect(reason.classList.contains('ldr-dropdown-item-disabled-reason')).toBe(true);
        // The label is rendered with the class the round-2 CSS
        // targets for `min-width: 1px`. The selector the CSS uses is
        // `.ldr-custom-dropdown-item--disabled .ldr-dropdown-item-label`,
        // so both ancestor and descendant classes must be present on
        // the rendered DOM (this is the structural pin the new rule
        // assumes). The actual computed `min-width` is enforced by
        // the stylesheet, not happy-dom, but the classes are enough
        // to catch a regression in the renderer.
        expect(row.classList.contains('ldr-custom-dropdown-item--disabled')).toBe(true);
        expect(label.classList.contains('ldr-dropdown-item-label')).toBe(true);
        dd.destroy();
    });
});