Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocomplete tests #31

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { timeUnitsToSuggest } from '../../definitions/literals';
import { groupingFunctionDefinitions } from '../../definitions/grouping';
import * as autocomplete from '../autocomplete';
import type { ESQLCallbacks } from '../../shared/types';
import type { EditorContext } from '../types';
import type { EditorContext, SuggestionRawDefinition } from '../types';
import { TIME_SYSTEM_PARAMS } from '../factories';

export interface Integration {
Expand All @@ -28,6 +28,13 @@ export interface Integration {
}>;
}

export type PartialSuggestionWithText = Partial<SuggestionRawDefinition> & { text: string };

export const TIME_PICKER_SUGGESTION: PartialSuggestionWithText = {
text: '',
label: 'Choose from the time picker',
};

export const triggerCharacters = [',', '(', '=', ' '];

export const fields: Array<{ name: string; type: string; suggestedAs?: string }> = [
Expand Down Expand Up @@ -224,9 +231,7 @@ export function getLiteralsByType(_type: string | string[]) {

export function getDateLiteralsByFieldType(_requestedType: string | string[]) {
const requestedType = Array.isArray(_requestedType) ? _requestedType : [_requestedType];
return requestedType.includes('date')
? ['Choose from the time picker', ...TIME_SYSTEM_PARAMS]
: [];
return requestedType.includes('date') ? [TIME_PICKER_SUGGESTION, ...TIME_SYSTEM_PARAMS] : [];
}

export function createCustomCallbackMocks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ import {
createCustomCallbackMocks,
createSuggestContext,
getPolicyFields,
PartialSuggestionWithText,
TIME_PICKER_SUGGESTION,
} from './__tests__/helpers';

describe('autocomplete', () => {
type TestArgs = [
string,
string[],
Array<string | PartialSuggestionWithText>,
(string | number)?,
Parameters<typeof createCustomCallbackMocks>?
];

const testSuggestionsFn = (
statement: string,
expected: string[],
expected: Array<string | PartialSuggestionWithText>,
triggerCharacter: string | number = '',
customCallbacksArgs: Parameters<typeof createCustomCallbackMocks> = [
undefined,
Expand Down Expand Up @@ -67,10 +69,20 @@ describe('autocomplete', () => {
callbackMocks
);

const sortedSuggestions = suggestions.map((suggestion) => suggestion.text).sort();
const sortedExpected = expected.sort();
const sortedSuggestionTexts = suggestions.map((suggestion) => suggestion.text).sort();
const sortedExpectedTexts = expected
.map((suggestion) => (typeof suggestion === 'string' ? suggestion : suggestion.text ?? ''))
.sort();

expect(sortedSuggestions).toEqual(sortedExpected);
expect(sortedSuggestionTexts).toEqual(sortedExpectedTexts);
const expectedNonStringSuggestions = expected.filter(
(suggestion) => typeof suggestion !== 'string'
) as PartialSuggestionWithText[];

for (const expectedSuggestion of expectedNonStringSuggestions) {
const suggestion = suggestions.find((s) => s.text === expectedSuggestion.text);
expect(suggestion).toEqual(expect.objectContaining(expectedSuggestion));
}
});
};

Expand Down Expand Up @@ -726,28 +738,30 @@ describe('autocomplete', () => {

const suggestedConstants = param.literalSuggestions || param.literalOptions;

const addCommaIfRequired = (s: string | PartialSuggestionWithText) => {
// don't add commas to the empty string or if there are no more required args
if (!requiresMoreArgs || s === '' || (typeof s === 'object' && s.text === '')) {
return s;
}
return typeof s === 'string' ? `${s},` : { ...s, text: `${s.text},` };
};

testSuggestions(
`from a | eval ${fn.name}(${Array(i).fill('field').join(', ')}${i ? ',' : ''} )`,
suggestedConstants?.length
? suggestedConstants.map((option) => `"${option}"${requiresMoreArgs ? ',' : ''}`)
: [
...getDateLiteralsByFieldType(
getTypesFromParamDefs(acceptsFieldParamDefs)
).map((l) => (requiresMoreArgs ? `${l},` : l)),
...getFieldNamesByType(getTypesFromParamDefs(acceptsFieldParamDefs)).map(
(f) => (requiresMoreArgs ? `${f},` : f)
),
...getDateLiteralsByFieldType(getTypesFromParamDefs(acceptsFieldParamDefs)),
...getFieldNamesByType(getTypesFromParamDefs(acceptsFieldParamDefs)),
...getFunctionSignaturesByReturnType(
'eval',
getTypesFromParamDefs(acceptsFieldParamDefs),
{ evalMath: true },
undefined,
[fn.name]
).map((l) => (requiresMoreArgs ? `${l},` : l)),
...getLiteralsByType(getTypesFromParamDefs(constantOnlyParamDefs)).map((d) =>
requiresMoreArgs ? `${d},` : d
),
]
...getLiteralsByType(getTypesFromParamDefs(constantOnlyParamDefs)),
].map(addCommaIfRequired)
);
testSuggestions(
`from a | eval var0 = ${fn.name}(${Array(i).fill('field').join(', ')}${
Expand All @@ -756,23 +770,17 @@ describe('autocomplete', () => {
suggestedConstants?.length
? suggestedConstants.map((option) => `"${option}"${requiresMoreArgs ? ',' : ''}`)
: [
...getDateLiteralsByFieldType(
getTypesFromParamDefs(acceptsFieldParamDefs)
).map((l) => (requiresMoreArgs ? `${l},` : l)),
...getFieldNamesByType(getTypesFromParamDefs(acceptsFieldParamDefs)).map(
(f) => (requiresMoreArgs ? `${f},` : f)
),
...getDateLiteralsByFieldType(getTypesFromParamDefs(acceptsFieldParamDefs)),
...getFieldNamesByType(getTypesFromParamDefs(acceptsFieldParamDefs)),
...getFunctionSignaturesByReturnType(
'eval',
getTypesFromParamDefs(acceptsFieldParamDefs),
{ evalMath: true },
undefined,
[fn.name]
).map((l) => (requiresMoreArgs ? `${l},` : l)),
...getLiteralsByType(getTypesFromParamDefs(constantOnlyParamDefs)).map((d) =>
requiresMoreArgs ? `${d},` : d
),
]
...getLiteralsByType(getTypesFromParamDefs(constantOnlyParamDefs)),
].map(addCommaIfRequired)
);
}
});
Expand Down Expand Up @@ -814,15 +822,17 @@ describe('autocomplete', () => {
'number',
]),
]);

testSuggestions(
'from a | eval var0=date_trunc()',
[
...[...TIME_SYSTEM_PARAMS, 'Choose from the time picker'].map((t) => `${t},`),
...[...TIME_SYSTEM_PARAMS].map((t) => `${t},`),
...getLiteralsByType('time_literal').map((t) => `${t},`),
...getFunctionSignaturesByReturnType('eval', 'date', { evalMath: true }, undefined, [
'date_trunc',
]).map((t) => `${t},`),
...getFieldNamesByType('date').map((t) => `${t},`),
TIME_PICKER_SUGGESTION,
],
'('
);
Expand Down
Loading