PromucFlow_constructor/app/client/src/utils/autocomplete/TernServer.test.ts
Druthi Polisetty 499de5a5b1
fix: Query missing in AUTO_COMPLETE_SHOW event (#24898)
## Description
Query missing in AUTO_COMPLETE_SHOW event

#### PR fixes following issue(s)
Fixes #23838

#### Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video
>
>
#### Type of change
> Please delete options that are not relevant.
- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-07-05 20:11:52 +05:30

420 lines
11 KiB
TypeScript

import type {
Completion,
DataTreeDefEntityInformation,
} from "./CodemirrorTernService";
import CodemirrorTernService, {
createCompletionHeader,
} from "./CodemirrorTernService";
import { AutocompleteDataType } from "./AutocompleteDataType";
import { MockCodemirrorEditor } from "../../../test/__mocks__/CodeMirrorEditorMock";
import { ENTITY_TYPE } from "entities/DataTree/dataTreeFactory";
import _ from "lodash";
import { AutocompleteSorter, ScoredCompletion } from "./AutocompleteSortRules";
describe("Tern server", () => {
it("Check whether the correct value is being sent to tern", () => {
const testCases = [
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 0, line: 0 }),
getLine: () => "{{Api.}}",
getValue: () => "{{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: "{{Api.}}",
},
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 0, line: 0 }),
getLine: () => "a{{Api.}}",
getValue: () => "a{{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: "a{{Api.}}",
},
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 10, line: 0 }),
getLine: () => "a{{Api.}}bc",
getValue: () => "a{{Api.}}bc",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: "a{{Api.}}bc",
},
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 4, line: 0 }),
getLine: () => "a{{Api.}}",
getValue: () => "a{{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: "Api.",
},
];
testCases.forEach((testCase) => {
const { value } = CodemirrorTernService.getFocusedDocValueAndPos(
testCase.input,
);
expect(value).toBe(testCase.expectedOutput);
});
});
it("Check whether the correct position is sent for querying autocomplete", () => {
const testCases = [
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 0, line: 0 }),
getLine: () => "{{Api.}}",
somethingSelected: () => false,
getValue: () => "{{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: { ch: 0, line: 0 },
},
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 0, line: 0 }),
getLine: () => "{{Api.}}",
somethingSelected: () => false,
getValue: () => "{{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: { ch: 0, line: 0 },
},
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 8, line: 0 }),
getLine: () => "g {{Api.}}",
somethingSelected: () => false,
getValue: () => "g {{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: { ch: 4, line: 0 },
},
{
input: {
name: "test",
doc: {
getCursor: () => ({ ch: 7, line: 1 }),
getLine: () => "c{{Api.}}",
somethingSelected: () => false,
getValue: () => "ab\nc{{Api.}}",
} as unknown as CodeMirror.Doc,
changed: null,
},
expectedOutput: { ch: 4, line: 0 },
},
];
testCases.forEach((testCase) => {
MockCodemirrorEditor.getTokenAt.mockReturnValueOnce({
type: "string",
string: "",
});
const request = CodemirrorTernService.buildRequest(testCase.input, {});
expect(request.query.end).toEqual(testCase.expectedOutput);
});
});
it(`Check whether the position is evaluated correctly for placing the selected autocomplete value`, () => {
const testCases = [
{
input: {
codeEditor: {
value: "{{}}",
cursor: { ch: 2, line: 0 },
doc: {
getCursor: () => ({ ch: 2, line: 0 }),
getLine: () => "{{}}",
somethingSelected: () => false,
getValue: () => "{{}}",
getEditor: () => MockCodemirrorEditor,
} as unknown as CodeMirror.Doc,
},
requestCallbackData: {
completions: [{ name: "Api1" }],
start: { ch: 2, line: 0 },
end: { ch: 6, line: 0 },
},
},
expectedOutput: { ch: 2, line: 0 },
},
{
input: {
codeEditor: {
value: "\n {{}}",
cursor: { ch: 3, line: 0 },
doc: {
getCursor: () => ({ ch: 3, line: 0 }),
getLine: () => " {{}}",
somethingSelected: () => false,
getValue: () => " {{}}",
getEditor: () => MockCodemirrorEditor,
} as unknown as CodeMirror.Doc,
},
requestCallbackData: {
completions: [{ name: "Api1" }],
start: { ch: 0, line: 0 },
end: { ch: 4, line: 0 },
},
},
expectedOutput: { ch: 3, line: 0 },
},
];
testCases.forEach((testCase) => {
MockCodemirrorEditor.getValue.mockReturnValueOnce(
testCase.input.codeEditor.value,
);
MockCodemirrorEditor.getCursor.mockReturnValueOnce(
testCase.input.codeEditor.cursor,
);
MockCodemirrorEditor.getDoc.mockReturnValue(
testCase.input.codeEditor.doc,
);
MockCodemirrorEditor.getTokenAt.mockReturnValueOnce({
type: "string",
string: "",
});
const mockAddFile = jest.fn();
CodemirrorTernService.server.addFile = mockAddFile;
const value: any = CodemirrorTernService.requestCallback(
null,
testCase.input.requestCallbackData,
MockCodemirrorEditor as unknown as CodeMirror.Editor,
() => null,
);
expect(mockAddFile).toBeCalled();
expect(value.from).toEqual(testCase.expectedOutput);
});
});
});
describe("Tern server sorting", () => {
const defEntityInformation: Map<string, DataTreeDefEntityInformation> =
new Map();
const contextCompletion: Completion = {
text: "context",
type: AutocompleteDataType.STRING,
origin: "[doc]",
data: {
doc: "",
},
};
const sameEntityCompletion: Completion = {
text: "sameEntity.tableData",
type: AutocompleteDataType.ARRAY,
origin: "DATA_TREE",
data: {
doc: "",
},
};
defEntityInformation.set("sameEntity", {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET",
});
defEntityInformation.set("sameEntity", {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET_V2",
});
const priorityCompletion: Completion = {
text: "selectedRow",
type: AutocompleteDataType.OBJECT,
origin: "DATA_TREE",
data: {
doc: "",
},
};
defEntityInformation.set("sameType", {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET",
});
defEntityInformation.set("sameType", {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET_V2",
});
const diffTypeCompletion: Completion = {
text: "diffType.tableData",
type: AutocompleteDataType.ARRAY,
origin: "DATA_TREE.WIDGET",
data: {
doc: "",
},
};
defEntityInformation.set("diffType", {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET",
});
defEntityInformation.set("diffType", {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET_V2",
});
const sameTypeDiffEntityTypeCompletion: Completion = {
text: "diffEntity.data",
type: AutocompleteDataType.OBJECT,
origin: "DATA_TREE",
data: {
doc: "",
},
};
defEntityInformation.set("diffEntity", {
type: ENTITY_TYPE.ACTION,
subType: ENTITY_TYPE.ACTION,
});
const dataTreeCompletion: Completion = {
text: "otherDataTree",
type: AutocompleteDataType.STRING,
origin: "DATA_TREE",
data: {
doc: "",
},
};
defEntityInformation.set("otherDataTree", {
type: ENTITY_TYPE.WIDGET,
subType: "TEXT_WIDGET",
});
const functionCompletion: Completion = {
text: "otherDataFunction",
type: AutocompleteDataType.FUNCTION,
origin: "DATA_TREE.APPSMITH.FUNCTIONS",
data: {
doc: "",
},
};
const ecmascriptCompletion: Completion = {
text: "otherJS",
type: AutocompleteDataType.OBJECT,
origin: "ecmascript",
data: {
doc: "",
},
};
const libCompletion: Completion = {
text: "libValue",
type: AutocompleteDataType.OBJECT,
origin: "LIB/lodash",
data: {
doc: "",
},
};
const unknownCompletion: Completion = {
text: "unknownSuggestion",
type: AutocompleteDataType.UNKNOWN,
origin: "unknown",
data: {
doc: "",
},
};
const completions = [
sameEntityCompletion,
priorityCompletion,
contextCompletion,
libCompletion,
unknownCompletion,
diffTypeCompletion,
sameTypeDiffEntityTypeCompletion,
ecmascriptCompletion,
functionCompletion,
dataTreeCompletion,
];
it("shows best match results", () => {
CodemirrorTernService.setEntityInformation({
entityName: "sameEntity",
entityType: ENTITY_TYPE.WIDGET,
expectedType: AutocompleteDataType.OBJECT,
});
CodemirrorTernService.defEntityInformation = defEntityInformation;
const sortedCompletions = AutocompleteSorter.sort(
_.shuffle(completions),
{
entityName: "sameEntity",
entityType: ENTITY_TYPE.WIDGET,
expectedType: AutocompleteDataType.STRING,
},
{
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET",
},
);
expect(sortedCompletions[1]).toStrictEqual(contextCompletion);
expect(sortedCompletions).toEqual(
expect.arrayContaining([
createCompletionHeader("Best match"),
sameTypeDiffEntityTypeCompletion,
createCompletionHeader("Search results"),
dataTreeCompletion,
]),
);
});
it("tests score of completions", function () {
AutocompleteSorter.entityDefInfo = {
type: ENTITY_TYPE.WIDGET,
subType: "TABLE_WIDGET",
};
AutocompleteSorter.currentFieldInfo = {
entityName: "sameEntity",
entityType: ENTITY_TYPE.WIDGET,
expectedType: AutocompleteDataType.STRING,
};
//completion that matches type and is present in dataTree.
const scoredCompletion1 = new ScoredCompletion(
dataTreeCompletion,
AutocompleteSorter.currentFieldInfo,
);
expect(scoredCompletion1.score).toEqual(2 ** 5 + 2 ** 4 + 2 ** 3);
//completion that belongs to the same entity.
const scoredCompletion2 = new ScoredCompletion(
sameEntityCompletion,
AutocompleteSorter.currentFieldInfo,
);
expect(scoredCompletion2.score).toEqual(-Infinity);
//completion that is a priority.
const scoredCompletion3 = new ScoredCompletion(
priorityCompletion,
AutocompleteSorter.currentFieldInfo,
);
expect(scoredCompletion3.score).toBe(2 ** 6 + 2 ** 4 + 2 ** 3);
});
});