@mastra/client-js 0.1.0-alpha.17 → 0.1.0-alpha.18

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/src/index.test.ts CHANGED
@@ -1,587 +1,597 @@
1
- import { describe, expect, beforeEach, it, vi } from 'vitest';
2
- import { MastraClient } from "./client";
3
1
  import type { MessageType } from '@mastra/core';
2
+ import { describe, expect, beforeEach, it, vi } from 'vitest';
3
+
4
+ import { MastraClient } from './client';
4
5
 
5
6
  // Mock fetch globally
6
7
  global.fetch = vi.fn();
7
8
 
8
9
  describe('MastraClient Resources', () => {
9
- let client: MastraClient;
10
- const clientOptions = {
11
- baseUrl: 'http://localhost:4111',
10
+ let client: MastraClient;
11
+ const clientOptions = {
12
+ baseUrl: 'http://localhost:4111',
13
+ headers: {
14
+ Authorization: 'Bearer test-key',
15
+ },
16
+ };
17
+
18
+ // Helper to mock successful API responses
19
+ const mockFetchResponse = (data: any, options: { isStream?: boolean } = {}) => {
20
+ if (options.isStream) {
21
+ const stream = new ReadableStream({
22
+ start(controller) {
23
+ controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
24
+ controller.close();
25
+ },
26
+ });
27
+ (global.fetch as any).mockResolvedValueOnce({
28
+ ok: true,
29
+ headers: {
30
+ get: (name: string) => (name === 'Content-Type' ? 'text/event-stream' : null),
31
+ },
32
+ body: stream,
33
+ });
34
+ } else {
35
+ (global.fetch as any).mockResolvedValueOnce({
36
+ ok: true,
12
37
  headers: {
13
- 'Authorization': 'Bearer test-key'
14
- }
15
- };
16
-
17
- // Helper to mock successful API responses
18
- const mockFetchResponse = (data: any, options: { isStream?: boolean } = {}) => {
19
- if (options.isStream) {
20
- const stream = new ReadableStream({
21
- start(controller) {
22
- controller.enqueue(new TextEncoder().encode(JSON.stringify(data)));
23
- controller.close();
24
- }
25
- });
26
- (global.fetch as any).mockResolvedValueOnce({
27
- ok: true,
28
- headers: {
29
- get: (name: string) => name === 'Content-Type' ? 'text/event-stream' : null
30
- },
31
- body: stream
32
- });
33
- } else {
34
- (global.fetch as any).mockResolvedValueOnce({
35
- ok: true,
36
- headers: {
37
- get: (name: string) => name === 'Content-Type' ? 'application/json' : null
38
- },
39
- json: async () => data
40
- });
41
- }
42
- };
38
+ get: (name: string) => (name === 'Content-Type' ? 'application/json' : null),
39
+ },
40
+ json: async () => data,
41
+ });
42
+ }
43
+ };
44
+
45
+ beforeEach(() => {
46
+ vi.clearAllMocks();
47
+ client = new MastraClient(clientOptions);
48
+ });
49
+
50
+ describe('Vector Resource', () => {
51
+ const vectorName = 'test-vector';
52
+ let vector: ReturnType<typeof client.getVector>;
43
53
 
44
54
  beforeEach(() => {
45
- vi.clearAllMocks();
46
- client = new MastraClient(clientOptions);
55
+ vector = client.getVector(vectorName);
47
56
  });
48
57
 
49
- describe('Vector Resource', () => {
50
- const vectorName = 'test-vector';
51
- let vector: ReturnType<typeof client.getVector>;
58
+ it('should get vector index details', async () => {
59
+ const mockResponse = {
60
+ dimension: 128,
61
+ metric: 'cosine',
62
+ count: 1000,
63
+ };
64
+ mockFetchResponse(mockResponse);
65
+
66
+ const result = await vector.details('test-index');
67
+ expect(result).toEqual(mockResponse);
68
+ expect(global.fetch).toHaveBeenCalledWith(
69
+ `${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
70
+ expect.objectContaining({
71
+ headers: expect.objectContaining(clientOptions.headers),
72
+ }),
73
+ );
74
+ });
52
75
 
53
- beforeEach(() => {
54
- vector = client.getVector(vectorName);
55
- });
76
+ it('should delete vector index', async () => {
77
+ mockFetchResponse({ success: true });
78
+ const result = await vector.delete('test-index');
79
+ expect(result).toEqual({ success: true });
80
+ expect(global.fetch).toHaveBeenCalledWith(
81
+ `${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
82
+ expect.objectContaining({
83
+ method: 'DELETE',
84
+ headers: expect.objectContaining(clientOptions.headers),
85
+ }),
86
+ );
87
+ });
56
88
 
57
- it('should get vector index details', async () => {
58
- const mockResponse = {
59
- dimension: 128,
60
- metric: 'cosine',
61
- count: 1000
62
- };
63
- mockFetchResponse(mockResponse);
64
-
65
- const result = await vector.details('test-index');
66
- expect(result).toEqual(mockResponse);
67
- expect(global.fetch).toHaveBeenCalledWith(
68
- `${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
69
- expect.objectContaining({
70
- headers: expect.objectContaining(clientOptions.headers)
71
- })
72
- );
73
- });
89
+ it('should get all indexes', async () => {
90
+ const mockResponse = { indexes: ['index1', 'index2'] };
91
+ mockFetchResponse(mockResponse);
92
+ const result = await vector.getIndexes();
93
+ expect(result).toEqual(mockResponse);
94
+ expect(global.fetch).toHaveBeenCalledWith(
95
+ `${clientOptions.baseUrl}/api/vector/test-vector/indexes`,
96
+ expect.objectContaining({
97
+ headers: expect.objectContaining(clientOptions.headers),
98
+ }),
99
+ );
100
+ });
74
101
 
75
- it('should delete vector index', async () => {
76
- mockFetchResponse({ success: true });
77
- const result = await vector.delete('test-index');
78
- expect(result).toEqual({ success: true });
79
- expect(global.fetch).toHaveBeenCalledWith(
80
- `${clientOptions.baseUrl}/api/vector/test-vector/indexes/test-index`,
81
- expect.objectContaining({
82
- method: 'DELETE',
83
- headers: expect.objectContaining(clientOptions.headers)
84
- })
85
- );
86
- });
102
+ it('should create vector index with all parameters', async () => {
103
+ mockFetchResponse({ success: true });
104
+ const result = await vector.createIndex({
105
+ indexName: 'test-index',
106
+ dimension: 128,
107
+ metric: 'cosine',
108
+ });
109
+ expect(result).toEqual({ success: true });
110
+ expect(global.fetch).toHaveBeenCalledWith(
111
+ `${clientOptions.baseUrl}/api/vector/test-vector/create-index`,
112
+ expect.objectContaining({
113
+ method: 'POST',
114
+ headers: expect.objectContaining(clientOptions.headers),
115
+ body: JSON.stringify({
116
+ indexName: 'test-index',
117
+ dimension: 128,
118
+ metric: 'cosine',
119
+ }),
120
+ }),
121
+ );
122
+ });
87
123
 
88
- it('should get all indexes', async () => {
89
- const mockResponse = { indexes: ['index1', 'index2'] };
90
- mockFetchResponse(mockResponse);
91
- const result = await vector.getIndexes();
92
- expect(result).toEqual(mockResponse);
93
- expect(global.fetch).toHaveBeenCalledWith(
94
- `${clientOptions.baseUrl}/api/vector/test-vector/indexes`,
95
- expect.objectContaining({
96
- headers: expect.objectContaining(clientOptions.headers)
97
- })
98
- );
99
- });
124
+ it('should upsert vectors with metadata and ids', async () => {
125
+ const mockResponse = ['id1', 'id2'];
126
+ mockFetchResponse(mockResponse);
127
+ const result = await vector.upsert({
128
+ indexName: 'test-index',
129
+ vectors: [
130
+ [1, 2],
131
+ [3, 4],
132
+ ],
133
+ metadata: [{ label: 'a' }, { label: 'b' }],
134
+ ids: ['id1', 'id2'],
135
+ });
136
+ expect(result).toEqual(mockResponse);
137
+ expect(global.fetch).toHaveBeenCalledWith(
138
+ `${clientOptions.baseUrl}/api/vector/test-vector/upsert`,
139
+ expect.objectContaining({
140
+ method: 'POST',
141
+ headers: expect.objectContaining(clientOptions.headers),
142
+ body: JSON.stringify({
143
+ indexName: 'test-index',
144
+ vectors: [
145
+ [1, 2],
146
+ [3, 4],
147
+ ],
148
+ metadata: [{ label: 'a' }, { label: 'b' }],
149
+ ids: ['id1', 'id2'],
150
+ }),
151
+ }),
152
+ );
153
+ });
100
154
 
101
- it('should create vector index with all parameters', async () => {
102
- mockFetchResponse({ success: true });
103
- const result = await vector.createIndex({
104
- indexName: 'test-index',
105
- dimension: 128,
106
- metric: 'cosine'
107
- });
108
- expect(result).toEqual({ success: true });
109
- expect(global.fetch).toHaveBeenCalledWith(
110
- `${clientOptions.baseUrl}/api/vector/test-vector/create-index`,
111
- expect.objectContaining({
112
- method: 'POST',
113
- headers: expect.objectContaining(clientOptions.headers),
114
- body: JSON.stringify({
115
- indexName: 'test-index',
116
- dimension: 128,
117
- metric: 'cosine'
118
- })
119
- })
120
- );
121
- });
155
+ it('should query vectors with all parameters', async () => {
156
+ const mockResponse = {
157
+ results: [
158
+ {
159
+ id: 'id1',
160
+ score: 0.9,
161
+ metadata: { label: 'a' },
162
+ vector: [1, 2],
163
+ },
164
+ ],
165
+ };
166
+ mockFetchResponse(mockResponse);
167
+ const result = await vector.query({
168
+ indexName: 'test-index',
169
+ queryVector: [1, 2],
170
+ topK: 10,
171
+ filter: { label: 'a' },
172
+ includeVector: true,
173
+ });
174
+ expect(result).toEqual(mockResponse);
175
+ expect(global.fetch).toHaveBeenCalledWith(
176
+ `${clientOptions.baseUrl}/api/vector/test-vector/query`,
177
+ expect.objectContaining({
178
+ method: 'POST',
179
+ headers: expect.objectContaining(clientOptions.headers),
180
+ body: JSON.stringify({
181
+ indexName: 'test-index',
182
+ queryVector: [1, 2],
183
+ topK: 10,
184
+ filter: { label: 'a' },
185
+ includeVector: true,
186
+ }),
187
+ }),
188
+ );
189
+ });
190
+ });
122
191
 
123
- it('should upsert vectors with metadata and ids', async () => {
124
- const mockResponse = ['id1', 'id2'];
125
- mockFetchResponse(mockResponse);
126
- const result = await vector.upsert({
127
- indexName: 'test-index',
128
- vectors: [[1, 2], [3, 4]],
129
- metadata: [{ label: 'a' }, { label: 'b' }],
130
- ids: ['id1', 'id2']
131
- });
132
- expect(result).toEqual(mockResponse);
133
- expect(global.fetch).toHaveBeenCalledWith(
134
- `${clientOptions.baseUrl}/api/vector/test-vector/upsert`,
135
- expect.objectContaining({
136
- method: 'POST',
137
- headers: expect.objectContaining(clientOptions.headers),
138
- body: JSON.stringify({
139
- indexName: 'test-index',
140
- vectors: [[1, 2], [3, 4]],
141
- metadata: [{ label: 'a' }, { label: 'b' }],
142
- ids: ['id1', 'id2']
143
- })
144
- })
145
- );
146
- });
192
+ describe('Agent Resource', () => {
193
+ const agentId = 'test-agent';
194
+ let agent: ReturnType<typeof client.getAgent>;
147
195
 
148
- it('should query vectors with all parameters', async () => {
149
- const mockResponse = {
150
- results: [{
151
- id: 'id1',
152
- score: 0.9,
153
- metadata: { label: 'a' },
154
- vector: [1, 2]
155
- }]
156
- };
157
- mockFetchResponse(mockResponse);
158
- const result = await vector.query({
159
- indexName: 'test-index',
160
- queryVector: [1, 2],
161
- topK: 10,
162
- filter: { label: 'a' },
163
- includeVector: true
164
- });
165
- expect(result).toEqual(mockResponse);
166
- expect(global.fetch).toHaveBeenCalledWith(
167
- `${clientOptions.baseUrl}/api/vector/test-vector/query`,
168
- expect.objectContaining({
169
- method: 'POST',
170
- headers: expect.objectContaining(clientOptions.headers),
171
- body: JSON.stringify({
172
- indexName: 'test-index',
173
- queryVector: [1, 2],
174
- topK: 10,
175
- filter: { label: 'a' },
176
- includeVector: true
177
- })
178
- })
179
- );
180
- });
196
+ beforeEach(() => {
197
+ agent = client.getAgent(agentId);
181
198
  });
182
199
 
183
- describe('Agent Resource', () => {
184
- const agentId = 'test-agent';
185
- let agent: ReturnType<typeof client.getAgent>;
200
+ it('should get all agents', async () => {
201
+ const mockResponse = {
202
+ agent1: { name: 'Agent 1', model: 'gpt-4' },
203
+ agent2: { name: 'Agent 2', model: 'gpt-3.5' },
204
+ };
205
+ mockFetchResponse(mockResponse);
206
+ const result = await client.getAgents();
207
+ expect(result).toEqual(mockResponse);
208
+ expect(global.fetch).toHaveBeenCalledWith(
209
+ `${clientOptions.baseUrl}/api/agents`,
210
+ expect.objectContaining({
211
+ headers: expect.objectContaining(clientOptions.headers),
212
+ }),
213
+ );
214
+ });
186
215
 
187
- beforeEach(() => {
188
- agent = client.getAgent(agentId);
189
- });
216
+ it('should get agent details', async () => {
217
+ const mockResponse = {
218
+ name: 'Test Agent',
219
+ model: 'gpt-4',
220
+ instructions: 'Test instructions',
221
+ tools: {},
222
+ };
223
+ mockFetchResponse(mockResponse);
224
+
225
+ const result = await agent.details();
226
+ expect(result).toEqual(mockResponse);
227
+ expect(global.fetch).toHaveBeenCalledWith(
228
+ `${clientOptions.baseUrl}/api/agents/test-agent`,
229
+ expect.objectContaining({
230
+ headers: expect.objectContaining(clientOptions.headers),
231
+ }),
232
+ );
233
+ });
190
234
 
191
- it('should get all agents', async () => {
192
- const mockResponse = {
193
- 'agent1': { name: 'Agent 1', model: 'gpt-4' },
194
- 'agent2': { name: 'Agent 2', model: 'gpt-3.5' }
195
- };
196
- mockFetchResponse(mockResponse);
197
- const result = await client.getAgents();
198
- expect(result).toEqual(mockResponse);
199
- expect(global.fetch).toHaveBeenCalledWith(
200
- `${clientOptions.baseUrl}/api/agents`,
201
- expect.objectContaining({
202
- headers: expect.objectContaining(clientOptions.headers)
203
- })
204
- );
205
- });
235
+ it('should generate response', async () => {
236
+ const mockResponse = {
237
+ response: 'Generated response',
238
+ };
239
+ mockFetchResponse(mockResponse);
240
+
241
+ const result = await agent.generate({
242
+ messages: [],
243
+ threadId: 'test-thread',
244
+ resourceid: 'test-resource',
245
+ output: {},
246
+ });
247
+ expect(result).toEqual(mockResponse);
248
+ expect(global.fetch).toHaveBeenCalledWith(
249
+ `${clientOptions.baseUrl}/api/agents/test-agent/generate`,
250
+ expect.objectContaining({
251
+ method: 'POST',
252
+ headers: expect.objectContaining(clientOptions.headers),
253
+ body: JSON.stringify({
254
+ messages: [],
255
+ threadId: 'test-thread',
256
+ resourceid: 'test-resource',
257
+ output: {},
258
+ }),
259
+ }),
260
+ );
261
+ });
206
262
 
207
- it('should get agent details', async () => {
208
- const mockResponse = {
209
- name: 'Test Agent',
210
- model: 'gpt-4',
211
- instructions: 'Test instructions',
212
- tools: {}
213
- };
214
- mockFetchResponse(mockResponse);
215
-
216
- const result = await agent.details();
217
- expect(result).toEqual(mockResponse);
218
- expect(global.fetch).toHaveBeenCalledWith(
219
- `${clientOptions.baseUrl}/api/agents/test-agent`,
220
- expect.objectContaining({
221
- headers: expect.objectContaining(clientOptions.headers)
222
- })
223
- );
224
- });
263
+ it('should stream responses', async () => {
264
+ const mockChunk = { content: 'test response' };
265
+ mockFetchResponse(mockChunk, { isStream: true });
266
+
267
+ const response = await agent.stream({
268
+ messages: [
269
+ {
270
+ role: 'user',
271
+ content: 'test',
272
+ },
273
+ ],
274
+ });
275
+
276
+ expect(response.body).toBeInstanceOf(ReadableStream);
277
+ const reader = response?.body?.getReader();
278
+ expect(reader).toBeDefined();
279
+
280
+ if (reader) {
281
+ const { value, done } = await reader.read();
282
+ expect(done).toBe(false);
283
+ expect(new TextDecoder().decode(value)).toBe(JSON.stringify(mockChunk));
284
+ }
285
+ });
225
286
 
226
- it('should generate response', async () => {
227
- const mockResponse = {
228
- response: 'Generated response'
229
- };
230
- mockFetchResponse(mockResponse);
231
-
232
- const result = await agent.generate({
233
- messages: [],
234
- threadId: 'test-thread',
235
- resourceid: 'test-resource',
236
- output: {}
237
- });
238
- expect(result).toEqual(mockResponse);
239
- expect(global.fetch).toHaveBeenCalledWith(
240
- `${clientOptions.baseUrl}/api/agents/test-agent/generate`,
241
- expect.objectContaining({
242
- method: 'POST',
243
- headers: expect.objectContaining(clientOptions.headers),
244
- body: JSON.stringify({
245
- messages: [],
246
- threadId: 'test-thread',
247
- resourceid: 'test-resource',
248
- output: {}
249
- })
250
- })
251
- );
252
- });
287
+ it('should get agent tool', async () => {
288
+ const mockResponse = {
289
+ id: 'tool1',
290
+ description: 'Test Tool',
291
+ };
292
+ mockFetchResponse(mockResponse);
293
+ const result = await agent.getTool('tool1');
294
+ expect(result).toEqual(mockResponse);
295
+ expect(global.fetch).toHaveBeenCalledWith(
296
+ `${clientOptions.baseUrl}/api/agents/test-agent/tools/tool1`,
297
+ expect.objectContaining({
298
+ headers: expect.objectContaining(clientOptions.headers),
299
+ }),
300
+ );
301
+ });
253
302
 
254
- it('should stream responses', async () => {
255
- const mockChunk = { content: 'test response' };
256
- mockFetchResponse(mockChunk, { isStream: true });
257
-
258
- const response = await agent.stream({
259
- messages: [{
260
- role: 'user',
261
- content: 'test',
262
- id: '1',
263
- createdAt: new Date(),
264
- threadId: '1',
265
- type: 'text'
266
- }]
267
- });
268
-
269
- expect(response.body).toBeInstanceOf(ReadableStream);
270
- const reader = response?.body?.getReader();
271
- expect(reader).toBeDefined();
272
-
273
- if (reader) {
274
- const { value, done } = await reader.read();
275
- expect(done).toBe(false);
276
- expect(new TextDecoder().decode(value)).toBe(JSON.stringify(mockChunk));
277
- }
278
- });
303
+ it('should get agent evals', async () => {
304
+ const mockResponse = {
305
+ name: 'Test Agent',
306
+ evals: [{ id: 'eval1' }],
307
+ };
308
+ mockFetchResponse(mockResponse);
309
+ const result = await agent.evals();
310
+ expect(result).toEqual(mockResponse);
311
+ expect(global.fetch).toHaveBeenCalledWith(`${clientOptions.baseUrl}/api/agents/test-agent/evals/ci`, {
312
+ headers: {
313
+ Authorization: 'Bearer test-key',
314
+ 'Content-Type': 'application/json',
315
+ },
316
+ });
317
+ });
279
318
 
280
- it('should get agent tool', async () => {
281
- const mockResponse = {
282
- id: 'tool1',
283
- description: 'Test Tool'
284
- };
285
- mockFetchResponse(mockResponse);
286
- const result = await agent.getTool('tool1');
287
- expect(result).toEqual(mockResponse);
288
- expect(global.fetch).toHaveBeenCalledWith(
289
- `${clientOptions.baseUrl}/api/agents/test-agent/tools/tool1`,
290
- expect.objectContaining({
291
- headers: expect.objectContaining(clientOptions.headers)
292
- })
293
- );
294
- });
319
+ it('should get live evals', async () => {
320
+ const mockResponse = {
321
+ name: 'Test Agent',
322
+ evals: [{ id: 'eval1', live: true }],
323
+ };
324
+ mockFetchResponse(mockResponse);
325
+ const result = await agent.liveEvals();
326
+ expect(result).toEqual(mockResponse);
327
+ expect(global.fetch).toHaveBeenCalledWith(
328
+ `${clientOptions.baseUrl}/api/agents/test-agent/evals/live`,
329
+ expect.objectContaining({
330
+ headers: expect.objectContaining(clientOptions.headers),
331
+ }),
332
+ );
333
+ });
334
+ });
295
335
 
296
- it('should get agent evals', async () => {
297
- const mockResponse = {
298
- name: 'Test Agent',
299
- evals: [{ id: 'eval1' }]
300
- };
301
- mockFetchResponse(mockResponse);
302
- const result = await agent.evals();
303
- expect(result).toEqual(mockResponse);
304
- expect(global.fetch).toHaveBeenCalledWith(
305
- `${clientOptions.baseUrl}/api/agents/test-agent/evals`,
306
- expect.objectContaining({
307
- headers: expect.objectContaining(clientOptions.headers)
308
- })
309
- );
310
- });
336
+ const agentId = 'test-agent';
311
337
 
312
- it('should get live evals', async () => {
313
- const mockResponse = {
314
- name: 'Test Agent',
315
- evals: [{ id: 'eval1', live: true }]
316
- };
317
- mockFetchResponse(mockResponse);
318
- const result = await agent.liveEvals();
319
- expect(result).toEqual(mockResponse);
320
- expect(global.fetch).toHaveBeenCalledWith(
321
- `${clientOptions.baseUrl}/api/agents/test-agent/evals/live`,
322
- expect.objectContaining({
323
- headers: expect.objectContaining(clientOptions.headers)
324
- })
325
- );
326
- });
338
+ describe('Memory Thread Resource', () => {
339
+ const threadId = 'test-thread';
340
+ let memoryThread: ReturnType<typeof client.getMemoryThread>;
341
+
342
+ beforeEach(() => {
343
+ memoryThread = client.getMemoryThread(threadId, agentId);
327
344
  });
328
345
 
329
- const agentId = 'test-agent'
346
+ it('should get thread details', async () => {
347
+ const mockResponse = {
348
+ id: threadId,
349
+ title: 'Test Thread',
350
+ metadata: {},
351
+ };
352
+ mockFetchResponse(mockResponse);
353
+
354
+ const result = await memoryThread.get();
355
+ expect(result).toEqual(mockResponse);
356
+ expect(global.fetch).toHaveBeenCalledWith(
357
+ `${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
358
+ expect.objectContaining({
359
+ headers: expect.objectContaining(clientOptions.headers),
360
+ }),
361
+ );
362
+ });
330
363
 
331
- describe('Memory Thread Resource', () => {
332
- const threadId = 'test-thread';
333
- let memoryThread: ReturnType<typeof client.getMemoryThread>;
364
+ it('should update thread', async () => {
365
+ const mockResponse = {
366
+ id: threadId,
367
+ title: 'Updated Thread',
368
+ metadata: { updated: true },
369
+ };
370
+ mockFetchResponse(mockResponse);
371
+
372
+ const result = await memoryThread.update({
373
+ title: 'Updated Thread',
374
+ metadata: { updated: true },
375
+ resourceid: 'test-resource',
376
+ });
377
+ expect(result).toEqual(mockResponse);
378
+ expect(global.fetch).toHaveBeenCalledWith(
379
+ `${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
380
+ expect.objectContaining({
381
+ method: 'PATCH',
382
+ headers: expect.objectContaining(clientOptions.headers),
383
+ }),
384
+ );
385
+ });
334
386
 
335
- beforeEach(() => {
336
- memoryThread = client.getMemoryThread(threadId, agentId);
337
- });
387
+ it('should delete thread', async () => {
388
+ const mockResponse = { result: 'deleted' };
389
+ mockFetchResponse(mockResponse);
390
+ const result = await memoryThread.delete();
391
+ expect(result).toEqual(mockResponse);
392
+ expect(global.fetch).toHaveBeenCalledWith(
393
+ `${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
394
+ expect.objectContaining({
395
+ method: 'DELETE',
396
+ headers: expect.objectContaining(clientOptions.headers),
397
+ }),
398
+ );
399
+ });
338
400
 
339
- it('should get thread details', async () => {
340
- const mockResponse = {
341
- id: threadId,
342
- title: 'Test Thread',
343
- metadata: {}
344
- };
345
- mockFetchResponse(mockResponse);
346
-
347
- const result = await memoryThread.get();
348
- expect(result).toEqual(mockResponse);
349
- expect(global.fetch).toHaveBeenCalledWith(
350
- `${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
351
- expect.objectContaining({
352
- headers: expect.objectContaining(clientOptions.headers)
353
- })
354
- );
355
- });
401
+ it('should get memory status', async () => {
402
+ const mockResponse = { result: true };
403
+ mockFetchResponse(mockResponse);
404
+ const result = await client.getMemoryStatus(agentId);
405
+ expect(result).toEqual(mockResponse);
406
+ expect(global.fetch).toHaveBeenCalledWith(
407
+ `${clientOptions.baseUrl}/api/memory/status?agentId=${agentId}`,
408
+ expect.objectContaining({
409
+ headers: expect.objectContaining(clientOptions.headers),
410
+ }),
411
+ );
412
+ });
356
413
 
357
- it('should update thread', async () => {
358
- const mockResponse = {
359
- id: threadId,
360
- title: 'Updated Thread',
361
- metadata: { updated: true }
362
- };
363
- mockFetchResponse(mockResponse);
364
-
365
- const result = await memoryThread.update({
366
- title: 'Updated Thread',
367
- metadata: { updated: true },
368
- resourceid: 'test-resource'
369
- });
370
- expect(result).toEqual(mockResponse);
371
- expect(global.fetch).toHaveBeenCalledWith(
372
- `${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
373
- expect.objectContaining({
374
- method: 'PATCH',
375
- headers: expect.objectContaining(clientOptions.headers)
376
- })
377
- );
378
- });
414
+ it('should save messages to memory', async () => {
415
+ const messages: MessageType[] = [
416
+ {
417
+ id: '1',
418
+ type: 'text',
419
+ content: 'test',
420
+ role: 'user',
421
+ threadId: 'test-thread',
422
+ createdAt: new Date(),
423
+ },
424
+ ];
425
+ mockFetchResponse(messages);
426
+ const result = await client.saveMessageToMemory({ messages, agentId });
427
+ expect(result).toEqual(messages);
428
+ expect(global.fetch).toHaveBeenCalledWith(
429
+ `${clientOptions.baseUrl}/api/memory/save-messages?agentId=${agentId}`,
430
+ {
431
+ method: 'POST',
432
+ headers: {
433
+ Authorization: 'Bearer test-key',
434
+ 'Content-Type': 'application/json',
435
+ },
436
+ body: JSON.stringify({ messages, agentId }),
437
+ },
438
+ );
439
+ });
440
+ });
379
441
 
380
- it('should delete thread', async () => {
381
- const mockResponse = { result: 'deleted' };
382
- mockFetchResponse(mockResponse);
383
- const result = await memoryThread.delete();
384
- expect(result).toEqual(mockResponse);
385
- expect(global.fetch).toHaveBeenCalledWith(
386
- `${clientOptions.baseUrl}/api/memory/threads/test-thread?agentId=${agentId}`,
387
- expect.objectContaining({
388
- method: 'DELETE',
389
- headers: expect.objectContaining(clientOptions.headers)
390
- })
391
- );
392
- });
442
+ describe('Tool Resource', () => {
443
+ const toolId = 'test-tool';
444
+ let tool: ReturnType<typeof client.getTool>;
393
445
 
394
- it('should get memory status', async () => {
395
- const mockResponse = { result: true };
396
- mockFetchResponse(mockResponse);
397
- const result = await client.getMemoryStatus(agentId);
398
- expect(result).toEqual(mockResponse);
399
- expect(global.fetch).toHaveBeenCalledWith(
400
- `${clientOptions.baseUrl}/api/memory/status?agentId=${agentId}`,
401
- expect.objectContaining({
402
- headers: expect.objectContaining(clientOptions.headers)
403
- })
404
- );
405
- });
446
+ beforeEach(() => {
447
+ tool = client.getTool(toolId);
448
+ });
406
449
 
407
- it('should save messages to memory', async () => {
408
- const messages: MessageType[] = [{
409
- id: '1',
410
- type: 'text',
411
- content: 'test',
412
- role: 'user',
413
- threadId: 'test-thread',
414
- createdAt: new Date()
415
- }];
416
- mockFetchResponse(messages);
417
- const result = await client.saveMessageToMemory({ messages, agentId });
418
- expect(result).toEqual(messages);
419
- expect(global.fetch).toHaveBeenCalledWith(
420
- `${clientOptions.baseUrl}/api/memory/save-messages?agentId=${agentId}`,
421
- expect.objectContaining({
422
- method: 'POST',
423
- headers: expect.objectContaining(clientOptions.headers),
424
- body: JSON.stringify({ messages })
425
- })
426
- );
427
- });
450
+ it('should get tool details', async () => {
451
+ const mockResponse = {
452
+ id: toolId,
453
+ description: 'Test Tool',
454
+ inputSchema: '{}',
455
+ outputSchema: '{}',
456
+ };
457
+ mockFetchResponse(mockResponse);
458
+
459
+ const result = await tool.details();
460
+ expect(result).toEqual(mockResponse);
461
+ expect(global.fetch).toHaveBeenCalledWith(
462
+ `${clientOptions.baseUrl}/api/tools/test-tool`,
463
+ expect.objectContaining({
464
+ headers: expect.objectContaining(clientOptions.headers),
465
+ }),
466
+ );
428
467
  });
429
468
 
430
- describe('Tool Resource', () => {
431
- const toolId = 'test-tool';
432
- let tool: ReturnType<typeof client.getTool>;
469
+ it('should execute tool', async () => {
470
+ const mockResponse = {
471
+ result: 'Tool execution result',
472
+ };
473
+ mockFetchResponse(mockResponse);
433
474
 
434
- beforeEach(() => {
435
- tool = client.getTool(toolId);
436
- });
475
+ const result = await tool.execute({ data: '' });
476
+ expect(result).toEqual(mockResponse);
477
+ expect(global.fetch).toHaveBeenCalledWith(`${clientOptions.baseUrl}/api/tools/test-tool/execute`, {
478
+ method: 'POST',
479
+ headers: {
480
+ Authorization: 'Bearer test-key',
481
+ 'Content-Type': 'application/json',
482
+ },
483
+ body: JSON.stringify({ data: '' }),
484
+ });
485
+ });
486
+ });
437
487
 
438
- it('should get tool details', async () => {
439
- const mockResponse = {
440
- id: toolId,
441
- description: 'Test Tool',
442
- inputSchema: '{}',
443
- outputSchema: '{}'
444
- };
445
- mockFetchResponse(mockResponse);
446
-
447
- const result = await tool.details();
448
- expect(result).toEqual(mockResponse);
449
- expect(global.fetch).toHaveBeenCalledWith(
450
- `${clientOptions.baseUrl}/api/tools/test-tool`,
451
- expect.objectContaining({
452
- headers: expect.objectContaining(clientOptions.headers)
453
- })
454
- );
455
- });
488
+ describe('Workflow Resource', () => {
489
+ const workflowId = 'test-workflow';
490
+ let workflow: ReturnType<typeof client.getWorkflow>;
456
491
 
457
- it('should execute tool', async () => {
458
- const mockResponse = {
459
- result: 'Tool execution result'
460
- };
461
- mockFetchResponse(mockResponse);
462
-
463
- const result = await tool.execute({ input: 'test' });
464
- expect(result).toEqual(mockResponse);
465
- expect(global.fetch).toHaveBeenCalledWith(
466
- `${clientOptions.baseUrl}/api/tools/test-tool/execute`,
467
- expect.objectContaining({
468
- method: 'POST',
469
- headers: expect.objectContaining(clientOptions.headers),
470
- body: JSON.stringify({ input: 'test' })
471
- })
472
- );
473
- });
492
+ beforeEach(() => {
493
+ workflow = client.getWorkflow(workflowId);
474
494
  });
475
495
 
476
- describe('Workflow Resource', () => {
477
- const workflowId = 'test-workflow';
478
- let workflow: ReturnType<typeof client.getWorkflow>;
479
-
480
- beforeEach(() => {
481
- workflow = client.getWorkflow(workflowId);
482
- });
496
+ it('should get workflow details', async () => {
497
+ const mockResponse = {
498
+ name: 'Test Workflow',
499
+ triggerSchema: '{}',
500
+ steps: {},
501
+ stepGraph: {},
502
+ stepSubscriberGraph: {},
503
+ };
504
+ mockFetchResponse(mockResponse);
505
+
506
+ const result = await workflow.details();
507
+ expect(result).toEqual(mockResponse);
508
+ expect(global.fetch).toHaveBeenCalledWith(
509
+ `${clientOptions.baseUrl}/api/workflows/test-workflow`,
510
+ expect.objectContaining({
511
+ headers: expect.objectContaining(clientOptions.headers),
512
+ }),
513
+ );
514
+ });
483
515
 
484
- it('should get workflow details', async () => {
485
- const mockResponse = {
486
- name: 'Test Workflow',
487
- triggerSchema: '{}',
488
- steps: {},
489
- stepGraph: {},
490
- stepSubscriberGraph: {}
491
- };
492
- mockFetchResponse(mockResponse);
493
-
494
- const result = await workflow.details();
495
- expect(result).toEqual(mockResponse);
496
- expect(global.fetch).toHaveBeenCalledWith(
497
- `${clientOptions.baseUrl}/api/workflows/test-workflow`,
498
- expect.objectContaining({
499
- headers: expect.objectContaining(clientOptions.headers)
500
- })
501
- );
516
+ it('should execute workflow', async () => {
517
+ const mockResponse = {
518
+ result: 'Workflow execution result',
519
+ };
520
+ mockFetchResponse(mockResponse);
521
+
522
+ const result = await workflow.execute({ trigger: 'test' });
523
+ expect(result).toEqual(mockResponse);
524
+ expect(global.fetch).toHaveBeenCalledWith(
525
+ `${clientOptions.baseUrl}/api/workflows/test-workflow/execute`,
526
+ expect.objectContaining({
527
+ method: 'POST',
528
+ headers: expect.objectContaining(clientOptions.headers),
529
+ body: JSON.stringify({ trigger: 'test' }),
530
+ }),
531
+ );
532
+ });
533
+ });
534
+
535
+ describe('Client Error Handling', () => {
536
+ it('should retry failed requests', async () => {
537
+ // Mock first two calls to fail, third to succeed
538
+ (global.fetch as any)
539
+ .mockRejectedValueOnce(new Error('Network error'))
540
+ .mockRejectedValueOnce(new Error('Network error'))
541
+ .mockResolvedValueOnce({
542
+ ok: true,
543
+ headers: {
544
+ get: () => 'application/json',
545
+ },
546
+ json: async () => ({ success: true }),
502
547
  });
503
548
 
504
- it('should execute workflow', async () => {
505
- const mockResponse = {
506
- result: 'Workflow execution result'
507
- };
508
- mockFetchResponse(mockResponse);
509
-
510
- const result = await workflow.execute({ trigger: 'test' });
511
- expect(result).toEqual(mockResponse);
512
- expect(global.fetch).toHaveBeenCalledWith(
513
- `${clientOptions.baseUrl}/api/workflows/test-workflow/execute`,
514
- expect.objectContaining({
515
- method: 'POST',
516
- headers: expect.objectContaining(clientOptions.headers),
517
- body: JSON.stringify({ trigger: 'test' })
518
- })
519
- );
520
- });
549
+ const result = await client.request('/test-endpoint');
550
+ expect(result).toEqual({ success: true });
551
+ expect(global.fetch).toHaveBeenCalledTimes(3);
521
552
  });
522
553
 
523
- describe('Client Error Handling', () => {
524
- it('should retry failed requests', async () => {
525
- // Mock first two calls to fail, third to succeed
526
- (global.fetch as any)
527
- .mockRejectedValueOnce(new Error('Network error'))
528
- .mockRejectedValueOnce(new Error('Network error'))
529
- .mockResolvedValueOnce({
530
- ok: true,
531
- headers: {
532
- get: () => 'application/json'
533
- },
534
- json: async () => ({ success: true })
535
- });
536
-
537
- const result = await client.request('/test-endpoint');
538
- expect(result).toEqual({ success: true });
539
- expect(global.fetch).toHaveBeenCalledTimes(3);
540
- });
541
-
542
- it('should throw error after max retries', async () => {
543
- (global.fetch as any).mockRejectedValue(new Error('Network error'));
554
+ it('should throw error after max retries', async () => {
555
+ (global.fetch as any).mockRejectedValue(new Error('Network error'));
544
556
 
545
- await expect(client.request('/test-endpoint'))
546
- .rejects
547
- .toThrow('Network error');
557
+ await expect(client.request('/test-endpoint')).rejects.toThrow('Network error');
548
558
 
549
- expect(global.fetch).toHaveBeenCalledTimes(4);
550
- });
559
+ expect(global.fetch).toHaveBeenCalledTimes(4);
551
560
  });
561
+ });
552
562
 
553
- describe('Client Configuration', () => {
554
- it('should handle custom retry configuration', async () => {
555
- const customClient = new MastraClient({
556
- baseUrl: 'http://localhost:4111',
557
- retries: 2,
558
- backoffMs: 100,
559
- maxBackoffMs: 1000,
560
- headers: { 'Custom-Header': 'value' }
561
- });
562
-
563
- (global.fetch as any)
564
- .mockRejectedValueOnce(new Error('Network error'))
565
- .mockRejectedValueOnce(new Error('Network error'))
566
- .mockResolvedValueOnce({
567
- ok: true,
568
- headers: {
569
- get: () => 'application/json'
570
- },
571
- json: async () => ({ success: true })
572
- });
573
-
574
- const result = await customClient.request('/test');
575
- expect(result).toEqual({ success: true });
576
- expect(global.fetch).toHaveBeenCalledTimes(3);
577
- expect(global.fetch).toHaveBeenCalledWith(
578
- 'http://localhost:4111/test',
579
- expect.objectContaining({
580
- headers: expect.objectContaining({
581
- 'Custom-Header': 'value'
582
- })
583
- })
584
- );
563
+ describe('Client Configuration', () => {
564
+ it('should handle custom retry configuration', async () => {
565
+ const customClient = new MastraClient({
566
+ baseUrl: 'http://localhost:4111',
567
+ retries: 2,
568
+ backoffMs: 100,
569
+ maxBackoffMs: 1000,
570
+ headers: { 'Custom-Header': 'value' },
571
+ });
572
+
573
+ (global.fetch as any)
574
+ .mockRejectedValueOnce(new Error('Network error'))
575
+ .mockRejectedValueOnce(new Error('Network error'))
576
+ .mockResolvedValueOnce({
577
+ ok: true,
578
+ headers: {
579
+ get: () => 'application/json',
580
+ },
581
+ json: async () => ({ success: true }),
585
582
  });
583
+
584
+ const result = await customClient.request('/test');
585
+ expect(result).toEqual({ success: true });
586
+ expect(global.fetch).toHaveBeenCalledTimes(3);
587
+ expect(global.fetch).toHaveBeenCalledWith(
588
+ 'http://localhost:4111/test',
589
+ expect.objectContaining({
590
+ headers: expect.objectContaining({
591
+ 'Custom-Header': 'value',
592
+ }),
593
+ }),
594
+ );
586
595
  });
596
+ });
587
597
  });