From f64dec58a7335cf5ba88e816474240118225dc3b Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Fri, 29 Nov 2024 12:58:39 +0100 Subject: [PATCH] [Console] Avoid duplicate suggestions in autocomplete (#201766) --- .../console/public/lib/autocomplete/engine.js | 11 +++++++--- test/functional/apps/console/_autocomplete.ts | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/plugins/console/public/lib/autocomplete/engine.js b/src/plugins/console/public/lib/autocomplete/engine.js index 6ba2445543f9b..8f9235ceb0a0f 100644 --- a/src/plugins/console/public/lib/autocomplete/engine.js +++ b/src/plugins/console/public/lib/autocomplete/engine.js @@ -119,7 +119,7 @@ export function populateContext(tokenPath, context, editor, includeAutoComplete, editor ); if (includeAutoComplete) { - let autoCompleteSet = []; + let autoCompleteSet = new Map(); _.each(walkStates, function (ws) { const contextForState = passThroughContext(context, ws.contextExtensionList); _.each(ws.components, function (component) { @@ -127,11 +127,16 @@ export function populateContext(tokenPath, context, editor, includeAutoComplete, if (!_.isObject(term)) { term = { name: term }; } - autoCompleteSet.push(term); + + // Add the term to the autoCompleteSet if it doesn't already exist + if (!autoCompleteSet.has(term.name)) { + autoCompleteSet.set(term.name, term); + } }); }); }); - autoCompleteSet = _.uniq(autoCompleteSet); + // Convert Map values to an array of objects + autoCompleteSet = Array.from(autoCompleteSet.values()); context.autoCompleteSet = autoCompleteSet; } diff --git a/test/functional/apps/console/_autocomplete.ts b/test/functional/apps/console/_autocomplete.ts index 451e546135599..8f856a81c4a2c 100644 --- a/test/functional/apps/console/_autocomplete.ts +++ b/test/functional/apps/console/_autocomplete.ts @@ -60,6 +60,28 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(true); }); + it('should not show duplicate suggestions', async () => { + await PageObjects.console.enterText(`POST _ingest/pipeline/_simulate +{ + "pipeline": { + "processors": [ + { + "script": {`); + await PageObjects.console.pressEnter(); + await PageObjects.console.sleepForDebouncePeriod(); + await PageObjects.console.enterText(`"`); + expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(true); + + // Iterate on the first 10 suggestions (the ones that are only visible without scrolling) + const suggestions = []; + for (let i = 0; i < 10; i++) { + suggestions.push(await PageObjects.console.getAutocompleteSuggestion(i)); + } + + // and expect the array to not have duplicates + expect(suggestions).to.eql(_.uniq(suggestions)); + }); + describe('Autocomplete behavior', () => { beforeEach(async () => { await PageObjects.console.clearEditorText();