fix(editor): Improve handling of trailing 'Trigger' in NodeCreator search (#14612)

This commit is contained in:
Charlie Kolb
2025-04-16 13:43:14 +02:00
committed by GitHub
parent a8fee9a4f3
commit 8b3b4749ea
4 changed files with 68 additions and 25 deletions

View File

@@ -2,20 +2,20 @@ import topLevel from './snapshots/toplevel.snapshot.json';
import { sublimeSearch } from './sublimeSearch';
describe('sublimeSearch', () => {
const testCases = [{ filter: 'agent', expectedOrder: ['Magento 2', 'AI Agent'] }];
describe('search finds at least one match', () => {
const testCases: Array<[string, string[]]> = [['agent', ['Magento 2', 'AI Agent']]];
test.each(testCases)(
'should return results in the correct order for filter "$filter"',
({ filter, expectedOrder }) => {
const results = sublimeSearch(filter, topLevel, [
{ key: 'properties.displayName', weight: 1.3 },
{ key: 'properties.codex.alias', weight: 1 },
]);
test.each(testCases)(
'should return at least "$expectedOrder" for filter "$filter"',
(filter, expectedOrder) => {
// These match the weights in the production use case
const results = sublimeSearch(filter, topLevel);
const resultNames = results.map((result) => result.item.properties.displayName);
expectedOrder.forEach((expectedName, index) => {
expect(resultNames[index]).toBe(expectedName);
});
},
);
const resultNames = results.map((result) => result.item.properties.displayName);
expectedOrder.forEach((expectedName, index) => {
expect(resultNames[index]).toBe(expectedName);
});
},
);
});
});

View File

@@ -12,6 +12,11 @@ const LEADING_LETTER_PENALTY = -20; // penalty applied for every letter in str b
const MAX_LEADING_LETTER_PENALTY = -200; // maximum penalty for leading letters
const UNMATCHED_LETTER_PENALTY = -5;
export const DEFAULT_KEYS = [
{ key: 'properties.displayName', weight: 1.3 },
{ key: 'properties.codex.alias', weight: 1 },
];
/**
* Returns true if each character in pattern is found sequentially within target
* @param {*} pattern string
@@ -217,7 +222,7 @@ function getValue<T extends object>(obj: T, prop: string): unknown {
export function sublimeSearch<T extends object>(
filter: string,
data: Readonly<T[]>,
keys: Array<{ key: string; weight: number }>,
keys: Array<{ key: string; weight: number }> = DEFAULT_KEYS,
): Array<{ score: number; item: T }> {
const results = data.reduce((accu: Array<{ score: number; item: T }>, item: T) => {
let values: Array<{ value: string; weight: number }> = [];