From 0cf9ec046c8ce7140adcf2693405d65af1138b1c Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Tue, 26 Nov 2024 12:33:35 +0100 Subject: [PATCH 1/3] Avoid having duplicate entries --- .../console/public/lib/autocomplete/engine.js | 5 ++++- test/functional/apps/console/_autocomplete.ts | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/plugins/console/public/lib/autocomplete/engine.js b/src/plugins/console/public/lib/autocomplete/engine.js index 6ba2445543f9b..e0c20d1afa745 100644 --- a/src/plugins/console/public/lib/autocomplete/engine.js +++ b/src/plugins/console/public/lib/autocomplete/engine.js @@ -127,7 +127,10 @@ export function populateContext(tokenPath, context, editor, includeAutoComplete, if (!_.isObject(term)) { term = { name: term }; } - autoCompleteSet.push(term); + // Make sure we don't have duplicates + if (!_.find(autoCompleteSet, { name: term.name })) { + autoCompleteSet.push(term); + } }); }); }); 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(); From eaa4488243e825264c5d6e84916bcbf738b59888 Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Tue, 26 Nov 2024 12:33:58 +0100 Subject: [PATCH 2/3] commit with @elastic email From 8e144557302e3241daa099dce5c0c3f4b2e25b26 Mon Sep 17 00:00:00 2001 From: Ignacio Rivas Date: Wed, 27 Nov 2024 09:45:21 +0100 Subject: [PATCH 3/3] Use map for deduping rather than lodash --- .../console/public/lib/autocomplete/engine.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/plugins/console/public/lib/autocomplete/engine.js b/src/plugins/console/public/lib/autocomplete/engine.js index e0c20d1afa745..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,14 +127,16 @@ export function populateContext(tokenPath, context, editor, includeAutoComplete, if (!_.isObject(term)) { term = { name: term }; } - // Make sure we don't have duplicates - if (!_.find(autoCompleteSet, { name: term.name })) { - 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; }