-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
567 lines (462 loc) · 18 KB
/
index.test.js
File metadata and controls
567 lines (462 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* Tests for RoboSystems MCP Client
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
// Mock environment variables before importing index.js
process.env.ROBOSYSTEMS_API_KEY = 'test-api-key'
process.env.ROBOSYSTEMS_GRAPH_ID = 'test-graph-id'
// Mock EventSource before importing index.js
vi.mock('eventsource', () => {
const MockEventSource = class {
constructor() {
this.close = vi.fn()
this.addEventListener = vi.fn()
this.onmessage = vi.fn()
}
}
return {
EventSource: MockEventSource,
}
})
// Import after mocking
import { RoboSystemsMCPClient, SSEConnectionPool, ResultCache } from './index.js'
describe('SSEConnectionPool', () => {
let pool
beforeEach(() => {
pool = new SSEConnectionPool(3)
})
afterEach(() => {
pool.closeAll()
})
it('should create a new connection when none exists', () => {
const eventSource = pool.getConnection('test-1', 'http://example.com', {})
expect(eventSource).toBeDefined()
expect(pool.connections.size).toBe(1)
})
it('should reuse an existing connection', () => {
const eventSource1 = pool.getConnection('test-1', 'http://example.com', {})
const eventSource2 = pool.getConnection('test-1', 'http://example.com', {})
expect(eventSource1).toBe(eventSource2)
expect(pool.connections.size).toBe(1)
})
it('should evict oldest connection when at max capacity', async () => {
// Create connections with small delays to ensure different lastUsed times
pool.getConnection('test-1', 'http://example.com', {})
await new Promise((resolve) => setTimeout(resolve, 10))
pool.getConnection('test-2', 'http://example.com', {})
await new Promise((resolve) => setTimeout(resolve, 10))
pool.getConnection('test-3', 'http://example.com', {})
expect(pool.connections.size).toBe(3)
// Small delay before adding 4th connection
await new Promise((resolve) => setTimeout(resolve, 10))
// Should evict oldest when adding a 4th connection
pool.getConnection('test-4', 'http://example.com', {})
expect(pool.connections.size).toBe(3)
expect(pool.connections.has('test-1')).toBe(false)
})
it('should close a specific connection', () => {
pool.getConnection('test-1', 'http://example.com', {})
expect(pool.connections.size).toBe(1)
pool.closeConnection('test-1')
expect(pool.connections.size).toBe(0)
})
it('should close all connections', () => {
pool.getConnection('test-1', 'http://example.com', {})
pool.getConnection('test-2', 'http://example.com', {})
expect(pool.connections.size).toBe(2)
pool.closeAll()
expect(pool.connections.size).toBe(0)
})
})
describe('ResultCache', () => {
let cache
beforeEach(() => {
cache = new ResultCache()
})
afterEach(() => {
cache.clear()
})
it('should generate consistent cache keys for the same inputs', () => {
const key1 = cache.generateKey('tool-1', { arg1: 'value1', arg2: 'value2' })
const key2 = cache.generateKey('tool-1', { arg2: 'value2', arg1: 'value1' })
expect(key1).toBe(key2)
})
it('should generate different cache keys for different inputs', () => {
const key1 = cache.generateKey('tool-1', { arg1: 'value1' })
const key2 = cache.generateKey('tool-1', { arg1: 'value2' })
expect(key1).not.toBe(key2)
})
it('should generate different cache keys for different workspaces', () => {
const key1 = cache.generateKey('tool-1', { arg1: 'value1' }, 'workspace-1')
const key2 = cache.generateKey('tool-1', { arg1: 'value1' }, 'workspace-2')
const key3 = cache.generateKey('tool-1', { arg1: 'value1' })
expect(key1).not.toBe(key2)
expect(key1).not.toBe(key3)
expect(key2).not.toBe(key3)
})
it('should isolate cache entries by workspace', () => {
const result1 = { type: 'text', text: 'workspace 1 result' }
const result2 = { type: 'text', text: 'workspace 2 result' }
cache.set('tool-1', { arg1: 'value1' }, result1, 10, 'workspace-1')
cache.set('tool-1', { arg1: 'value1' }, result2, 10, 'workspace-2')
const retrieved1 = cache.get('tool-1', { arg1: 'value1' }, 'workspace-1')
const retrieved2 = cache.get('tool-1', { arg1: 'value1' }, 'workspace-2')
expect(retrieved1).toEqual(result1)
expect(retrieved2).toEqual(result2)
expect(retrieved1).not.toEqual(retrieved2)
})
it('should store and retrieve cached results', () => {
const result = { type: 'text', text: 'test result' }
cache.set('tool-1', { arg1: 'value1' }, result, 10)
const retrieved = cache.get('tool-1', { arg1: 'value1' })
expect(retrieved).toEqual(result)
})
it('should return null for non-existent cache entries', () => {
const retrieved = cache.get('tool-1', { arg1: 'value1' })
expect(retrieved).toBeNull()
})
it('should expire cached results after TTL', async () => {
const result = { type: 'text', text: 'test result' }
cache.set('tool-1', { arg1: 'value1' }, result, 0.05) // 50ms TTL
// Should be cached initially
let retrieved = cache.get('tool-1', { arg1: 'value1' })
expect(retrieved).toEqual(result)
// Wait for expiration
await new Promise((resolve) => setTimeout(resolve, 100))
// Should be expired now
retrieved = cache.get('tool-1', { arg1: 'value1' })
expect(retrieved).toBeNull()
})
it('should handle nested objects in cache keys', () => {
const args1 = { level1: { level2: { value: 'test' } } }
const args2 = { level1: { level2: { value: 'test' } } }
const key1 = cache.generateKey('tool-1', args1)
const key2 = cache.generateKey('tool-1', args2)
expect(key1).toBe(key2)
})
it('should handle arrays in cache keys', () => {
const args1 = { items: [1, 2, 3] }
const args2 = { items: [1, 2, 3] }
const key1 = cache.generateKey('tool-1', args1)
const key2 = cache.generateKey('tool-1', args2)
expect(key1).toBe(key2)
})
it('should clear all cached results', () => {
cache.set('tool-1', { arg1: 'value1' }, { type: 'text', text: 'result1' }, 10)
cache.set('tool-2', { arg1: 'value1' }, { type: 'text', text: 'result2' }, 10)
expect(cache.cache.size).toBe(2)
cache.clear()
expect(cache.cache.size).toBe(0)
})
})
describe('RoboSystemsMCPClient', () => {
let client
let fetchMock
beforeEach(() => {
fetchMock = vi.fn()
global.fetch = fetchMock
client = new RoboSystemsMCPClient('https://api.example.com', 'test-api-key', 'test-graph-id')
})
afterEach(() => {
client.cleanup()
vi.clearAllMocks()
})
describe('constructor', () => {
it('should initialize with correct base URL', () => {
expect(client.baseUrl).toBe('https://api.example.com')
})
it('should remove trailing slash from base URL', () => {
const clientWithSlash = new RoboSystemsMCPClient(
'https://api.example.com/',
'test-api-key',
'test-graph-id'
)
expect(clientWithSlash.baseUrl).toBe('https://api.example.com')
})
it('should initialize with correct API key', () => {
expect(client.apiKey).toBe('test-api-key')
})
it('should initialize with correct graph IDs', () => {
expect(client.primaryGraphId).toBe('test-graph-id')
expect(client.activeGraphId).toBe('test-graph-id')
expect(client.graphId).toBe('test-graph-id')
})
it('should set up proper headers', () => {
expect(client.headers['X-API-Key']).toBe('test-api-key')
expect(client.headers['Content-Type']).toBe('application/json')
expect(client.headers['User-Agent']).toContain('robosystems-mcp/')
})
it('should initialize components', () => {
expect(client.connectionPool).toBeInstanceOf(SSEConnectionPool)
expect(client.resultCache).toBeInstanceOf(ResultCache)
})
it('should initialize primary workspace', () => {
expect(client.workspaces.size).toBe(1)
expect(client.workspaces.has('test-graph-id')).toBe(true)
const primaryWorkspace = client.workspaces.get('test-graph-id')
expect(primaryWorkspace.type).toBe('primary')
expect(primaryWorkspace.name).toBe('main')
})
it('should initialize metrics', () => {
expect(client.metrics.totalRequests).toBe(0)
expect(client.metrics.cacheHits).toBe(0)
expect(client.metrics.errors).toBe(0)
expect(client.metrics.workspaceSwitches).toBe(0)
})
})
describe('getTools', () => {
it('should fetch tools from API successfully', async () => {
const mockTools = [
{
name: 'test-tool',
description: 'A test tool',
inputSchema: { type: 'object' },
},
]
fetchMock.mockResolvedValueOnce({
ok: true,
json: async () => ({ tools: mockTools }),
})
const tools = await client.getTools()
expect(tools).toHaveLength(mockTools.length + 4) // +4 for workspace tools
expect(tools[0].name).toBe('test-tool')
})
it('should add workspace tools when not provided by server', async () => {
fetchMock.mockResolvedValueOnce({
ok: true,
json: async () => ({ tools: [] }),
})
const tools = await client.getTools()
const workspaceToolNames = tools.map((t) => t.name)
expect(workspaceToolNames).toContain('create-workspace')
expect(workspaceToolNames).toContain('switch-workspace')
expect(workspaceToolNames).toContain('delete-workspace')
expect(workspaceToolNames).toContain('list-workspaces')
})
it('should handle API errors gracefully', async () => {
fetchMock.mockResolvedValueOnce({
ok: false,
status: 500,
statusText: 'Internal Server Error',
text: async () => 'Error details',
})
const tools = await client.getTools()
expect(tools).toEqual([])
})
it('should handle network errors gracefully', async () => {
fetchMock.mockRejectedValueOnce(new Error('Network error'))
const tools = await client.getTools()
expect(tools).toEqual([])
})
})
describe('callTool - caching', () => {
it('should cache results for cacheable tools', async () => {
const mockResult = { result: { type: 'text', text: 'test result' } }
fetchMock.mockResolvedValue({
ok: true,
headers: new Map([['content-type', 'application/json']]),
json: async () => mockResult,
})
// First call - should hit API
await client.callTool('get-graph-schema', { arg1: 'value1' })
expect(fetchMock).toHaveBeenCalledTimes(1)
// Second call - should use cache
await client.callTool('get-graph-schema', { arg1: 'value1' })
expect(fetchMock).toHaveBeenCalledTimes(1) // Still 1, not 2
expect(client.metrics.cacheHits).toBe(1)
})
it('should not cache results for non-cacheable tools', async () => {
const mockResult = { result: { type: 'text', text: 'test result' } }
fetchMock.mockResolvedValue({
ok: true,
headers: new Map([['content-type', 'application/json']]),
json: async () => mockResult,
})
// First call
await client.callTool('non-cacheable-tool', { arg1: 'value1' })
expect(fetchMock).toHaveBeenCalledTimes(1)
// Second call - should hit API again
await client.callTool('non-cacheable-tool', { arg1: 'value1' })
expect(fetchMock).toHaveBeenCalledTimes(2)
expect(client.metrics.cacheHits).toBe(0)
})
})
describe('isCacheable', () => {
it('should identify cacheable tools', () => {
expect(client.isCacheable('get-graph-schema')).toBe(true)
expect(client.isCacheable('get-graph-info')).toBe(true)
expect(client.isCacheable('describe-graph-structure')).toBe(true)
})
it('should identify non-cacheable tools', () => {
expect(client.isCacheable('some-other-tool')).toBe(false)
})
})
describe('getCacheTTL', () => {
it('should return correct TTL for known tools', () => {
expect(client.getCacheTTL('get-graph-schema')).toBe(3600)
expect(client.getCacheTTL('get-graph-info')).toBe(300)
expect(client.getCacheTTL('describe-graph-structure')).toBe(1800)
})
it('should return default TTL for unknown tools', () => {
expect(client.getCacheTTL('unknown-tool')).toBe(300)
})
})
describe('workspace management', () => {
it('should switch workspace correctly', async () => {
// Add a test workspace
client.workspaces.set('test-workspace-1', {
type: 'workspace',
name: 'test',
parent_graph_id: client.primaryGraphId,
})
const result = await client._handleSwitchWorkspace({
workspace_id: 'test-workspace-1',
})
expect(client.activeGraphId).toBe('test-workspace-1')
expect(client.metrics.workspaceSwitches).toBe(1)
const parsedResult = JSON.parse(result.text)
expect(parsedResult.success).toBe(true)
expect(parsedResult.switched_to).toBe('test-workspace-1')
})
it('should switch to primary workspace using "primary" alias', async () => {
// First switch to a different workspace
client.workspaces.set('test-workspace-1', {
type: 'workspace',
name: 'test',
parent_graph_id: client.primaryGraphId,
})
client.activeGraphId = 'test-workspace-1'
// Now switch back to primary
const result = await client._handleSwitchWorkspace({
workspace_id: 'primary',
})
expect(client.activeGraphId).toBe(client.primaryGraphId)
const parsedResult = JSON.parse(result.text)
expect(parsedResult.success).toBe(true)
})
it('should handle switching to unknown workspace', async () => {
const result = await client._handleSwitchWorkspace({
workspace_id: 'unknown-workspace',
})
const parsedResult = JSON.parse(result.text)
expect(parsedResult.error).toBe('Unknown workspace')
})
it('should handle switching to current workspace', async () => {
const result = await client._handleSwitchWorkspace({
workspace_id: client.primaryGraphId,
})
const parsedResult = JSON.parse(result.text)
expect(parsedResult.success).toBe(true)
expect(parsedResult.message).toContain('Already in workspace')
})
})
describe('aggregateStreamedResults', () => {
it('should return error event if present', () => {
const events = [
{ event: 'data', data: { value: 'test' } },
{ event: 'error', data: { error: 'Something went wrong' } },
]
const result = client.aggregateStreamedResults(events)
expect(result.type).toBe('text')
expect(result.text).toContain('Error: Something went wrong')
})
it('should aggregate query chunks', () => {
const events = [
{
event: 'query_chunk',
data: { columns: ['col1', 'col2'], data: [[1, 2]] },
},
{ event: 'query_chunk', data: { data: [[3, 4]] } },
]
const result = client.aggregateStreamedResults(events)
expect(result.type).toBe('text')
const parsed = JSON.parse(result.text)
expect(parsed.columns).toEqual(['col1', 'col2'])
expect(parsed.data).toEqual([
[1, 2],
[3, 4],
])
expect(parsed.row_count).toBe(2)
})
it('should handle operation_completed event', () => {
const events = [
{
event: 'operation_completed',
data: { result: { success: true, message: 'Done' } },
},
]
const result = client.aggregateStreamedResults(events)
expect(result.type).toBe('text')
const parsed = JSON.parse(result.text)
expect(parsed.success).toBe(true)
})
it('should return all events as fallback', () => {
const events = [
{ event: 'unknown', data: { value: 1 } },
{ event: 'unknown', data: { value: 2 } },
]
const result = client.aggregateStreamedResults(events)
expect(result.type).toBe('text')
const parsed = JSON.parse(result.text)
expect(parsed).toHaveLength(2)
})
})
describe('getMetrics', () => {
it('should return correct metrics', () => {
client.metrics.totalRequests = 100
client.metrics.cacheHits = 25
client.metrics.errors = 5
const metrics = client.getMetrics()
expect(metrics.totalRequests).toBe(100)
expect(metrics.cacheHits).toBe(25)
expect(metrics.cacheHitRate).toBe('25.0%')
expect(metrics.errors).toBe(5)
expect(metrics.activeWorkspace).toBe('test-graph-id')
expect(metrics.totalWorkspaces).toBe(1)
})
it('should handle zero requests', () => {
const metrics = client.getMetrics()
expect(metrics.cacheHitRate).toBe('0%')
})
})
describe('cleanup', () => {
it('should close all connections and clear cache', () => {
const closeAllSpy = vi.spyOn(client.connectionPool, 'closeAll')
const clearCacheSpy = vi.spyOn(client.resultCache, 'clear')
client.cleanup()
expect(closeAllSpy).toHaveBeenCalled()
expect(clearCacheSpy).toHaveBeenCalled()
})
})
describe('executeWithRetry', () => {
it('should succeed on first attempt', async () => {
const fn = vi.fn().mockResolvedValue({ success: true })
const result = await client.executeWithRetry(fn)
expect(fn).toHaveBeenCalledTimes(1)
expect(result.success).toBe(true)
})
it('should retry on failure', async () => {
const fn = vi
.fn()
.mockRejectedValueOnce(new Error('500 error'))
.mockResolvedValueOnce({ success: true })
const result = await client.executeWithRetry(fn)
expect(fn).toHaveBeenCalledTimes(2)
expect(result.success).toBe(true)
})
it('should not retry on auth errors', async () => {
const fn = vi.fn().mockRejectedValue(new Error('401 Unauthorized'))
const result = await client.executeWithRetry(fn)
expect(fn).toHaveBeenCalledTimes(1)
expect(result.type).toBe('text')
expect(result.text).toContain('401 Unauthorized')
})
it('should return error after max retries', async () => {
const fn = vi.fn().mockRejectedValue(new Error('500 error'))
const result = await client.executeWithRetry(fn)
expect(fn).toHaveBeenCalledTimes(3) // maxRetries = 3
expect(result.type).toBe('text')
expect(result.text).toContain('Error after 3 attempts')
})
})
})