From cc7d41e8dc4b7524e917b7b0cdb1ed655e033b4c Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 31 Oct 2024 21:27:10 -0700 Subject: [PATCH] WIP testing rootpathcontext --- core/autocomplete/READEME.md | 6 ++ .../context/ContextRetrievalService.ts | 2 +- .../RootPathContextService.test.ts | 97 +++++++++++++++++++ .../RootPathContextService.ts | 10 +- .../test/typescript/file1.ts | 13 +++ .../test/typescript/types.ts | 11 +++ 6 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 core/autocomplete/context/root-path-context/RootPathContextService.test.ts rename core/autocomplete/context/{ => root-path-context}/RootPathContextService.ts (92%) create mode 100644 core/autocomplete/context/root-path-context/test/typescript/file1.ts create mode 100644 core/autocomplete/context/root-path-context/test/typescript/types.ts diff --git a/core/autocomplete/READEME.md b/core/autocomplete/READEME.md index 170e673a5..d5e822273 100644 --- a/core/autocomplete/READEME.md +++ b/core/autocomplete/READEME.md @@ -1,3 +1,9 @@ TODO - The generator reuse thing isn't working it seems + +Tests for context: + +- You need a static set of files to test. Ideally you don't have to rewrite the exact same files every time you want to make a new test. +- So put some example files in a few folders. There should be a folder for each language I think. +- You should definitely test each of the services separately diff --git a/core/autocomplete/context/ContextRetrievalService.ts b/core/autocomplete/context/ContextRetrievalService.ts index 3c6ed3376..a12d1962e 100644 --- a/core/autocomplete/context/ContextRetrievalService.ts +++ b/core/autocomplete/context/ContextRetrievalService.ts @@ -3,7 +3,7 @@ import { HelperVars } from "../util/HelperVars"; import { RecentlyEditedRange } from "../util/types"; import { ImportDefinitionsService } from "./ImportDefinitionsService"; import { AutocompleteSnippet, getSymbolsForSnippet } from "./ranking"; -import { RootPathContextService } from "./RootPathContextService"; +import { RootPathContextService } from "./root-path-context/RootPathContextService"; export class ContextRetrievalService { private importDefinitionsService: ImportDefinitionsService; diff --git a/core/autocomplete/context/root-path-context/RootPathContextService.test.ts b/core/autocomplete/context/root-path-context/RootPathContextService.test.ts new file mode 100644 index 000000000..87795611b --- /dev/null +++ b/core/autocomplete/context/root-path-context/RootPathContextService.test.ts @@ -0,0 +1,97 @@ +import fs from "fs"; +import path from "path"; +import { Range } from "../../.."; +import { testIde } from "../../../test/util/fixtures"; +import { getAst, getTreePathAtCursor } from "../../util/ast"; +import { ImportDefinitionsService } from "../ImportDefinitionsService"; +import { RootPathContextService } from "./RootPathContextService"; + +function splitTextAtRange(fileContent: string, range: Range): [string, string] { + const lines = fileContent.split("\n"); + let currentPos = 0; + + if (range.start.line === range.end.line) { + // If range is on the same line, calculate position once + for (let i = 0; i < range.start.line; i++) { + currentPos += lines[i].length + 1; // +1 for the newline character + } + const startPos = currentPos + range.start.character; + const endPos = currentPos + range.end.character; + return [fileContent.slice(0, startPos), fileContent.slice(endPos)]; + } + + // Calculate position of range start + for (let i = 0; i < range.start.line; i++) { + currentPos += lines[i].length + 1; + } + const startPos = currentPos + range.start.character; + + // Calculate position of range end + currentPos = 0; + for (let i = 0; i < range.end.line; i++) { + currentPos += lines[i].length + 1; + } + const endPos = currentPos + range.end.character; + + return [fileContent.slice(0, startPos), fileContent.slice(endPos)]; +} + +export async function testRootPathContext( + folderName: string, + relativeFilepath: string, + rangeToFill: Range, + expectedSnippets: string[], +) { + const ide = testIde; + const importDefinitionsService = new ImportDefinitionsService(ide); + const service = new RootPathContextService(importDefinitionsService, ide); + + // Copy the folder to the test directory + const folderPath = path.join( + __dirname, + "autocomplete", + "context", + "root-path-context", + "test", + folderName, + ); + const workspaceDir = (await ide.getWorkspaceDirs())[0]; + const testFolderPath = path.join(workspaceDir, folderName); + fs.cpSync(folderPath, testFolderPath, { + recursive: true, + force: true, + }); + + // Get results of root path context + const startPath = path.join(testFolderPath, relativeFilepath); + const [prefix, suffix] = splitTextAtRange( + fs.readFileSync(startPath, "utf8"), + rangeToFill, + ); + const fileContents = prefix + suffix; + const ast = await getAst(startPath, fileContents); + if (!ast) { + throw new Error("AST is undefined"); + } + + const treePath = await getTreePathAtCursor(ast, rangeToFill.start.character); + const snippets = await service.getContextForPath(startPath, treePath); + + expectedSnippets.forEach((expectedSnippet) => { + const found = snippets.find((snippet) => + snippet.contents.includes(expectedSnippet), + ); + expect(found).toBeDefined(); + }); +} + +describe("RootPathContextService", () => { + it("should be true", async () => { + await testRootPathContext( + "typescript", + "file1.ts", + { start: { line: 3, character: 2 }, end: { line: 3, character: 24 } }, + ["export interface Person", "export interface Address"], + ); + }); +}); diff --git a/core/autocomplete/context/RootPathContextService.ts b/core/autocomplete/context/root-path-context/RootPathContextService.ts similarity index 92% rename from core/autocomplete/context/RootPathContextService.ts rename to core/autocomplete/context/root-path-context/RootPathContextService.ts index 2b2986b75..ba99b2e83 100644 --- a/core/autocomplete/context/RootPathContextService.ts +++ b/core/autocomplete/context/root-path-context/RootPathContextService.ts @@ -1,11 +1,11 @@ import { createHash } from "crypto"; import { LRUCache } from "lru-cache"; import Parser from "web-tree-sitter"; -import { IDE } from "../.."; -import { getQueryForFile, TSQueryType } from "../../util/treeSitter"; -import { AstPath } from "../util/ast"; -import { ImportDefinitionsService } from "./ImportDefinitionsService"; -import { AutocompleteSnippet } from "./ranking"; +import { IDE } from "../../.."; +import { getQueryForFile, TSQueryType } from "../../../util/treeSitter"; +import { AstPath } from "../../util/ast"; +import { ImportDefinitionsService } from "../ImportDefinitionsService"; +import { AutocompleteSnippet } from "../ranking"; export class RootPathContextService { private cache = new LRUCache({ diff --git a/core/autocomplete/context/root-path-context/test/typescript/file1.ts b/core/autocomplete/context/root-path-context/test/typescript/file1.ts new file mode 100644 index 000000000..c67ae91e2 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/typescript/file1.ts @@ -0,0 +1,13 @@ +import { Address, Person } from "./types"; + +function getAddress(person: Person): Address { + return person.address; +} + +class Group { + people: Person[]; + + constructor(people: Person[]) { + this.people = people; + } +} diff --git a/core/autocomplete/context/root-path-context/test/typescript/types.ts b/core/autocomplete/context/root-path-context/test/typescript/types.ts new file mode 100644 index 000000000..272ec27d7 --- /dev/null +++ b/core/autocomplete/context/root-path-context/test/typescript/types.ts @@ -0,0 +1,11 @@ +export interface Person { + firstName: string; + lastName: string; + age: number; + address: Address; +} + +export interface Address { + street: string; + city: string; +}