|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | +import { CodebaseIndexer } from '../src/core/indexer.js'; |
| 6 | +import { dispatchTool } from '../src/tools/index.js'; |
| 7 | +import type { ToolContext } from '../src/tools/types.js'; |
| 8 | +import { |
| 9 | + CODEBASE_CONTEXT_DIRNAME, |
| 10 | + INTELLIGENCE_FILENAME, |
| 11 | + KEYWORD_INDEX_FILENAME, |
| 12 | + VECTOR_DB_DIRNAME, |
| 13 | + MEMORY_FILENAME |
| 14 | +} from '../src/constants/codebase-context.js'; |
| 15 | + |
| 16 | +describe('Impact candidates (2-hop)', () => { |
| 17 | + let tempRoot: string | null = null; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'impact-2hop-')); |
| 21 | + const srcDir = path.join(tempRoot, 'src'); |
| 22 | + await fs.mkdir(srcDir, { recursive: true }); |
| 23 | + await fs.writeFile(path.join(tempRoot, 'package.json'), JSON.stringify({ name: 'impact-2hop' })); |
| 24 | + |
| 25 | + await fs.writeFile( |
| 26 | + path.join(srcDir, 'c.ts'), |
| 27 | + `export function cFn() { return 'UNIQUE_TOKEN_123'; }\n` |
| 28 | + ); |
| 29 | + await fs.writeFile(path.join(srcDir, 'b.ts'), `import { cFn } from './c';\nexport const b = cFn();\n`); |
| 30 | + await fs.writeFile(path.join(srcDir, 'a.ts'), `import { b } from './b';\nexport const a = b;\n`); |
| 31 | + |
| 32 | + const indexer = new CodebaseIndexer({ |
| 33 | + rootPath: tempRoot, |
| 34 | + config: { skipEmbedding: true } |
| 35 | + }); |
| 36 | + await indexer.index(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(async () => { |
| 40 | + if (tempRoot) { |
| 41 | + await fs.rm(tempRoot, { recursive: true, force: true }); |
| 42 | + tempRoot = null; |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it('includes hop 1 and hop 2 candidates in preflight impact.details', async () => { |
| 47 | + if (!tempRoot) throw new Error('tempRoot not initialized'); |
| 48 | + |
| 49 | + const rootPath = tempRoot; |
| 50 | + const paths = { |
| 51 | + baseDir: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME), |
| 52 | + memory: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, MEMORY_FILENAME), |
| 53 | + intelligence: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, INTELLIGENCE_FILENAME), |
| 54 | + keywordIndex: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, KEYWORD_INDEX_FILENAME), |
| 55 | + vectorDb: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, VECTOR_DB_DIRNAME) |
| 56 | + }; |
| 57 | + |
| 58 | + const ctx: ToolContext = { |
| 59 | + indexState: { status: 'ready' }, |
| 60 | + paths, |
| 61 | + rootPath, |
| 62 | + performIndexing: () => {} |
| 63 | + }; |
| 64 | + |
| 65 | + const resp = await dispatchTool( |
| 66 | + 'search_codebase', |
| 67 | + { query: 'UNIQUE_TOKEN_123', intent: 'edit', includeSnippets: false }, |
| 68 | + ctx |
| 69 | + ); |
| 70 | + |
| 71 | + const text = resp.content?.[0]?.text ?? ''; |
| 72 | + const parsed = JSON.parse(text) as { preflight?: { impact?: { details?: Array<{ file: string; hop: 1 | 2 }> } } }; |
| 73 | + const details = parsed.preflight?.impact?.details ?? []; |
| 74 | + |
| 75 | + expect(details.some((d) => d.file.endsWith('src/b.ts') && d.hop === 1)).toBe(true); |
| 76 | + expect(details.some((d) => d.file.endsWith('src/a.ts') && d.hop === 2)).toBe(true); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
0 commit comments