@mastra/client-js 0.1.0-alpha.9 → 0.1.1

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,585 +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>;
52
-
53
- beforeEach(() => {
54
- vector = client.getVector(vectorName);
55
- });
56
-
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
- });
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
+ });
74
75
 
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
- });
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
+ });
87
88
 
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
- });
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
+ });
100
101
 
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
- });
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
+ });
122
123
 
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
- });
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
+ });
147
154
 
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
- });
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
+ );
181
189
  });
190
+ });
182
191
 
183
- describe('Agent Resource', () => {
184
- const agentId = 'test-agent';
185
- let agent: ReturnType<typeof client.getAgent>;
192
+ describe('Agent Resource', () => {
193
+ const agentId = 'test-agent';
194
+ let agent: ReturnType<typeof client.getAgent>;
186
195
 
187
- beforeEach(() => {
188
- agent = client.getAgent(agentId);
189
- });
196
+ beforeEach(() => {
197
+ agent = client.getAgent(agentId);
198
+ });
190
199
 
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
- });
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
+ });
206
215
 
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
- });
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
+ });
225
234
 
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
- });
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
+ });
253
262
 
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
- });
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
+ });
279
286
 
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
- });
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
+ });
295
302
 
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
- });
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
+ });
311
318
 
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
- });
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
+ );
327
333
  });
334
+ });
328
335
 
329
- describe('Memory Thread Resource', () => {
330
- const threadId = 'test-thread';
331
- let memoryThread: ReturnType<typeof client.getMemoryThread>;
336
+ const agentId = 'test-agent';
332
337
 
333
- beforeEach(() => {
334
- memoryThread = client.getMemoryThread(threadId);
335
- });
338
+ describe('Memory Thread Resource', () => {
339
+ const threadId = 'test-thread';
340
+ let memoryThread: ReturnType<typeof client.getMemoryThread>;
336
341
 
337
- it('should get thread details', async () => {
338
- const mockResponse = {
339
- id: threadId,
340
- title: 'Test Thread',
341
- metadata: {}
342
- };
343
- mockFetchResponse(mockResponse);
344
-
345
- const result = await memoryThread.get();
346
- expect(result).toEqual(mockResponse);
347
- expect(global.fetch).toHaveBeenCalledWith(
348
- `${clientOptions.baseUrl}/api/memory/threads/test-thread`,
349
- expect.objectContaining({
350
- headers: expect.objectContaining(clientOptions.headers)
351
- })
352
- );
353
- });
342
+ beforeEach(() => {
343
+ memoryThread = client.getMemoryThread(threadId, agentId);
344
+ });
354
345
 
355
- it('should update thread', async () => {
356
- const mockResponse = {
357
- id: threadId,
358
- title: 'Updated Thread',
359
- metadata: { updated: true }
360
- };
361
- mockFetchResponse(mockResponse);
362
-
363
- const result = await memoryThread.update({
364
- title: 'Updated Thread',
365
- metadata: { updated: true },
366
- resourceid: 'test-resource'
367
- });
368
- expect(result).toEqual(mockResponse);
369
- expect(global.fetch).toHaveBeenCalledWith(
370
- `${clientOptions.baseUrl}/api/memory/threads/test-thread`,
371
- expect.objectContaining({
372
- method: 'PATCH',
373
- headers: expect.objectContaining(clientOptions.headers)
374
- })
375
- );
376
- });
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
+ });
377
363
 
378
- it('should delete thread', async () => {
379
- const mockResponse = { result: 'deleted' };
380
- mockFetchResponse(mockResponse);
381
- const result = await memoryThread.delete();
382
- expect(result).toEqual(mockResponse);
383
- expect(global.fetch).toHaveBeenCalledWith(
384
- `${clientOptions.baseUrl}/api/memory/threads/test-thread`,
385
- expect.objectContaining({
386
- method: 'DELETE',
387
- headers: expect.objectContaining(clientOptions.headers)
388
- })
389
- );
390
- });
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
+ });
391
386
 
