|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { CodebaseIndexer } from '../src/core/indexer.js'; |
| 5 | +import { dispatchTool } from '../src/tools/index.js'; |
| 6 | +import type { ToolContext } from '../src/tools/types.js'; |
| 7 | +import { |
| 8 | + CODEBASE_CONTEXT_DIRNAME, |
| 9 | + INTELLIGENCE_FILENAME, |
| 10 | + KEYWORD_INDEX_FILENAME, |
| 11 | + VECTOR_DB_DIRNAME, |
| 12 | + MEMORY_FILENAME, |
| 13 | + RELATIONSHIPS_FILENAME |
| 14 | +} from '../src/constants/codebase-context.js'; |
| 15 | + |
| 16 | +describe('Impact candidates (2-hop)', () => { |
| 17 | + let tempRoot: string | null = null; |
| 18 | + const token = 'UNIQUETOKEN123'; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + // Keep test artifacts under CWD (mirrors other indexer tests and avoids OS tmp quirks) |
| 22 | + tempRoot = await fs.mkdtemp(path.join(process.cwd(), '.tmp-impact-2hop-')); |
| 23 | + const srcDir = path.join(tempRoot, 'src'); |
| 24 | + await fs.mkdir(srcDir, { recursive: true }); |
| 25 | + await fs.writeFile(path.join(tempRoot, 'package.json'), JSON.stringify({ name: 'impact-2hop' })); |
| 26 | + |
| 27 | + await fs.writeFile( |
| 28 | + path.join(srcDir, 'c.ts'), |
| 29 | + `export function cFn() { return '${token}'; }\n` |
| 30 | + ); |
| 31 | + await fs.writeFile(path.join(srcDir, 'b.ts'), `import { cFn } from './c';\nexport const b = cFn();\n`); |
| 32 | + await fs.writeFile(path.join(srcDir, 'a.ts'), `import { b } from './b';\nexport const a = b;\n`); |
| 33 | + |
| 34 | + const indexer = new CodebaseIndexer({ |
| 35 | + rootPath: tempRoot, |
| 36 | + config: { skipEmbedding: true } |
| 37 | + }); |
| 38 | + await indexer.index(); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(async () => { |
| 42 | + if (tempRoot) { |
| 43 | + await fs.rm(tempRoot, { recursive: true, force: true }); |
| 44 | + tempRoot = null; |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('includes hop 1 and hop 2 candidates in preflight impact.details', async () => { |
| 49 | + if (!tempRoot) throw new Error('tempRoot not initialized'); |
| 50 | + |
| 51 | + const rootPath = tempRoot; |
| 52 | + const paths = { |
| 53 | + baseDir: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME), |
| 54 | + memory: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, MEMORY_FILENAME), |
| 55 | + intelligence: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, INTELLIGENCE_FILENAME), |
| 56 | + keywordIndex: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, KEYWORD_INDEX_FILENAME), |
| 57 | + vectorDb: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, VECTOR_DB_DIRNAME) |
| 58 | + }; |
| 59 | + |
| 60 | + const ctx: ToolContext = { |
| 61 | + indexState: { status: 'ready' }, |
| 62 | + paths, |
| 63 | + rootPath, |
| 64 | + performIndexing: () => {} |
| 65 | + }; |
| 66 | + |
| 67 | + const relationshipsPath = path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, RELATIONSHIPS_FILENAME); |
| 68 | + const relationshipsRaw = await fs.readFile(relationshipsPath, 'utf-8'); |
| 69 | + const relationships = JSON.parse(relationshipsRaw) as { |
| 70 | + graph?: { imports?: Record<string, string[]> }; |
| 71 | + }; |
| 72 | + const imports = relationships.graph?.imports ?? {}; |
| 73 | + const hasInternalEdge = |
| 74 | + (imports['src/b.ts'] ?? []).some((d) => d.endsWith('src/c.ts') || d === 'src/c.ts') && |
| 75 | + (imports['src/a.ts'] ?? []).some((d) => d.endsWith('src/b.ts') || d === 'src/b.ts'); |
| 76 | + if (!hasInternalEdge) { |
| 77 | + throw new Error( |
| 78 | + `Expected relationships graph to include src/a.ts -> src/b.ts and src/b.ts -> src/c.ts, got imports keys=${JSON.stringify( |
| 79 | + Object.keys(imports) |
| 80 | + )}` |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + const resp = await dispatchTool( |
| 85 | + 'search_codebase', |
| 86 | + { query: token, intent: 'edit', includeSnippets: false, limit: 1 }, |
| 87 | + ctx |
| 88 | + ); |
| 89 | + |
| 90 | + const text = resp.content?.[0]?.text ?? ''; |
| 91 | + const parsed = JSON.parse(text) as { |
| 92 | + status?: string; |
| 93 | + results?: Array<{ file?: string }>; |
| 94 | + preflight?: { impact?: { details?: Array<{ file: string; hop: 1 | 2 }> } }; |
| 95 | + }; |
| 96 | + const results = parsed.results ?? []; |
| 97 | + if (!Array.isArray(results) || results.length === 0) { |
| 98 | + throw new Error( |
| 99 | + `Expected at least one search result for token, got status=${String(parsed.status)} results=${JSON.stringify( |
| 100 | + results |
| 101 | + )}` |
| 102 | + ); |
| 103 | + } |
| 104 | + const details = parsed.preflight?.impact?.details ?? []; |
| 105 | + |
| 106 | + const hasHop1 = details.some((d) => d.file.endsWith('src/b.ts') && d.hop === 1); |
| 107 | + if (!hasHop1) { |
| 108 | + throw new Error( |
| 109 | + `Expected hop 1 candidate src/b.ts, got impact.details=${JSON.stringify(details)}` |
| 110 | + ); |
| 111 | + } |
| 112 | + const hasHop2 = details.some((d) => d.file.endsWith('src/a.ts') && d.hop === 2); |
| 113 | + if (!hasHop2) { |
| 114 | + throw new Error( |
| 115 | + `Expected hop 2 candidate src/a.ts, got impact.details=${JSON.stringify(details)}` |
| 116 | + ); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
0 commit comments