| 1 | import fs from "fs"; |
| 2 | import path from "path"; |
| 3 | import { expect, vi } from "vitest"; |
| 4 | |
| 5 | import Parser from "web-tree-sitter"; |
| 6 | import { Position } from "../../../.."; |
| 7 | import { testIde } from "../../../../test/fixtures"; |
| 8 | import { getAst, getTreePathAtCursor } from "../../../util/ast"; |
| 9 | import { ImportDefinitionsService } from "../../ImportDefinitionsService"; |
| 10 | import { RootPathContextService } from "../RootPathContextService"; |
| 11 | |
| 12 | function splitTextAtPosition( |
| 13 | fileContent: string, |
| 14 | position: Position, |
| 15 | ): [string, string] { |
| 16 | const lines = fileContent.split("\n"); |
| 17 | let currentPos = 0; |
| 18 | |
| 19 | // Calculate position based on the provided line and character |
| 20 | for (let i = 0; i < position.line; i++) { |
| 21 | currentPos += lines[i].length + 1; // +1 for the newline character |
| 22 | } |
| 23 | const splitPos = currentPos + position.character; |
| 24 | |
| 25 | return [fileContent.slice(0, splitPos), fileContent.slice(splitPos)]; |
| 26 | } |
| 27 | |
| 28 | export async function testRootPathContext( |
| 29 | folderName: string, |
| 30 | relativeFilepath: string, |
| 31 | position: Position, |
| 32 | expectedDefinitionPositions: Parser.Point[], |
| 33 | ) { |
| 34 | // Create a mocked instance of RootPathContextService |
| 35 | const ide = testIde; |
| 36 | const importDefinitionsService = new ImportDefinitionsService(ide); |
| 37 | const service = new RootPathContextService(importDefinitionsService, ide); |
| 38 | |
| 39 | const getSnippetsMock = vi |
| 40 | // @ts-ignore |
| 41 | .spyOn(service, "getSnippets") |
| 42 | // @ts-ignore |
| 43 | .mockImplementation((_filePath, _endPosition) => { |
| 44 | return []; |
| 45 | }); |
| 46 | |
| 47 | // Copy the folder to the test directory |
| 48 | const folderPath = path.join( |
| 49 | process.cwd(), |
| 50 | "autocomplete", |
| 51 | "context", |
| 52 | "root-path-context", |
| 53 | "test", |
| 54 | folderName, |
| 55 | ); |
| 56 | const workspaceDir = (await ide.getWorkspaceDirs())[0]; |
| 57 | const testFolderPath = path.join(workspaceDir, folderName); |
| 58 | fs.cpSync(folderPath, testFolderPath, { |
| 59 | recursive: true, |
| 60 | force: true, |
| 61 | }); |
| 62 | |
| 63 | // Get results of root path context |
| 64 | const startPath = path.join(testFolderPath, relativeFilepath); |
| 65 | const [prefix, suffix] = splitTextAtPosition( |
| 66 | fs.readFileSync(startPath, "utf8"), |
| 67 | position, |
| 68 | ); |
| 69 | const fileContents = prefix + suffix; |
| 70 | const ast = await getAst(startPath, fileContents); |
| 71 | if (!ast) { |
| 72 | throw new Error("AST is undefined"); |
| 73 | } |
| 74 | |
| 75 | const treePath = await getTreePathAtCursor(ast, prefix.length); |
| 76 | await service.getContextForPath(startPath, treePath); |
| 77 | |
| 78 | expect(getSnippetsMock).toHaveBeenCalledTimes( |
| 79 | expectedDefinitionPositions.length, |
| 80 | ); |
| 81 | |
| 82 | expectedDefinitionPositions.forEach((position, index) => { |
| 83 | expect(getSnippetsMock).toHaveBeenNthCalledWith( |
| 84 | index + 1, |
| 85 | expect.any(String), // filepath argument |
| 86 | position, |
| 87 | expect.any(String), // language argument |
| 88 | ); |
| 89 | }); |
| 90 | } |