392
- it('should get memory status', async () => {
393
- const mockResponse = { result: true };
394
- mockFetchResponse(mockResponse);
395
- const result = await client.getMemoryStatus();
396
- expect(result).toEqual(mockResponse);
397
- expect(global.fetch).toHaveBeenCalledWith(
398
- `${clientOptions.baseUrl}/api/memory/status`,
399
- expect.objectContaining({
400
- headers: expect.objectContaining(clientOptions.headers)
401
- })
402
- );
403
- });
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
+ });
404
400
 
405
- it('should save messages to memory', async () => {
406
- const messages: MessageType[] = [{
407
- id: '1',
408
- type: 'text',
409
- content: 'test',
410
- role: 'user',
411
- threadId: 'test-thread',
412
- createdAt: new Date()
413
- }];
414
- mockFetchResponse(messages);
415
- const result = await client.saveMessageToMemory({ messages });
416
- expect(result).toEqual(messages);
417
- expect(global.fetch).toHaveBeenCalledWith(
418
- `${clientOptions.baseUrl}/api/memory/save-messages`,
419
- expect.objectContaining({
420
- method: 'POST',
421
- headers: expect.objectContaining(clientOptions.headers),
422
- body: JSON.stringify({ messages })
423
- })
424
- );
425
- });
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
+ );
426
412
  });
427
413
 
428
- describe('Tool Resource', () => {
429
- const toolId = 'test-tool';
430
- let tool: ReturnType<typeof client.getTool>;
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
+ });
431
441
 
432
- beforeEach(() => {
433
- tool = client.getTool(toolId);
434
- });
442
+ describe('Tool Resource', () => {
443
+ const toolId = 'test-tool';
444
+ let tool: ReturnType<typeof client.getTool>;
435
445
 
436
- it('should get tool details', async () => {
437
- const mockResponse = {
438
- id: toolId,
439
- description: 'Test Tool',
440
- inputSchema: '{}',
441
- outputSchema: '{}'
442
- };
443
- mockFetchResponse(mockResponse);
444
-
445
- const result = await tool.details();
446
- expect(result).toEqual(mockResponse);
447
- expect(global.fetch).toHaveBeenCalledWith(
448
- `${clientOptions.baseUrl}/api/tools/test-tool`,
449
- expect.objectContaining({
450
- headers: expect.objectContaining(clientOptions.headers)
451
- })
452
- );
453
- });
446
+ beforeEach(() => {
447
+ tool = client.getTool(toolId);
448
+ });
454
449
 
455
- it('should execute tool', async () => {
456
- const mockResponse = {
457
- result: 'Tool execution result'
458
- };
459
- mockFetchResponse(mockResponse);
460
-
461
- const result = await tool.execute({ input: 'test' });
462
- expect(result).toEqual(mockResponse);
463
- expect(global.fetch).toHaveBeenCalledWith(
464
- `${clientOptions.baseUrl}/api/tools/test-tool/execute`,
465
- expect.objectContaining({
466
- method: 'POST',
467
- headers: expect.objectContaining(clientOptions.headers),
468
- body: JSON.stringify({ input: 'test' })
469
- })
470
- );
471
- });
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
+ );
472
467
  });
473
468
 
