Elffuss BraidHub

kikocis/continue public

3a1e5cc69df2 · 2.3 KiBRawPermalink
1import { DatabaseConnection, SqliteDb } from "./refreshIndex.js";
2import {
3 IndexResultType,
4 MarkCompleteCallback,
5 RefreshIndexResults,
6 type CodebaseIndex,
7} from "./types.js";
8
9import type { IndexTag, IndexingProgressUpdate } from "../index.js";
10
11/**
12 * This is a CodebaseIndex used for testing which files get indexed.
13 * It maintains a SQLite database of all file/tag pairs
14 */
15export class TestCodebaseIndex implements CodebaseIndex {
16 relativeExpectedTime: number = 0.1;
17 artifactId = "__test__";
18
19 private static async _createTables(db: DatabaseConnection) {
20 await db.exec(`CREATE TABLE IF NOT EXISTS test_index (
21 id INTEGER PRIMARY KEY,
22 path TEXT NOT NULL,
23 branch TEXT NOT NULL,
24 directory TEXT NOT NULL
25 )`);
26 }
27
28 async *update(
29 tag: IndexTag,
30 results: RefreshIndexResults,
31 markComplete: MarkCompleteCallback,
32 repoName: string | undefined,
33 ): AsyncGenerator<IndexingProgressUpdate, any, unknown> {
34 const db = await SqliteDb.get();
35 await TestCodebaseIndex._createTables(db);
36
37 for (const item of [...results.compute, ...results.addTag]) {
38 await db.run(
39 "INSERT INTO test_index (path, branch, directory) VALUES (?, ?, ?)",
40 [item.path, tag.branch, tag.directory],
41 );
42 }
43
44 for (const item of [...results.del, ...results.removeTag]) {
45 await db.run(
46 "DELETE FROM test_index WHERE path = ? AND branch = ? AND directory = ?",
47 [item.path, tag.branch, tag.directory],
48 );
49 }
50
51 await markComplete(results.compute, IndexResultType.Compute);
52 await markComplete(results.addTag, IndexResultType.AddTag);
53 await markComplete(results.del, IndexResultType.Delete);
54 await markComplete(results.removeTag, IndexResultType.RemoveTag);
55 }
56
57 async getIndexedFilesForTags(tags: IndexTag[]): Promise<string[]> {
58 const db = await SqliteDb.get();
59 await TestCodebaseIndex._createTables(db);
60
61 const rows = await db.all(
62 `SELECT path FROM test_index WHERE (branch, directory) IN (VALUES ${tags
63 .map(() => "(?, ?)")
64 .join(", ")})`,
65 tags.flatMap((tag) => [tag.branch, tag.directory]),
66 );
67
68 return rows.map((row: any) => row.path);
69 }
70
71 async clearDatabase(): Promise<void> {
72 const db = await SqliteDb.get();
73 await TestCodebaseIndex._createTables(db);
74 await db.run("DELETE FROM test_index");
75 }
76}