2021-08-04 05:34:44 +00:00
|
|
|
import TernServer, {
|
|
|
|
|
AutocompleteDataType,
|
|
|
|
|
Completion,
|
|
|
|
|
createCompletionHeader,
|
|
|
|
|
DataTreeDefEntityInformation,
|
|
|
|
|
} from "./TernServer";
|
2020-07-14 10:30:33 +00:00
|
|
|
import { MockCodemirrorEditor } from "../../../test/__mocks__/CodeMirrorEditorMock";
|
2021-07-20 10:02:56 +00:00
|
|
|
import { ENTITY_TYPE } from "entities/DataTree/dataTreeFactory";
|
|
|
|
|
import _ from "lodash";
|
2020-07-14 10:30:33 +00:00
|
|
|
|
|
|
|
|
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.}}",
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
changed: null,
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: "{{Api.}}",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: {
|
|
|
|
|
name: "test",
|
|
|
|
|
doc: ({
|
|
|
|
|
getCursor: () => ({ ch: 0, line: 0 }),
|
|
|
|
|
getLine: () => "a{{Api.}}",
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
changed: null,
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: "a{{Api.}}",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: {
|
|
|
|
|
name: "test",
|
|
|
|
|
doc: ({
|
|
|
|
|
getCursor: () => ({ ch: 2, line: 0 }),
|
|
|
|
|
getLine: () => "a{{Api.}}",
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
changed: null,
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: "{{Api.}}",
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2020-12-24 04:32:25 +00:00
|
|
|
testCases.forEach((testCase) => {
|
2021-07-08 07:04:47 +00:00
|
|
|
const value = TernServer.getFocusedDynamicValue(testCase.input);
|
2020-07-14 10:30:33 +00:00
|
|
|
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,
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
changed: null,
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: { ch: 0, line: 0 },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: {
|
|
|
|
|
name: "test",
|
|
|
|
|
doc: ({
|
|
|
|
|
getCursor: () => ({ ch: 0, line: 1 }),
|
|
|
|
|
getLine: () => "{{Api.}}",
|
|
|
|
|
somethingSelected: () => false,
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
changed: null,
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: { ch: 0, line: 0 },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: {
|
|
|
|
|
name: "test",
|
|
|
|
|
doc: ({
|
|
|
|
|
getCursor: () => ({ ch: 3, line: 1 }),
|
|
|
|
|
getLine: () => "g {{Api.}}",
|
|
|
|
|
somethingSelected: () => false,
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
changed: null,
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: { ch: 1, line: 0 },
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2021-01-12 01:22:31 +00:00
|
|
|
testCases.forEach((testCase) => {
|
2021-07-08 07:04:47 +00:00
|
|
|
const request = TernServer.buildRequest(testCase.input, {});
|
2020-07-14 10:30:33 +00:00
|
|
|
|
|
|
|
|
expect(request.query.end).toEqual(testCase.expectedOutput);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-21 04:25:32 +00:00
|
|
|
it(`Check whether the position is evaluated correctly for placing the selected
|
2020-07-14 10:30:33 +00:00
|
|
|
autocomplete value`, () => {
|
|
|
|
|
const testCases = [
|
|
|
|
|
{
|
|
|
|
|
input: {
|
|
|
|
|
codeEditor: {
|
|
|
|
|
value: "{{}}",
|
|
|
|
|
cursor: { ch: 2, line: 0 },
|
|
|
|
|
doc: ({
|
|
|
|
|
getCursor: () => ({ ch: 2, line: 0 }),
|
|
|
|
|
getLine: () => "{{}}",
|
|
|
|
|
somethingSelected: () => false,
|
|
|
|
|
} 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: 1 },
|
|
|
|
|
doc: ({
|
|
|
|
|
getCursor: () => ({ ch: 3, line: 1 }),
|
|
|
|
|
getLine: () => " {{}}",
|
|
|
|
|
somethingSelected: () => false,
|
|
|
|
|
} as unknown) as CodeMirror.Doc,
|
|
|
|
|
},
|
|
|
|
|
requestCallbackData: {
|
|
|
|
|
completions: [{ name: "Api1" }],
|
|
|
|
|
start: { ch: 2, line: 1 },
|
|
|
|
|
end: { ch: 6, line: 1 },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expectedOutput: { ch: 3, line: 1 },
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2021-01-12 01:22:31 +00:00
|
|
|
testCases.forEach((testCase) => {
|
2020-07-14 10:30:33 +00:00
|
|
|
MockCodemirrorEditor.getValue.mockReturnValueOnce(
|
|
|
|
|
testCase.input.codeEditor.value,
|
|
|
|
|
);
|
|
|
|
|
MockCodemirrorEditor.getCursor.mockReturnValueOnce(
|
|
|
|
|
testCase.input.codeEditor.cursor,
|
|
|
|
|
);
|
|
|
|
|
MockCodemirrorEditor.getDoc.mockReturnValueOnce(
|
|
|
|
|
testCase.input.codeEditor.doc,
|
|
|
|
|
);
|
|
|
|
|
|
2021-07-08 07:04:47 +00:00
|
|
|
const value: any = TernServer.requestCallback(
|
2020-07-14 10:30:33 +00:00
|
|
|
null,
|
|
|
|
|
testCase.input.requestCallbackData,
|
|
|
|
|
(MockCodemirrorEditor as unknown) as CodeMirror.Editor,
|
|
|
|
|
() => null,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(value.from).toEqual(testCase.expectedOutput);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-07-20 10:02:56 +00:00
|
|
|
|
|
|
|
|
describe("Tern server sorting", () => {
|
2021-08-04 05:34:44 +00:00
|
|
|
const defEntityInformation: Map<
|
|
|
|
|
string,
|
|
|
|
|
DataTreeDefEntityInformation
|
|
|
|
|
> = new Map();
|
2021-07-20 10:02:56 +00:00
|
|
|
const contextCompletion: Completion = {
|
|
|
|
|
text: "context",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.STRING,
|
2021-07-20 10:02:56 +00:00
|
|
|
origin: "[doc]",
|
|
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sameEntityCompletion: Completion = {
|
|
|
|
|
text: "sameEntity.tableData",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.ARRAY,
|
|
|
|
|
origin: "DATA_TREE",
|
2021-07-20 10:02:56 +00:00
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
2021-08-04 05:34:44 +00:00
|
|
|
defEntityInformation.set("sameEntity", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
2021-09-09 15:10:22 +00:00
|
|
|
subType: "TABLE_WIDGET",
|
2021-08-04 05:34:44 +00:00
|
|
|
});
|
2022-07-14 07:02:35 +00:00
|
|
|
defEntityInformation.set("sameEntity", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
|
|
|
|
subType: "TABLE_WIDGET_V2",
|
|
|
|
|
});
|
2021-07-20 10:02:56 +00:00
|
|
|
|
|
|
|
|
const sameTypeCompletion: Completion = {
|
|
|
|
|
text: "sameType.selectedRow",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.OBJECT,
|
|
|
|
|
origin: "DATA_TREE",
|
2021-07-20 10:02:56 +00:00
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
2021-08-04 05:34:44 +00:00
|
|
|
defEntityInformation.set("sameType", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
2021-09-09 15:10:22 +00:00
|
|
|
subType: "TABLE_WIDGET",
|
2021-08-04 05:34:44 +00:00
|
|
|
});
|
2022-07-14 07:02:35 +00:00
|
|
|
defEntityInformation.set("sameType", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
|
|
|
|
subType: "TABLE_WIDGET_V2",
|
|
|
|
|
});
|
2021-07-20 10:02:56 +00:00
|
|
|
|
|
|
|
|
const diffTypeCompletion: Completion = {
|
|
|
|
|
text: "diffType.tableData",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.ARRAY,
|
|
|
|
|
origin: "DATA_TREE.WIDGET",
|
2021-07-20 10:02:56 +00:00
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-04 05:34:44 +00:00
|
|
|
defEntityInformation.set("diffType", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
2021-09-09 15:10:22 +00:00
|
|
|
subType: "TABLE_WIDGET",
|
2021-08-04 05:34:44 +00:00
|
|
|
});
|
2022-07-14 07:02:35 +00:00
|
|
|
defEntityInformation.set("diffType", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
|
|
|
|
subType: "TABLE_WIDGET_V2",
|
|
|
|
|
});
|
2021-08-04 05:34:44 +00:00
|
|
|
|
2021-07-20 10:02:56 +00:00
|
|
|
const sameTypeDiffEntityTypeCompletion: Completion = {
|
|
|
|
|
text: "diffEntity.data",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.OBJECT,
|
|
|
|
|
origin: "DATA_TREE",
|
2021-07-20 10:02:56 +00:00
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-04 05:34:44 +00:00
|
|
|
defEntityInformation.set("diffEntity", {
|
|
|
|
|
type: ENTITY_TYPE.ACTION,
|
|
|
|
|
subType: ENTITY_TYPE.ACTION,
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-20 10:02:56 +00:00
|
|
|
const dataTreeCompletion: Completion = {
|
|
|
|
|
text: "otherDataTree",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.STRING,
|
|
|
|
|
origin: "DATA_TREE",
|
2021-07-20 10:02:56 +00:00
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-04 05:34:44 +00:00
|
|
|
defEntityInformation.set("otherDataTree", {
|
|
|
|
|
type: ENTITY_TYPE.WIDGET,
|
2021-09-09 15:10:22 +00:00
|
|
|
subType: "TEXT_WIDGET",
|
2021-08-04 05:34:44 +00:00
|
|
|
});
|
|
|
|
|
|
2021-07-20 10:02:56 +00:00
|
|
|
const functionCompletion: Completion = {
|
|
|
|
|
text: "otherDataFunction",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.FUNCTION,
|
2021-07-20 10:02:56 +00:00
|
|
|
origin: "DATA_TREE.APPSMITH.FUNCTIONS",
|
|
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ecmascriptCompletion: Completion = {
|
|
|
|
|
text: "otherJS",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.OBJECT,
|
2021-07-20 10:02:56 +00:00
|
|
|
origin: "ecmascript",
|
|
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const libCompletion: Completion = {
|
|
|
|
|
text: "libValue",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.OBJECT,
|
2021-07-20 10:02:56 +00:00
|
|
|
origin: "LIB/lodash",
|
|
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const unknownCompletion: Completion = {
|
|
|
|
|
text: "unknownSuggestion",
|
2021-08-04 05:34:44 +00:00
|
|
|
type: AutocompleteDataType.UNKNOWN,
|
2021-07-20 10:02:56 +00:00
|
|
|
origin: "unknown",
|
|
|
|
|
data: {
|
|
|
|
|
doc: "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const completions = [
|
|
|
|
|
sameEntityCompletion,
|
|
|
|
|
sameTypeCompletion,
|
|
|
|
|
contextCompletion,
|
|
|
|
|
libCompletion,
|
|
|
|
|
unknownCompletion,
|
|
|
|
|
diffTypeCompletion,
|
|
|
|
|
sameTypeDiffEntityTypeCompletion,
|
|
|
|
|
ecmascriptCompletion,
|
|
|
|
|
functionCompletion,
|
|
|
|
|
dataTreeCompletion,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
it("shows best match results", () => {
|
|
|
|
|
TernServer.setEntityInformation({
|
|
|
|
|
entityName: "sameEntity",
|
|
|
|
|
entityType: ENTITY_TYPE.WIDGET,
|
2021-08-04 05:34:44 +00:00
|
|
|
expectedType: AutocompleteDataType.OBJECT,
|
2021-07-20 10:02:56 +00:00
|
|
|
});
|
2021-08-04 05:34:44 +00:00
|
|
|
TernServer.defEntityInformation = defEntityInformation;
|
2021-09-28 05:39:43 +00:00
|
|
|
const sortedCompletions = TernServer.sortAndFilterCompletions(
|
2021-07-20 10:02:56 +00:00
|
|
|
_.shuffle(completions),
|
|
|
|
|
true,
|
|
|
|
|
"",
|
|
|
|
|
);
|
|
|
|
|
expect(sortedCompletions[0]).toStrictEqual(contextCompletion);
|
|
|
|
|
expect(sortedCompletions).toEqual(
|
|
|
|
|
expect.arrayContaining([
|
|
|
|
|
createCompletionHeader("Best Match"),
|
|
|
|
|
sameTypeDiffEntityTypeCompletion,
|
|
|
|
|
createCompletionHeader("Search Results"),
|
|
|
|
|
dataTreeCompletion,
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
expect(sortedCompletions).toEqual(
|
|
|
|
|
expect.not.arrayContaining([diffTypeCompletion]),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|