onbuzz 3.6.1 → 3.6.2

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 (83) 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__/fileTreeTool.test.js +274 -0
  53. package/src/tools/__tests__/filesystemTool.test.js +717 -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
@@ -0,0 +1,584 @@
1
+ import { jest, describe, test, expect, beforeEach } from '@jest/globals';
2
+ import { ToolCommandValidator, ToolCommandFactory, ToolCommandUtils } from '../toolCommand.js';
3
+
4
+ describe('ToolCommandFactory', () => {
5
+ test('create returns command with toolId, status, and id', () => {
6
+ const cmd = ToolCommandFactory.create('terminal', 'run', { script: 'test' }, { agentId: 'agent_1' });
7
+
8
+ expect(cmd).toBeDefined();
9
+ expect(typeof cmd.id).toBe('string');
10
+ expect(cmd.id).toMatch(/^cmd_/);
11
+ expect(cmd.toolId).toBe('terminal');
12
+ expect(cmd.command).toBe('run');
13
+ });
14
+
15
+ test('create sets default status to pending', () => {
16
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'agent_1' });
17
+ expect(cmd.status).toBe('pending');
18
+ });
19
+
20
+ test('create sets execution with executionId', () => {
21
+ const cmd = ToolCommandFactory.create('terminal', 'run', { x: 1 }, { agentId: 'a1' });
22
+ expect(cmd.execution).toBeDefined();
23
+ expect(cmd.execution.executionId).toMatch(/^exec_/);
24
+ expect(cmd.execution.input).toEqual({ command: 'run', parameters: { x: 1 } });
25
+ expect(cmd.execution.output).toBeNull();
26
+ expect(cmd.execution.error).toBeNull();
27
+ });
28
+
29
+ test('create applies options', () => {
30
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, {
31
+ agentId: 'a1',
32
+ conversationId: 'conv_1',
33
+ messageId: 'msg_1',
34
+ priority: 1,
35
+ timeout: 60000,
36
+ maxRetries: 5,
37
+ workingDirectory: '/tmp'
38
+ });
39
+ expect(cmd.agentId).toBe('a1');
40
+ expect(cmd.conversationId).toBe('conv_1');
41
+ expect(cmd.messageId).toBe('msg_1');
42
+ expect(cmd.priority).toBe(1);
43
+ expect(cmd.timeout).toBe(60000);
44
+ expect(cmd.maxRetries).toBe(5);
45
+ expect(cmd.execution.workingDirectory).toBe('/tmp');
46
+ });
47
+
48
+ test('create uses default values when options not provided', () => {
49
+ const cmd = ToolCommandFactory.create('terminal', 'run', {});
50
+ expect(cmd.agentId).toBe('');
51
+ expect(cmd.priority).toBe(3);
52
+ expect(cmd.timeout).toBe(30000);
53
+ expect(cmd.maxRetries).toBe(3);
54
+ expect(cmd.retryCount).toBe(0);
55
+ });
56
+
57
+ test('generateCommandId returns string starting with cmd_', () => {
58
+ const id = ToolCommandFactory.generateCommandId();
59
+ expect(typeof id).toBe('string');
60
+ expect(id).toMatch(/^cmd_/);
61
+ });
62
+
63
+ test('generateExecutionId returns string starting with exec_', () => {
64
+ const id = ToolCommandFactory.generateExecutionId();
65
+ expect(typeof id).toBe('string');
66
+ expect(id).toMatch(/^exec_/);
67
+ });
68
+
69
+ test('generateLogId returns string starting with log_', () => {
70
+ const id = ToolCommandFactory.generateLogId();
71
+ expect(typeof id).toBe('string');
72
+ expect(id).toMatch(/^log_/);
73
+ });
74
+
75
+ test('createLogEntry returns log with level and message', () => {
76
+ const log = ToolCommandFactory.createLogEntry('info', 'Task started');
77
+ expect(log.level).toBe('info');
78
+ expect(log.message).toBe('Task started');
79
+ expect(typeof log.timestamp).toBe('string');
80
+ expect(typeof log.id).toBe('string');
81
+ expect(log.source).toBe('tool-execution');
82
+ });
83
+
84
+ test('createLogEntry includes data when provided', () => {
85
+ const log = ToolCommandFactory.createLogEntry('error', 'Failed', { code: 500 });
86
+ expect(log.data).toEqual({ code: 500 });
87
+ });
88
+
89
+ test('createDefaultMetadata returns expected shape', () => {
90
+ const meta = ToolCommandFactory.createDefaultMetadata();
91
+ expect(meta.toolVersion).toBe('1.0.0');
92
+ expect(meta.capabilities).toEqual([]);
93
+ expect(meta.requiresAuth).toBe(false);
94
+ expect(meta.tags).toEqual([]);
95
+ });
96
+
97
+ test('createDefaultMetadata applies overrides', () => {
98
+ const meta = ToolCommandFactory.createDefaultMetadata({ toolVersion: '2.0.0', requiresAuth: true });
99
+ expect(meta.toolVersion).toBe('2.0.0');
100
+ expect(meta.requiresAuth).toBe(true);
101
+ });
102
+ });
103
+
104
+ describe('ToolCommandValidator', () => {
105
+ test('validate accepts valid command', () => {
106
+ const cmd = ToolCommandFactory.create('terminal', 'run', { script: 'test' }, { agentId: 'agent_1' });
107
+ const result = ToolCommandValidator.validate(cmd);
108
+ expect(result.isValid).toBe(true);
109
+ expect(result.errors).toHaveLength(0);
110
+ });
111
+
112
+ test('validate rejects missing required fields', () => {
113
+ const result = ToolCommandValidator.validate({});
114
+ expect(result.isValid).toBe(false);
115
+ expect(result.errors.some(e => e.includes('Command ID'))).toBe(true);
116
+ expect(result.errors.some(e => e.includes('Tool ID'))).toBe(true);
117
+ expect(result.errors.some(e => e.includes('Command is required'))).toBe(true);
118
+ expect(result.errors.some(e => e.includes('Agent ID'))).toBe(true);
119
+ });
120
+
121
+ test('validate warns on unknown toolId', () => {
122
+ const cmd = ToolCommandFactory.create('custom_tool', 'run', {}, { agentId: 'a1' });
123
+ const result = ToolCommandValidator.validate(cmd);
124
+ // custom_ prefix is allowed, should not warn
125
+ expect(result.warnings.some(w => w.includes('Unknown tool ID'))).toBe(false);
126
+ });
127
+
128
+ test('validate warns on truly unknown toolId', () => {
129
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
130
+ cmd.toolId = 'totally_unknown_tool';
131
+ const result = ToolCommandValidator.validate(cmd);
132
+ expect(result.warnings.some(w => w.includes('Unknown tool ID'))).toBe(true);
133
+ });
134
+
135
+ test('validate rejects invalid status', () => {
136
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
137
+ cmd.status = 'invalid-status';
138
+ const result = ToolCommandValidator.validate(cmd);
139
+ expect(result.errors.some(e => e.includes('Invalid tool status'))).toBe(true);
140
+ });
141
+
142
+ test('validate rejects non-object parameters', () => {
143
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
144
+ cmd.parameters = 'string';
145
+ const result = ToolCommandValidator.validate(cmd);
146
+ expect(result.errors.some(e => e.includes('Parameters must be an object'))).toBe(true);
147
+ });
148
+
149
+ test('validate rejects invalid priority', () => {
150
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
151
+ cmd.priority = 10;
152
+ const result = ToolCommandValidator.validate(cmd);
153
+ expect(result.errors.some(e => e.includes('Priority'))).toBe(true);
154
+ });
155
+
156
+ test('validate rejects negative timeout', () => {
157
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
158
+ cmd.timeout = -1;
159
+ const result = ToolCommandValidator.validate(cmd);
160
+ expect(result.errors.some(e => e.includes('Timeout'))).toBe(true);
161
+ });
162
+
163
+ test('validate warns on very long timeout', () => {
164
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
165
+ cmd.timeout = 7200000;
166
+ const result = ToolCommandValidator.validate(cmd);
167
+ expect(result.warnings.some(w => w.includes('Timeout is very long'))).toBe(true);
168
+ });
169
+
170
+ test('validate rejects non-number retryCount', () => {
171
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
172
+ cmd.retryCount = 'three';
173
+ const result = ToolCommandValidator.validate(cmd);
174
+ expect(result.errors.some(e => e.includes('Retry count'))).toBe(true);
175
+ });
176
+
177
+ test('validate warns when retryCount exceeds maxRetries', () => {
178
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
179
+ cmd.retryCount = 5;
180
+ cmd.maxRetries = 3;
181
+ const result = ToolCommandValidator.validate(cmd);
182
+ expect(result.warnings.some(w => w.includes('Retry count exceeds'))).toBe(true);
183
+ });
184
+
185
+ test('validate rejects invalid timestamps', () => {
186
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
187
+ cmd.createdAt = 'not-a-date';
188
+ const result = ToolCommandValidator.validate(cmd);
189
+ expect(result.errors.some(e => e.includes('Invalid timestamp'))).toBe(true);
190
+ });
191
+
192
+ describe('validateExecution', () => {
193
+ test('accepts valid execution', () => {
194
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
195
+ const result = ToolCommandValidator.validateExecution(cmd.execution);
196
+ expect(result.errors).toHaveLength(0);
197
+ });
198
+
199
+ test('rejects missing executionId', () => {
200
+ const result = ToolCommandValidator.validateExecution({ input: {} });
201
+ expect(result.errors.some(e => e.includes('Execution ID'))).toBe(true);
202
+ });
203
+
204
+ test('rejects negative executionTime', () => {
205
+ const result = ToolCommandValidator.validateExecution({
206
+ executionId: 'exec_1',
207
+ input: {},
208
+ executionTime: -1
209
+ });
210
+ expect(result.errors.some(e => e.includes('Execution time'))).toBe(true);
211
+ });
212
+
213
+ test('rejects invalid cpuUsage', () => {
214
+ const result = ToolCommandValidator.validateExecution({
215
+ executionId: 'exec_1',
216
+ input: {},
217
+ cpuUsage: 150
218
+ });
219
+ expect(result.errors.some(e => e.includes('CPU usage'))).toBe(true);
220
+ });
221
+
222
+ test('rejects invalid log entries', () => {
223
+ const result = ToolCommandValidator.validateExecution({
224
+ executionId: 'exec_1',
225
+ input: {},
226
+ logs: [{ level: 'info' }] // missing message and timestamp
227
+ });
228
+ expect(result.errors.some(e => e.includes('Log entry'))).toBe(true);
229
+ });
230
+ });
231
+
232
+ describe('validateToolDefinition', () => {
233
+ test('accepts valid tool definition', () => {
234
+ const result = ToolCommandValidator.validateToolDefinition({
235
+ id: 'terminal',
236
+ name: 'Terminal',
237
+ description: 'Execute commands',
238
+ version: '1.0.0',
239
+ capabilities: [{ id: 'run', name: 'Run' }]
240
+ });
241
+ expect(result.errors).toHaveLength(0);
242
+ });
243
+
244
+ test('rejects missing required fields', () => {
245
+ const result = ToolCommandValidator.validateToolDefinition({});
246
+ expect(result.errors.length).toBeGreaterThanOrEqual(4);
247
+ });
248
+
249
+ test('warns on empty capabilities', () => {
250
+ const result = ToolCommandValidator.validateToolDefinition({
251
+ id: 'test', name: 'Test', description: 'Desc', version: '1.0', capabilities: []
252
+ });
253
+ expect(result.warnings.some(w => w.includes('no capabilities'))).toBe(true);
254
+ });
255
+ });
256
+
257
+ describe('validateParameters', () => {
258
+ test('validates required parameters', () => {
259
+ const result = ToolCommandValidator.validateParameters({}, {
260
+ properties: { cmd: { type: 'string' } },
261
+ required: ['cmd']
262
+ });
263
+ expect(result.errors.some(e => e.includes('Required parameter missing: cmd'))).toBe(true);
264
+ });
265
+
266
+ test('validates parameter types', () => {
267
+ const result = ToolCommandValidator.validateParameters({ count: 'five' }, {
268
+ properties: { count: { type: 'number' } },
269
+ required: []
270
+ });
271
+ expect(result.errors.some(e => e.includes('must be of type number'))).toBe(true);
272
+ });
273
+
274
+ test('validates number ranges', () => {
275
+ const result = ToolCommandValidator.validateParameters({ count: 0 }, {
276
+ properties: { count: { type: 'number', minimum: 1, maximum: 10 } },
277
+ required: []
278
+ });
279
+ expect(result.errors.some(e => e.includes('>= 1'))).toBe(true);
280
+ });
281
+
282
+ test('validates string lengths', () => {
283
+ const result = ToolCommandValidator.validateParameters({ name: 'ab' }, {
284
+ properties: { name: { type: 'string', minLength: 3 } },
285
+ required: []
286
+ });
287
+ expect(result.errors.some(e => e.includes('at least 3'))).toBe(true);
288
+ });
289
+
290
+ test('validates enum values', () => {
291
+ const result = ToolCommandValidator.validateParameters({ mode: 'invalid' }, {
292
+ properties: { mode: { type: 'string', enum: ['read', 'write'] } },
293
+ required: []
294
+ });
295
+ expect(result.errors.some(e => e.includes('must be one of'))).toBe(true);
296
+ });
297
+
298
+ test('returns warning when no schema provided', () => {
299
+ const result = ToolCommandValidator.validateParameters({}, null);
300
+ expect(result.warnings.some(w => w.includes('No parameter schema'))).toBe(true);
301
+ });
302
+ });
303
+ });
304
+
305
+ describe('ToolCommandUtils', () => {
306
+ test('isPending returns true for pending command', () => {
307
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'agent_1' });
308
+ expect(ToolCommandUtils.isPending(cmd)).toBe(true);
309
+ });
310
+
311
+ test('isExecuting returns true for executing command', () => {
312
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
313
+ cmd.status = 'executing';
314
+ expect(ToolCommandUtils.isExecuting(cmd)).toBe(true);
315
+ });
316
+
317
+ test('isExecuting returns false for non-executing command', () => {
318
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
319
+ expect(ToolCommandUtils.isExecuting(cmd)).toBe(false);
320
+ });
321
+
322
+ test('isCompleted returns true for completed command', () => {
323
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
324
+ cmd.status = 'completed';
325
+ expect(ToolCommandUtils.isCompleted(cmd)).toBe(true);
326
+ });
327
+
328
+ test('isCompleted returns false for pending command', () => {
329
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
330
+ expect(ToolCommandUtils.isCompleted(cmd)).toBe(false);
331
+ });
332
+
333
+ test('isFailed returns true for failed command', () => {
334
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
335
+ cmd.status = 'failed';
336
+ expect(ToolCommandUtils.isFailed(cmd)).toBe(true);
337
+ });
338
+
339
+ test('isFailed returns false for completed command', () => {
340
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
341
+ cmd.status = 'completed';
342
+ expect(ToolCommandUtils.isFailed(cmd)).toBe(false);
343
+ });
344
+
345
+ describe('isTimedOut', () => {
346
+ test('returns false when no startedAt', () => {
347
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
348
+ expect(ToolCommandUtils.isTimedOut(cmd)).toBe(false);
349
+ });
350
+
351
+ test('returns false when status is not executing', () => {
352
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
353
+ cmd.startedAt = new Date(Date.now() - 60000).toISOString();
354
+ cmd.status = 'completed';
355
+ expect(ToolCommandUtils.isTimedOut(cmd)).toBe(false);
356
+ });
357
+
358
+ test('returns true when executing and elapsed > timeout', () => {
359
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1', timeout: 1000 });
360
+ cmd.status = 'executing';
361
+ cmd.startedAt = new Date(Date.now() - 5000).toISOString();
362
+ expect(ToolCommandUtils.isTimedOut(cmd)).toBe(true);
363
+ });
364
+
365
+ test('returns false when executing and elapsed < timeout', () => {
366
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1', timeout: 60000 });
367
+ cmd.status = 'executing';
368
+ cmd.startedAt = new Date().toISOString();
369
+ expect(ToolCommandUtils.isTimedOut(cmd)).toBe(false);
370
+ });
371
+ });
372
+
373
+ describe('getExecutionTime', () => {
374
+ test('returns null when no startedAt', () => {
375
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
376
+ expect(ToolCommandUtils.getExecutionTime(cmd)).toBeNull();
377
+ });
378
+
379
+ test('returns elapsed time to completedAt when completed', () => {
380
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
381
+ cmd.startedAt = '2025-01-01T00:00:00.000Z';
382
+ cmd.completedAt = '2025-01-01T00:00:05.000Z';
383
+ expect(ToolCommandUtils.getExecutionTime(cmd)).toBe(5000);
384
+ });
385
+
386
+ test('returns elapsed time to now when no completedAt', () => {
387
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
388
+ cmd.startedAt = new Date(Date.now() - 2000).toISOString();
389
+ const result = ToolCommandUtils.getExecutionTime(cmd);
390
+ expect(result).toBeGreaterThanOrEqual(1900);
391
+ expect(result).toBeLessThan(5000);
392
+ });
393
+ });
394
+
395
+ describe('getProgress', () => {
396
+ test('returns 100% for completed command', () => {
397
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
398
+ cmd.status = 'completed';
399
+ const progress = ToolCommandUtils.getProgress(cmd);
400
+ expect(progress.percentage).toBe(100);
401
+ expect(progress.status).toBe('completed');
402
+ });
403
+
404
+ test('returns estimated progress for executing command', () => {
405
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1', timeout: 10000 });
406
+ cmd.status = 'executing';
407
+ cmd.startedAt = new Date(Date.now() - 5000).toISOString();
408
+ const progress = ToolCommandUtils.getProgress(cmd);
409
+ expect(progress.percentage).toBeGreaterThan(0);
410
+ expect(progress.percentage).toBeLessThanOrEqual(95);
411
+ expect(progress.remainingTime).toBeGreaterThanOrEqual(0);
412
+ });
413
+
414
+ test('returns 0% for pending command', () => {
415
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
416
+ const progress = ToolCommandUtils.getProgress(cmd);
417
+ expect(progress.percentage).toBe(0);
418
+ expect(progress.remainingTime).toBeNull();
419
+ });
420
+
421
+ test('includes isTimedOut flag', () => {
422
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1', timeout: 1000 });
423
+ cmd.status = 'executing';
424
+ cmd.startedAt = new Date(Date.now() - 5000).toISOString();
425
+ const progress = ToolCommandUtils.getProgress(cmd);
426
+ expect(progress.isTimedOut).toBe(true);
427
+ });
428
+ });
429
+
430
+ describe('getMetrics', () => {
431
+ test('returns metrics for a command', () => {
432
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
433
+ cmd.execution.executionTime = 1500;
434
+ cmd.execution.memoryUsage = 1024;
435
+ cmd.execution.cpuUsage = 25;
436
+ const metrics = ToolCommandUtils.getMetrics(cmd);
437
+ expect(metrics.executionTime).toBe(1500);
438
+ expect(metrics.memoryUsage).toBe(1024);
439
+ expect(metrics.cpuUsage).toBe(25);
440
+ expect(metrics.status).toBe('pending');
441
+ expect(metrics.retryCount).toBe(0);
442
+ expect(metrics.priority).toBe(3);
443
+ expect(metrics.logEntries).toBe(0);
444
+ expect(metrics.hasError).toBe(false);
445
+ expect(metrics.errorCode).toBeNull();
446
+ });
447
+
448
+ test('includes error info when present', () => {
449
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
450
+ cmd.execution.error = 'Something went wrong';
451
+ cmd.execution.errorCode = 'ERR_TIMEOUT';
452
+ const metrics = ToolCommandUtils.getMetrics(cmd);
453
+ expect(metrics.hasError).toBe(true);
454
+ expect(metrics.errorCode).toBe('ERR_TIMEOUT');
455
+ });
456
+
457
+ test('handles missing execution', () => {
458
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
459
+ delete cmd.execution;
460
+ const metrics = ToolCommandUtils.getMetrics(cmd);
461
+ expect(metrics.memoryUsage).toBe(0);
462
+ expect(metrics.cpuUsage).toBe(0);
463
+ });
464
+ });
465
+
466
+ describe('formatForDisplay', () => {
467
+ test('returns formatted command data', () => {
468
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
469
+ cmd.status = 'completed';
470
+ cmd.startedAt = '2025-01-01T00:00:00.000Z';
471
+ cmd.completedAt = '2025-01-01T00:00:05.000Z';
472
+ const result = ToolCommandUtils.formatForDisplay(cmd);
473
+ expect(result.id).toBe(cmd.id);
474
+ expect(result.toolId).toBe('terminal');
475
+ expect(result.command).toBe('run');
476
+ expect(result.status).toBe('completed');
477
+ expect(result.progress).toBe(100);
478
+ expect(result.hasError).toBe(false);
479
+ expect(result.retryCount).toBe(0);
480
+ });
481
+ });
482
+
483
+ describe('sanitize', () => {
484
+ test('removes environmentVariables and environment from execution', () => {
485
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
486
+ cmd.execution.environmentVariables = { SECRET: 'value' };
487
+ cmd.execution.environment = { PATH: '/usr/bin' };
488
+ const result = ToolCommandUtils.sanitize(cmd);
489
+ expect(result.execution.environmentVariables).toBeUndefined();
490
+ expect(result.execution.environment).toBeUndefined();
491
+ });
492
+
493
+ test('truncates long logs to last 10', () => {
494
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
495
+ cmd.execution.logs = Array.from({ length: 20 }, (_, i) => ({
496
+ id: `log_${i}`,
497
+ level: 'info',
498
+ message: `Log ${i}`,
499
+ timestamp: new Date().toISOString()
500
+ }));
501
+ const result = ToolCommandUtils.sanitize(cmd);
502
+ expect(result.execution.logs).toHaveLength(10);
503
+ });
504
+
505
+ test('redacts sensitive parameter keys', () => {
506
+ const cmd = ToolCommandFactory.create('terminal', 'run', {
507
+ password: 'secret123',
508
+ apiToken: 'tok_abc',
509
+ normalParam: 'visible'
510
+ }, { agentId: 'a1' });
511
+ const result = ToolCommandUtils.sanitize(cmd);
512
+ expect(result.parameters.password).toBe('[REDACTED]');
513
+ expect(result.parameters.apiToken).toBe('[REDACTED]');
514
+ expect(result.parameters.normalParam).toBe('visible');
515
+ });
516
+
517
+ test('handles missing execution gracefully', () => {
518
+ const cmd = ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' });
519
+ delete cmd.execution;
520
+ const result = ToolCommandUtils.sanitize(cmd);
521
+ expect(result.execution).toBeUndefined();
522
+ });
523
+ });
524
+
525
+ describe('summarizeCommands', () => {
526
+ test('returns summary with total count', () => {
527
+ const commands = [
528
+ ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'agent_1' }),
529
+ ToolCommandFactory.create('filesystem', 'read', {}, { agentId: 'agent_1' }),
530
+ ToolCommandFactory.create('terminal', 'exec', {}, { agentId: 'agent_1' }),
531
+ ];
532
+
533
+ const summary = ToolCommandUtils.summarizeCommands(commands);
534
+ expect(summary.total).toBe(3);
535
+ expect(summary.byStatus).toBeDefined();
536
+ expect(summary.byTool).toBeDefined();
537
+ expect(summary.byTool['terminal']).toBe(2);
538
+ expect(summary.byTool['filesystem']).toBe(1);
539
+ });
540
+
541
+ test('calculates success rate', () => {
542
+ const commands = [
543
+ ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' }),
544
+ ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' }),
545
+ ];
546
+ commands[0].status = 'completed';
547
+ commands[1].status = 'failed';
548
+
549
+ const summary = ToolCommandUtils.summarizeCommands(commands);
550
+ expect(summary.successRate).toBe(50);
551
+ });
552
+
553
+ test('returns mostUsedTools sorted by count', () => {
554
+ const commands = [
555
+ ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' }),
556
+ ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' }),
557
+ ToolCommandFactory.create('filesystem', 'read', {}, { agentId: 'a1' }),
558
+ ];
559
+ const summary = ToolCommandUtils.summarizeCommands(commands);
560
+ expect(summary.mostUsedTools[0].toolId).toBe('terminal');
561
+ expect(summary.mostUsedTools[0].count).toBe(2);
562
+ });
563
+
564
+ test('returns recentCommands sorted by creation date', () => {
565
+ const commands = [
566
+ ToolCommandFactory.create('terminal', 'run', {}, { agentId: 'a1' }),
567
+ ToolCommandFactory.create('filesystem', 'read', {}, { agentId: 'a1' }),
568
+ ];
569
+ const summary = ToolCommandUtils.summarizeCommands(commands);
570
+ expect(summary.recentCommands).toHaveLength(2);
571
+ expect(summary.recentCommands[0]).toHaveProperty('id');
572
+ expect(summary.recentCommands[0]).toHaveProperty('toolId');
573
+ });
574
+
575
+ test('handles empty commands array', () => {
576
+ const summary = ToolCommandUtils.summarizeCommands([]);
577
+ expect(summary.total).toBe(0);
578
+ expect(summary.averageExecutionTime).toBe(0);
579
+ expect(summary.successRate).toBe(0);
580
+ expect(summary.mostUsedTools).toEqual([]);
581
+ expect(summary.recentCommands).toEqual([]);
582
+ });
583
+ });
584
+ });
@@ -7,7 +7,7 @@
7
7
  * - Handle context reference resolution and management
8
8
  */
9
9
 
10
- import { CONTEXT_REFERENCE_TYPES, FILE_EXTENSIONS, CONTEXT_ICONS } from '../utilities/constants.js';
10
+ import { CONTEXT_REFERENCE_TYPES, FILE_EXTENSIONS, CONTEXT_ICONS, FILE_ICONS } from '../utilities/constants.js';
11
11
 
12
12
  /**
13
13
  * Context Reference data model
@@ -0,0 +1,80 @@
1
+ import { jest, describe, test, expect, beforeEach } from '@jest/globals';
2
+ import AttachmentValidator from '../attachmentValidator.js';
3
+
4
+ describe('AttachmentValidator', () => {
5
+ let validator;
6
+
7
+ beforeEach(() => {
8
+ validator = new AttachmentValidator();
9
+ });
10
+
11
+ describe('validateFileType', () => {
12
+ test('allows .js, .txt, .json files', () => {
13
+ expect(validator.validateFileType('script.js').valid).toBe(true);
14
+ expect(validator.validateFileType('readme.txt').valid).toBe(true);
15
+ expect(validator.validateFileType('config.json').valid).toBe(true);
16
+ });
17
+
18
+ test('blocks executable files', () => {
19
+ // These are platform-specific; on Windows .exe, .bat, .cmd are blocked
20
+ // On all platforms .jar and .apk are blocked
21
+ const result = validator.validateFileType('malware.jar');
22
+ expect(result.valid).toBe(false);
23
+ expect(result.error).toContain('Executable');
24
+
25
+ const apkResult = validator.validateFileType('app.apk');
26
+ expect(apkResult.valid).toBe(false);
27
+ });
28
+ });
29
+
30
+ describe('isExecutable', () => {
31
+ test('returns true for platform executables', () => {
32
+ // Universal blocked extensions
33
+ expect(validator.isExecutable('file.jar')).toBe(true);
34
+ expect(validator.isExecutable('file.apk')).toBe(true);
35
+ });
36
+
37
+ test('returns false for non-executable files', () => {
38
+ expect(validator.isExecutable('file.js')).toBe(false);
39
+ expect(validator.isExecutable('file.txt')).toBe(false);
40
+ expect(validator.isExecutable('file.json')).toBe(false);
41
+ });
42
+ });
43
+
44
+ describe('validateSize', () => {
45
+ test('accepts files under content limit (1MB)', () => {
46
+ const result = validator.validateSize(500 * 1024, 'content');
47
+ expect(result.valid).toBe(true);
48
+ });
49
+
50
+ test('rejects files over content limit', () => {
51
+ const result = validator.validateSize(2 * 1024 * 1024, 'content');
52
+ expect(result.valid).toBe(false);
53
+ expect(result.error).toBeDefined();
54
+ });
55
+ });
56
+
57
+ describe('validatePath', () => {
58
+ test('rejects directory traversal (../)', () => {
59
+ const result = validator.validatePath('../../etc/passwd');
60
+ expect(result.valid).toBe(false);
61
+ expect(result.error).toContain('traversal');
62
+ });
63
+
64
+ test('accepts normal paths', () => {
65
+ const result = validator.validatePath('src/file.js');
66
+ expect(result.valid).toBe(true);
67
+ });
68
+ });
69
+
70
+ describe('getContentType', () => {
71
+ test('returns correct content type for various extensions', () => {
72
+ expect(validator.getContentType('app.js')).toBe('text');
73
+ expect(validator.getContentType('photo.png')).toBe('image');
74
+ expect(validator.getContentType('doc.pdf')).toBe('pdf');
75
+ expect(validator.getContentType('lib.dll')).toBe('binary');
76
+ expect(validator.getContentType('style.css')).toBe('text');
77
+ expect(validator.getContentType('pic.jpg')).toBe('image');
78
+ });
79
+ });
80
+ });