@mastra/client-js 0.1.0-alpha.2 → 0.1.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (292) hide show
  1. package/.eslintrc.js +10 -0
  2. package/.prettierignore +3 -0
  3. package/.prettierrc.json +26 -0
  4. package/LICENSE +44 -201
  5. package/README.md +90 -271
  6. package/dist/index.d.mts +359 -0
  7. package/dist/index.mjs +399 -0
  8. package/package.json +30 -95
  9. package/src/client.ts +201 -0
  10. package/src/index.test.ts +528 -0
  11. package/src/index.ts +2 -200
  12. package/src/resources/agent.ts +97 -0
  13. package/src/resources/index.ts +5 -14
  14. package/src/resources/memory-thread.ts +51 -0
  15. package/src/resources/tool.ts +28 -0
  16. package/src/resources/vector.ts +79 -0
  17. package/src/resources/workflow.ts +28 -0
  18. package/src/types.ts +135 -0
  19. package/tsconfig.json +29 -0
  20. package/vitest.config.js +8 -0
  21. package/CHANGELOG.md +0 -29
  22. package/_shims/MultipartBody.d.ts +0 -9
  23. package/_shims/MultipartBody.d.ts.map +0 -1
  24. package/_shims/MultipartBody.js +0 -16
  25. package/_shims/MultipartBody.js.map +0 -1
  26. package/_shims/MultipartBody.mjs +0 -12
  27. package/_shims/MultipartBody.mjs.map +0 -1
  28. package/_shims/README.md +0 -46
  29. package/_shims/auto/runtime-bun.d.ts +0 -5
  30. package/_shims/auto/runtime-bun.d.ts.map +0 -1
  31. package/_shims/auto/runtime-bun.js +0 -21
  32. package/_shims/auto/runtime-bun.js.map +0 -1
  33. package/_shims/auto/runtime-bun.mjs +0 -2
  34. package/_shims/auto/runtime-bun.mjs.map +0 -1
  35. package/_shims/auto/runtime-node.d.ts +0 -5
  36. package/_shims/auto/runtime-node.d.ts.map +0 -1
  37. package/_shims/auto/runtime-node.js +0 -21
  38. package/_shims/auto/runtime-node.js.map +0 -1
  39. package/_shims/auto/runtime-node.mjs +0 -2
  40. package/_shims/auto/runtime-node.mjs.map +0 -1
  41. package/_shims/auto/runtime.d.ts +0 -5
  42. package/_shims/auto/runtime.d.ts.map +0 -1
  43. package/_shims/auto/runtime.js +0 -21
  44. package/_shims/auto/runtime.js.map +0 -1
  45. package/_shims/auto/runtime.mjs +0 -2
  46. package/_shims/auto/runtime.mjs.map +0 -1
  47. package/_shims/auto/types-node.d.ts +0 -5
  48. package/_shims/auto/types-node.d.ts.map +0 -1
  49. package/_shims/auto/types-node.js +0 -21
  50. package/_shims/auto/types-node.js.map +0 -1
  51. package/_shims/auto/types-node.mjs +0 -2
  52. package/_shims/auto/types-node.mjs.map +0 -1
  53. package/_shims/auto/types.d.ts +0 -101
  54. package/_shims/auto/types.js +0 -3
  55. package/_shims/auto/types.mjs +0 -3
  56. package/_shims/bun-runtime.d.ts +0 -6
  57. package/_shims/bun-runtime.d.ts.map +0 -1
  58. package/_shims/bun-runtime.js +0 -14
  59. package/_shims/bun-runtime.js.map +0 -1
  60. package/_shims/bun-runtime.mjs +0 -10
  61. package/_shims/bun-runtime.mjs.map +0 -1
  62. package/_shims/index.d.ts +0 -81
  63. package/_shims/index.js +0 -13
  64. package/_shims/index.mjs +0 -7
  65. package/_shims/manual-types.d.ts +0 -12
  66. package/_shims/manual-types.js +0 -3
  67. package/_shims/manual-types.mjs +0 -3
  68. package/_shims/node-runtime.d.ts +0 -3
  69. package/_shims/node-runtime.d.ts.map +0 -1
  70. package/_shims/node-runtime.js +0 -89
  71. package/_shims/node-runtime.js.map +0 -1
  72. package/_shims/node-runtime.mjs +0 -56
  73. package/_shims/node-runtime.mjs.map +0 -1
  74. package/_shims/node-types.d.ts +0 -42
  75. package/_shims/node-types.js +0 -3
  76. package/_shims/node-types.mjs +0 -3
  77. package/_shims/registry.d.ts +0 -37
  78. package/_shims/registry.d.ts.map +0 -1
  79. package/_shims/registry.js +0 -41
  80. package/_shims/registry.js.map +0 -1
  81. package/_shims/registry.mjs +0 -37
  82. package/_shims/registry.mjs.map +0 -1
  83. package/_shims/web-runtime.d.ts +0 -5
  84. package/_shims/web-runtime.d.ts.map +0 -1
  85. package/_shims/web-runtime.js +0 -78
  86. package/_shims/web-runtime.js.map +0 -1
  87. package/_shims/web-runtime.mjs +0 -71
  88. package/_shims/web-runtime.mjs.map +0 -1
  89. package/_shims/web-types.d.ts +0 -83
  90. package/_shims/web-types.js +0 -3
  91. package/_shims/web-types.mjs +0 -3
  92. package/core.d.ts +0 -241
  93. package/core.d.ts.map +0 -1
  94. package/core.js +0 -908
  95. package/core.js.map +0 -1
  96. package/core.mjs +0 -876
  97. package/core.mjs.map +0 -1
  98. package/error.d.ts +0 -47
  99. package/error.d.ts.map +0 -1
  100. package/error.js +0 -113
  101. package/error.js.map +0 -1
  102. package/error.mjs +0 -97
  103. package/error.mjs.map +0 -1
  104. package/index.d.mts +0 -121
  105. package/index.d.ts +0 -121
  106. package/index.d.ts.map +0 -1
  107. package/index.js +0 -131
  108. package/index.js.map +0 -1
  109. package/index.mjs +0 -88
  110. package/index.mjs.map +0 -1
  111. package/resource.d.ts +0 -6
  112. package/resource.d.ts.map +0 -1
  113. package/resource.js +0 -11
  114. package/resource.js.map +0 -1
  115. package/resource.mjs +0 -7
  116. package/resource.mjs.map +0 -1
  117. package/resources/agents/agents.d.ts +0 -41
  118. package/resources/agents/agents.d.ts.map +0 -1
  119. package/resources/agents/agents.js +0 -74
  120. package/resources/agents/agents.js.map +0 -1
  121. package/resources/agents/agents.mjs +0 -47
  122. package/resources/agents/agents.mjs.map +0 -1
  123. package/resources/agents/index.d.ts +0 -3
  124. package/resources/agents/index.d.ts.map +0 -1
  125. package/resources/agents/index.js +0 -9
  126. package/resources/agents/index.js.map +0 -1
  127. package/resources/agents/index.mjs +0 -4
  128. package/resources/agents/index.mjs.map +0 -1
  129. package/resources/agents/tools.d.ts +0 -17
  130. package/resources/agents/tools.d.ts.map +0 -1
  131. package/resources/agents/tools.js +0 -19
  132. package/resources/agents/tools.js.map +0 -1
  133. package/resources/agents/tools.mjs +0 -15
  134. package/resources/agents/tools.mjs.map +0 -1
  135. package/resources/index.d.ts +0 -8
  136. package/resources/index.d.ts.map +0 -1
  137. package/resources/index.js +0 -19
  138. package/resources/index.js.map +0 -1
  139. package/resources/index.mjs +0 -9
  140. package/resources/index.mjs.map +0 -1
  141. package/resources/logs.d.ts +0 -13
  142. package/resources/logs.d.ts.map +0 -1
  143. package/resources/logs.js +0 -24
  144. package/resources/logs.js.map +0 -1
  145. package/resources/logs.mjs +0 -20
  146. package/resources/logs.mjs.map +0 -1
  147. package/resources/memory/index.d.ts +0 -4
  148. package/resources/memory/index.d.ts.map +0 -1
  149. package/resources/memory/index.js +0 -11
  150. package/resources/memory/index.js.map +0 -1
  151. package/resources/memory/index.mjs +0 -5
  152. package/resources/memory/index.mjs.map +0 -1
  153. package/resources/memory/memory.d.ts +0 -23
  154. package/resources/memory/memory.d.ts.map +0 -1
  155. package/resources/memory/memory.js +0 -53
  156. package/resources/memory/memory.js.map +0 -1
  157. package/resources/memory/memory.mjs +0 -26
  158. package/resources/memory/memory.mjs.map +0 -1
  159. package/resources/memory/status.d.ts +0 -9
  160. package/resources/memory/status.d.ts.map +0 -1
  161. package/resources/memory/status.js +0 -18
  162. package/resources/memory/status.js.map +0 -1
  163. package/resources/memory/status.mjs +0 -14
  164. package/resources/memory/status.mjs.map +0 -1
  165. package/resources/memory/threads/index.d.ts +0 -3
  166. package/resources/memory/threads/index.d.ts.map +0 -1
  167. package/resources/memory/threads/index.js +0 -9
  168. package/resources/memory/threads/index.js.map +0 -1
  169. package/resources/memory/threads/index.mjs +0 -4
  170. package/resources/memory/threads/index.mjs.map +0 -1
  171. package/resources/memory/threads/messages.d.ts +0 -9
  172. package/resources/memory/threads/messages.d.ts.map +0 -1
  173. package/resources/memory/threads/messages.js +0 -18
  174. package/resources/memory/threads/messages.js.map +0 -1
  175. package/resources/memory/threads/messages.mjs +0 -14
  176. package/resources/memory/threads/messages.mjs.map +0 -1
  177. package/resources/memory/threads/threads.d.ts +0 -45
  178. package/resources/memory/threads/threads.d.ts.map +0 -1
  179. package/resources/memory/threads/threads.js +0 -104
  180. package/resources/memory/threads/threads.js.map +0 -1
  181. package/resources/memory/threads/threads.mjs +0 -77
  182. package/resources/memory/threads/threads.mjs.map +0 -1
  183. package/resources/syncs.d.ts +0 -15
  184. package/resources/syncs.d.ts.map +0 -1
  185. package/resources/syncs.js +0 -19
  186. package/resources/syncs.js.map +0 -1
  187. package/resources/syncs.mjs +0 -15
  188. package/resources/syncs.mjs.map +0 -1
  189. package/resources/system.d.ts +0 -9
  190. package/resources/system.d.ts.map +0 -1
  191. package/resources/system.js +0 -15
  192. package/resources/system.js.map +0 -1
  193. package/resources/system.mjs +0 -11
  194. package/resources/system.mjs.map +0 -1
  195. package/resources/tools/index.d.ts +0 -3
  196. package/resources/tools/index.d.ts.map +0 -1
  197. package/resources/tools/index.js +0 -9
  198. package/resources/tools/index.js.map +0 -1
  199. package/resources/tools/index.mjs +0 -4
  200. package/resources/tools/index.mjs.map +0 -1
  201. package/resources/tools/result.d.ts +0 -9
  202. package/resources/tools/result.d.ts.map +0 -1
  203. package/resources/tools/result.js +0 -18
  204. package/resources/tools/result.js.map +0 -1
  205. package/resources/tools/result.mjs +0 -14
  206. package/resources/tools/result.mjs.map +0 -1
  207. package/resources/tools/tools.d.ts +0 -29
  208. package/resources/tools/tools.d.ts.map +0 -1
  209. package/resources/tools/tools.js +0 -64
  210. package/resources/tools/tools.js.map +0 -1
  211. package/resources/tools/tools.mjs +0 -37
  212. package/resources/tools/tools.mjs.map +0 -1
  213. package/resources/workflows.d.ts +0 -23
  214. package/resources/workflows.d.ts.map +0 -1
  215. package/resources/workflows.js +0 -37
  216. package/resources/workflows.js.map +0 -1
  217. package/resources/workflows.mjs +0 -33
  218. package/resources/workflows.mjs.map +0 -1
  219. package/shims/node.d.ts +0 -30
  220. package/shims/node.d.ts.map +0 -1
  221. package/shims/node.js +0 -31
  222. package/shims/node.js.map +0 -1
  223. package/shims/node.mjs +0 -5
  224. package/shims/node.mjs.map +0 -1
  225. package/shims/web.d.ts +0 -26
  226. package/shims/web.d.ts.map +0 -1
  227. package/shims/web.js +0 -31
  228. package/shims/web.js.map +0 -1
  229. package/shims/web.mjs +0 -5
  230. package/shims/web.mjs.map +0 -1
  231. package/src/_shims/MultipartBody.ts +0 -9
  232. package/src/_shims/README.md +0 -46
  233. package/src/_shims/auto/runtime-bun.ts +0 -4
  234. package/src/_shims/auto/runtime-node.ts +0 -4
  235. package/src/_shims/auto/runtime.ts +0 -4
  236. package/src/_shims/auto/types-node.ts +0 -4
  237. package/src/_shims/auto/types.d.ts +0 -101
  238. package/src/_shims/auto/types.js +0 -3
  239. package/src/_shims/auto/types.mjs +0 -3
  240. package/src/_shims/bun-runtime.ts +0 -14
  241. package/src/_shims/index.d.ts +0 -81
  242. package/src/_shims/index.js +0 -13
  243. package/src/_shims/index.mjs +0 -7
  244. package/src/_shims/manual-types.d.ts +0 -12
  245. package/src/_shims/manual-types.js +0 -3
  246. package/src/_shims/manual-types.mjs +0 -3
  247. package/src/_shims/node-runtime.ts +0 -81
  248. package/src/_shims/node-types.d.ts +0 -42
  249. package/src/_shims/node-types.js +0 -3
  250. package/src/_shims/node-types.mjs +0 -3
  251. package/src/_shims/registry.ts +0 -67
  252. package/src/_shims/web-runtime.ts +0 -103
  253. package/src/_shims/web-types.d.ts +0 -83
  254. package/src/_shims/web-types.js +0 -3
  255. package/src/_shims/web-types.mjs +0 -3
  256. package/src/core.ts +0 -1201
  257. package/src/error.ts +0 -130
  258. package/src/lib/.keep +0 -4
  259. package/src/resource.ts +0 -11
  260. package/src/resources/agents/agents.ts +0 -83
  261. package/src/resources/agents/index.ts +0 -4
  262. package/src/resources/agents/tools.ts +0 -34
  263. package/src/resources/logs.ts +0 -23
  264. package/src/resources/memory/index.ts +0 -5
  265. package/src/resources/memory/memory.ts +0 -43
  266. package/src/resources/memory/status.ts +0 -16
  267. package/src/resources/memory/threads/index.ts +0 -4
  268. package/src/resources/memory/threads/messages.ts +0 -16
  269. package/src/resources/memory/threads/threads.ts +0 -105
  270. package/src/resources/syncs.ts +0 -25
  271. package/src/resources/system.ts +0 -13
  272. package/src/resources/tools/index.ts +0 -4
  273. package/src/resources/tools/result.ts +0 -16
  274. package/src/resources/tools/tools.ts +0 -54
  275. package/src/resources/workflows.ts +0 -49
  276. package/src/shims/node.ts +0 -50
  277. package/src/shims/web.ts +0 -50
  278. package/src/tsconfig.json +0 -11
  279. package/src/uploads.ts +0 -255
  280. package/src/version.ts +0 -1
  281. package/uploads.d.ts +0 -75
  282. package/uploads.d.ts.map +0 -1
  283. package/uploads.js +0 -171
  284. package/uploads.js.map +0 -1
  285. package/uploads.mjs +0 -158
  286. package/uploads.mjs.map +0 -1
  287. package/version.d.ts +0 -2
  288. package/version.d.ts.map +0 -1
  289. package/version.js +0 -5
  290. package/version.js.map +0 -1
  291. package/version.mjs +0 -2
  292. package/version.mjs.map +0 -1
