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.

fix(dropdown): preserve custom model text on Enter (#5340)

LearningCircuit1d7331436a22
diff --git a/changelog.d/4839.bugfix.md b/changelog.d/4839.bugfix.md
new file mode 100644
index 0000000000..25cbfa60d4
--- /dev/null
+++ b/changelog.d/4839.bugfix.md
@@ -0,0 +1 @@
+Pressing Enter in a custom model dropdown keeps the typed model name instead of replacing it with the first suggestion. An exact match — a known value first, then a unique label — always wins before any case-insensitive match; known option labels and values also resolve to their model IDs case-insensitively, a padded known value resolves to its option, and a disabled match is not committed.
diff --git a/src/local_deep_research/web/static/js/components/custom_dropdown.js b/src/local_deep_research/web/static/js/components/custom_dropdown.js
index 74a0128864..e7770e467a 100644
--- a/src/local_deep_research/web/static/js/components/custom_dropdown.js
+++ b/src/local_deep_research/web/static/js/components/custom_dropdown.js
@@ -582,6 +582,15 @@
// Keyboard navigation for dropdown
input.addEventListener('keydown', (e) => {
+ // Ignore key events while an IME (e.g. CJK input methods) is
+ // composing — Enter confirms the composition, not a selection.
+ // Enter is excluded here and checked again inside its own
+ // branch below, after e.preventDefault(): a guarded Enter
+ // must still stop the keystroke from reaching the form's
+ // submit handler instead of falling through to it.
+ if ((e.isComposing || e.keyCode === 229) && e.key !== 'Enter') {
+ return;
+ }
let items = dropdownList.querySelectorAll('.ldr-custom-dropdown-item');
// Helper: find the next non-disabled index in `items` starting
@@ -632,6 +641,15 @@
} else if (e.key === 'Enter') {
e.preventDefault();
+ // An IME confirms its composition with Enter; that
+ // keystroke is not a dropdown selection and must not
+ // commit one, but (unlike other keys) it still needs
+ // the preventDefault() above so it doesn't fall through
+ // to the form's submit.
+ if (e.isComposing || e.keyCode === 229) {
+ return;
+ }
+
if (selectedIndex >= 0 && selectedIndex < items.length) {
// Select the highlighted item, but only if it's
// enabled. A disabled entry that's been Tab'd
@@ -656,6 +674,85 @@
onSelect(value, item);
// Update hidden input (dispatches change)
updateHiddenField(value);
+ } else if (allowCustomValues && input.value.trim()) {
+ // Resolve known values against the full option list:
+ // surrounding whitespace may have filtered their rows out.
+ // A displayed label must retain its underlying model ID.
+ const customValue = input.value.trim();
+ const options = getOptions();
+ // An exact match always wins first: two options
+ // whose labels or values differ only by case must
+ // never resolve to each other just because the
+ // user typed one of them verbatim. The hidden
+ // tie-break runs first, so reconfirming a selected
+ // label keeps its own ID even when another
+ // option's value equals that label. Otherwise an
+ // exact model ID typed verbatim must win over
+ // another option's label, so a unique exact label
+ // is tried last — the same preference order as
+ // the case-insensitive tiers below, which this
+ // tier shadows and which only run once this one
+ // finds nothing.
+ const exactLabelMatches = options.filter((o) =>
+ typeof o.label === 'string' && o.label === customValue
+ );
+ const exactCurrentItem = hiddenInput
+ ? exactLabelMatches.find((o) =>
+ typeof o.value === 'string' && o.value === hiddenInput.value
+ )
+ : null;
+ // An exact model ID typed verbatim must win over
+ // another option's label, so the value check runs
+ // before the label one. An ambiguous exact label
+ // (matched by more than one option) never resolves
+ // to an arbitrary option, so it is tried last, and
+ // only when it uniquely identifies one option.
+ const exactMatch = exactCurrentItem
+ || options.find((o) =>
+ typeof o.value === 'string' && o.value === customValue
+ )
+ || (exactLabelMatches.length === 1 ? exactLabelMatches[0] : null);
+ // Case-insensitive: the label check matches the
+ // filtering rule filterOptions() already applies
+ // above, and the value check matches the
+ // comparator setValue() already uses below — a
+ // displayed label or value can be retyped in a
+ // different case and must still resolve. Only
+ // consulted when the exact tier above finds
+ // nothing, so it can never override an exact
+ // match.
+ const labelMatches = options.filter((o) =>
+ typeof o.label === 'string' && o.label.toLowerCase() === customValue.toLowerCase()
+ );
+ const currentItem = hiddenInput
+ ? labelMatches.find((o) =>
+ typeof o.value === 'string' && o.value.toLowerCase() === String(hiddenInput.value).toLowerCase()
+ )
+ : null;
+ // Keep an existing selection when its label is also
+ // another option's value; confirming it must not switch IDs.
+ const exactItem = exactMatch
+ || currentItem
+ || options.find((o) =>
+ typeof o.value === 'string' && o.value.toLowerCase() === customValue.toLowerCase()
+ )
+ || (labelMatches.length === 1 ? labelMatches[0] : null);
+ // Do not resolve an ambiguous label to an arbitrary option.
+ if (exactItem?.disabled === true) {
+ hideDropdown();
+ return;
+ }
+ if (exactItem) {
+ input.value = exactItem.label;
+ onSelect(exactItem.value, exactItem);
+ updateHiddenField(exactItem.value);
+ } else {
+ // Trim the visible field too, for consistency with the
+ // hidden value we just wrote.
+ input.value = customValue;
+ onSelect(customValue, null);
+ updateHiddenField(customValue);
+ }
} else if (items.length > 0 && selectedIndex === -1) {
// No item explicitly selected, but there are filtered results
// Auto-select the first ENABLED item in the filtered
@@ -677,13 +774,6 @@
updateHiddenField(value);
}
}
- } else if (allowCustomValues && input.value.trim()) {
- // Use the custom value
- const customValue = input.value.trim();
- // onSelect first so any change listener sees the
- // new value, then update the hidden input.
- onSelect(customValue, null);
- updateHiddenField(customValue);
}
hideDropdown();
} else if (e.key === 'Escape') {
diff --git a/tests/js/components/custom-dropdown.test.js b/tests/js/components/custom-dropdown.test.js
index b664199dc5..1a224b77dd 100644
--- a/tests/js/components/custom-dropdown.test.js
+++ b/tests/js/components/custom-dropdown.test.js
@@ -275,6 +275,179 @@ describe('setupCustomDropdown', () => {
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
@@ -284,6 +457,258 @@ describe('setupCustomDropdown', () => {
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)', () => {
+ // Fails on the unedited head: setValue() itself matches
+ // case-insensitively and writes the RAW value into the hidden
+ // field, so the hidden input can legitimately hold a case
+ // variant the user never typed. 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');
+ expect(hiddenInput.value).toBe('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', () => {
diff --git a/tests/ui_tests/test_autocomplete_selection.js b/tests/ui_tests/test_autocomplete_selection.js
index edf5b4b3ea..04eb673f5e 100644
--- a/tests/ui_tests/test_autocomplete_selection.js
+++ b/tests/ui_tests/test_autocomplete_selection.js
@@ -253,8 +253,9 @@ async function runTests() {
allTestsPassed = false;
}
- // Test 4: Type and Enter behavior (reveals the issue)
- console.log('\n🧪 Test 4: Type and immediate Enter (reveals the issue)');
+ // Test 4: Type and Enter behavior (typed text that does not exactly
+ // match a known option label or value is preserved as custom text)
+ console.log('\n🧪 Test 4: Type and immediate Enter (unmatched text is preserved)');
try {
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
@@ -276,29 +277,49 @@ async function runTests() {
});
if (firstModelText) {
- // Clear and type part of the model name
- await page.evaluate(() => { document.getElementById('model').value = ''; });
- const searchText = firstModelText.substring(0, Math.min(5, firstModelText.length));
- await page.type('#model', searchText);
- console.log(` Typed "${searchText}"`);
+ // Derive the prefix from the TRIMMED label, and require that
+ // prefix to itself have no leading/trailing whitespace.
+ // custom_dropdown.js's Enter handler trims the typed text
+ // before comparing or committing it, so a fixed-length cut
+ // that happens to land on whitespace inside the label (e.g.
+ // "phi4 (Ollama)".substring(0, 5) === "phi4 ") gets rewritten
+ // by that trim -- correctly -- which would otherwise make
+ // this equality check fail on a correct build. Skip the
+ // check rather than fail when the prefix isn't strict in
+ // that sense; only assert equality when trimming cannot
+ // alter what was typed.
+ const trimmedLabel = firstModelText.trim();
+ const searchText = trimmedLabel.substring(0, Math.min(5, trimmedLabel.length));
+ const isStrictPrefix = searchText.length > 0 && searchText === searchText.trim();
+
+ if (!isStrictPrefix) {
+ console.log(` ⚠️ Skipping: prefix "${searchText}" of "${trimmedLabel}" is not a strict prefix after trimming`);
+ } else {
+ // Clear and type part of the model name
+ await page.evaluate(() => { document.getElementById('model').value = ''; });
+ await page.type('#model', searchText);
+ console.log(` Typed "${searchText}"`);
- await new Promise(resolve => setTimeout(resolve, 300));
+ await new Promise(resolve => setTimeout(resolve, 300));
- // Press Enter without using arrow keys
- await page.keyboard.press('Enter');
- await new Promise(resolve => setTimeout(resolve, 500));
+ // Press Enter without using arrow keys
+ await page.keyboard.press('Enter');
+ await new Promise(resolve => setTimeout(resolve, 500));
- // Check what was selected
- const selectedValue = await page.evaluate(() => document.getElementById('model').value);
+ // Check what was selected
+ const selectedValue = await page.evaluate(() => document.getElementById('model').value);
- if (selectedValue === searchText) {
- console.log(` ⚠️ Enter key only kept typed text: "${selectedValue}"`);
- console.log(' 📝 Issue confirmed: User must either:');
- console.log(' - Click on the autocomplete suggestion');
- console.log(' - Use arrow keys then Enter');
- console.log(' - Typing and pressing Enter alone does NOT select from autocomplete');
- } else {
- console.log(` ✅ Enter key selected from autocomplete: "${selectedValue}"`);
+ if (selectedValue === searchText) {
+ console.log(` ✅ Enter kept the typed custom text: "${selectedValue}"`);
+ console.log(' 📝 A typed prefix that does not exactly match a known option');
+ console.log(' label or value is preserved as-is, enabling free-text custom');
+ console.log(' model entry.');
+ } else {
+ console.log(` ❌ Enter did not preserve the typed text; got: "${selectedValue}"`);
+ console.log(' Expected the typed prefix to remain untouched, since it does not');
+ console.log(' exactly match a known option label or value.');
+ allTestsPassed = false;
+ }
}
}
@@ -321,18 +342,16 @@ async function runTests() {
console.log(' - Model autocomplete: Tests if clicking selects the item');
console.log(' - Search engine autocomplete: Tests if clicking selects the item');
console.log(' - Keyboard navigation: Tests arrow key + Enter selection');
- console.log(' - Type and Enter: Reveals if typing + Enter selects first match');
+ console.log(' - Type and Enter: Unmatched typed text is preserved, not silently replaced');
console.log('='.repeat(60));
if (allTestsPassed) {
console.log('✅ All tests completed successfully\n');
process.exit(0);
} else {
- console.log('❌ Some tests revealed issues with autocomplete selection\n');
- console.log('The tests confirm that when typing in the autocomplete fields:');
- console.log('1. The dropdown appears and filters correctly');
- console.log('2. However, pressing Enter does not select the first match');
- console.log('3. Users must either click or use arrow keys to select\n');
+ console.log('❌ One or more autocomplete selection checks failed\n');
+ console.log('Review the ❌ lines above for which check failed and why -- each');
+ console.log('test logs its own expectation next to the value it observed.\n');
process.exit(1);
}
}