474
- describe('Workflow Resource', () => {
475
- const workflowId = 'test-workflow';
476
- let workflow: ReturnType<typeof client.getWorkflow>;
469
+ it('should execute tool', async () => {
470
+ const mockResponse = {
471
+ result: 'Tool execution result',
472
+ };
473
+ mockFetchResponse(mockResponse);
477
474
 
478
- beforeEach(() => {
479
- workflow = client.getWorkflow(workflowId);
480
- });
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
+ });
481
487
 
482
- it('should get workflow details', async () => {
483
- const mockResponse = {
484
- name: 'Test Workflow',
485
- triggerSchema: '{}',
486
- steps: {},
487
- stepGraph: {},
488
- stepSubscriberGraph: {}
489
- };
490
- mockFetchResponse(mockResponse);
491
-
492
- const result = await workflow.details();
493
- expect(result).toEqual(mockResponse);
494
- expect(global.fetch).toHaveBeenCalledWith(
495
- `${clientOptions.baseUrl}/api/workflows/test-workflow`,
496
- expect.objectContaining({
497
- headers: expect.objectContaining(clientOptions.headers)
498
- })
499
- );
500
- });
488
+ describe('Workflow Resource', () => {
489
+ const workflowId = 'test-workflow';
490
+ let workflow: ReturnType<typeof client.getWorkflow>;
501
491
 
502
- it('should execute workflow', async () => {
503
- const mockResponse = {
504
- result: 'Workflow execution result'
505
- };
506
- mockFetchResponse(mockResponse);
507
-
508
- const result = await workflow.execute({ trigger: 'test' });
509
- expect(result).toEqual(mockResponse);
510
- expect(global.fetch).toHaveBeenCalledWith(
511
- `${clientOptions.baseUrl}/api/workflows/test-workflow/execute`,
512
- expect.objectContaining({
513
- method: 'POST',
514
- headers: expect.objectContaining(clientOptions.headers),
515
- body: JSON.stringify({ trigger: 'test' })
516
- })
517
- );
518
- });
492
+ beforeEach(() => {
493
+ workflow = client.getWorkflow(workflowId);
519
494
  });
520
495
 
521
- describe('Client Error Handling', () => {
522
- it('should retry failed requests', async () => {
523
- // Mock first two calls to fail, third to succeed
524
- (global.fetch as any)
525
- .mockRejectedValueOnce(new Error('Network error'))
526
- .mockRejectedValueOnce(new Error('Network error'))
527
- .mockResolvedValueOnce({
528
- ok: true,
529
- headers: {
530
- get: () => 'application/json'
531
- },
532
- json: async () => ({ success: true })
533
- });
534
-
535
- const result = await client.request('/test-endpoint');
536
- expect(result).toEqual({ success: true });
537
- expect(global.fetch).toHaveBeenCalledTimes(3);
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
+ });
515
+
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 }),
538
547
  });
539
548
 
540
- it('should throw error after max retries', async () => {
541
- (global.fetch as any).mockRejectedValue(new Error('Network error'));
549
+ const result = await client.request('/test-endpoint');
550
+ expect(result).toEqual({ success: true });
551
+ expect(global.fetch).toHaveBeenCalledTimes(3);
552
+ });
553
+
554
+ it('should throw error after max retries', async () => {
555
+ (global.fetch as any).mockRejectedValue(new Error('Network error'));
542
556
 
543
- await expect(client.request('/test-endpoint'))
544
- .rejects
545
- .toThrow('Network error');
557
+ await expect(client.request('/test-endpoint')).rejects.toThrow('Network error');
546
558
 
547
- expect(global.fetch).toHaveBeenCalledTimes(4);
548
- });
559
+ expect(global.fetch).toHaveBeenCalledTimes(4);
549
560
  });
561
+ });
550
562
 
551
- describe('Client Configuration', () => {
552
- it('should handle custom retry configuration', async () => {
553
- const customClient = new MastraClient({
554
- baseUrl: 'http://localhost:4111',
555
- retries: 2,
556
- backoffMs: 100,
557
- maxBackoffMs: 1000,
558
- headers: { 'Custom-Header': 'value' }
559
- });
560
-
561
- (global.fetch as any)
562
- .mockRejectedValueOnce(new Error('Network error'))
563
- .mockRejectedValueOnce(new Error('Network error'))
564
- .mockResolvedValueOnce({
565
- ok: true,
566
- headers: {
567
- get: () => 'application/json'
568
- },
569
- json: async () => ({ success: true })
570
- });
571
-
572
- const result = await customClient.request('/test');
573
- expect(result).toEqual({ success: true });
574
- expect(global.fetch).toHaveBeenCalledTimes(3);
575
- expect(global.fetch).toHaveBeenCalledWith(
576
- 'http://localhost:4111/test',
577
- expect.objectContaining({
578
- headers: expect.objectContaining({
579
- 'Custom-Header': 'value'
580
- })
581
- })
582
- );
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 }),
583
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
+ );
584
595
  });
596
+ });
585
597
  });