@@ -0,0 +1,528 @@
1
+ import { describe, expect, beforeEach, it, vi } from 'vitest';
2
+ import { MastraClient } from "./client";
3
+ import type { MessageType } from '@mastra/core';
4
+
5
+ // Mock fetch globally
6
+ global.fetch = vi.fn();
7
+
8
+ describe('MastraClient Resources', () => {
9
+ let client: MastraClient;
10
+
11
+ // Helper to mock successful API responses
12
+ const mockFetchResponse = (data: any) => {
13
+ (global.fetch as any).mockResolvedValueOnce({
14
+ ok: true,
15
+ json: async () => data
16
+ });
17
+ };
18
+
19
+ beforeEach(() => {
20
+ // Reset mocks
21
+ vi.clearAllMocks();
22
+
23
+ // Create fresh client for each test
24
+ client = new MastraClient({
25
+ baseUrl: 'http://localhost:4111',
26
+ headers: {
27
+ 'Authorization': 'Bearer test-key'
28
+ }
29
+ });
30
+ });
31
+
32
+ describe('Vector Resource', () => {
33
+ const vectorName = 'test-vector';
34
+ let vector: ReturnType<typeof client.getVector>;
35
+
36
+ beforeEach(() => {
37
+ vector = client.getVector(vectorName);
38
+ });
39
+
40
+ it('should get vector index details', async () => {
41
+ const mockResponse = {
42
+ dimension: 128,
43
+ metric: 'cosine',
44
+ count: 1000
45
+ };
46
+ mockFetchResponse(mockResponse);
47
+
48
+ const result = await vector.details('test-index');
49
+ expect(result).toEqual(mockResponse);
50
+ expect(global.fetch).toHaveBeenCalledWith(
51
+ `${client.baseUrl}/api/vector/test-vector/indexes/test-index`,
52
+ expect.any(Object)
53
+ );
54
+ });
55
+
56
+ it('should delete vector index', async () => {
57
+ mockFetchResponse({ success: true });
58
+ const result = await vector.delete('test-index');
59
+ expect(result).toEqual({ success: true });
60
+ expect(global.fetch).toHaveBeenCalledWith(
61
+ `${client.baseUrl}/api/vector/test-vector/indexes/test-index`,
62
+ expect.objectContaining({ method: 'DELETE' })
63
+ );
64
+ });
65
+
66
+ it('should get all indexes', async () => {
67
+ const mockResponse = { indexes: ['index1', 'index2'] };
68
+ mockFetchResponse(mockResponse);
69
+ const result = await vector.getIndexes();
70
+ expect(result).toEqual(mockResponse);
71
+ expect(global.fetch).toHaveBeenCalledWith(
72
+ `${client.baseUrl}/api/vector/test-vector/indexes`,
73
+ expect.any(Object)
74
+ );
75
+ });
76
+
77
+ it('should create vector index with all parameters', async () => {
78
+ mockFetchResponse({ success: true });
79
+ const result = await vector.createIndex({
80
+ indexName: 'test-index',
81
+ dimension: 128,
82
+ metric: 'cosine'
83
+ });
84
+ expect(result).toEqual({ success: true });
85
+ expect(global.fetch).toHaveBeenCalledWith(
86
+ `${client.baseUrl}/api/vector/test-vector/create-index`,
87
+ expect.objectContaining({
88
+ method: 'POST',
89
+ body: JSON.stringify({
90
+ indexName: 'test-index',
91
+ dimension: 128,
92
+ metric: 'cosine'
93
+ })
94
+ })
95
+ );
96
+ });
97
+
98
+ it('should upsert vectors with metadata and ids', async () => {
99
+ const mockResponse = ['id1', 'id2'];
100
+ mockFetchResponse(mockResponse);
101
+ const result = await vector.upsert({
102
+ indexName: 'test-index',
103
+ vectors: [[1, 2], [3, 4]],
104
+ metadata: [{ label: 'a' }, { label: 'b' }],
105
+ ids: ['id1', 'id2']
106
+ });
107
+ expect(result).toEqual(mockResponse);
108
+ expect(global.fetch).toHaveBeenCalledWith(
109
+ `${client.baseUrl}/api/vector/test-vector/upsert`,
110
+ expect.objectContaining({
111
+ method: 'POST',
112
+ body: JSON.stringify({
113
+ indexName: 'test-index',
114
+ vectors: [[1, 2], [3, 4]],
115
+ metadata: [{ label: 'a' }, { label: 'b' }],
116
+ ids: ['id1', 'id2']
117
+ })
118
+ })
119
+ );
120
+ });
121
+
122
+ it('should query vectors with all parameters', async () => {
123
+ const mockResponse = {
124
+ results: [{
125
+ id: 'id1',
126
+ score: 0.9,
127
+ metadata: { label: 'a' },
128
+ vector: [1, 2]
129
+ }]
130
+ };
131
+ mockFetchResponse(mockResponse);
132
+ const result = await vector.query({
133
+ indexName: 'test-index',
134
+ queryVector: [1, 2],
135
+ topK: 10,
136
+ filter: { label: 'a' },
137
+ includeVector: true
138
+ });
139
+ expect(result).toEqual(mockResponse);
140
+ expect(global.fetch).toHaveBeenCalledWith(
141
+ `${client.baseUrl}/api/vector/test-vector/query`,
142
+ expect.objectContaining({
143
+ method: 'POST',
144
+ body: JSON.stringify({
145
+ indexName: 'test-index',
146
+ queryVector: [1, 2],
147
+ topK: 10,
148
+ filter: { label: 'a' },
149
+ includeVector: true
150
+ })
151
+ })
152
+ );
153
+ });
154
+ });
155
+
156
+ describe('Agent Resource', () => {
157
+ const agentId = 'test-agent';
158
+ let agent: ReturnType<typeof client.getAgent>;
159
+
160
+ beforeEach(() => {
161
+ agent = client.getAgent(agentId);
162
+ });
163
+
164
+ it('should get all agents', async () => {
165
+ const mockResponse = {
166
+ 'agent1': { name: 'Agent 1', model: 'gpt-4' },
167
+ 'agent2': { name: 'Agent 2', model: 'gpt-3.5' }
168
+ };
169
+ mockFetchResponse(mockResponse);
170
+ const result = await client.getAgents();
171
+ expect(result).toEqual(mockResponse);
172
+ expect(global.fetch).toHaveBeenCalledWith(
173
+ `${client.baseUrl}/api/agents`,
174
+ expect.any(Object)
175
+ );
176
+ });
177
+
178
+ it('should get agent details', async () => {
179
+ const mockResponse = {
180
+ name: 'Test Agent',
181
+ model: 'gpt-4',
182
+ instructions: 'Test instructions',
183
+ tools: {}
184
+ };
185
+ mockFetchResponse(mockResponse);
186
+
187
+ const result = await agent.details();
188
+ expect(result).toEqual(mockResponse);
189
+ expect(global.fetch).toHaveBeenCalledWith(
190
+ `${client.baseUrl}/api/agents/test-agent`,
191
+ expect.any(Object)
192
+ );
193
+ });
194
+
195
+ it('should generate response', async () => {
196
+ const mockResponse = {
197
+ response: 'Generated response'
198
+ };
199
+ mockFetchResponse(mockResponse);
200
+
201
+ const result = await agent.generate({
202
+ messages: [],
203
+ threadId: 'test-thread',
204
+ resourceid: 'test-resource',
205
+ output: {}
206
+ });
207
+ expect(result).toEqual(mockResponse);
208
+ expect(global.fetch).toHaveBeenCalledWith(
209
+ `${client.baseUrl}/api/agents/test-agent/generate`,
210
+ expect.objectContaining({
211
+ method: 'POST',
212
+ body: JSON.stringify({
213
+ messages: [],
214
+ threadId: 'test-thread',
215
+ resourceid: 'test-resource',
216
+ output: {}
217
+ })
218
+ })
219
+ );
220
+ });
221
+
222
+ it('should stream responses', async () => {
223
+ const mockResponse = {
224
+ stream: true,
225
+ chunks: ['chunk1', 'chunk2']
226
+ };
227
+ mockFetchResponse(mockResponse);
228
+ const result = await agent.stream({
229
+ messages: [],
230
+ threadId: 'test-thread',
231
+ resourceid: 'test-resource',
232
+ output: undefined
233
+ });
234
+ expect(result).toEqual(mockResponse);
235
+ expect(global.fetch).toHaveBeenCalledWith(
236
+ `${client.baseUrl}/api/agents/test-agent/generate`,
237
+ expect.objectContaining({
238
+ method: 'POST',
239
+ body: JSON.stringify({
240
+ messages: [],
241
+ threadId: 'test-thread',
242
+ resourceid: 'test-resource',
243
+ output: undefined,
244
+ stream: true
245
+ })
246
+ })
247
+ );
248
+ });
249
+
250
+ it('should get agent tool', async () => {
251
+ const mockResponse = {
252
+ id: 'tool1',
253
+ description: 'Test Tool'
254
+ };
255
+ mockFetchResponse(mockResponse);
256
+ const result = await agent.getTool('tool1');
257
+ expect(result).toEqual(mockResponse);
258
+ expect(global.fetch).toHaveBeenCalledWith(
259
+ `${client.baseUrl}/api/agents/test-agent/tools/tool1`,
260
+ expect.any(Object)
261
+ );
262
+ });
263
+
264
+ it('should get agent evals', async () => {
265
+ const mockResponse = {
266
+ name: 'Test Agent',
267
+ evals: [{ id: 'eval1' }]
268
+ };
269
+ mockFetchResponse(mockResponse);
270
+ const result = await agent.evals();
271
+ expect(result).toEqual(mockResponse);
272
+ expect(global.fetch).toHaveBeenCalledWith(
273
+ `${client.baseUrl}/api/agents/test-agent/evals`,
274
+ expect.any(Object)
275
+ );
276
+ });
277
+
278
+ it('should get live evals', async () => {
279
+ const mockResponse = {
280
+ name: 'Test Agent',
281
+ evals: [{ id: 'eval1', live: true }]
282
+ };
283
+ mockFetchResponse(mockResponse);
284
+ const result = await agent.liveEvals();
285
+ expect(result).toEqual(mockResponse);
286
+ expect(global.fetch).toHaveBeenCalledWith(
287
+ `${client.baseUrl}/api/agents/test-agent/evals/live`,
288
+ expect.any(Object)
289
+ );
290
+ });
291
+ });
292
+
293
+ describe('Memory Thread Resource', () => {
294
+ const threadId = 'test-thread';
295
+ let memoryThread: ReturnType<typeof client.getMemoryThread>;
296
+
297
+ beforeEach(() => {
298
+ memoryThread = client.getMemoryThread(threadId);
299
+ });
300
+
301
+ it('should get thread details', async () => {
302
+ const mockResponse = {
303
+ id: threadId,
304
+ title: 'Test Thread',
305
+ metadata: {}
306
+ };
307
+ mockFetchResponse(mockResponse);
308
+
309
+ const result = await memoryThread.get();
310
+ expect(result).toEqual(mockResponse);
311
+ expect(global.fetch).toHaveBeenCalledWith(
312
+ `${client.baseUrl}/api/memory/threads/test-thread`,
313
+ expect.any(Object)
314
+ );
315
+ });
316
+
317
+ it('should update thread', async () => {
318
+ const mockResponse = {
319
+ id: threadId,
320
+ title: 'Updated Thread',
321
+ metadata: { updated: true }
322
+ };
323
+ mockFetchResponse(mockResponse);
324
+
325
+ const result = await memoryThread.update({
326
+ title: 'Updated Thread',
327
+ metadata: { updated: true },
328
+ resourceid: 'test-resource'
329
+ });
330
+ expect(result).toEqual(mockResponse);
331
+ expect(global.fetch).toHaveBeenCalledWith(
332
+ `${client.baseUrl}/api/memory/threads/test-thread`,
333
+ expect.objectContaining({
334
+ method: 'PATCH'
335
+ })
336
+ );
337
+ });
338
+
339
+ it('should delete thread', async () => {
340
+ const mockResponse = { result: 'deleted' };
341
+ mockFetchResponse(mockResponse);
342
+ const result = await memoryThread.delete();
343
+ expect(result).toEqual(mockResponse);
344
+ expect(global.fetch).toHaveBeenCalledWith(
345
+ `${client.baseUrl}/api/memory/threads/test-thread`,
346
+ expect.objectContaining({ method: 'DELETE' })
347
+ );
348
+ });
349
+
350
+ it('should get memory status', async () => {
351
+ const mockResponse = { result: true };
352
+ mockFetchResponse(mockResponse);
353
+ const result = await client.getMemoryStatus();
354
+ expect(result).toEqual(mockResponse);
355
+ expect(global.fetch).toHaveBeenCalledWith(
356
+ `${client.baseUrl}/api/memory/status`,
357
+ expect.any(Object)
358
+ );
359
+ });
360
+
361
+ it('should save messages to memory', async () => {
362
+ const messages: MessageType[] = [{
363
+ id: '1',
364
+ type: 'text',
365
+ content: 'test',
366
+ role: 'user',
367
+ threadId: 'test-thread',
368
+ createdAt: new Date()
369
+ }];
370
+ mockFetchResponse(messages);
371
+ const result = await client.saveMessageToMemory({ messages });
372
+ expect(result).toEqual(messages);
373
+ expect(global.fetch).toHaveBeenCalledWith(
374
+ `${client.baseUrl}/api/memory/save-messages`,
375
+ expect.objectContaining({
376
+ method: 'POST',
377
+ body: JSON.stringify({ messages })
378
+ })
379
+ );
380
+ });
381
+ });
382
+
383
+ describe('Tool Resource', () => {
384
+ const toolId = 'test-tool';
385
+ let tool: ReturnType<typeof client.getTool>;
386
+
387
+ beforeEach(() => {
388
+ tool = client.getTool(toolId);
389
+ });
390
+
391
+ it('should get tool details', async () => {
392
+ const mockResponse = {
393
+ id: toolId,
394
+ description: 'Test Tool',
395
+ inputSchema: '{}',
396
+ outputSchema: '{}'
397
+ };
398
+ mockFetchResponse(mockResponse);
399
+
400
+ const result = await tool.details();
401
+ expect(result).toEqual(mockResponse);
402
+ expect(global.fetch).toHaveBeenCalledWith(
403
+ `${client.baseUrl}/api/tools/test-tool`,
404
+ expect.any(Object)
405
+ );
406
+ });
407
+
408
+ it('should execute tool', async () => {
409
+ const mockResponse = {
410
+ result: 'Tool execution result'
411
+ };
412
+ mockFetchResponse(mockResponse);
413
+
414
+ const result = await tool.execute({ input: 'test' });
415
+ expect(result).toEqual(mockResponse);
416
+ expect(global.fetch).toHaveBeenCalledWith(
417
+ `${client.baseUrl}/api/tools/test-tool/execute`,
418
+ expect.objectContaining({
419
+ method: 'POST',
420
+ body: JSON.stringify({ input: 'test' })
421
+ })
422
+ );
423
+ });
424
+ });
425
+
426
+ describe('Workflow Resource', () => {
427
+ const workflowId = 'test-workflow';
428
+ let workflow: ReturnType<typeof client.getWorkflow>;
429
+
430
+ beforeEach(() => {
431
+ workflow = client.getWorkflow(workflowId);
432
+ });
433
+
434
+ it('should get workflow details', async () => {
435
+ const mockResponse = {
436
+ name: 'Test Workflow',
437
+ triggerSchema: '{}',
438
+ steps: {},
439
+ stepGraph: {},
440
+ stepSubscriberGraph: {}
441
+ };
442
+ mockFetchResponse(mockResponse);
443
+
444
+ const result = await workflow.details();
445
+ expect(result).toEqual(mockResponse);
446
+ expect(global.fetch).toHaveBeenCalledWith(
447
+ `${client.baseUrl}/api/workflows/test-workflow`,
448
+ expect.any(Object)
449
+ );
450
+ });
451
+
452
+ it('should execute workflow', async () => {
453
+ const mockResponse = {
454
+ result: 'Workflow execution result'
455
+ };
456
+ mockFetchResponse(mockResponse);
457
+
458
+ const result = await workflow.execute({ trigger: 'test' });
459
+ expect(result).toEqual(mockResponse);
460
+ expect(global.fetch).toHaveBeenCalledWith(
461
+ `${client.baseUrl}/api/workflows/test-workflow/execute`,
462
+ expect.objectContaining({
463
+ method: 'POST',
464
+ body: JSON.stringify({ trigger: 'test' })
465
+ })
466
+ );
467
+ });
468
+ });
469
+
470
+ describe('Client Error Handling', () => {
471
+ it('should retry failed requests', async () => {
472
+ // Mock first two calls to fail, third to succeed
473
+ (global.fetch as any)
474
+ .mockRejectedValueOnce(new Error('Network error'))
475
+ .mockRejectedValueOnce(new Error('Network error'))
476
+ .mockResolvedValueOnce({
477
+ ok: true,
478
+ json: async () => ({ success: true })
479
+ });
480
+
481
+ const result = await client.request('/test-endpoint');
482
+ expect(result).toEqual({ success: true });
483
+ expect(global.fetch).toHaveBeenCalledTimes(3);
484
+ });
485
+
486
+ it('should throw error after max retries', async () => {
487
+ (global.fetch as any).mockRejectedValue(new Error('Network error'));
488
+
489
+ await expect(client.request('/test-endpoint'))
490
+ .rejects
491
+ .toThrow('Network error');
492
+
493
+ expect(global.fetch).toHaveBeenCalledTimes(4);
494
+ });
495
+ });
496
+
497
+ describe('Client Configuration', () => {
498
+ it('should handle custom retry configuration', async () => {
499
+ const customClient = new MastraClient({
500
+ baseUrl: 'http://localhost:4111',
501
+ retries: 2,
502
+ backoffMs: 100,
503
+ maxBackoffMs: 1000,
504
+ headers: { 'Custom-Header': 'value' }
505
+ });
506
+
507
+ (global.fetch as any)
508
+ .mockRejectedValueOnce(new Error('Network error'))
509
+ .mockRejectedValueOnce(new Error('Network error'))
510
+ .mockResolvedValueOnce({
511
+ ok: true,
512
+ json: async () => ({ success: true })
513
+ });
514
+
515
+ const result = await customClient.request('/test');
516
+ expect(result).toEqual({ success: true });
517
+ expect(global.fetch).toHaveBeenCalledTimes(3);
518
+ expect(global.fetch).toHaveBeenCalledWith(
519
+ 'http://localhost:4111/test',
520
+ expect.objectContaining({
521
+ headers: expect.objectContaining({
522
+ 'Custom-Header': 'value'
523
+ })
524
+ })
525
+ );
526
+ });
527
+ });
528
+ });