Skip to content

Commit

Permalink
[Console] Avoid duplicate suggestions in autocomplete (elastic#201766)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabarasaba authored Nov 29, 2024
1 parent 4784022 commit 82de734
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/plugins/console/public/lib/autocomplete/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,24 @@ 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) {
_.each(component.getTerms(contextForState, editor), function (term) {
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;
}

Expand Down
22 changes: 22 additions & 0 deletions test/functional/apps/console/_autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 82de734

Please sign in to comment.