|
7 | 7 | import * as fs from 'node:fs/promises'; |
8 | 8 | import * as os from 'node:os'; |
9 | 9 | import * as path from 'node:path'; |
10 | | -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 10 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
11 | 11 | import { |
12 | 12 | extractErrorHandlingFromContent, |
13 | 13 | extractImportStyleFromContent, |
@@ -110,6 +110,153 @@ describe('Pure Pattern Extractors', () => { |
110 | 110 | }); |
111 | 111 | }); |
112 | 112 |
|
| 113 | +// ======================================================================== |
| 114 | +// analyzeFileFromIndex (index-based, no ts-morph) |
| 115 | +// ======================================================================== |
| 116 | + |
| 117 | +describe('analyzeFileFromIndex', () => { |
| 118 | + let tempDir: string; |
| 119 | + let service: PatternAnalysisService; |
| 120 | + |
| 121 | + beforeEach(async () => { |
| 122 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'analyze-from-index-')); |
| 123 | + service = new PatternAnalysisService({ repositoryPath: tempDir }); |
| 124 | + }); |
| 125 | + |
| 126 | + afterEach(async () => { |
| 127 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should extract patterns from indexed metadata', async () => { |
| 131 | + await fs.mkdir(path.join(tempDir, 'src'), { recursive: true }); |
| 132 | + await fs.writeFile( |
| 133 | + path.join(tempDir, 'src/test.ts'), |
| 134 | + 'import { foo } from "./bar";\n\nexport function test(): string {\n throw new Error("oops");\n return "hello";\n}\n' |
| 135 | + ); |
| 136 | + |
| 137 | + const indexedDocs = [ |
| 138 | + { |
| 139 | + id: 'src/test.ts:test:3', |
| 140 | + score: 0.9, |
| 141 | + metadata: { |
| 142 | + path: 'src/test.ts', |
| 143 | + type: 'function', |
| 144 | + name: 'test', |
| 145 | + signature: 'function test(): string', |
| 146 | + exported: true, |
| 147 | + }, |
| 148 | + }, |
| 149 | + ]; |
| 150 | + |
| 151 | + const result = await service.analyzeFileFromIndex('src/test.ts', indexedDocs); |
| 152 | + expect(result.importStyle.style).toBe('esm'); |
| 153 | + expect(result.typeAnnotations.coverage).toBe('full'); |
| 154 | + expect(result.fileSize.lines).toBe(7); |
| 155 | + expect(result.errorHandling.style).toBe('throw'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should handle files with no indexed docs', async () => { |
| 159 | + await fs.writeFile(path.join(tempDir, 'empty.ts'), 'const x = 1;\n'); |
| 160 | + const result = await service.analyzeFileFromIndex('empty.ts', []); |
| 161 | + expect(result.typeAnnotations.coverage).toBe('none'); |
| 162 | + expect(result.typeAnnotations.totalCount).toBe(0); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should handle deleted files gracefully (ENOENT)', async () => { |
| 166 | + const result = await service.analyzeFileFromIndex('nonexistent.ts', []); |
| 167 | + expect(result.fileSize.lines).toBe(0); |
| 168 | + expect(result.fileSize.bytes).toBe(0); |
| 169 | + expect(result.importStyle.style).toBe('unknown'); |
| 170 | + expect(result.errorHandling.style).toBe('unknown'); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +// ======================================================================== |
| 175 | +// comparePatterns with vectorStorage (fast path) |
| 176 | +// ======================================================================== |
| 177 | + |
| 178 | +describe('comparePatterns with vectorStorage', () => { |
| 179 | + let tempDir: string; |
| 180 | + |
| 181 | + beforeEach(async () => { |
| 182 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'compare-index-')); |
| 183 | + }); |
| 184 | + |
| 185 | + afterEach(async () => { |
| 186 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should use index path when vectorStorage is provided', async () => { |
| 190 | + await fs.mkdir(path.join(tempDir, 'src'), { recursive: true }); |
| 191 | + await fs.writeFile( |
| 192 | + path.join(tempDir, 'src/target.ts'), |
| 193 | + 'import { x } from "./y";\nexport function target(): string { throw new Error("oops"); }\n' |
| 194 | + ); |
| 195 | + await fs.writeFile( |
| 196 | + path.join(tempDir, 'src/similar.ts'), |
| 197 | + 'import { a } from "./b";\nexport function similar(): number { throw new Error("bad"); }\n' |
| 198 | + ); |
| 199 | + |
| 200 | + const mockGetDocsByFilePath = vi.fn().mockResolvedValue( |
| 201 | + new Map([ |
| 202 | + [ |
| 203 | + 'src/target.ts', |
| 204 | + [ |
| 205 | + { |
| 206 | + id: 'src/target.ts:target:2', |
| 207 | + score: 0.9, |
| 208 | + metadata: { |
| 209 | + path: 'src/target.ts', |
| 210 | + type: 'function', |
| 211 | + signature: 'function target(): string', |
| 212 | + }, |
| 213 | + }, |
| 214 | + ], |
| 215 | + ], |
| 216 | + [ |
| 217 | + 'src/similar.ts', |
| 218 | + [ |
| 219 | + { |
| 220 | + id: 'src/similar.ts:similar:2', |
| 221 | + score: 0.9, |
| 222 | + metadata: { |
| 223 | + path: 'src/similar.ts', |
| 224 | + type: 'function', |
| 225 | + signature: 'function similar(): number', |
| 226 | + }, |
| 227 | + }, |
| 228 | + ], |
| 229 | + ], |
| 230 | + ]) |
| 231 | + ); |
| 232 | + |
| 233 | + const mockVectorStorage = { getDocsByFilePath: mockGetDocsByFilePath } as any; |
| 234 | + const serviceWithIndex = new PatternAnalysisService({ |
| 235 | + repositoryPath: tempDir, |
| 236 | + vectorStorage: mockVectorStorage, |
| 237 | + }); |
| 238 | + |
| 239 | + const result = await serviceWithIndex.comparePatterns('src/target.ts', ['src/similar.ts']); |
| 240 | + expect(mockGetDocsByFilePath).toHaveBeenCalledWith(['src/target.ts', 'src/similar.ts']); |
| 241 | + expect(result.importStyle.yourFile).toBe('esm'); |
| 242 | + expect(result.typeAnnotations.yourFile).toBe('full'); |
| 243 | + }); |
| 244 | + |
| 245 | + it('should handle empty results from index gracefully', async () => { |
| 246 | + await fs.writeFile(path.join(tempDir, 'solo.ts'), 'const x = 1;\n'); |
| 247 | + |
| 248 | + const mockGetDocsByFilePath = vi.fn().mockResolvedValue(new Map()); |
| 249 | + const mockVectorStorage = { getDocsByFilePath: mockGetDocsByFilePath } as any; |
| 250 | + const serviceWithIndex = new PatternAnalysisService({ |
| 251 | + repositoryPath: tempDir, |
| 252 | + vectorStorage: mockVectorStorage, |
| 253 | + }); |
| 254 | + |
| 255 | + const result = await serviceWithIndex.comparePatterns('solo.ts', []); |
| 256 | + expect(result.fileSize.yourFile).toBeGreaterThan(0); |
| 257 | + }); |
| 258 | +}); |
| 259 | + |
113 | 260 | // ======================================================================== |
114 | 261 | // PatternAnalysisService (integration — uses file I/O) |
115 | 262 | // ======================================================================== |
|
0 commit comments