bluera-knowledge 0.9.26 → 0.9.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/.claude/commands/commit.md +4 -7
  2. package/.claude/hooks/post-edit-check.sh +21 -24
  3. package/.claude/skills/atomic-commits/SKILL.md +6 -0
  4. package/.claude-plugin/plugin.json +1 -1
  5. package/.env.example +4 -0
  6. package/.husky/pre-push +12 -2
  7. package/.versionrc.json +0 -4
  8. package/BUGS-FOUND.md +71 -0
  9. package/CHANGELOG.md +76 -0
  10. package/README.md +55 -20
  11. package/bun.lock +35 -1
  12. package/commands/crawl.md +2 -0
  13. package/dist/{chunk-BICFAWMN.js → chunk-2SJHNRXD.js} +73 -8
  14. package/dist/chunk-2SJHNRXD.js.map +1 -0
  15. package/dist/{chunk-J7J6LXOJ.js → chunk-OGEY66FZ.js} +106 -41
  16. package/dist/chunk-OGEY66FZ.js.map +1 -0
  17. package/dist/{chunk-5QMHZUC4.js → chunk-RWSXP3PQ.js} +482 -106
  18. package/dist/chunk-RWSXP3PQ.js.map +1 -0
  19. package/dist/index.js +73 -28
  20. package/dist/index.js.map +1 -1
  21. package/dist/mcp/server.js +2 -2
  22. package/dist/workers/background-worker-cli.js +2 -2
  23. package/eslint.config.js +1 -1
  24. package/package.json +3 -1
  25. package/src/analysis/ast-parser.test.ts +46 -0
  26. package/src/cli/commands/crawl.test.ts +99 -12
  27. package/src/cli/commands/crawl.ts +76 -24
  28. package/src/cli/commands/store.test.ts +68 -1
  29. package/src/cli/commands/store.ts +9 -3
  30. package/src/crawl/article-converter.ts +36 -1
  31. package/src/crawl/bridge.ts +18 -7
  32. package/src/crawl/intelligent-crawler.ts +45 -4
  33. package/src/db/embeddings.test.ts +16 -0
  34. package/src/db/lance.test.ts +31 -0
  35. package/src/db/lance.ts +8 -0
  36. package/src/logging/index.ts +29 -0
  37. package/src/logging/logger.test.ts +75 -0
  38. package/src/logging/logger.ts +147 -0
  39. package/src/logging/payload.test.ts +152 -0
  40. package/src/logging/payload.ts +121 -0
  41. package/src/mcp/handlers/search.handler.test.ts +28 -9
  42. package/src/mcp/handlers/search.handler.ts +69 -29
  43. package/src/mcp/handlers/store.handler.test.ts +1 -0
  44. package/src/mcp/server.ts +44 -16
  45. package/src/services/chunking.service.ts +23 -0
  46. package/src/services/index.service.test.ts +921 -1
  47. package/src/services/index.service.ts +76 -1
  48. package/src/services/index.ts +20 -2
  49. package/src/services/search.service.test.ts +573 -21
  50. package/src/services/search.service.ts +257 -105
  51. package/src/services/services.test.ts +2 -2
  52. package/src/services/snippet.service.ts +28 -3
  53. package/src/services/store.service.test.ts +28 -0
  54. package/src/services/store.service.ts +4 -0
  55. package/src/services/token.service.test.ts +45 -0
  56. package/src/services/token.service.ts +33 -0
  57. package/src/types/result.test.ts +10 -0
  58. package/tests/integration/cli-consistency.test.ts +1 -4
  59. package/vitest.config.ts +4 -0
  60. package/dist/chunk-5QMHZUC4.js.map +0 -1
  61. package/dist/chunk-BICFAWMN.js.map +0 -1
  62. package/dist/chunk-J7J6LXOJ.js.map +0 -1
  63. package/scripts/readme-version-updater.cjs +0 -18
@@ -141,21 +141,16 @@ describe('SearchService - RRF Ranking Algorithm', () => {
141
141
  expect(results.results.some(r => r.id === createDocumentId('doc2'))).toBe(true);
142
142
  });
143
143
 
