| 1 | import { jest } from "@jest/globals"; |
| 2 | |
| 3 | import { IndexTag } from "../.."; |
| 4 | import { IContinueServerClient } from "../../continueServer/interface"; |
| 5 | import { ChunkCodebaseIndex } from "../chunk/ChunkCodebaseIndex"; |
| 6 | import { CodebaseIndex, RefreshIndexResults } from "../types"; |
| 7 | |
| 8 | import { testIde } from "../../test/fixtures"; |
| 9 | import { addToTestDir, TEST_DIR } from "../../test/testDir"; |
| 10 | import { tagToString } from "../utils"; |
| 11 | |
| 12 | export const mockFilename = "test.py"; |
| 13 | export const mockPathAndCacheKey = { |
| 14 | path: `${TEST_DIR}/${mockFilename}`, |
| 15 | cacheKey: "abc123", |
| 16 | }; |
| 17 | |
| 18 | export const mockFileContents = `\ |
| 19 | def main(): |
| 20 | print("Hello, world!") |
| 21 | |
| 22 | class Foo: |
| 23 | def __init__(self, bar: str): |
| 24 | self.bar = bar |
| 25 | `; |
| 26 | |
| 27 | export const mockTag: IndexTag = { |
| 28 | branch: "main", |
| 29 | directory: "/", |
| 30 | artifactId: "artifactId", |
| 31 | }; |
| 32 | |
| 33 | export const mockTagString = tagToString(mockTag); |
| 34 | |
| 35 | export const testContinueServerClient = { |
| 36 | connected: false, |
| 37 | getFromIndexCache: jest.fn(), |
| 38 | } as unknown as IContinueServerClient; |
| 39 | |
| 40 | const mockContinueServerClient = { |
| 41 | connected: false, |
| 42 | getFromIndexCache: jest.fn(), |
| 43 | } as unknown as IContinueServerClient; |
| 44 | |
| 45 | const mockResults: RefreshIndexResults = { |
| 46 | compute: [], |
| 47 | addTag: [], |
| 48 | removeTag: [], |
| 49 | del: [], |
| 50 | }; |
| 51 | |
| 52 | const mockMarkComplete = jest |
| 53 | .fn() |
| 54 | .mockImplementation(() => Promise.resolve()) as any; |
| 55 | |
| 56 | export async function insertMockChunks() { |
| 57 | const index = new ChunkCodebaseIndex( |
| 58 | testIde.readFile.bind(testIde), |
| 59 | mockContinueServerClient, |
| 60 | 1000, |
| 61 | ); |
| 62 | |
| 63 | addToTestDir([[mockFilename, mockFileContents]]); |
| 64 | |
| 65 | await updateIndexAndAwaitGenerator(index, "compute", mockMarkComplete); |
| 66 | await updateIndexAndAwaitGenerator(index, "addTag", mockMarkComplete); |
| 67 | } |
| 68 | |
| 69 | export async function updateIndexAndAwaitGenerator( |
| 70 | index: CodebaseIndex, |
| 71 | resultType: keyof RefreshIndexResults, |
| 72 | markComplete: any, |
| 73 | tag: IndexTag = mockTag, |
| 74 | ) { |
| 75 | const computeGenerator = index.update( |
| 76 | tag, |
| 77 | { ...mockResults, [resultType]: [mockPathAndCacheKey] }, |
| 78 | markComplete, |
| 79 | "test-repo", |
| 80 | ); |
| 81 | |
| 82 | while (!(await computeGenerator.next()).done) {} |
| 83 | } |