testing for autocomplete filtering
This commit is contained in:
parent
d5491cc21c
commit
853a62f28f
4 changed files with 146 additions and 7 deletions
66
core/autocomplete/test/filter.test.ts
Normal file
66
core/autocomplete/test/filter.test.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import { dedent } from "../../util";
|
||||
import { testAutocompleteFiltering } from "./util";
|
||||
|
||||
describe("llms/Mock", () => {
|
||||
it("should pass unless maybe I messed up the whitespace at the start or something idk", async () => {
|
||||
const input = dedent`
|
||||
class Calculator {
|
||||
constructor() {
|
||||
this.result = 0;
|
||||
}
|
||||
|
||||
add(number) {
|
||||
this.result += number;
|
||||
return this;
|
||||
}
|
||||
|
||||
subtract(number) {
|
||||
this.result -= number;
|
||||
return this;
|
||||
}
|
||||
|
||||
multiply(number) {
|
||||
this.result *= number;
|
||||
return this;
|
||||
}
|
||||
|
||||
divide(number) {
|
||||
<|fim|>
|
||||
|
||||
getResult() {
|
||||
return this.result;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.result = 0;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
`;
|
||||
const completion = dedent`
|
||||
if (number === 0) {
|
||||
throw new Error("Cannot divide by zero");
|
||||
}
|
||||
this.result /= number;
|
||||
return this;
|
||||
}`;
|
||||
await testAutocompleteFiltering({
|
||||
input,
|
||||
completion,
|
||||
expectToDisplay: completion,
|
||||
description: "divide",
|
||||
filename: "test.js",
|
||||
});
|
||||
});
|
||||
|
||||
it("should pass", async () => {
|
||||
await testAutocompleteFiltering({
|
||||
input: "console.log('Hello <|fim|>!');",
|
||||
completion: "World",
|
||||
expectToDisplay: "World",
|
||||
description: "Hello World",
|
||||
filename: "test.js",
|
||||
});
|
||||
});
|
||||
});
|
||||
71
core/autocomplete/test/util.ts
Normal file
71
core/autocomplete/test/util.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import Mock from "../../llm/llms/Mock";
|
||||
import { testConfigHandler, testIde } from "../../test/util/fixtures";
|
||||
import { AutocompleteInput, CompletionProvider } from "../completionProvider";
|
||||
|
||||
const FIM_DELIMITER = "<|fim|>";
|
||||
|
||||
function parseFimExample(text: string): { prefix: string; suffix: string } {
|
||||
const [prefix, suffix] = text.split(FIM_DELIMITER);
|
||||
return { prefix, suffix };
|
||||
}
|
||||
|
||||
interface AutocompleteFileringTestInput {
|
||||
description: string;
|
||||
filename: string;
|
||||
input: string;
|
||||
completion: string;
|
||||
expectToDisplay: string | null;
|
||||
}
|
||||
|
||||
export async function testAutocompleteFiltering(
|
||||
test: AutocompleteFileringTestInput,
|
||||
) {
|
||||
const { prefix, suffix } = parseFimExample(test.input);
|
||||
|
||||
// Setup necessary objects
|
||||
const llm = new Mock({
|
||||
model: "mock",
|
||||
});
|
||||
llm.completion = test.completion;
|
||||
const ide = testIde;
|
||||
const configHandler = testConfigHandler;
|
||||
|
||||
// Create a real file
|
||||
const [workspaceDir] = await ide.getWorkspaceDirs();
|
||||
const filepath = path.join(workspaceDir, test.filename);
|
||||
fs.writeFileSync(filepath, test.input.replace(FIM_DELIMITER, ""));
|
||||
|
||||
// Prepare completion input and provider
|
||||
const completionProvider = new CompletionProvider(
|
||||
configHandler,
|
||||
ide,
|
||||
async () => llm,
|
||||
() => {},
|
||||
async () => [],
|
||||
);
|
||||
|
||||
const line = prefix.split("\n").length - 1;
|
||||
const character = prefix.split("\n")[line].length;
|
||||
const autocompleteInput: AutocompleteInput = {
|
||||
clipboardText: "",
|
||||
completionId: "test-completion-id",
|
||||
filepath,
|
||||
pos: {
|
||||
line,
|
||||
character,
|
||||
},
|
||||
recentlyEditedFiles: [],
|
||||
recentlyEditedRanges: [],
|
||||
};
|
||||
|
||||
// Generate a completion
|
||||
const result = await completionProvider.provideInlineCompletionItems(
|
||||
autocompleteInput,
|
||||
undefined,
|
||||
);
|
||||
|
||||
// Ensure that we return the text that is wanted to be displayed
|
||||
expect(result?.completion).toEqual(test.expectToDisplay);
|
||||
}
|
||||
|
|
@ -2,24 +2,26 @@ import { ChatMessage, CompletionOptions, ModelProvider } from "../../index.js";
|
|||
import { BaseLLM } from "../index.js";
|
||||
|
||||
class Mock extends BaseLLM {
|
||||
static Completion = "Test Completion";
|
||||
public completion: string = "Test Completion";
|
||||
static providerName: ModelProvider = "mock";
|
||||
|
||||
protected async *_streamComplete(
|
||||
prompt: string,
|
||||
options: CompletionOptions,
|
||||
): AsyncGenerator<string> {
|
||||
yield Mock.Completion;
|
||||
yield this.completion;
|
||||
}
|
||||
|
||||
protected async *_streamChat(
|
||||
messages: ChatMessage[],
|
||||
options: CompletionOptions,
|
||||
): AsyncGenerator<ChatMessage> {
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: Mock.Completion,
|
||||
};
|
||||
for (const char of this.completion) {
|
||||
yield {
|
||||
role: "assistant",
|
||||
content: char,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class FileSystemIde implements IDE {
|
|||
throw new Error("Method not implemented.");
|
||||
}
|
||||
onDidChangeActiveTextEditor(callback: (filepath: string) => void): void {
|
||||
throw new Error("Method not implemented.");
|
||||
return;
|
||||
}
|
||||
|
||||
async getIdeSettings(): Promise<IdeSettings> {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue