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.
- package/package.json +1 -1
- package/src/__test-utils__/fixtures/malformedJson.js +31 -0
- package/src/__test-utils__/globalSetup.js +9 -0
- package/src/__test-utils__/globalTeardown.js +12 -0
- package/src/__test-utils__/mockFactories.js +101 -0
- package/src/analyzers/__tests__/CSSAnalyzer.test.js +41 -0
- package/src/analyzers/__tests__/ConfigValidator.test.js +362 -0
- package/src/analyzers/__tests__/ESLintAnalyzer.test.js +271 -0
- package/src/analyzers/__tests__/JavaScriptAnalyzer.test.js +40 -0
- package/src/analyzers/__tests__/PrettierFormatter.test.js +197 -0
- package/src/analyzers/__tests__/PythonAnalyzer.test.js +208 -0
- package/src/analyzers/__tests__/SecurityAnalyzer.test.js +303 -0
- package/src/analyzers/__tests__/SparrowAnalyzer.test.js +270 -0
- package/src/analyzers/__tests__/TypeScriptAnalyzer.test.js +187 -0
- package/src/core/__tests__/agentPool.test.js +601 -0
- package/src/core/__tests__/agentScheduler.test.js +576 -0
- package/src/core/__tests__/contextManager.test.js +252 -0
- package/src/core/__tests__/flowExecutor.test.js +262 -0
- package/src/core/__tests__/messageProcessor.test.js +627 -0
- package/src/core/__tests__/orchestrator.test.js +257 -0
- package/src/core/__tests__/stateManager.test.js +375 -0
- package/src/core/agentPool.js +11 -1
- package/src/index.js +25 -9
- package/src/interfaces/terminal/__tests__/smoke/imports.test.js +3 -5
- package/src/services/__tests__/agentActivityService.test.js +319 -0
- package/src/services/__tests__/apiKeyManager.test.js +206 -0
- package/src/services/__tests__/benchmarkService.test.js +184 -0
- package/src/services/__tests__/budgetService.test.js +211 -0
- package/src/services/__tests__/contextInjectionService.test.js +205 -0
- package/src/services/__tests__/conversationCompactionService.test.js +280 -0
- package/src/services/__tests__/credentialVault.test.js +469 -0
- package/src/services/__tests__/errorHandler.test.js +314 -0
- package/src/services/__tests__/fileAttachmentService.test.js +278 -0
- package/src/services/__tests__/flowContextService.test.js +199 -0
- package/src/services/__tests__/memoryService.test.js +450 -0
- package/src/services/__tests__/modelRouterService.test.js +388 -0
- package/src/services/__tests__/modelsService.test.js +261 -0
- package/src/services/__tests__/portRegistry.test.js +123 -0
- package/src/services/__tests__/projectDetector.test.js +34 -0
- package/src/services/__tests__/promptService.test.js +242 -0
- package/src/services/__tests__/qualityInspector.test.js +97 -0
- package/src/services/__tests__/scheduleService.test.js +308 -0
- package/src/services/__tests__/serviceRegistry.test.js +74 -0
- package/src/services/__tests__/skillsService.test.js +402 -0
- package/src/services/__tests__/tokenCountingService.test.js +48 -0
- package/src/tools/__tests__/agentCommunicationTool.test.js +500 -0
- package/src/tools/__tests__/agentDelayTool.test.js +342 -0
- package/src/tools/__tests__/asyncToolManager.test.js +344 -0
- package/src/tools/__tests__/baseTool.test.js +420 -0
- package/src/tools/__tests__/codeMapTool.test.js +348 -0
- package/src/tools/__tests__/fileContentReplaceTool.test.js +309 -0
- package/src/tools/__tests__/fileSystemTool.test.js +717 -0
- package/src/tools/__tests__/fileTreeTool.test.js +274 -0
- package/src/tools/__tests__/helpTool.test.js +204 -0
- package/src/tools/__tests__/jobDoneTool.test.js +296 -0
- package/src/tools/__tests__/memoryTool.test.js +297 -0
- package/src/tools/__tests__/seekTool.test.js +282 -0
- package/src/tools/__tests__/skillsTool.test.js +226 -0
- package/src/tools/__tests__/staticAnalysisTool.test.js +509 -0
- package/src/tools/__tests__/taskManagerTool.test.js +725 -0
- package/src/tools/__tests__/terminalTool.test.js +384 -0
- package/src/tools/__tests__/userPromptTool.test.js +297 -0
- package/src/tools/__tests__/webTool.e2e.test.js +25 -11
- package/src/tools/webTool.js +6 -12
- package/src/types/__tests__/agent.test.js +499 -0
- package/src/types/__tests__/contextReference.test.js +606 -0
- package/src/types/__tests__/conversation.test.js +555 -0
- package/src/types/__tests__/toolCommand.test.js +584 -0
- package/src/types/contextReference.js +1 -1
- package/src/utilities/__tests__/attachmentValidator.test.js +80 -0
- package/src/utilities/__tests__/configManager.test.js +397 -0
- package/src/utilities/__tests__/constants.test.js +49 -0
- package/src/utilities/__tests__/directoryAccessManager.test.js +388 -0
- package/src/utilities/__tests__/fileProcessor.test.js +104 -0
- package/src/utilities/__tests__/jsonRepair.test.js +104 -0
- package/src/utilities/__tests__/logger.test.js +129 -0
- package/src/utilities/__tests__/platformUtils.test.js +87 -0
- package/src/utilities/__tests__/structuredFileValidator.test.js +263 -0
- package/src/utilities/__tests__/tagParser.test.js +887 -0
- package/src/utilities/__tests__/toolConstants.test.js +94 -0
- package/src/utilities/tagParser.js +2 -2
- package/src/tools/browserTool.js +0 -897
- package/src/utilities/platformUtils.test.js +0 -98
- /package/src/tools/{filesystemTool.js → fileSystemTool.js} +0 -0
|
@@ -7,17 +7,31 @@
|
|
|
7
7
|
*
|
|
8
8
|
* NOTE: These tests require network access and a working Puppeteer install.
|
|
9
9
|
* They are slower (~60s total) and are tagged for separate runs.
|
|
10
|
+
* Skipped on WSL/headless Linux (no display server for Chromium).
|
|
10
11
|
*
|
|
11
|
-
* Run with: npm test
|
|
12
|
+
* Run with: npm run test:e2e
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
import { describe, test, expect, beforeAll, afterAll } from '@jest/globals';
|
|
16
|
+
import os from 'os';
|
|
17
|
+
import fs from 'fs';
|
|
18
|
+
|
|
19
|
+
// Skip on WSL (no display) or when SKIP_BROWSER_TESTS is set
|
|
20
|
+
const isWSL = (() => {
|
|
21
|
+
try {
|
|
22
|
+
return os.platform() === 'linux' && fs.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft');
|
|
23
|
+
} catch { return false; }
|
|
24
|
+
})();
|
|
25
|
+
const skipBrowser = isWSL || process.env.SKIP_BROWSER_TESTS === 'true';
|
|
26
|
+
|
|
27
|
+
const describeIfBrowser = skipBrowser ? describe.skip : describe;
|
|
15
28
|
|
|
16
29
|
let WebTool;
|
|
17
30
|
let wt;
|
|
18
31
|
const silentLogger = { info() {}, warn() {}, error() {}, debug() {} };
|
|
19
32
|
|
|
20
33
|
beforeAll(async () => {
|
|
34
|
+
if (skipBrowser) return;
|
|
21
35
|
const mod = await import('../../tools/webTool.js');
|
|
22
36
|
WebTool = mod.default;
|
|
23
37
|
wt = new WebTool({ logger: silentLogger });
|
|
@@ -29,7 +43,7 @@ afterAll(async () => {
|
|
|
29
43
|
|
|
30
44
|
// ─── HTTP Status Detection ──────────────────────────────────────────────────
|
|
31
45
|
|
|
32
|
-
|
|
46
|
+
describeIfBrowser('HTTP status detection', () => {
|
|
33
47
|
test('fetch valid page returns success with HTTP 200', async () => {
|
|
34
48
|
const result = await wt.execute(
|
|
35
49
|
{ operation: 'fetch', url: 'https://example.com', formats: ['title'] },
|
|
@@ -63,7 +77,7 @@ describe('HTTP status detection', () => {
|
|
|
63
77
|
|
|
64
78
|
// ─── Selector Pre-Validation ─────────────────────────────────────────────────
|
|
65
79
|
|
|
66
|
-
|
|
80
|
+
describeIfBrowser('Selector pre-validation', () => {
|
|
67
81
|
test('click on non-existent selector returns actionable error', async () => {
|
|
68
82
|
const result = await wt.execute({
|
|
69
83
|
operation: 'interactive',
|
|
@@ -174,7 +188,7 @@ describe('Selector pre-validation', () => {
|
|
|
174
188
|
|
|
175
189
|
// ─── URL Validation in Interactive ───────────────────────────────────────────
|
|
176
190
|
|
|
177
|
-
|
|
191
|
+
describeIfBrowser('URL validation in interactive actions', () => {
|
|
178
192
|
test('navigate to invalid URL returns error without crashing', async () => {
|
|
179
193
|
const result = await wt.execute({
|
|
180
194
|
operation: 'interactive',
|
|
@@ -212,7 +226,7 @@ describe('URL validation in interactive actions', () => {
|
|
|
212
226
|
|
|
213
227
|
// ─── Wait-for Timeout Feedback ───────────────────────────────────────────────
|
|
214
228
|
|
|
215
|
-
|
|
229
|
+
describeIfBrowser('Wait-for timeout feedback', () => {
|
|
216
230
|
test('wait-for non-existent element returns timeout error with context', async () => {
|
|
217
231
|
const result = await wt.execute({
|
|
218
232
|
operation: 'interactive',
|
|
@@ -253,7 +267,7 @@ describe('Wait-for timeout feedback', () => {
|
|
|
253
267
|
|
|
254
268
|
// ─── Successful Operations ───────────────────────────────────────────────────
|
|
255
269
|
|
|
256
|
-
|
|
270
|
+
describeIfBrowser('Successful operations', () => {
|
|
257
271
|
test('extract-text on existing element returns content', async () => {
|
|
258
272
|
const result = await wt.execute({
|
|
259
273
|
operation: 'interactive',
|
|
@@ -329,7 +343,7 @@ describe('Successful operations', () => {
|
|
|
329
343
|
|
|
330
344
|
// ─── Search ──────────────────────────────────────────────────────────────────
|
|
331
345
|
|
|
332
|
-
|
|
346
|
+
describeIfBrowser('Search operations', () => {
|
|
333
347
|
test('search with valid query returns results', async () => {
|
|
334
348
|
const result = await wt.execute(
|
|
335
349
|
{ operation: 'search', query: 'javascript MDN', engine: 'duckduckgo', maxResults: 3 },
|
|
@@ -355,7 +369,7 @@ describe('Search operations', () => {
|
|
|
355
369
|
|
|
356
370
|
// ─── Multi-Action Chains ─────────────────────────────────────────────────────
|
|
357
371
|
|
|
358
|
-
|
|
372
|
+
describeIfBrowser('Multi-action chains', () => {
|
|
359
373
|
test('chain of actions reports per-action success/failure', async () => {
|
|
360
374
|
const result = await wt.execute({
|
|
361
375
|
operation: 'interactive',
|
|
@@ -409,7 +423,7 @@ describe('Multi-action chains', () => {
|
|
|
409
423
|
|
|
410
424
|
// ─── Tab Reuse ───────────────────────────────────────────────────────────────
|
|
411
425
|
|
|
412
|
-
|
|
426
|
+
describeIfBrowser('Tab reuse', () => {
|
|
413
427
|
test('reusing existing tab preserves session and returns reused flag', async () => {
|
|
414
428
|
// First: open tab
|
|
415
429
|
await wt.execute({
|
|
@@ -441,7 +455,7 @@ describe('Tab reuse', () => {
|
|
|
441
455
|
|
|
442
456
|
// ─── JS Error & Network Failure Detection ────────────────────────────────────
|
|
443
457
|
|
|
444
|
-
|
|
458
|
+
describeIfBrowser('JS error and network failure detection', () => {
|
|
445
459
|
test('JS errors on page are captured in pageErrors tracker', async () => {
|
|
446
460
|
// Open tab first
|
|
447
461
|
await wt.execute({
|
|
@@ -518,7 +532,7 @@ describe('JS error and network failure detection', () => {
|
|
|
518
532
|
|
|
519
533
|
// ─── Result Structure ────────────────────────────────────────────────────────
|
|
520
534
|
|
|
521
|
-
|
|
535
|
+
describeIfBrowser('Result structure and data consistency', () => {
|
|
522
536
|
test('fetch result has required fields', async () => {
|
|
523
537
|
const result = await wt.execute(
|
|
524
538
|
{ operation: 'fetch', url: 'https://example.com', formats: ['title', 'text'] },
|
package/src/tools/webTool.js
CHANGED
|
@@ -9,18 +9,13 @@
|
|
|
9
9
|
* - Screenshot capture and AI-powered analysis
|
|
10
10
|
* - Mouse and keyboard event simulation
|
|
11
11
|
*
|
|
12
|
-
* NOTE: This tool replaces the deprecated BrowserTool (December 2024).
|
|
13
|
-
* WebTool provides equivalent functionality with a singleton browser instance
|
|
14
|
-
* architecture for better resource management.
|
|
15
|
-
*
|
|
16
12
|
* ============================================================================
|
|
17
|
-
* TODO: FEATURES TO ADD
|
|
13
|
+
* TODO: FEATURES TO ADD
|
|
18
14
|
* ============================================================================
|
|
19
15
|
*
|
|
20
16
|
* TODO 1: TIME-BASED WAIT ACTION
|
|
21
17
|
* -----------------------------------------------------------------------------
|
|
22
18
|
* Add a time-based wait capability alongside the existing 'wait-for-selector'.
|
|
23
|
-
* The deprecated BrowserTool supported: { "action": "wait", "waitTime": 3000 }
|
|
24
19
|
*
|
|
25
20
|
* Implementation details:
|
|
26
21
|
* - Add new action type: 'wait' or 'delay' (distinct from 'wait-for-selector')
|
|
@@ -46,8 +41,7 @@
|
|
|
46
41
|
* TODO 2: IMPROVED TYPE ACTION WITH ELEMENT WAITING AND CLEARING
|
|
47
42
|
* -----------------------------------------------------------------------------
|
|
48
43
|
* Enhance the 'type' action to automatically wait for the element and optionally
|
|
49
|
-
* clear existing content before typing.
|
|
50
|
-
* handling for form inputs.
|
|
44
|
+
* clear existing content before typing.
|
|
51
45
|
*
|
|
52
46
|
* Implementation details:
|
|
53
47
|
* - Before typing, wait for the selector to be visible (with configurable timeout)
|
|
@@ -279,7 +273,7 @@ First FETCH the login page to identify form selectors, then authenticate:
|
|
|
279
273
|
|
|
280
274
|
1. SEARCH - Search the web
|
|
281
275
|
\`\`\`json
|
|
282
|
-
{"toolId": "web", "operation": "search", "query": "search terms", "engine": "
|
|
276
|
+
{"toolId": "web", "operation": "search", "query": "search terms", "engine": "duckduckgo", "maxResults": 10}
|
|
283
277
|
\`\`\`
|
|
284
278
|
|
|
285
279
|
2. FETCH - Get page content (public pages only)
|
|
@@ -404,7 +398,7 @@ If blocked (CAPTCHA, access denied), use stealthLevel: "maximum" (visible browse
|
|
|
404
398
|
switch (params.operation) {
|
|
405
399
|
case 'search':
|
|
406
400
|
params.query = TagParser.extractContent(content, 'query')[0]?.trim();
|
|
407
|
-
params.engine = TagParser.extractContent(content, 'engine')[0]?.trim() || '
|
|
401
|
+
params.engine = TagParser.extractContent(content, 'engine')[0]?.trim() || 'duckduckgo';
|
|
408
402
|
const maxResults = TagParser.extractContent(content, 'max-results')[0]?.trim();
|
|
409
403
|
params.maxResults = maxResults ? parseInt(maxResults, 10) : 10;
|
|
410
404
|
// stealthLevel for search (default standard, use maximum if getting blocked)
|
|
@@ -666,7 +660,7 @@ If blocked (CAPTCHA, access denied), use stealthLevel: "maximum" (visible browse
|
|
|
666
660
|
switch (operation) {
|
|
667
661
|
case 'search':
|
|
668
662
|
result = await this.search(params.query, {
|
|
669
|
-
engine: params.engine || '
|
|
663
|
+
engine: params.engine || 'duckduckgo',
|
|
670
664
|
maxResults: params.maxResults || 10,
|
|
671
665
|
stealthLevel: effectiveStealthLevel,
|
|
672
666
|
agentId
|
|
@@ -973,7 +967,7 @@ If blocked (CAPTCHA, access denied), use stealthLevel: "maximum" (visible browse
|
|
|
973
967
|
* @returns {Promise<Object>} Search results
|
|
974
968
|
*/
|
|
975
969
|
async search(query, options = {}) {
|
|
976
|
-
const { engine = '
|
|
970
|
+
const { engine = 'duckduckgo', maxResults = 10, agentId, humanMode = true, stealthLevel = 'standard' } = options;
|
|
977
971
|
|
|
978
972
|
// Validate query
|
|
979
973
|
if (!query || typeof query !== 'string' || query.trim().length === 0) {
|
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { jest, describe, test, expect, beforeEach } from '@jest/globals';
|
|
2
|
+
import { AgentValidator, AgentFactory, AgentUtils } from '../agent.js';
|
|
3
|
+
|
|
4
|
+
describe('AgentFactory', () => {
|
|
5
|
+
test('create with name returns valid agent with generated id', () => {
|
|
6
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
7
|
+
|
|
8
|
+
expect(agent).toBeDefined();
|
|
9
|
+
expect(typeof agent.id).toBe('string');
|
|
10
|
+
expect(agent.id).toMatch(/^agent_/);
|
|
11
|
+
expect(agent.name).toBe('Test Agent');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('create sets default status to IDLE', () => {
|
|
15
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
16
|
+
expect(agent.status).toBe('idle');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('create includes default metrics with all zeros', () => {
|
|
20
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
21
|
+
|
|
22
|
+
expect(agent.metrics).toBeDefined();
|
|
23
|
+
expect(agent.metrics.totalMessages).toBe(0);
|
|
24
|
+
expect(agent.metrics.totalTokensUsed).toBe(0);
|
|
25
|
+
expect(agent.metrics.totalCost).toBe(0);
|
|
26
|
+
expect(agent.metrics.averageResponseTime).toBe(0);
|
|
27
|
+
expect(agent.metrics.errorCount).toBe(0);
|
|
28
|
+
expect(agent.metrics.toolExecutions).toBe(0);
|
|
29
|
+
expect(agent.metrics.conversationsStarted).toBe(0);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('create includes default state', () => {
|
|
33
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
34
|
+
expect(agent.state).toBeDefined();
|
|
35
|
+
expect(agent.state.currentConversationId).toBeNull();
|
|
36
|
+
expect(agent.state.messageCount).toBe(0);
|
|
37
|
+
expect(agent.state.isProcessing).toBe(false);
|
|
38
|
+
expect(agent.state.lastError).toBeNull();
|
|
39
|
+
expect(agent.state.activeTools).toEqual([]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('create uses provided type and model', () => {
|
|
43
|
+
const agent = AgentFactory.create({
|
|
44
|
+
name: 'Custom Agent',
|
|
45
|
+
type: 'system-agent',
|
|
46
|
+
model: 'anthropic-opus'
|
|
47
|
+
});
|
|
48
|
+
expect(agent.type).toBe('system-agent');
|
|
49
|
+
expect(agent.currentModel).toBe('anthropic-opus');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('create uses defaults when type/model not provided', () => {
|
|
53
|
+
const agent = AgentFactory.create({ name: 'Default Agent' });
|
|
54
|
+
expect(agent.type).toBe('user-created');
|
|
55
|
+
expect(agent.currentModel).toBe('anthropic-sonnet');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('create sets systemPrompt when provided', () => {
|
|
59
|
+
const agent = AgentFactory.create({
|
|
60
|
+
name: 'Prompted Agent',
|
|
61
|
+
systemPrompt: 'You are a helpful assistant.'
|
|
62
|
+
});
|
|
63
|
+
expect(agent.systemPrompt).toBe('You are a helpful assistant.');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('create sets empty systemPrompt by default', () => {
|
|
67
|
+
const agent = AgentFactory.create({ name: 'No Prompt' });
|
|
68
|
+
expect(agent.systemPrompt).toBe('');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('create sets metadata when provided', () => {
|
|
72
|
+
const agent = AgentFactory.create({ name: 'Meta', metadata: { project: 'test' } });
|
|
73
|
+
expect(agent.metadata).toEqual({ project: 'test' });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('create sets pause fields to null', () => {
|
|
77
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
78
|
+
expect(agent.pausedUntil).toBeNull();
|
|
79
|
+
expect(agent.pauseReason).toBeNull();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('create sets ISO timestamp fields', () => {
|
|
83
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
84
|
+
expect(new Date(agent.createdAt).toISOString()).toBe(agent.createdAt);
|
|
85
|
+
expect(new Date(agent.updatedAt).toISOString()).toBe(agent.updatedAt);
|
|
86
|
+
expect(new Date(agent.lastActivity).toISOString()).toBe(agent.lastActivity);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('create throws for missing name', () => {
|
|
90
|
+
expect(() => AgentFactory.create({})).toThrow();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('create throws for name too short', () => {
|
|
94
|
+
expect(() => AgentFactory.create({ name: 'A' })).toThrow();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('generateAgentId returns string starting with agent_', () => {
|
|
98
|
+
const id = AgentFactory.generateAgentId();
|
|
99
|
+
expect(typeof id).toBe('string');
|
|
100
|
+
expect(id).toMatch(/^agent_/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('createDefaultConfiguration', () => {
|
|
104
|
+
test('returns default config shape', () => {
|
|
105
|
+
const config = AgentFactory.createDefaultConfiguration();
|
|
106
|
+
expect(config.maxContextLength).toBe(50000);
|
|
107
|
+
expect(config.temperature).toBe(0.7);
|
|
108
|
+
expect(config.maxTokens).toBe(4096);
|
|
109
|
+
expect(config.timeout).toBe(30000);
|
|
110
|
+
expect(config.persistConversations).toBe(true);
|
|
111
|
+
expect(config.enabledTools).toEqual(['terminal', 'filesys', 'editor']);
|
|
112
|
+
expect(config.autoRetry).toBe(true);
|
|
113
|
+
expect(config.maxRetries).toBe(3);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('applies overrides', () => {
|
|
117
|
+
const config = AgentFactory.createDefaultConfiguration({ temperature: 0.5, maxTokens: 8192 });
|
|
118
|
+
expect(config.temperature).toBe(0.5);
|
|
119
|
+
expect(config.maxTokens).toBe(8192);
|
|
120
|
+
expect(config.maxContextLength).toBe(50000); // unchanged default
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('clone', () => {
|
|
125
|
+
test('clones agent with new name and id', () => {
|
|
126
|
+
const original = AgentFactory.create({ name: 'Original' });
|
|
127
|
+
const cloned = AgentFactory.clone(original, 'Cloned');
|
|
128
|
+
|
|
129
|
+
expect(cloned.name).toBe('Cloned');
|
|
130
|
+
expect(cloned.id).not.toBe(original.id);
|
|
131
|
+
expect(cloned.id).toMatch(/^agent_/);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('clone resets status to idle', () => {
|
|
135
|
+
const original = AgentFactory.create({ name: 'Original' });
|
|
136
|
+
original.status = 'active';
|
|
137
|
+
const cloned = AgentFactory.clone(original, 'Cloned');
|
|
138
|
+
expect(cloned.status).toBe('idle');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('clone resets metrics and state', () => {
|
|
142
|
+
const original = AgentFactory.create({ name: 'Original' });
|
|
143
|
+
original.metrics.totalMessages = 100;
|
|
144
|
+
original.state.messageCount = 50;
|
|
145
|
+
const cloned = AgentFactory.clone(original, 'Cloned');
|
|
146
|
+
expect(cloned.metrics.totalMessages).toBe(0);
|
|
147
|
+
expect(cloned.state.messageCount).toBe(0);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('clone resets pause fields', () => {
|
|
151
|
+
const original = AgentFactory.create({ name: 'Original' });
|
|
152
|
+
original.pausedUntil = '2025-01-01T00:00:00.000Z';
|
|
153
|
+
original.pauseReason = 'testing';
|
|
154
|
+
const cloned = AgentFactory.clone(original, 'Cloned');
|
|
155
|
+
expect(cloned.pausedUntil).toBeNull();
|
|
156
|
+
expect(cloned.pauseReason).toBeNull();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('clone preserves systemPrompt and configuration', () => {
|
|
160
|
+
const original = AgentFactory.create({ name: 'Original', systemPrompt: 'Test prompt' });
|
|
161
|
+
const cloned = AgentFactory.clone(original, 'Cloned');
|
|
162
|
+
expect(cloned.systemPrompt).toBe('Test prompt');
|
|
163
|
+
expect(cloned.configuration.maxContextLength).toBe(original.configuration.maxContextLength);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe('AgentValidator', () => {
|
|
169
|
+
test('validate returns isValid=true for valid agent', () => {
|
|
170
|
+
const agent = AgentFactory.create({ name: 'Valid Agent' });
|
|
171
|
+
const result = AgentValidator.validate(agent);
|
|
172
|
+
expect(result.isValid).toBe(true);
|
|
173
|
+
expect(result.errors).toHaveLength(0);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('validate returns errors for agent missing name', () => {
|
|
177
|
+
const agent = { id: 'agent_123', status: 'idle' };
|
|
178
|
+
const result = AgentValidator.validate(agent);
|
|
179
|
+
expect(result.isValid).toBe(false);
|
|
180
|
+
expect(result.errors.some(e => e.toLowerCase().includes('name'))).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('validate returns errors for agent missing id', () => {
|
|
184
|
+
const agent = { name: 'Test', status: 'idle' };
|
|
185
|
+
const result = AgentValidator.validate(agent);
|
|
186
|
+
expect(result.isValid).toBe(false);
|
|
187
|
+
expect(result.errors.some(e => e.toLowerCase().includes('id'))).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('validate returns errors for name too short', () => {
|
|
191
|
+
const agent = { id: 'agent_123', name: 'A', status: 'idle' };
|
|
192
|
+
const result = AgentValidator.validate(agent);
|
|
193
|
+
expect(result.isValid).toBe(false);
|
|
194
|
+
expect(result.errors.some(e => e.includes('at least 2'))).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('validate returns errors for name too long', () => {
|
|
198
|
+
const agent = { id: 'agent_123', name: 'A'.repeat(101), status: 'idle' };
|
|
199
|
+
const result = AgentValidator.validate(agent);
|
|
200
|
+
expect(result.isValid).toBe(false);
|
|
201
|
+
expect(result.errors.some(e => e.includes('less than 100'))).toBe(true);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('validate returns errors for invalid type', () => {
|
|
205
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
206
|
+
agent.type = 'invalid-type';
|
|
207
|
+
const result = AgentValidator.validate(agent);
|
|
208
|
+
expect(result.isValid).toBe(false);
|
|
209
|
+
expect(result.errors.some(e => e.includes('Invalid agent type'))).toBe(true);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test('validate returns errors for invalid status', () => {
|
|
213
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
214
|
+
agent.status = 'invalid-status';
|
|
215
|
+
const result = AgentValidator.validate(agent);
|
|
216
|
+
expect(result.isValid).toBe(false);
|
|
217
|
+
expect(result.errors.some(e => e.includes('Invalid agent status'))).toBe(true);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('validate returns warning for unknown model', () => {
|
|
221
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
222
|
+
agent.currentModel = 'unknown-model-xyz';
|
|
223
|
+
const result = AgentValidator.validate(agent);
|
|
224
|
+
expect(result.warnings.some(w => w.includes('Unknown AI model'))).toBe(true);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test('validate returns errors for non-string systemPrompt', () => {
|
|
228
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
229
|
+
agent.systemPrompt = 123;
|
|
230
|
+
const result = AgentValidator.validate(agent);
|
|
231
|
+
expect(result.errors.some(e => e.includes('System prompt must be a string'))).toBe(true);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('validate returns warning for very long systemPrompt', () => {
|
|
235
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
236
|
+
agent.systemPrompt = 'x'.repeat(10001);
|
|
237
|
+
const result = AgentValidator.validate(agent);
|
|
238
|
+
expect(result.warnings.some(w => w.includes('System prompt is very long'))).toBe(true);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test('validate returns errors for invalid timestamps', () => {
|
|
242
|
+
const agent = AgentFactory.create({ name: 'Test Agent' });
|
|
243
|
+
agent.createdAt = 'not-a-date';
|
|
244
|
+
const result = AgentValidator.validate(agent);
|
|
245
|
+
expect(result.isValid).toBe(false);
|
|
246
|
+
expect(result.errors.some(e => e.includes('Invalid timestamp'))).toBe(true);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe('validateConfiguration', () => {
|
|
250
|
+
test('accepts valid configuration', () => {
|
|
251
|
+
const result = AgentValidator.validateConfiguration({
|
|
252
|
+
maxContextLength: 50000,
|
|
253
|
+
temperature: 0.7,
|
|
254
|
+
maxTokens: 4096,
|
|
255
|
+
timeout: 30000,
|
|
256
|
+
maxRetries: 3,
|
|
257
|
+
enabledTools: ['terminal']
|
|
258
|
+
});
|
|
259
|
+
expect(result.errors).toHaveLength(0);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('errors on non-number maxContextLength', () => {
|
|
263
|
+
const result = AgentValidator.validateConfiguration({ maxContextLength: 'big' });
|
|
264
|
+
expect(result.errors.some(e => e.includes('maxContextLength'))).toBe(true);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('warns on low maxContextLength', () => {
|
|
268
|
+
const result = AgentValidator.validateConfiguration({ maxContextLength: 500 });
|
|
269
|
+
expect(result.warnings.some(w => w.includes('maxContextLength is very low'))).toBe(true);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('errors on non-number temperature', () => {
|
|
273
|
+
const result = AgentValidator.validateConfiguration({ temperature: 'warm' });
|
|
274
|
+
expect(result.errors.some(e => e.includes('temperature must be a number'))).toBe(true);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test('errors on temperature out of range', () => {
|
|
278
|
+
const result = AgentValidator.validateConfiguration({ temperature: 5 });
|
|
279
|
+
expect(result.errors.some(e => e.includes('temperature must be between'))).toBe(true);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('errors on non-number maxTokens', () => {
|
|
283
|
+
const result = AgentValidator.validateConfiguration({ maxTokens: 'many' });
|
|
284
|
+
expect(result.errors.some(e => e.includes('maxTokens'))).toBe(true);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('errors on non-number timeout', () => {
|
|
288
|
+
const result = AgentValidator.validateConfiguration({ timeout: 'slow' });
|
|
289
|
+
expect(result.errors.some(e => e.includes('timeout must be a number'))).toBe(true);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test('warns on low timeout', () => {
|
|
293
|
+
const result = AgentValidator.validateConfiguration({ timeout: 500 });
|
|
294
|
+
expect(result.warnings.some(w => w.includes('timeout is very low'))).toBe(true);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test('errors on non-number maxRetries', () => {
|
|
298
|
+
const result = AgentValidator.validateConfiguration({ maxRetries: 'three' });
|
|
299
|
+
expect(result.errors.some(e => e.includes('maxRetries'))).toBe(true);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('errors on non-array enabledTools', () => {
|
|
303
|
+
const result = AgentValidator.validateConfiguration({ enabledTools: 'terminal' });
|
|
304
|
+
expect(result.errors.some(e => e.includes('enabledTools must be an array'))).toBe(true);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
describe('validateCreationParams', () => {
|
|
309
|
+
test('rejects missing name', () => {
|
|
310
|
+
const result = AgentValidator.validateCreationParams({});
|
|
311
|
+
expect(result.isValid).toBe(false);
|
|
312
|
+
expect(result.errors.some(e => e.toLowerCase().includes('name'))).toBe(true);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
test('rejects short name', () => {
|
|
316
|
+
const result = AgentValidator.validateCreationParams({ name: 'A' });
|
|
317
|
+
expect(result.isValid).toBe(false);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
test('rejects invalid type', () => {
|
|
321
|
+
const result = AgentValidator.validateCreationParams({ name: 'Test', type: 'bad-type' });
|
|
322
|
+
expect(result.isValid).toBe(false);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
test('warns on unknown model', () => {
|
|
326
|
+
const result = AgentValidator.validateCreationParams({ name: 'Test', model: 'unknown-xyz' });
|
|
327
|
+
expect(result.isValid).toBe(true);
|
|
328
|
+
expect(result.warnings.some(w => w.includes('Unknown AI model'))).toBe(true);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('rejects non-string systemPrompt', () => {
|
|
332
|
+
const result = AgentValidator.validateCreationParams({ name: 'Test', systemPrompt: 123 });
|
|
333
|
+
expect(result.isValid).toBe(false);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test('rejects non-array enabledTools', () => {
|
|
337
|
+
const result = AgentValidator.validateCreationParams({ name: 'Test', enabledTools: 'terminal' });
|
|
338
|
+
expect(result.isValid).toBe(false);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test('accepts valid params', () => {
|
|
342
|
+
const result = AgentValidator.validateCreationParams({ name: 'Valid Agent' });
|
|
343
|
+
expect(result.isValid).toBe(true);
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
describe('isValidTimestamp', () => {
|
|
348
|
+
test('returns true for valid ISO string', () => {
|
|
349
|
+
expect(AgentValidator.isValidTimestamp('2025-01-01T00:00:00.000Z')).toBe(true);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
test('returns false for non-string', () => {
|
|
353
|
+
expect(AgentValidator.isValidTimestamp(12345)).toBe(false);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('returns false for invalid date string', () => {
|
|
357
|
+
expect(AgentValidator.isValidTimestamp('not-a-date')).toBe(false);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe('AgentUtils', () => {
|
|
363
|
+
test('isActive returns false for idle agent', () => {
|
|
364
|
+
const agent = AgentFactory.create({ name: 'Idle Agent' });
|
|
365
|
+
expect(AgentUtils.isActive(agent)).toBe(false);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test('isActive returns true for active agent', () => {
|
|
369
|
+
const agent = AgentFactory.create({ name: 'Active Agent' });
|
|
370
|
+
agent.status = 'active';
|
|
371
|
+
expect(AgentUtils.isActive(agent)).toBe(true);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
test('isActive returns true for busy agent', () => {
|
|
375
|
+
const agent = AgentFactory.create({ name: 'Busy Agent' });
|
|
376
|
+
agent.status = 'busy';
|
|
377
|
+
expect(AgentUtils.isActive(agent)).toBe(true);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
describe('isPaused', () => {
|
|
381
|
+
test('returns false for non-paused agent', () => {
|
|
382
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
383
|
+
expect(AgentUtils.isPaused(agent)).toBe(false);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
test('returns true for paused agent without expiry', () => {
|
|
387
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
388
|
+
agent.status = 'paused';
|
|
389
|
+
expect(AgentUtils.isPaused(agent)).toBe(true);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
test('returns true for paused agent with future expiry', () => {
|
|
393
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
394
|
+
agent.status = 'paused';
|
|
395
|
+
agent.pausedUntil = new Date(Date.now() + 60000).toISOString();
|
|
396
|
+
expect(AgentUtils.isPaused(agent)).toBe(true);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test('returns false for paused agent with past expiry', () => {
|
|
400
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
401
|
+
agent.status = 'paused';
|
|
402
|
+
agent.pausedUntil = new Date(Date.now() - 60000).toISOString();
|
|
403
|
+
expect(AgentUtils.isPaused(agent)).toBe(false);
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe('getEffectiveStatus', () => {
|
|
408
|
+
test('returns idle for paused agent with expired pause', () => {
|
|
409
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
410
|
+
agent.status = 'paused';
|
|
411
|
+
agent.pausedUntil = new Date(Date.now() - 60000).toISOString();
|
|
412
|
+
expect(AgentUtils.getEffectiveStatus(agent)).toBe('idle');
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
test('returns paused for agent with future pause', () => {
|
|
416
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
417
|
+
agent.status = 'paused';
|
|
418
|
+
agent.pausedUntil = new Date(Date.now() + 60000).toISOString();
|
|
419
|
+
expect(AgentUtils.getEffectiveStatus(agent)).toBe('paused');
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
test('returns actual status for non-paused agent', () => {
|
|
423
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
424
|
+
agent.status = 'active';
|
|
425
|
+
expect(AgentUtils.getEffectiveStatus(agent)).toBe('active');
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
describe('getTimeUntilPauseExpiry', () => {
|
|
430
|
+
test('returns null for non-paused agent', () => {
|
|
431
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
432
|
+
expect(AgentUtils.getTimeUntilPauseExpiry(agent)).toBeNull();
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
test('returns null for paused agent without pausedUntil', () => {
|
|
436
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
437
|
+
agent.status = 'paused';
|
|
438
|
+
expect(AgentUtils.getTimeUntilPauseExpiry(agent)).toBeNull();
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
test('returns positive ms for paused agent with future expiry', () => {
|
|
442
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
443
|
+
agent.status = 'paused';
|
|
444
|
+
agent.pausedUntil = new Date(Date.now() + 60000).toISOString();
|
|
445
|
+
const result = AgentUtils.getTimeUntilPauseExpiry(agent);
|
|
446
|
+
expect(result).toBeGreaterThan(0);
|
|
447
|
+
expect(result).toBeLessThanOrEqual(60000);
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
describe('formatForDisplay', () => {
|
|
452
|
+
test('returns formatted agent data', () => {
|
|
453
|
+
const agent = AgentFactory.create({ name: 'Display Agent' });
|
|
454
|
+
const result = AgentUtils.formatForDisplay(agent);
|
|
455
|
+
expect(result.id).toBe(agent.id);
|
|
456
|
+
expect(result.name).toBe('Display Agent');
|
|
457
|
+
expect(result.type).toBe('user-created');
|
|
458
|
+
expect(result.status).toBe('idle');
|
|
459
|
+
expect(result.model).toBe('anthropic-sonnet');
|
|
460
|
+
expect(result.messageCount).toBe(0);
|
|
461
|
+
expect(result.lastActivity).toBeDefined();
|
|
462
|
+
expect(result.isPaused).toBe(false);
|
|
463
|
+
expect(result.timeUntilPauseExpiry).toBeNull();
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
describe('sanitize', () => {
|
|
468
|
+
test('removes cache and sessionData from state', () => {
|
|
469
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
470
|
+
agent.state.cache = { key: 'value' };
|
|
471
|
+
agent.state.sessionData = { session: 'data' };
|
|
472
|
+
const result = AgentUtils.sanitize(agent);
|
|
473
|
+
expect(result.state.cache).toBeUndefined();
|
|
474
|
+
expect(result.state.sessionData).toBeUndefined();
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
test('truncates long systemPrompt', () => {
|
|
478
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
479
|
+
agent.systemPrompt = 'x'.repeat(1000);
|
|
480
|
+
const result = AgentUtils.sanitize(agent);
|
|
481
|
+
expect(result.systemPrompt.length).toBeLessThanOrEqual(503); // 500 + '...'
|
|
482
|
+
expect(result.systemPrompt.endsWith('...')).toBe(true);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
test('preserves short systemPrompt', () => {
|
|
486
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
487
|
+
agent.systemPrompt = 'short prompt';
|
|
488
|
+
const result = AgentUtils.sanitize(agent);
|
|
489
|
+
expect(result.systemPrompt).toBe('short prompt');
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
test('preserves other agent fields', () => {
|
|
493
|
+
const agent = AgentFactory.create({ name: 'Agent' });
|
|
494
|
+
const result = AgentUtils.sanitize(agent);
|
|
495
|
+
expect(result.id).toBe(agent.id);
|
|
496
|
+
expect(result.name).toBe(agent.name);
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
});
|