onbuzz 3.6.1 → 3.6.3

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 (84) hide show
  1. package/package.json +1 -1
  2. package/src/__test-utils__/fixtures/malformedJson.js +31 -0
  3. package/src/__test-utils__/globalSetup.js +9 -0
  4. package/src/__test-utils__/globalTeardown.js +12 -0
  5. package/src/__test-utils__/mockFactories.js +101 -0
  6. package/src/analyzers/__tests__/CSSAnalyzer.test.js +41 -0
  7. package/src/analyzers/__tests__/ConfigValidator.test.js +362 -0
  8. package/src/analyzers/__tests__/ESLintAnalyzer.test.js +271 -0
  9. package/src/analyzers/__tests__/JavaScriptAnalyzer.test.js +40 -0
  10. package/src/analyzers/__tests__/PrettierFormatter.test.js +197 -0
  11. package/src/analyzers/__tests__/PythonAnalyzer.test.js +208 -0
  12. package/src/analyzers/__tests__/SecurityAnalyzer.test.js +303 -0
  13. package/src/analyzers/__tests__/SparrowAnalyzer.test.js +270 -0
  14. package/src/analyzers/__tests__/TypeScriptAnalyzer.test.js +187 -0
  15. package/src/core/__tests__/agentPool.test.js +601 -0
  16. package/src/core/__tests__/agentScheduler.test.js +576 -0
  17. package/src/core/__tests__/contextManager.test.js +252 -0
  18. package/src/core/__tests__/flowExecutor.test.js +262 -0
  19. package/src/core/__tests__/messageProcessor.test.js +627 -0
  20. package/src/core/__tests__/orchestrator.test.js +257 -0
  21. package/src/core/__tests__/stateManager.test.js +375 -0
  22. package/src/core/agentPool.js +11 -1
  23. package/src/index.js +25 -9
  24. package/src/interfaces/terminal/__tests__/smoke/imports.test.js +3 -5
  25. package/src/services/__tests__/agentActivityService.test.js +319 -0
  26. package/src/services/__tests__/apiKeyManager.test.js +206 -0
  27. package/src/services/__tests__/benchmarkService.test.js +184 -0
  28. package/src/services/__tests__/budgetService.test.js +211 -0
  29. package/src/services/__tests__/contextInjectionService.test.js +205 -0
  30. package/src/services/__tests__/conversationCompactionService.test.js +280 -0
  31. package/src/services/__tests__/credentialVault.test.js +469 -0
  32. package/src/services/__tests__/errorHandler.test.js +314 -0
  33. package/src/services/__tests__/fileAttachmentService.test.js +278 -0
  34. package/src/services/__tests__/flowContextService.test.js +199 -0
  35. package/src/services/__tests__/memoryService.test.js +450 -0
  36. package/src/services/__tests__/modelRouterService.test.js +388 -0
  37. package/src/services/__tests__/modelsService.test.js +261 -0
  38. package/src/services/__tests__/portRegistry.test.js +123 -0
  39. package/src/services/__tests__/projectDetector.test.js +34 -0
  40. package/src/services/__tests__/promptService.test.js +242 -0
  41. package/src/services/__tests__/qualityInspector.test.js +97 -0
  42. package/src/services/__tests__/scheduleService.test.js +308 -0
  43. package/src/services/__tests__/serviceRegistry.test.js +74 -0
  44. package/src/services/__tests__/skillsService.test.js +402 -0
  45. package/src/services/__tests__/tokenCountingService.test.js +48 -0
  46. package/src/tools/__tests__/agentCommunicationTool.test.js +500 -0
  47. package/src/tools/__tests__/agentDelayTool.test.js +342 -0
  48. package/src/tools/__tests__/asyncToolManager.test.js +344 -0
  49. package/src/tools/__tests__/baseTool.test.js +420 -0
  50. package/src/tools/__tests__/codeMapTool.test.js +348 -0
  51. package/src/tools/__tests__/fileContentReplaceTool.test.js +309 -0
  52. package/src/tools/__tests__/fileSystemTool.test.js +717 -0
  53. package/src/tools/__tests__/fileTreeTool.test.js +274 -0
  54. package/src/tools/__tests__/helpTool.test.js +204 -0
  55. package/src/tools/__tests__/jobDoneTool.test.js +296 -0
  56. package/src/tools/__tests__/memoryTool.test.js +297 -0
  57. package/src/tools/__tests__/seekTool.test.js +282 -0
  58. package/src/tools/__tests__/skillsTool.test.js +226 -0
  59. package/src/tools/__tests__/staticAnalysisTool.test.js +509 -0
  60. package/src/tools/__tests__/taskManagerTool.test.js +725 -0
  61. package/src/tools/__tests__/terminalTool.test.js +384 -0
  62. package/src/tools/__tests__/userPromptTool.test.js +297 -0
  63. package/src/tools/__tests__/webTool.e2e.test.js +25 -11
  64. package/src/tools/webTool.js +6 -12
  65. package/src/types/__tests__/agent.test.js +499 -0
  66. package/src/types/__tests__/contextReference.test.js +606 -0
  67. package/src/types/__tests__/conversation.test.js +555 -0
  68. package/src/types/__tests__/toolCommand.test.js +584 -0
  69. package/src/types/contextReference.js +1 -1
  70. package/src/utilities/__tests__/attachmentValidator.test.js +80 -0
  71. package/src/utilities/__tests__/configManager.test.js +397 -0
  72. package/src/utilities/__tests__/constants.test.js +49 -0
  73. package/src/utilities/__tests__/directoryAccessManager.test.js +388 -0
  74. package/src/utilities/__tests__/fileProcessor.test.js +104 -0
  75. package/src/utilities/__tests__/jsonRepair.test.js +104 -0
  76. package/src/utilities/__tests__/logger.test.js +129 -0
  77. package/src/utilities/__tests__/platformUtils.test.js +87 -0
  78. package/src/utilities/__tests__/structuredFileValidator.test.js +263 -0
  79. package/src/utilities/__tests__/tagParser.test.js +887 -0
  80. package/src/utilities/__tests__/toolConstants.test.js +94 -0
  81. package/src/utilities/tagParser.js +2 -2
  82. package/src/tools/browserTool.js +0 -897
  83. package/src/utilities/platformUtils.test.js +0 -98
  84. /package/src/tools/{filesystemTool.js → fileSystemTool.js} +0 -0
