From 853a62f28f54ddfd3f1633c611d043faa63fb3d2 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 21 Oct 2024 23:40:48 -0700 Subject: [PATCH] testing for autocomplete filtering --- core/autocomplete/test/filter.test.ts | 66 +++++++++++++++++++++++++ core/autocomplete/test/util.ts | 71 +++++++++++++++++++++++++++ core/llm/llms/Mock.ts | 14 +++--- core/util/filesystem.ts | 2 +- 4 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 core/autocomplete/test/filter.test.ts create mode 100644 core/autocomplete/test/util.ts diff --git a/core/autocomplete/test/filter.test.ts b/core/autocomplete/test/filter.test.ts new file mode 100644 index 000000000..a146c870d --- /dev/null +++ b/core/autocomplete/test/filter.test.ts @@ -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", + }); + }); +}); diff --git a/core/autocomplete/test/util.ts b/core/autocomplete/test/util.ts new file mode 100644 index 000000000..1b2e21ca9 --- /dev/null +++ b/core/autocomplete/test/util.ts @@ -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); +} diff --git a/core/llm/llms/Mock.ts b/core/llm/llms/Mock.ts index d5e2ef59c..e6097a60d 100644 --- a/core/llm/llms/Mock.ts +++ b/core/llm/llms/Mock.ts @@ -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 { - yield Mock.Completion; + yield this.completion; } protected async *_streamChat( messages: ChatMessage[], options: CompletionOptions, ): AsyncGenerator { - yield { - role: "assistant", - content: Mock.Completion, - }; + for (const char of this.completion) { + yield { + role: "assistant", + content: char, + }; + } } } diff --git a/core/util/filesystem.ts b/core/util/filesystem.ts index 6e8bd6c0d..0f64d912c 100644 --- a/core/util/filesystem.ts +++ b/core/util/filesystem.ts @@ -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 {