144
- it('applies custom RRF k parameter correctly', async () => {
145
- const customSearchService = new SearchService(
146
- mockLanceStore,
147
- mockEmbeddingEngine,
148
- { k: 50, vectorWeight: 0.6, ftsWeight: 0.4 }
149
- );
150
-
144
+ it('uses web RRF preset for web content (url metadata)', async () => {
145
+ // Web content has url in metadata - should use web preset (k=30)
151
146
  vi.mocked(mockLanceStore.search).mockResolvedValue([
152
- { id: createDocumentId('doc1'), score: 0.9, content: 'result 1', metadata: { type: 'file' as const, storeId, indexedAt: new Date() } },
147
+ { id: createDocumentId('doc1'), score: 0.9, content: 'result 1', metadata: { type: 'web' as const, storeId, indexedAt: new Date(), url: 'https://example.com/docs' } },
153
148
  ]);
154
149
  vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([
155
- { id: createDocumentId('doc1'), score: 0.95, content: 'result 1', metadata: { type: 'file' as const, storeId, indexedAt: new Date() } },
150
+ { id: createDocumentId('doc1'), score: 0.95, content: 'result 1', metadata: { type: 'web' as const, storeId, indexedAt: new Date(), url: 'https://example.com/docs' } },
156
151
  ]);
157
152
 
158
- const results = await customSearchService.search({
153
+ const results = await searchService.search({
159
154
  query: 'test query',
160
155
  stores: [storeId],
161
156
  mode: 'hybrid',
@@ -166,28 +161,22 @@ describe('SearchService - RRF Ranking Algorithm', () => {
166
161
  expect(results.results[0]?.score).toBeGreaterThan(0);
167
162
  });
168
163
 
169
- it('applies vector and FTS weights correctly', async () => {
170
- const vectorOnlyService = new SearchService(
171
- mockLanceStore,
172
- mockEmbeddingEngine,
173
- { k: 20, vectorWeight: 1.0, ftsWeight: 0.0 }
174
- );
175
-
164
+ it('uses code RRF preset for file content (path metadata)', async () => {
165
+ // File content has path, no url - should use code preset (k=20)
176
166
  vi.mocked(mockLanceStore.search).mockResolvedValue([
177
- { id: createDocumentId('doc1'), score: 0.9, content: 'vector result', metadata: { type: 'file' as const, storeId, indexedAt: new Date() } },
167
+ { id: createDocumentId('doc1'), score: 0.9, content: 'function test() {}', metadata: { type: 'file' as const, storeId, indexedAt: new Date(), path: '/src/test.ts' } },
178
168
  ]);
179
169
  vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([
180
- { id: createDocumentId('doc2'), score: 0.95, content: 'fts result', metadata: { type: 'file' as const, storeId, indexedAt: new Date() } },
170
+ { id: createDocumentId('doc2'), score: 0.95, content: 'class Example {}', metadata: { type: 'file' as const, storeId, indexedAt: new Date(), path: '/src/example.ts' } },
181
171
  ]);
182
172
 
183
- const results = await vectorOnlyService.search({
173
+ const results = await searchService.search({
184
174
  query: 'test query',
185
175
  stores: [storeId],
186
176
  mode: 'hybrid',
187
177
  limit: 10,
188
178
  });
189
179
 
190
- // With vectorWeight=1.0 and ftsWeight=0.0, vector results should dominate
191
180
  expect(results.results.length).toBeGreaterThan(0);
192
181
  });
193
182
 
@@ -1103,3 +1092,566 @@ describe('SearchService - Edge Cases', () => {
1103
1092
  );
1104
1093
  });
1105
1094
  });
1095
+
1096
+ describe('SearchService - Path Keyword Boosting', () => {
1097
+ let mockLanceStore: LanceStore;
1098
+ let mockEmbeddingEngine: EmbeddingEngine;
1099
+ let searchService: SearchService;
1100
+ const storeId = createStoreId('test-store');
1101
+
1102
+ beforeEach(() => {
1103
+ mockLanceStore = {
1104
+ search: vi.fn(),
1105
+ fullTextSearch: vi.fn(),
1106
+ } as unknown as LanceStore;
1107
+
1108
+ mockEmbeddingEngine = {
1109
+ embed: vi.fn().mockResolvedValue([0.1, 0.2, 0.3]),
1110
+ } as unknown as EmbeddingEngine;
1111
+
1112
+ searchService = new SearchService(mockLanceStore, mockEmbeddingEngine);
1113
+ });
1114
+
1115
+ it('boosts results when path contains query keywords', async () => {
1116
+ // Two files with same base score, but one has "dispatcher" in the path
1117
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1118
+ {
1119
+ id: createDocumentId('generic-file'),
1120
+ score: 0.85,
1121
+ content: 'handles async operations',
1122
+ metadata: {
1123
+ type: 'file' as const,
1124
+ storeId,
1125
+ indexedAt: new Date(),
1126
+ path: '/src/utils/helpers.py'
1127
+ }
1128
+ },
1129
+ {
1130
+ id: createDocumentId('dispatcher-file'),
1131
+ score: 0.85,
1132
+ content: 'handles async operations',
1133
+ metadata: {
1134
+ type: 'file' as const,
1135
+ storeId,
1136
+ indexedAt: new Date(),
1137
+ path: '/src/async_dispatcher.py'
1138
+ }
1139
+ },
1140
+ ]);
1141
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1142
+
1143
+ const results = await searchService.search({
1144
+ query: 'dispatcher',
1145
+ stores: [storeId],
1146
+ mode: 'hybrid',
1147
+ limit: 10,
1148
+ });
1149
+
1150
+ // dispatcher-file should rank higher due to path keyword match
1151
+ expect(results.results[0]?.id).toBe(createDocumentId('dispatcher-file'));
1152
+ });
1153
+
1154
+ it('boosts results with multiple path keyword matches', async () => {
1155
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1156
+ {
1157
+ id: createDocumentId('single-match'),
1158
+ score: 0.85,
1159
+ content: 'crawler implementation',
1160
+ metadata: {
1161
+ type: 'file' as const,
1162
+ storeId,
1163
+ indexedAt: new Date(),
1164
+ path: '/src/crawler.py'
1165
+ }
1166
+ },
1167
+ {
1168
+ id: createDocumentId('double-match'),
1169
+ score: 0.85,
1170
+ content: 'crawler implementation',
1171
+ metadata: {
1172
+ type: 'file' as const,
1173
+ storeId,
1174
+ indexedAt: new Date(),
1175
+ path: '/src/deep_crawling/crawler.py'
1176
+ }
1177
+ },
1178
+ ]);
1179
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1180
+
1181
+ const results = await searchService.search({
1182
+ query: 'deep crawler',
1183
+ stores: [storeId],
1184
+ mode: 'hybrid',
1185
+ limit: 10,
1186
+ });
1187
+
1188
+ // double-match should rank higher (both "deep" and "crawler" in path)
1189
+ expect(results.results[0]?.id).toBe(createDocumentId('double-match'));
1190
+ });
1191
+
1192
+ it('ignores stop words when matching path keywords', async () => {
1193
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1194
+ {
1195
+ id: createDocumentId('doc1'),
1196
+ score: 0.85,
1197
+ content: 'configuration guide',
1198
+ metadata: {
1199
+ type: 'file' as const,
1200
+ storeId,
1201
+ indexedAt: new Date(),
1202
+ path: '/src/config.ts'
1203
+ }
1204
+ },
1205
+ ]);
1206
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1207
+
1208
+ const results = await searchService.search({
1209
+ query: 'how to configure',
1210
+ stores: [storeId],
1211
+ mode: 'hybrid',
1212
+ limit: 10,
1213
+ });
1214
+
1215
+ // Should match "config" from "configure", not boost from "how" or "to"
1216
+ expect(results.results.length).toBeGreaterThan(0);
1217
+ });
1218
+
1219
+ it('does not boost when path has no matching keywords', async () => {
1220
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1221
+ {
1222
+ id: createDocumentId('unrelated-path'),
1223
+ score: 0.9,
1224
+ content: 'dispatcher implementation details',
1225
+ metadata: {
1226
+ type: 'file' as const,
1227
+ storeId,
1228
+ indexedAt: new Date(),
1229
+ path: '/src/utils/helpers.ts'
1230
+ }
1231
+ },
1232
+ ]);
1233
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1234
+
1235
+ const results = await searchService.search({
1236
+ query: 'dispatcher',
1237
+ stores: [storeId],
1238
+ mode: 'hybrid',
1239
+ limit: 10,
1240
+ });
1241
+
1242
+ // Should still return result (content matches), just no path boost
1243
+ expect(results.results.length).toBe(1);
1244
+ });
1245
+ });
1246
+
1247
+ describe('SearchService - Code Graph Integration', () => {
1248
+ let mockLanceStore: LanceStore;
1249
+ let mockEmbeddingEngine: EmbeddingEngine;
1250
+ let mockCodeGraphService: { loadGraph: ReturnType<typeof vi.fn> };
1251
+ let searchService: SearchService;
1252
+ const storeId = createStoreId('test-store');
1253
+
1254
+ beforeEach(() => {
1255
+ mockLanceStore = {
1256
+ search: vi.fn(),
1257
+ fullTextSearch: vi.fn(),
1258
+ } as unknown as LanceStore;
1259
+
1260
+ mockEmbeddingEngine = {
1261
+ embed: vi.fn().mockResolvedValue([0.1, 0.2, 0.3]),
1262
+ } as unknown as EmbeddingEngine;
1263
+
1264
+ mockCodeGraphService = {
1265
+ loadGraph: vi.fn(),
1266
+ };
1267
+
1268
+ // Create SearchService with mock codeGraphService
1269
+ searchService = new SearchService(
1270
+ mockLanceStore,
1271
+ mockEmbeddingEngine,
1272
+ mockCodeGraphService as unknown as import('./code-graph.service.js').CodeGraphService
1273
+ );
1274
+ });
1275
+
1276
+ it('includes usage stats from code graph when detail is contextual', async () => {
1277
+ // Create a mock CodeGraph with getCalledByCount and getCallsCount methods
1278
+ const mockGraph = {
1279
+ getCalledByCount: vi.fn().mockReturnValue(3),
1280
+ getCallsCount: vi.fn().mockReturnValue(5),
1281
+ getIncomingEdges: vi.fn().mockReturnValue([]),
1282
+ getEdges: vi.fn().mockReturnValue([]),
1283
+ };
1284
+
1285
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1286
+
1287
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1288
+ {
1289
+ id: createDocumentId('doc1'),
1290
+ score: 0.9,
1291
+ content: 'export function myFunction() { return 42; }',
1292
+ metadata: {
1293
+ type: 'file' as const,
1294
+ storeId,
1295
+ indexedAt: new Date(),
1296
+ path: '/src/utils.ts'
1297
+ }
1298
+ },
1299
+ ]);
1300
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1301
+
1302
+ const results = await searchService.search({
1303
+ query: 'myFunction',
1304
+ stores: [storeId],
1305
+ mode: 'vector',
1306
+ limit: 10,
1307
+ detail: 'contextual',
1308
+ });
1309
+
1310
+ expect(results.results.length).toBe(1);
1311
+ expect(results.results[0]?.context).toBeDefined();
1312
+ expect(results.results[0]?.context?.usage).toBeDefined();
1313
+ expect(results.results[0]?.context?.usage?.calledBy).toBe(3);
1314
+ expect(results.results[0]?.context?.usage?.calls).toBe(5);
1315
+ expect(mockGraph.getCalledByCount).toHaveBeenCalledWith('/src/utils.ts:myFunction');
1316
+ expect(mockGraph.getCallsCount).toHaveBeenCalledWith('/src/utils.ts:myFunction');
1317
+ });
1318
+
1319
+ it('returns zero usage stats when symbol is anonymous', async () => {
1320
+ const mockGraph = {
1321
+ getCalledByCount: vi.fn(),
1322
+ getCallsCount: vi.fn(),
1323
+ getIncomingEdges: vi.fn().mockReturnValue([]),
1324
+ getEdges: vi.fn().mockReturnValue([]),
1325
+ };
1326
+
1327
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1328
+
1329
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1330
+ {
1331
+ id: createDocumentId('doc1'),
1332
+ score: 0.9,
1333
+ // Content that matches no symbol patterns (no function/class/const keywords followed by identifiers)
1334
+ content: 'This is just plain documentation text with no code symbols at all.',
1335
+ metadata: {
1336
+ type: 'file' as const,
1337
+ storeId,
1338
+ indexedAt: new Date(),
1339
+ path: '/src/readme.md'
1340
+ }
1341
+ },
1342
+ ]);
1343
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1344
+
1345
+ const results = await searchService.search({
1346
+ query: 'documentation text',
1347
+ stores: [storeId],
1348
+ mode: 'vector',
1349
+ limit: 10,
1350
+ detail: 'contextual',
1351
+ });
1352
+
1353
+ expect(results.results.length).toBe(1);
1354
+ expect(results.results[0]?.context?.usage?.calledBy).toBe(0);
1355
+ expect(results.results[0]?.context?.usage?.calls).toBe(0);
1356
+ // Graph methods should NOT be called for anonymous symbols
1357
+ expect(mockGraph.getCalledByCount).not.toHaveBeenCalled();
1358
+ expect(mockGraph.getCallsCount).not.toHaveBeenCalled();
1359
+ });
1360
+
1361
+ it('returns zero usage stats when symbol is empty', async () => {
1362
+ const mockGraph = {
1363
+ getCalledByCount: vi.fn(),
1364
+ getCallsCount: vi.fn(),
1365
+ getIncomingEdges: vi.fn().mockReturnValue([]),
1366
+ getEdges: vi.fn().mockReturnValue([]),
1367
+ };
1368
+
1369
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1370
+
1371
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1372
+ {
1373
+ id: createDocumentId('doc1'),
1374
+ score: 0.9,
1375
+ content: ' \n\n ', // whitespace only content
1376
+ metadata: {
1377
+ type: 'file' as const,
1378
+ storeId,
1379
+ indexedAt: new Date(),
1380
+ path: '/src/empty.ts'
1381
+ }
1382
+ },
1383
+ ]);
1384
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1385
+
1386
+ const results = await searchService.search({
1387
+ query: 'empty content',
1388
+ stores: [storeId],
1389
+ mode: 'vector',
1390
+ limit: 10,
1391
+ detail: 'contextual',
1392
+ });
1393
+
1394
+ // Graph methods should NOT be called for symbols that can't be extracted
1395
+ expect(mockGraph.getCalledByCount).not.toHaveBeenCalled();
1396
+ });
1397
+
1398
+ it('includes related code from graph when detail is full', async () => {
1399
+ const mockGraph = {
1400
+ getCalledByCount: vi.fn().mockReturnValue(2),
1401
+ getCallsCount: vi.fn().mockReturnValue(1),
1402
+ getIncomingEdges: vi.fn().mockReturnValue([
1403
+ { from: '/src/caller.ts:callerFunction', to: '/src/utils.ts:myFunction', type: 'calls', confidence: 0.8 },
1404
+ { from: '/src/main.ts:init', to: '/src/utils.ts:myFunction', type: 'calls', confidence: 0.9 },
1405
+ ]),
1406
+ getEdges: vi.fn().mockReturnValue([
1407
+ { from: '/src/utils.ts:myFunction', to: '/src/helper.ts:helperFn', type: 'calls', confidence: 0.8 },
1408
+ ]),
1409
+ };
1410
+
1411
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1412
+
1413
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1414
+ {
1415
+ id: createDocumentId('doc1'),
1416
+ score: 0.9,
1417
+ content: '/** My function does stuff */\nexport function myFunction() { return helperFn(); }',
1418
+ metadata: {
1419
+ type: 'file' as const,
1420
+ storeId,
1421
+ indexedAt: new Date(),
1422
+ path: '/src/utils.ts'
1423
+ }
1424
+ },
1425
+ ]);
1426
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1427
+
1428
+ const results = await searchService.search({
1429
+ query: 'myFunction',
1430
+ stores: [storeId],
1431
+ mode: 'vector',
1432
+ limit: 10,
1433
+ detail: 'full',
1434
+ });
1435
+
1436
+ expect(results.results.length).toBe(1);
1437
+ expect(results.results[0]?.full).toBeDefined();
1438
+ expect(results.results[0]?.full?.relatedCode).toBeDefined();
1439
+ expect(results.results[0]?.full?.relatedCode?.length).toBe(3);
1440
+
1441
+ // Check incoming (callers)
1442
+ const callers = results.results[0]?.full?.relatedCode?.filter(r => r.relationship === 'calls this');
1443
+ expect(callers?.length).toBe(2);
1444
+ expect(callers?.some(c => c.file === '/src/caller.ts' && c.summary === 'callerFunction()')).toBe(true);
1445
+ expect(callers?.some(c => c.file === '/src/main.ts' && c.summary === 'init()')).toBe(true);
1446
+
1447
+ // Check outgoing (callees)
1448
+ const callees = results.results[0]?.full?.relatedCode?.filter(r => r.relationship === 'called by this');
1449
+ expect(callees?.length).toBe(1);
1450
+ expect(callees?.[0]?.file).toBe('/src/helper.ts');
1451
+ expect(callees?.[0]?.summary).toBe('helperFn()');
1452
+ });
1453
+
1454
+ it('returns empty related code for anonymous symbols', async () => {
1455
+ const mockGraph = {
1456
+ getCalledByCount: vi.fn(),
1457
+ getCallsCount: vi.fn(),
1458
+ getIncomingEdges: vi.fn(),
1459
+ getEdges: vi.fn(),
1460
+ };
1461
+
1462
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1463
+
1464
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1465
+ {
1466
+ id: createDocumentId('doc1'),
1467
+ score: 0.9,
1468
+ content: 'Just plain text without code symbols',
1469
+ metadata: {
1470
+ type: 'file' as const,
1471
+ storeId,
1472
+ indexedAt: new Date(),
1473
+ path: '/src/notes.txt'
1474
+ }
1475
+ },
1476
+ ]);
1477
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1478
+
1479
+ const results = await searchService.search({
1480
+ query: 'notes',
1481
+ stores: [storeId],
1482
+ mode: 'vector',
1483
+ limit: 10,
1484
+ detail: 'full',
1485
+ });
1486
+
1487
+ expect(results.results.length).toBe(1);
1488
+ expect(results.results[0]?.full?.relatedCode).toEqual([]);
1489
+ // Graph methods should NOT be called for anonymous symbols
1490
+ expect(mockGraph.getIncomingEdges).not.toHaveBeenCalled();
1491
+ expect(mockGraph.getEdges).not.toHaveBeenCalled();
1492
+ });
1493
+
1494
+ it('handles edges with non-calls type gracefully', async () => {
1495
+ const mockGraph = {
1496
+ getCalledByCount: vi.fn().mockReturnValue(0),
1497
+ getCallsCount: vi.fn().mockReturnValue(0),
1498
+ getIncomingEdges: vi.fn().mockReturnValue([
1499
+ { from: '/src/index.ts', to: '/src/utils.ts:myFunction', type: 'imports', confidence: 1.0 },
1500
+ ]),
1501
+ getEdges: vi.fn().mockReturnValue([
1502
+ { from: '/src/utils.ts:myFunction', to: '/src/types.ts:MyInterface', type: 'implements', confidence: 1.0 },
1503
+ ]),
1504
+ };
1505
+
1506
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1507
+
1508
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1509
+ {
1510
+ id: createDocumentId('doc1'),
1511
+ score: 0.9,
1512
+ content: 'export function myFunction() { return 42; }',
1513
+ metadata: {
1514
+ type: 'file' as const,
1515
+ storeId,
1516
+ indexedAt: new Date(),
1517
+ path: '/src/utils.ts'
1518
+ }
1519
+ },
1520
+ ]);
1521
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1522
+
1523
+ const results = await searchService.search({
1524
+ query: 'myFunction',
1525
+ stores: [storeId],
1526
+ mode: 'vector',
1527
+ limit: 10,
1528
+ detail: 'full',
1529
+ });
1530
+
1531
+ expect(results.results.length).toBe(1);
1532
+ // No related code should be returned because edges are not 'calls' type
1533
+ expect(results.results[0]?.full?.relatedCode).toEqual([]);
1534
+ });
1535
+
1536
+ it('parses node IDs without colons correctly', async () => {
1537
+ const mockGraph = {
1538
+ getCalledByCount: vi.fn().mockReturnValue(1),
1539
+ getCallsCount: vi.fn().mockReturnValue(0),
1540
+ getIncomingEdges: vi.fn().mockReturnValue([
1541
+ // Edge with nodeId that has no colon (edge case)
1542
+ { from: 'simpleNodeId', to: '/src/utils.ts:myFunction', type: 'calls', confidence: 0.8 },
1543
+ ]),
1544
+ getEdges: vi.fn().mockReturnValue([]),
1545
+ };
1546
+
1547
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1548
+
1549
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1550
+ {
1551
+ id: createDocumentId('doc1'),
1552
+ score: 0.9,
1553
+ content: 'export function myFunction() { return 42; }',
1554
+ metadata: {
1555
+ type: 'file' as const,
1556
+ storeId,
1557
+ indexedAt: new Date(),
1558
+ path: '/src/utils.ts'
1559
+ }
1560
+ },
1561
+ ]);
1562
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1563
+
1564
+ const results = await searchService.search({
1565
+ query: 'myFunction',
1566
+ stores: [storeId],
1567
+ mode: 'vector',
1568
+ limit: 10,
1569
+ detail: 'full',
1570
+ });
1571
+
1572
+ expect(results.results.length).toBe(1);
1573
+ const callers = results.results[0]?.full?.relatedCode?.filter(r => r.relationship === 'calls this');
1574
+ expect(callers?.length).toBe(1);
1575
+ // When nodeId has no colon, file should be the whole nodeId and symbol should be empty -> 'unknown'
1576
+ expect(callers?.[0]?.file).toBe('simpleNodeId');
1577
+ expect(callers?.[0]?.summary).toBe('unknown');
1578
+ });
1579
+
1580
+ it('handles null graph gracefully', async () => {
1581
+ mockCodeGraphService.loadGraph.mockResolvedValue(null);
1582
+
1583
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1584
+ {
1585
+ id: createDocumentId('doc1'),
1586
+ score: 0.9,
1587
+ content: 'export function myFunction() { return 42; }',
1588
+ metadata: {
1589
+ type: 'file' as const,
1590
+ storeId,
1591
+ indexedAt: new Date(),
1592
+ path: '/src/utils.ts'
1593
+ }
1594
+ },
1595
+ ]);
1596
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1597
+
1598
+ const results = await searchService.search({
1599
+ query: 'myFunction',
1600
+ stores: [storeId],
1601
+ mode: 'vector',
1602
+ limit: 10,
1603
+ detail: 'full',
1604
+ });
1605
+
1606
+ expect(results.results.length).toBe(1);
1607
+ expect(results.results[0]?.context?.usage?.calledBy).toBe(0);
1608
+ expect(results.results[0]?.context?.usage?.calls).toBe(0);
1609
+ expect(results.results[0]?.full?.relatedCode).toEqual([]);
1610
+ });
1611
+
1612
+ it('limits related code to 10 items', async () => {
1613
+ // Create 15 incoming edges
1614
+ const manyIncomingEdges = Array.from({ length: 15 }, (_, i) => ({
1615
+ from: `/src/file${i}.ts:func${i}`,
1616
+ to: '/src/utils.ts:myFunction',
1617
+ type: 'calls' as const,
1618
+ confidence: 0.8
1619
+ }));
1620
+
1621
+ const mockGraph = {
1622
+ getCalledByCount: vi.fn().mockReturnValue(15),
1623
+ getCallsCount: vi.fn().mockReturnValue(0),
1624
+ getIncomingEdges: vi.fn().mockReturnValue(manyIncomingEdges),
1625
+ getEdges: vi.fn().mockReturnValue([]),
1626
+ };
1627
+
1628
+ mockCodeGraphService.loadGraph.mockResolvedValue(mockGraph);
1629
+
1630
+ vi.mocked(mockLanceStore.search).mockResolvedValue([
1631
+ {
1632
+ id: createDocumentId('doc1'),
1633
+ score: 0.9,
1634
+ content: 'export function myFunction() { return 42; }',
1635
+ metadata: {
1636
+ type: 'file' as const,
1637
+ storeId,
1638
+ indexedAt: new Date(),
1639
+ path: '/src/utils.ts'
1640
+ }
1641
+ },
1642
+ ]);
1643
+ vi.mocked(mockLanceStore.fullTextSearch).mockResolvedValue([]);
1644
+
1645
+ const results = await searchService.search({
1646
+ query: 'myFunction',
1647
+ stores: [storeId],
1648
+ mode: 'vector',
1649
+ limit: 10,
1650
+ detail: 'full',
1651
+ });
1652
+
1653
+ expect(results.results.length).toBe(1);
1654
+ // Should be limited to 10 related items
1655
+ expect(results.results[0]?.full?.relatedCode?.length).toBe(10);
1656
+ });
1657
+ });