@@ -0,0 +1,606 @@
1
+ import { jest, describe, test, expect, beforeEach } from '@jest/globals';
2
+ import { ContextReferenceValidator, ContextReferenceFactory, ContextReferenceUtils } from '../contextReference.js';
3
+
4
+ describe('ContextReferenceFactory', () => {
5
+ test('create returns reference with type, path, name, and id', () => {
6
+ const ref = ContextReferenceFactory.create('file', '/src/index.js', 'index.js');
7
+ expect(ref).toBeDefined();
8
+ expect(typeof ref.id).toBe('string');
9
+ expect(ref.id).toMatch(/^ref_/);
10
+ expect(ref.type).toBe('file');
11
+ expect(ref.path).toBe('/src/index.js');
12
+ expect(ref.name).toBe('index.js');
13
+ expect(ref.isValid).toBe(true);
14
+ expect(ref.accessCount).toBe(0);
15
+ expect(ref.lastAccessed).toBeNull();
16
+ });
17
+
18
+ test('createFileReference returns file reference with absolutePath', () => {
19
+ const ref = ContextReferenceFactory.createFileReference('/home/user/src/app.js', 'src/app.js');
20
+ expect(ref.absolutePath).toBe('/home/user/src/app.js');
21
+ expect(ref.relativePath).toBe('src/app.js');
22
+ expect(ref.type).toBe('file');
23
+ expect(ref.extension).toBe('.js');
24
+ expect(ref.exists).toBe(true);
25
+ });
26
+
27
+ test('createFileReference with exists=false', () => {
28
+ const ref = ContextReferenceFactory.createFileReference('/a.js', 'a.js', { exists: false });
29
+ expect(ref.exists).toBe(false);
30
+ });
31
+
32
+ test('createFileReference extracts name from path', () => {
33
+ const ref = ContextReferenceFactory.createFileReference('/home/user/src/app.js', 'src/app.js');
34
+ expect(ref.name).toBe('app.js');
35
+ });
36
+
37
+ test('createFileReference uses provided name', () => {
38
+ const ref = ContextReferenceFactory.createFileReference('/a.js', 'a.js', { name: 'Custom Name' });
39
+ expect(ref.name).toBe('Custom Name');
40
+ });
41
+
42
+ describe('createSelectionReference', () => {
43
+ test('creates selection reference with scope', () => {
44
+ const ref = ContextReferenceFactory.createSelectionReference(
45
+ '/src/app.js',
46
+ 'const x = 1;',
47
+ { startLine: 10, endLine: 15 }
48
+ );
49
+ expect(ref.type).toBe('selection');
50
+ expect(ref.sourceFile).toBe('/src/app.js');
51
+ expect(ref.selectedText).toBe('const x = 1;');
52
+ expect(ref.scope.startLine).toBe(10);
53
+ expect(ref.scope.endLine).toBe(15);
54
+ });
55
+
56
+ test('generates name from file and scope', () => {
57
+ const ref = ContextReferenceFactory.createSelectionReference(
58
+ '/src/app.js',
59
+ 'text',
60
+ { startLine: 10, endLine: 20 }
61
+ );
62
+ expect(ref.name).toBe('app.js:10-20');
63
+ });
64
+
65
+ test('generates single-line name when start equals end', () => {
66
+ const ref = ContextReferenceFactory.createSelectionReference(
67
+ '/src/app.js',
68
+ 'text',
69
+ { startLine: 10, endLine: 10 }
70
+ );
71
+ expect(ref.name).toBe('app.js:10');
72
+ });
73
+
74
+ test('sets content from selectedText', () => {
75
+ const ref = ContextReferenceFactory.createSelectionReference(
76
+ '/src/app.js',
77
+ 'selected text here',
78
+ { startLine: 1, endLine: 1 }
79
+ );
80
+ expect(ref.content).toBe('selected text here');
81
+ });
82
+
83
+ test('includes optional fields', () => {
84
+ const ref = ContextReferenceFactory.createSelectionReference(
85
+ '/src/app.js',
86
+ 'text',
87
+ { startLine: 1, endLine: 1 },
88
+ { purpose: 'review', syntax: { language: 'javascript' } }
89
+ );
90
+ expect(ref.purpose).toBe('review');
91
+ expect(ref.syntax.language).toBe('javascript');
92
+ });
93
+ });
94
+
95
+ describe('createDirectoryReference', () => {
96
+ test('creates directory reference', () => {
97
+ const ref = ContextReferenceFactory.createDirectoryReference('/home/user/src', 'src');
98
+ expect(ref.type).toBe('directory');
99
+ expect(ref.absolutePath).toBe('/home/user/src');
100
+ expect(ref.relativePath).toBe('src');
101
+ expect(ref.name).toBe('src');
102
+ });
103
+
104
+ test('includes optional directory fields', () => {
105
+ const ref = ContextReferenceFactory.createDirectoryReference('/src', 'src', {
106
+ fileCount: 10,
107
+ totalSize: 5000,
108
+ fileTypes: ['.js', '.ts']
109
+ });
110
+ expect(ref.fileCount).toBe(10);
111
+ expect(ref.totalSize).toBe(5000);
112
+ expect(ref.fileTypes).toEqual(['.js', '.ts']);
113
+ });
114
+
115
+ test('extracts directory name from path', () => {
116
+ const ref = ContextReferenceFactory.createDirectoryReference('/home/user/project/src', 'src');
117
+ expect(ref.name).toBe('src');
118
+ });
119
+ });
120
+
121
+ describe('createComponentReference', () => {
122
+ test('creates component reference', () => {
123
+ const ref = ContextReferenceFactory.createComponentReference(
124
+ 'React', '/src/Button.jsx', 'Button'
125
+ );
126
+ expect(ref.type).toBe('component');
127
+ expect(ref.componentType).toBe('React');
128
+ expect(ref.name).toBe('Button');
129
+ });
130
+
131
+ test('includes optional fields', () => {
132
+ const ref = ContextReferenceFactory.createComponentReference(
133
+ 'React', '/src/Button.jsx', 'Button',
134
+ {
135
+ sourceFile: '/src/Button.jsx',
136
+ properties: { color: 'string' },
137
+ dependencies: ['React'],
138
+ documentation: 'A button component'
139
+ }
140
+ );
141
+ expect(ref.sourceFile).toBe('/src/Button.jsx');
142
+ expect(ref.properties).toEqual({ color: 'string' });
143
+ expect(ref.dependencies).toEqual(['React']);
144
+ expect(ref.documentation).toBe('A button component');
145
+ });
146
+ });
147
+
148
+ test('generateReferenceId returns unique strings', () => {
149
+ const id1 = ContextReferenceFactory.generateReferenceId();
150
+ const id2 = ContextReferenceFactory.generateReferenceId();
151
+ expect(typeof id1).toBe('string');
152
+ expect(typeof id2).toBe('string');
153
+ expect(id1).not.toBe(id2);
154
+ });
155
+
156
+ test('getLanguageFromExtension returns javascript for .js', () => {
157
+ expect(ContextReferenceFactory.getLanguageFromExtension('.js')).toBe('javascript');
158
+ });
159
+
160
+ test('getLanguageFromExtension returns null for unknown extension', () => {
161
+ expect(ContextReferenceFactory.getLanguageFromExtension('.xyz')).toBeNull();
162
+ });
163
+
164
+ test('getLanguageFromExtension is case-insensitive', () => {
165
+ expect(ContextReferenceFactory.getLanguageFromExtension('.JS')).toBe('javascript');
166
+ });
167
+
168
+ test('getMimeTypeFromExtension returns correct types', () => {
169
+ expect(ContextReferenceFactory.getMimeTypeFromExtension('.js')).toBe('application/javascript');
170
+ expect(ContextReferenceFactory.getMimeTypeFromExtension('.html')).toBe('text/html');
171
+ });
172
+
173
+ test('getMimeTypeFromExtension returns text/plain for unknown', () => {
174
+ expect(ContextReferenceFactory.getMimeTypeFromExtension('.xyz')).toBe('text/plain');
175
+ });
176
+
177
+ test('extractFileName returns filename from path', () => {
178
+ expect(ContextReferenceFactory.extractFileName('/home/user/src/app.js')).toBe('app.js');
179
+ expect(ContextReferenceFactory.extractFileName('app.js')).toBe('app.js');
180
+ });
181
+
182
+ test('extractDirectoryName returns last directory segment', () => {
183
+ expect(ContextReferenceFactory.extractDirectoryName('/home/user/src')).toBe('src');
184
+ expect(ContextReferenceFactory.extractDirectoryName('/')).toBe('Root');
185
+ });
186
+
187
+ test('extractFileExtension returns extension', () => {
188
+ expect(ContextReferenceFactory.extractFileExtension('/src/app.js')).toBe('.js');
189
+ expect(ContextReferenceFactory.extractFileExtension('/src/noext')).toBe('');
190
+ });
191
+
192
+ test('generateSelectionName handles functionName scope', () => {
193
+ const name = ContextReferenceFactory.generateSelectionName('/src/app.js', { functionName: 'handleClick' });
194
+ expect(name).toBe('app.js:handleClick()');
195
+ });
196
+
197
+ test('generateSelectionName handles className scope', () => {
198
+ const name = ContextReferenceFactory.generateSelectionName('/src/app.js', { className: 'MyComponent' });
199
+ expect(name).toBe('app.js:MyComponent');
200
+ });
201
+
202
+ test('generateSelectionName handles no scope details', () => {
203
+ const name = ContextReferenceFactory.generateSelectionName('/src/app.js', {});
204
+ expect(name).toBe('app.js (selection)');
205
+ });
206
+ });
207
+
208
+ describe('ContextReferenceValidator', () => {
209
+ test('validate accepts valid reference', () => {
210
+ const ref = ContextReferenceFactory.create('file', '/src/index.js', 'index.js', {
211
+ metadata: { size: 100 }
212
+ });
213
+ const result = ContextReferenceValidator.validate(ref);
214
+ expect(result.isValid).toBe(true);
215
+ expect(result.errors).toHaveLength(0);
216
+ });
217
+
218
+ test('validate rejects missing required fields', () => {
219
+ const result = ContextReferenceValidator.validate({});
220
+ expect(result.isValid).toBe(false);
221
+ expect(result.errors.some(e => e.includes('Reference ID'))).toBe(true);
222
+ expect(result.errors.some(e => e.includes('Reference type'))).toBe(true);
223
+ expect(result.errors.some(e => e.includes('Reference path'))).toBe(true);
224
+ expect(result.errors.some(e => e.includes('Reference name'))).toBe(true);
225
+ });
226
+
227
+ test('validate rejects invalid type', () => {
228
+ const ref = ContextReferenceFactory.create('file', '/src/a.js', 'a.js');
229
+ ref.type = 'invalid-type';
230
+ const result = ContextReferenceValidator.validate(ref);
231
+ expect(result.errors.some(e => e.includes('Invalid reference type'))).toBe(true);
232
+ });
233
+
234
+ test('validate rejects non-string content', () => {
235
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
236
+ ref.content = 123;
237
+ const result = ContextReferenceValidator.validate(ref);
238
+ expect(result.errors.some(e => e.includes('content must be a string'))).toBe(true);
239
+ });
240
+
241
+ test('validate warns on very large content', () => {
242
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
243
+ ref.content = 'x'.repeat(1000001);
244
+ const result = ContextReferenceValidator.validate(ref);
245
+ expect(result.warnings.some(w => w.includes('very large'))).toBe(true);
246
+ });
247
+
248
+ test('validate rejects non-number accessCount', () => {
249
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
250
+ ref.accessCount = 'many';
251
+ const result = ContextReferenceValidator.validate(ref);
252
+ expect(result.errors.some(e => e.includes('Access count'))).toBe(true);
253
+ });
254
+
255
+ test('validate validates file references', () => {
256
+ const ref = ContextReferenceFactory.createFileReference('/a.js', 'a.js');
257
+ ref.absolutePath = 123; // invalid
258
+ const result = ContextReferenceValidator.validate(ref);
259
+ expect(result.errors.some(e => e.includes('Absolute path'))).toBe(true);
260
+ });
261
+
262
+ test('validate validates selection references', () => {
263
+ const ref = ContextReferenceFactory.createSelectionReference('/a.js', 'text', {});
264
+ const result = ContextReferenceValidator.validate(ref);
265
+ // Should warn about missing line scope
266
+ expect(result.warnings.some(w => w.includes('line scope'))).toBe(true);
267
+ });
268
+
269
+ test('validate validates directory references', () => {
270
+ const ref = ContextReferenceFactory.createDirectoryReference('/src', 'src');
271
+ ref.fileCount = -1;
272
+ const result = ContextReferenceValidator.validate(ref);
273
+ expect(result.errors.some(e => e.includes('File count'))).toBe(true);
274
+ });
275
+
276
+ describe('validateScope', () => {
277
+ test('rejects negative line numbers', () => {
278
+ const result = ContextReferenceValidator.validateScope({ startLine: -1 });
279
+ expect(result.errors.some(e => e.includes('startLine'))).toBe(true);
280
+ });
281
+
282
+ test('rejects startLine > endLine', () => {
283
+ const result = ContextReferenceValidator.validateScope({ startLine: 20, endLine: 10 });
284
+ expect(result.errors.some(e => e.includes('Start line must be'))).toBe(true);
285
+ });
286
+
287
+ test('rejects startColumn > endColumn', () => {
288
+ const result = ContextReferenceValidator.validateScope({ startColumn: 20, endColumn: 10 });
289
+ expect(result.errors.some(e => e.includes('Start column must be'))).toBe(true);
290
+ });
291
+ });
292
+ });
293
+
294
+ describe('ContextReferenceUtils', () => {
295
+ test('isValid returns true for valid reference', () => {
296
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
297
+ expect(ContextReferenceUtils.isValid(ref)).toBe(true);
298
+ });
299
+
300
+ test('isValid returns false for invalid reference', () => {
301
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
302
+ ref.isValid = false;
303
+ ref.invalidReason = 'File deleted';
304
+ expect(ContextReferenceUtils.isValid(ref)).toBe(false);
305
+ });
306
+
307
+ describe('markAccessed', () => {
308
+ test('updates lastAccessed and increments accessCount', () => {
309
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
310
+ const updated = ContextReferenceUtils.markAccessed(ref);
311
+ expect(updated.lastAccessed).toBeDefined();
312
+ expect(updated.accessCount).toBe(1);
313
+ // Original should not be modified
314
+ expect(ref.accessCount).toBe(0);
315
+ });
316
+
317
+ test('increments accessCount on subsequent access', () => {
318
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
319
+ const first = ContextReferenceUtils.markAccessed(ref);
320
+ const second = ContextReferenceUtils.markAccessed(first);
321
+ expect(second.accessCount).toBe(2);
322
+ });
323
+ });
324
+
325
+ describe('markInvalid', () => {
326
+ test('sets isValid to false and adds reason', () => {
327
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
328
+ const invalid = ContextReferenceUtils.markInvalid(ref, 'File was deleted');
329
+ expect(invalid.isValid).toBe(false);
330
+ expect(invalid.invalidReason).toBe('File was deleted');
331
+ // Original should not be modified
332
+ expect(ref.isValid).toBe(true);
333
+ });
334
+ });
335
+
336
+ describe('getDisplayName', () => {
337
+ test('returns name for file reference', () => {
338
+ const ref = ContextReferenceFactory.create('file', '/src/app.js', 'app.js');
339
+ expect(ContextReferenceUtils.getDisplayName(ref)).toBe('app.js');
340
+ });
341
+
342
+ test('returns generated name for selection reference with scope', () => {
343
+ const ref = ContextReferenceFactory.createSelectionReference('/src/app.js', 'text', { startLine: 10, endLine: 20 });
344
+ expect(ContextReferenceUtils.getDisplayName(ref)).toBe('app.js:10-20');
345
+ });
346
+ });
347
+
348
+ describe('getDescription', () => {
349
+ test('returns metadata description when available', () => {
350
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js', {
351
+ metadata: { description: 'Main entry point' }
352
+ });
353
+ expect(ContextReferenceUtils.getDescription(ref)).toBe('Main entry point');
354
+ });
355
+
356
+ test('returns type-specific description for file', () => {
357
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
358
+ expect(ContextReferenceUtils.getDescription(ref)).toBe('File: /a.js');
359
+ });
360
+
361
+ test('returns type-specific description for directory', () => {
362
+ const ref = ContextReferenceFactory.create('directory', '/src', 'src');
363
+ expect(ContextReferenceUtils.getDescription(ref)).toBe('Directory: /src');
364
+ });
365
+
366
+ test('returns type-specific description for selection', () => {
367
+ const ref = ContextReferenceFactory.create('selection', '/a.js', 'sel');
368
+ expect(ContextReferenceUtils.getDescription(ref)).toBe('Selection from /a.js');
369
+ });
370
+
371
+ test('returns type-specific description for component', () => {
372
+ const ref = ContextReferenceFactory.create('component', '/src/Button', 'Button');
373
+ expect(ContextReferenceUtils.getDescription(ref)).toBe('Component: Button');
374
+ });
375
+
376
+ test('returns path for unknown type', () => {
377
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
378
+ ref.type = 'unknown';
379
+ expect(ContextReferenceUtils.getDescription(ref)).toBe('/a.js');
380
+ });
381
+ });
382
+
383
+ describe('calculateRelevance', () => {
384
+ test('returns base score for minimal reference', () => {
385
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
386
+ const score = ContextReferenceUtils.calculateRelevance(ref);
387
+ expect(score).toBeGreaterThanOrEqual(0);
388
+ expect(score).toBeLessThanOrEqual(1);
389
+ });
390
+
391
+ test('gives bonus for recent access', () => {
392
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
393
+ ref.lastAccessed = new Date().toISOString();
394
+ const scoreAccessed = ContextReferenceUtils.calculateRelevance(ref);
395
+
396
+ const ref2 = ContextReferenceFactory.create('file', '/b.js', 'b.js');
397
+ const scoreNotAccessed = ContextReferenceUtils.calculateRelevance(ref2);
398
+
399
+ expect(scoreAccessed).toBeGreaterThan(scoreNotAccessed);
400
+ });
401
+
402
+ test('gives bonus for high access count', () => {
403
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
404
+ ref.accessCount = 20;
405
+ const scoreFrequent = ContextReferenceUtils.calculateRelevance(ref);
406
+
407
+ const ref2 = ContextReferenceFactory.create('file', '/b.js', 'b.js');
408
+ const scoreLow = ContextReferenceUtils.calculateRelevance(ref2);
409
+
410
+ expect(scoreFrequent).toBeGreaterThan(scoreLow);
411
+ });
412
+
413
+ test('gives bonus for matching file type', () => {
414
+ const ref = ContextReferenceFactory.createFileReference('/a.js', 'a.js');
415
+ const score = ContextReferenceUtils.calculateRelevance(ref, { fileTypes: ['javascript'] });
416
+ const scoreNoMatch = ContextReferenceUtils.calculateRelevance(ref, { fileTypes: ['python'] });
417
+ expect(score).toBeGreaterThan(scoreNoMatch);
418
+ });
419
+
420
+ test('gives bonus for selection type', () => {
421
+ const selRef = ContextReferenceFactory.createSelectionReference('/a.js', 'text', { startLine: 1, endLine: 1 });
422
+ const fileRef = ContextReferenceFactory.create('file', '/a.js', 'a.js');
423
+ const selScore = ContextReferenceUtils.calculateRelevance(selRef);
424
+ const fileScore = ContextReferenceUtils.calculateRelevance(fileRef);
425
+ expect(selScore).toBeGreaterThan(fileScore);
426
+ });
427
+
428
+ test('gives bonus for keyword matches', () => {
429
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js', {
430
+ metadata: { keywords: ['auth', 'login'] }
431
+ });
432
+ const scoreMatch = ContextReferenceUtils.calculateRelevance(ref, { keywords: ['auth'] });
433
+ const scoreNoMatch = ContextReferenceUtils.calculateRelevance(ref, { keywords: ['database'] });
434
+ expect(scoreMatch).toBeGreaterThan(scoreNoMatch);
435
+ });
436
+
437
+ test('penalizes invalid references', () => {
438
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
439
+ const validScore = ContextReferenceUtils.calculateRelevance(ref);
440
+ ref.isValid = false;
441
+ const invalidScore = ContextReferenceUtils.calculateRelevance(ref);
442
+ expect(invalidScore).toBeLessThan(validScore);
443
+ });
444
+
445
+ test('clamps score between 0 and 1', () => {
446
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
447
+ ref.accessCount = 1000;
448
+ ref.lastAccessed = new Date().toISOString();
449
+ const score = ContextReferenceUtils.calculateRelevance(ref, {
450
+ fileTypes: ['javascript'],
451
+ keywords: ['auth', 'login', 'session']
452
+ });
453
+ expect(score).toBeLessThanOrEqual(1);
454
+ expect(score).toBeGreaterThanOrEqual(0);
455
+ });
456
+ });
457
+
458
+ describe('groupByType', () => {
459
+ test('groups references correctly', () => {
460
+ const refs = [
461
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
462
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
463
+ ContextReferenceFactory.create('directory', '/src', 'src'),
464
+ ];
465
+ const grouped = ContextReferenceUtils.groupByType(refs);
466
+ expect(grouped['file']).toHaveLength(2);
467
+ expect(grouped['directory']).toHaveLength(1);
468
+ });
469
+ });
470
+
471
+ describe('sortByRelevance', () => {
472
+ test('sorts references by relevance score descending', () => {
473
+ const refs = [
474
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
475
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
476
+ ];
477
+ refs[1].accessCount = 50;
478
+ refs[1].lastAccessed = new Date().toISOString();
479
+
480
+ const sorted = ContextReferenceUtils.sortByRelevance(refs);
481
+ expect(sorted[0].path).toBe('/b.js');
482
+ });
483
+
484
+ test('does not modify original array', () => {
485
+ const refs = [
486
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
487
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
488
+ ];
489
+ refs[1].accessCount = 50;
490
+ const sorted = ContextReferenceUtils.sortByRelevance(refs);
491
+ expect(refs[0].path).toBe('/a.js'); // original unchanged
492
+ expect(sorted).not.toBe(refs); // different array instance
493
+ });
494
+ });
495
+
496
+ describe('filter', () => {
497
+ test('filters by type', () => {
498
+ const refs = [
499
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
500
+ ContextReferenceFactory.create('directory', '/src', 'src'),
501
+ ];
502
+ const filtered = ContextReferenceUtils.filter(refs, { types: ['file'] });
503
+ expect(filtered).toHaveLength(1);
504
+ expect(filtered[0].type).toBe('file');
505
+ });
506
+
507
+ test('filters by validOnly', () => {
508
+ const refs = [
509
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
510
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
511
+ ];
512
+ refs[1].isValid = false;
513
+ const filtered = ContextReferenceUtils.filter(refs, { validOnly: true });
514
+ expect(filtered).toHaveLength(1);
515
+ });
516
+
517
+ test('filters by language', () => {
518
+ const refs = [
519
+ ContextReferenceFactory.createFileReference('/a.js', 'a.js'),
520
+ ContextReferenceFactory.createFileReference('/b.py', 'b.py'),
521
+ ];
522
+ const filtered = ContextReferenceUtils.filter(refs, { languages: ['javascript'] });
523
+ expect(filtered).toHaveLength(1);
524
+ expect(filtered[0].path).toBe('a.js');
525
+ });
526
+
527
+ test('filters by pathPattern', () => {
528
+ const refs = [
529
+ ContextReferenceFactory.create('file', '/src/components/Button.js', 'Button.js'),
530
+ ContextReferenceFactory.create('file', '/src/utils/helper.js', 'helper.js'),
531
+ ];
532
+ const filtered = ContextReferenceUtils.filter(refs, { pathPattern: 'components' });
533
+ expect(filtered).toHaveLength(1);
534
+ expect(filtered[0].name).toBe('Button.js');
535
+ });
536
+
537
+ test('filters by keywords', () => {
538
+ const refs = [
539
+ ContextReferenceFactory.create('file', '/a.js', 'a.js', { metadata: { keywords: ['auth'] } }),
540
+ ContextReferenceFactory.create('file', '/b.js', 'b.js', { metadata: { keywords: ['db'] } }),
541
+ ];
542
+ const filtered = ContextReferenceUtils.filter(refs, { keywords: ['auth'] });
543
+ expect(filtered).toHaveLength(1);
544
+ expect(filtered[0].name).toBe('a.js');
545
+ });
546
+
547
+ test('filters by createdAfter', () => {
548
+ const refs = [
549
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
550
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
551
+ ];
552
+ refs[0].createdAt = '2024-01-01T00:00:00.000Z';
553
+ refs[1].createdAt = '2025-06-01T00:00:00.000Z';
554
+ const filtered = ContextReferenceUtils.filter(refs, { createdAfter: '2025-01-01T00:00:00.000Z' });
555
+ expect(filtered).toHaveLength(1);
556
+ expect(filtered[0].name).toBe('b.js');
557
+ });
558
+
559
+ test('filters by createdBefore', () => {
560
+ const refs = [
561
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
562
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
563
+ ];
564
+ refs[0].createdAt = '2024-01-01T00:00:00.000Z';
565
+ refs[1].createdAt = '2025-06-01T00:00:00.000Z';
566
+ const filtered = ContextReferenceUtils.filter(refs, { createdBefore: '2025-01-01T00:00:00.000Z' });
567
+ expect(filtered).toHaveLength(1);
568
+ expect(filtered[0].name).toBe('a.js');
569
+ });
570
+
571
+ test('returns all when no criteria', () => {
572
+ const refs = [
573
+ ContextReferenceFactory.create('file', '/a.js', 'a.js'),
574
+ ContextReferenceFactory.create('file', '/b.js', 'b.js'),
575
+ ];
576
+ const filtered = ContextReferenceUtils.filter(refs);
577
+ expect(filtered).toHaveLength(2);
578
+ });
579
+ });
580
+
581
+ describe('formatForDisplay', () => {
582
+ test('returns formatted reference', () => {
583
+ const ref = ContextReferenceFactory.createFileReference('/src/app.js', 'src/app.js');
584
+ const result = ContextReferenceUtils.formatForDisplay(ref);
585
+ expect(result.id).toBe(ref.id);
586
+ expect(result.type).toBe('file');
587
+ expect(result.name).toBe('app.js');
588
+ expect(result.path).toBe('src/app.js');
589
+ expect(result.isValid).toBe(true);
590
+ expect(result.accessCount).toBe(0);
591
+ expect(result.language).toBe('javascript');
592
+ });
593
+
594
+ test('includes icon from metadata', () => {
595
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
596
+ const result = ContextReferenceUtils.formatForDisplay(ref);
597
+ expect(result.icon).toBeDefined();
598
+ });
599
+
600
+ test('includes description', () => {
601
+ const ref = ContextReferenceFactory.create('file', '/a.js', 'a.js');
602
+ const result = ContextReferenceUtils.formatForDisplay(ref);
603
+ expect(result.description).toBe('File: /a.js');
604
+ });
605
+ });
606
+ });