From 571609242a8d72b2b13bcfbfef600353f63136d6 Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Tue, 3 Dec 2024 11:25:01 +0100 Subject: [PATCH] [8.x] [Console] Avoid duplicate suggestions in autocomplete (#201766) (#202484) # Backport This will backport the following commits from `main` to `8.x`: - [[Console] Avoid duplicate suggestions in autocomplete (#201766)](https://github.com/elastic/kibana/pull/201766) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) --- .../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 a5ebef3dd0b79..0fa6a3d1b03ba 100644 --- a/test/functional/apps/console/_autocomplete.ts +++ b/test/functional/apps/console/_autocomplete.ts @@ -66,6 +66,28 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.console.clearEditorText(); }); + 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)); + }); + it('HTTP methods', async () => { const suggestions = { G: ['GET'],