@mastra/mongodb 0.0.0-working-memory-per-user-20250620163010 → 0.0.0-zod-v4-compat-part-2-20250822105954

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 (64) hide show
  1. package/CHANGELOG.md +251 -3
  2. package/LICENSE.md +11 -42
  3. package/dist/index.cjs +1846 -505
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +5 -7
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +1815 -474
  8. package/dist/index.js.map +1 -0
  9. package/dist/storage/MongoDBConnector.d.ts +23 -0
  10. package/dist/storage/MongoDBConnector.d.ts.map +1 -0
  11. package/dist/storage/connectors/MongoDBConnector.d.ts +23 -0
  12. package/dist/storage/connectors/MongoDBConnector.d.ts.map +1 -0
  13. package/dist/storage/connectors/base.d.ts +6 -0
  14. package/dist/storage/connectors/base.d.ts.map +1 -0
  15. package/dist/storage/domains/legacy-evals/index.d.ts +18 -0
  16. package/dist/storage/domains/legacy-evals/index.d.ts.map +1 -0
  17. package/dist/storage/domains/memory/index.d.ts +80 -0
  18. package/dist/storage/domains/memory/index.d.ts.map +1 -0
  19. package/dist/storage/domains/operations/index.d.ts +38 -0
  20. package/dist/storage/domains/operations/index.d.ts.map +1 -0
  21. package/dist/storage/domains/scores/index.d.ts +41 -0
  22. package/dist/storage/domains/scores/index.d.ts.map +1 -0
  23. package/dist/storage/domains/traces/index.d.ts +18 -0
  24. package/dist/storage/domains/traces/index.d.ts.map +1 -0
  25. package/dist/storage/domains/utils.d.ts +8 -0
  26. package/dist/storage/domains/utils.d.ts.map +1 -0
  27. package/dist/storage/domains/workflows/index.d.ts +33 -0
  28. package/dist/storage/domains/workflows/index.d.ts.map +1 -0
  29. package/dist/storage/index.d.ts +178 -0
  30. package/dist/storage/index.d.ts.map +1 -0
  31. package/dist/storage/types.d.ts +11 -0
  32. package/dist/storage/types.d.ts.map +1 -0
  33. package/dist/vector/filter.d.ts +21 -0
  34. package/dist/vector/filter.d.ts.map +1 -0
  35. package/dist/vector/index.d.ts +78 -0
  36. package/dist/vector/index.d.ts.map +1 -0
  37. package/dist/vector/prompt.d.ts +6 -0
  38. package/dist/vector/prompt.d.ts.map +1 -0
  39. package/docker-compose.yaml +1 -1
  40. package/package.json +10 -10
  41. package/src/index.ts +1 -0
  42. package/src/storage/MongoDBConnector.ts +93 -0
  43. package/src/storage/connectors/MongoDBConnector.ts +93 -0
  44. package/src/storage/connectors/base.ts +7 -0
  45. package/src/storage/domains/legacy-evals/index.ts +193 -0
  46. package/src/storage/domains/memory/index.ts +741 -0
  47. package/src/storage/domains/operations/index.ts +155 -0
  48. package/src/storage/domains/scores/index.ts +379 -0
  49. package/src/storage/domains/traces/index.ts +142 -0
  50. package/src/storage/domains/utils.ts +43 -0
  51. package/src/storage/domains/workflows/index.ts +196 -0
  52. package/src/storage/index.test.ts +27 -989
  53. package/src/storage/index.ts +241 -605
  54. package/src/storage/types.ts +14 -0
  55. package/src/vector/filter.test.ts +40 -30
  56. package/src/vector/filter.ts +25 -4
  57. package/src/vector/index.test.ts +48 -3
  58. package/src/vector/index.ts +301 -131
  59. package/tsconfig.build.json +9 -0
  60. package/tsconfig.json +1 -1
  61. package/tsup.config.ts +22 -0
  62. package/dist/_tsup-dts-rollup.d.cts +0 -274
  63. package/dist/_tsup-dts-rollup.d.ts +0 -274
  64. package/dist/index.d.cts +0 -7
@@ -1,151 +1,16 @@
1
- import { randomUUID } from 'crypto';
2
- import type { MastraMessageV1, MastraMessageV2, MetricResult, WorkflowRunState } from '@mastra/core';
3
- import type { TABLE_NAMES } from '@mastra/core/storage';
4
- import { TABLE_EVALS, TABLE_MESSAGES, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';
5
- import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
6
- import type { MongoDBConfig } from './index';
1
+ import { createTestSuite } from '@internal/storage-test-utils';
2
+ import { describe, expect, it } from 'vitest';
3
+ import type { ConnectorHandler } from './connectors/base';
4
+ import type { MongoDBConfig } from './types';
7
5
  import { MongoDBStore } from './index';
8
6
 
9
- class Test {
10
- store: MongoDBStore;
11
-
12
- constructor(store: MongoDBStore) {
13
- this.store = store;
14
- }
15
-
16
- build() {
17
- return this;
18
- }
19
-
20
- async clearTables() {
21
- try {
22
- await this.store.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
23
- await this.store.clearTable({ tableName: TABLE_MESSAGES });
24
- await this.store.clearTable({ tableName: TABLE_THREADS });
25
- await this.store.clearTable({ tableName: TABLE_EVALS });
26
- } catch (error) {
27
- // Ignore errors during table clearing
28
- console.warn('Error clearing tables:', error);
29
- }
30
- }
31
-
32
- generateSampleThread(options: any = {}) {
33
- return {
34
- id: `thread-${randomUUID()}`,
35
- resourceId: `resource-${randomUUID()}`,
36
- title: 'Test Thread',
37
- createdAt: new Date(),
38
- updatedAt: new Date(),
39
- metadata: { key: 'value' },
40
- ...options,
41
- };
42
- }
43
-
44
- generateSampleMessageV1({
45
- threadId,
46
- resourceId = randomUUID(),
47
- content = 'Hello',
48
- }: {
49
- threadId: string;
50
- resourceId?: string;
51
- content?: string;
52
- }): MastraMessageV1 {
53
- return {
54
- id: `msg-${randomUUID()}`,
55
- role: 'user',
56
- type: 'text',
57
- threadId,
58
- content: [{ type: 'text', text: content }],
59
- createdAt: new Date(),
60
- resourceId,
61
- };
62
- }
63
-
64
- generateSampleMessageV2({
65
- threadId,
66
- resourceId = randomUUID(),
67
- content = 'Hello',
68
- }: {
69
- threadId: string;
70
- resourceId?: string;
71
- content?: string;
72
- }): MastraMessageV2 {
73
- return {
74
- id: `msg-${randomUUID()}`,
75
- role: 'user',
76
- type: 'text',
77
- threadId,
78
- content: {
79
- format: 2,
80
- parts: [{ type: 'text', text: content }],
81
- },
82
- createdAt: new Date(),
83
- resourceId,
84
- };
85
- }
86
-
87
- generateSampleEval(isTest: boolean, options: any = {}) {
88
- const testInfo = isTest ? { testPath: 'test/path.ts', testName: 'Test Name' } : undefined;
89
-
90
- return {
91
- id: randomUUID(),
92
- agentName: 'Agent Name',
93
- input: 'Sample input',
94
- output: 'Sample output',
95
- result: { score: 0.8 } as MetricResult,
96
- metricName: 'sample-metric',
97
- instructions: 'Sample instructions',
98
- testInfo,
99
- globalRunId: `global-${randomUUID()}`,
100
- runId: `run-${randomUUID()}`,
101
- createdAt: new Date().toISOString(),
102
- ...options,
103
- };
104
- }
105
-
106
- generateSampleWorkflowSnapshot(options: any = {}) {
107
- const runId = `run-${randomUUID()}`;
108
- const stepId = `step-${randomUUID()}`;
109
- const timestamp = options.createdAt || new Date();
110
- const snapshot = {
111
- result: { success: true },
112
- value: {},
113
- context: {
114
- [stepId]: {
115
- status: options.status,
116
- payload: {},
117
- error: undefined,
118
- startedAt: timestamp.getTime(),
119
- endedAt: new Date(timestamp.getTime() + 15000).getTime(),
120
- },
121
- input: {},
122
- },
123
- serializedStepGraph: [],
124
- activePaths: [],
125
- suspendedPaths: {},
126
- runId,
127
- timestamp: timestamp.getTime(),
128
- status: options.status,
129
- } as WorkflowRunState;
130
- return { snapshot, runId, stepId };
131
- }
132
- }
133
-
134
7
  const TEST_CONFIG: MongoDBConfig = {
135
8
  url: process.env.MONGODB_URL || 'mongodb://localhost:27017',
136
9
  dbName: process.env.MONGODB_DB_NAME || 'mastra-test-db',
137
10
  };
138
11
 
139
- describe('MongoDBStore', () => {
140
- let store: MongoDBStore;
141
-
142
- beforeAll(async () => {
143
- store = new MongoDBStore(TEST_CONFIG);
144
- await store.init();
145
- });
146
-
147
- // --- Validation tests ---
148
- describe('Validation', () => {
12
+ describe('Validation', () => {
13
+ describe('with database options', () => {
149
14
  const validConfig = TEST_CONFIG;
150
15
  it('throws if url is empty', () => {
151
16
  expect(() => new MongoDBStore({ ...validConfig, url: '' })).toThrow(/url must be provided and cannot be empty/);
@@ -158,867 +23,40 @@ describe('MongoDBStore', () => {
158
23
  const { dbName, ...rest } = validConfig;
159
24
  expect(() => new MongoDBStore(rest as any)).toThrow(/dbName must be provided and cannot be empty/);
160
25
  });
26
+
161
27
  it('does not throw on valid config (host-based)', () => {
162
28
  expect(() => new MongoDBStore(validConfig)).not.toThrow();
163
29
  });
164
30
  });
165
31
 
166
- describe('Thread Operations', () => {
167
- it('should create and retrieve a thread', async () => {
168
- const test = new Test(store).build();
169
- await test.clearTables();
170
- const thread = test.generateSampleThread();
171
-
172
- // Save thread
173
- const savedThread = await store.saveThread({ thread });
174
- expect(savedThread).toEqual(thread);
175
-
176
- // Retrieve thread
177
- const retrievedThread = await store.getThreadById({ threadId: thread.id });
178
- expect(retrievedThread?.title).toEqual(thread.title);
179
- });
180
-
181
- it('should return null for non-existent thread', async () => {
182
- const test = new Test(store).build();
183
- await test.clearTables();
184
-
185
- const result = await store.getThreadById({ threadId: 'non-existent' });
186
- expect(result).toBeNull();
187
- });
188
-
189
- it('should get threads by resource ID', async () => {
190
- const test = new Test(store).build();
191
- await test.clearTables();
192
-
193
- const thread1 = test.generateSampleThread();
194
- const thread2 = test.generateSampleThread({ resourceId: thread1.resourceId });
195
-
196
- await store.saveThread({ thread: thread1 });
197
- await store.saveThread({ thread: thread2 });
198
-
199
- const threads = await store.getThreadsByResourceId({ resourceId: thread1.resourceId });
200
- expect(threads).toHaveLength(2);
201
- expect(threads.map(t => t.id)).toEqual(expect.arrayContaining([thread1.id, thread2.id]));
202
- });
203
-
204
- it('should update thread title and metadata', async () => {
205
- const test = new Test(store).build();
206
- await test.clearTables();
207
-
208
- const thread = test.generateSampleThread();
209
- await store.saveThread({ thread });
210
-
211
- const newMetadata = { newKey: 'newValue' };
212
- const updatedThread = await store.updateThread({
213
- id: thread.id,
214
- title: 'Updated Title',
215
- metadata: newMetadata,
216
- });
217
-
218
- expect(updatedThread.title).toBe('Updated Title');
219
- expect(updatedThread.metadata).toEqual({
220
- ...thread.metadata,
221
- ...newMetadata,
222
- });
223
-
224
- // Verify persistence
225
- const retrievedThread = await store.getThreadById({ threadId: thread.id });
226
- expect(retrievedThread).toEqual(updatedThread);
227
- });
228
-
229
- it('should delete thread and its messages', async () => {
230
- const test = new Test(store).build();
231
- await test.clearTables();
232
-
233
- const thread = test.generateSampleThread();
234
- await store.saveThread({ thread });
235
-
236
- // Add some messages
237
- const messages = [
238
- test.generateSampleMessageV1({ threadId: thread.id }),
239
- test.generateSampleMessageV1({ threadId: thread.id }),
240
- ];
241
- await store.saveMessages({ messages });
242
-
243
- await store.deleteThread({ threadId: thread.id });
244
-
245
- const retrievedThread = await store.getThreadById({ threadId: thread.id });
246
- expect(retrievedThread).toBeNull();
247
-
248
- // Verify messages were also deleted
249
- const retrievedMessages = await store.getMessages({ threadId: thread.id });
250
- expect(retrievedMessages).toHaveLength(0);
251
- });
252
-
253
- it('should not create duplicate threads with the same threadId but update the existing one', async () => {
254
- const test = new Test(store).build();
255
- await test.clearTables();
256
- const thread = test.generateSampleThread();
257
-
258
- // Save the thread for the first time
259
- await store.saveThread({ thread });
260
-
261
- // Modify the thread and save again with the same id
262
- const updatedThread = { ...thread, title: 'Updated Title', metadata: { key: 'newValue' } };
263
- await store.saveThread({ thread: updatedThread });
264
-
265
- // Retrieve all threads with this id (should only be one)
266
- const collection = await store['getCollection'](TABLE_THREADS);
267
- const allThreads = await collection.find({ id: thread.id }).toArray();
268
- expect(allThreads).toHaveLength(1);
269
-
270
- // Retrieve the thread and check it was updated
271
- const retrievedThread = await store.getThreadById({ threadId: thread.id });
272
- expect(retrievedThread?.title).toBe('Updated Title');
273
- expect(retrievedThread?.metadata).toEqual({ key: 'newValue' });
274
- });
275
-
276
- it('should update thread updatedAt when a message is saved to it', async () => {
277
- const test = new Test(store).build();
278
- await test.clearTables();
279
-
280
- const thread = test.generateSampleThread();
281
- await store.saveThread({ thread });
282
-
283
- const initialThread = await store.getThreadById({ threadId: thread.id });
284
- expect(initialThread).toBeDefined();
285
- const originalUpdatedAt = initialThread!.updatedAt;
286
-
287
- await new Promise(resolve => setTimeout(resolve, 10));
288
-
289
- const message = test.generateSampleMessageV1({ threadId: thread.id });
290
- await store.saveMessages({ messages: [message] });
291
-
292
- const updatedThread = await store.getThreadById({ threadId: thread.id });
293
- expect(updatedThread).toBeDefined();
294
- expect(updatedThread!.updatedAt.getTime()).toBeGreaterThan(originalUpdatedAt.getTime());
295
- });
296
- });
297
-
298
- describe('Message Operations', () => {
299
- it('should save and retrieve messages', async () => {
300
- const test = new Test(store).build();
301
- await test.clearTables();
302
- const thread = test.generateSampleThread();
303
- await store.saveThread({ thread });
304
-
305
- const messages = [
306
- test.generateSampleMessageV1({ threadId: thread.id }),
307
- { ...test.generateSampleMessageV1({ threadId: thread.id }), role: 'assistant' as const },
308
- ];
309
-
310
- // Save messages
311
- const savedMessages = await store.saveMessages({ messages });
312
- expect(savedMessages).toEqual(messages);
313
-
314
- // Retrieve messages
315
- const retrievedMessages = await store.getMessages({ threadId: thread.id });
316
- expect(retrievedMessages).toHaveLength(2);
317
- expect(messages[0]).toEqual(expect.objectContaining(retrievedMessages[0]));
318
- expect(messages[1]).toEqual(expect.objectContaining(retrievedMessages[1]));
319
- });
320
-
321
- it('should handle empty message array', async () => {
322
- const test = new Test(store).build();
323
- await test.clearTables();
324
-
325
- const result = await store.saveMessages({ messages: [] });
326
- expect(result).toEqual([]);
327
- });
328
-
329
- it('should maintain message order', async () => {
330
- const test = new Test(store).build();
331
- await test.clearTables();
332
- const thread = test.generateSampleThread();
333
- await store.saveThread({ thread });
334
-
335
- const messages = [
336
- {
337
- ...test.generateSampleMessageV2({ threadId: thread.id, content: 'First' }),
338
- },
339
- {
340
- ...test.generateSampleMessageV2({ threadId: thread.id, content: 'Second' }),
341
- },
342
- {
343
- ...test.generateSampleMessageV2({ threadId: thread.id, content: 'Third' }),
344
- },
345
- ];
346
-
347
- await store.saveMessages({ messages, format: 'v2' });
348
-
349
- const retrievedMessages = await store.getMessages({ threadId: thread.id, format: 'v2' });
350
- expect(retrievedMessages).toHaveLength(3);
351
-
352
- // Verify order is maintained
353
- retrievedMessages.forEach((msg, idx) => {
354
- expect((msg as any).content.parts).toEqual(messages[idx]!.content.parts);
355
- });
356
- });
357
-
358
- // it('should retrieve messages w/ next/prev messages by message id + resource id', async () => {
359
- // const test = new Test(store).build();
360
- // const messages: MastraMessageV2[] = [
361
- // test.generateSampleMessageV2({ threadId: 'thread-one', content: 'First', resourceId: 'cross-thread-resource' }),
362
- // test.generateSampleMessageV2({
363
- // threadId: 'thread-one',
364
- // content: 'Second',
365
- // resourceId: 'cross-thread-resource',
366
- // }),
367
- // test.generateSampleMessageV2({ threadId: 'thread-one', content: 'Third', resourceId: 'cross-thread-resource' }),
368
-
369
- // test.generateSampleMessageV2({
370
- // threadId: 'thread-two',
371
- // content: 'Fourth',
372
- // resourceId: 'cross-thread-resource',
373
- // }),
374
- // test.generateSampleMessageV2({ threadId: 'thread-two', content: 'Fifth', resourceId: 'cross-thread-resource' }),
375
- // test.generateSampleMessageV2({ threadId: 'thread-two', content: 'Sixth', resourceId: 'cross-thread-resource' }),
376
-
377
- // test.generateSampleMessageV2({ threadId: 'thread-three', content: 'Seventh', resourceId: 'other-resource' }),
378
- // test.generateSampleMessageV2({ threadId: 'thread-three', content: 'Eighth', resourceId: 'other-resource' }),
379
- // ];
380
-
381
- // await store.saveMessages({ messages: messages, format: 'v2' });
382
-
383
- // const retrievedMessages: MastraMessageV2[] = await store.getMessages({ threadId: 'thread-one', format: 'v2' });
384
- // expect(retrievedMessages).toHaveLength(3);
385
- // expect(retrievedMessages.map(m => (m.content.parts[0] as any).text)).toEqual(['First', 'Second', 'Third']);
386
-
387
- // const retrievedMessages2: MastraMessageV2[] = await store.getMessages({ threadId: 'thread-two', format: 'v2' });
388
- // expect(retrievedMessages2).toHaveLength(3);
389
- // expect(retrievedMessages2.map(m => (m.content.parts[0] as any).text)).toEqual(['Fourth', 'Fifth', 'Sixth']);
390
-
391
- // const retrievedMessages3: MastraMessageV2[] = await store.getMessages({ threadId: 'thread-three', format: 'v2' });
392
- // expect(retrievedMessages3).toHaveLength(2);
393
- // expect(retrievedMessages3.map(m => (m.content.parts[0] as any).text)).toEqual(['Seventh', 'Eighth']);
394
-
395
- // const crossThreadMessages: MastraMessageV2[] = await store.getMessages({
396
- // threadId: 'thread-doesnt-exist',
397
- // resourceId: 'cross-thread-resource',
398
- // format: 'v2',
399
- // selectBy: {
400
- // last: 0,
401
- // include: [
402
- // {
403
- // id: messages[1].id,
404
- // withNextMessages: 2,
405
- // withPreviousMessages: 2,
406
- // },
407
- // {
408
- // id: messages[4].id,
409
- // withPreviousMessages: 2,
410
- // withNextMessages: 2,
411
- // },
412
- // ],
413
- // },
414
- // });
415
-
416
- // expect(crossThreadMessages).toHaveLength(6);
417
- // expect(crossThreadMessages.filter(m => m.threadId === `thread-one`)).toHaveLength(3);
418
- // expect(crossThreadMessages.filter(m => m.threadId === `thread-two`)).toHaveLength(3);
419
-
420
- // const crossThreadMessages2: MastraMessageV2[] = await store.getMessages({
421
- // threadId: 'thread-one',
422
- // resourceId: 'cross-thread-resource',
423
- // format: 'v2',
424
- // selectBy: {
425
- // last: 0,
426
- // include: [
427
- // {
428
- // id: messages[4].id,
429
- // withPreviousMessages: 1,
430
- // withNextMessages: 30,
431
- // },
432
- // ],
433
- // },
434
- // });
435
-
436
- // expect(crossThreadMessages2).toHaveLength(3);
437
- // expect(crossThreadMessages2.filter(m => m.threadId === `thread-one`)).toHaveLength(0);
438
- // expect(crossThreadMessages2.filter(m => m.threadId === `thread-two`)).toHaveLength(3);
439
-
440
- // const crossThreadMessages3: MastraMessageV2[] = await store.getMessages({
441
- // threadId: 'thread-two',
442
- // resourceId: 'cross-thread-resource',
443
- // format: 'v2',
444
- // selectBy: {
445
- // last: 0,
446
- // include: [
447
- // {
448
- // id: messages[1].id,
449
- // withNextMessages: 1,
450
- // withPreviousMessages: 1,
451
- // },
452
- // ],
453
- // },
454
- // });
455
-
456
- // expect(crossThreadMessages3).toHaveLength(3);
457
- // expect(crossThreadMessages3.filter(m => m.threadId === `thread-one`)).toHaveLength(3);
458
- // expect(crossThreadMessages3.filter(m => m.threadId === `thread-two`)).toHaveLength(0);
459
- // });
460
- });
461
-
462
- describe('Edge Cases and Error Handling', () => {
463
- it('should handle large metadata objects', async () => {
464
- const test = new Test(store).build();
465
- await test.clearTables();
466
- const thread = test.generateSampleThread();
467
- const largeMetadata = {
468
- ...thread.metadata,
469
- largeArray: Array.from({ length: 1000 }, (_, i) => ({ index: i, data: 'test'.repeat(100) })),
470
- };
471
-
472
- const threadWithLargeMetadata = {
473
- ...thread,
474
- metadata: largeMetadata,
475
- };
476
-
477
- await store.saveThread({ thread: threadWithLargeMetadata });
478
- const retrieved = await store.getThreadById({ threadId: thread.id });
479
-
480
- expect(retrieved?.metadata).toEqual(largeMetadata);
481
- });
482
-
483
- it('should handle special characters in thread titles', async () => {
484
- const test = new Test(store).build();
485
- await test.clearTables();
486
- const thread = test.generateSampleThread({
487
- title: 'Special \'quotes\' and "double quotes" and emoji 🎉',
488
- });
489
-
490
- await store.saveThread({ thread });
491
- const retrieved = await store.getThreadById({ threadId: thread.id });
492
-
493
- expect(retrieved?.title).toBe(thread.title);
494
- });
495
-
496
- it('should handle concurrent thread updates', async () => {
497
- const test = new Test(store).build();
498
- await test.clearTables();
499
- const thread = test.generateSampleThread();
500
- await store.saveThread({ thread });
32
+ describe('with connection handler', () => {
33
+ const validWithConnectionHandlerConfig = {
34
+ connectorHandler: {} as ConnectorHandler,
35
+ };
501
36
 
502
- // Perform multiple updates concurrently
503
- const updates = Array.from({ length: 5 }, (_, i) =>
504
- store.updateThread({
505
- id: thread.id,
506
- title: `Update ${i}`,
507
- metadata: { update: i },
508
- }),
37
+ it('not throws if url is empty', () => {
38
+ expect(() => new MongoDBStore({ ...validWithConnectionHandlerConfig, url: '' })).not.toThrow(
39
+ /url must be provided and cannot be empty/,
509
40
  );
510
-
511
- await expect(Promise.all(updates)).resolves.toBeDefined();
512
-
513
- // Verify final state
514
- const finalThread = await store.getThreadById({ threadId: thread.id });
515
- expect(finalThread).toBeDefined();
516
- });
517
- });
518
-
519
- describe('Workflow Snapshots', () => {
520
- it('should persist and load workflow snapshots', async () => {
521
- const test = new Test(store).build();
522
- await test.clearTables();
523
- const workflowName = 'test-workflow';
524
- const runId = `run-${randomUUID()}`;
525
- const snapshot = {
526
- status: 'running',
527
- context: {
528
- stepResults: {},
529
- attempts: {},
530
- triggerData: { type: 'manual' },
531
- },
532
- } as any;
533
-
534
- await store.persistWorkflowSnapshot({
535
- workflowName,
536
- runId,
537
- snapshot,
538
- });
539
-
540
- const loadedSnapshot = await store.loadWorkflowSnapshot({
541
- workflowName,
542
- runId,
543
- });
544
-
545
- expect(loadedSnapshot).toEqual(snapshot);
546
- });
547
-
548
- it('should return null for non-existent workflow snapshot', async () => {
549
- const result = await store.loadWorkflowSnapshot({
550
- workflowName: 'non-existent',
551
- runId: 'non-existent',
552
- });
553
-
554
- expect(result).toBeNull();
555
- });
556
-
557
- it('should update existing workflow snapshot', async () => {
558
- const workflowName = 'test-workflow';
559
- const runId = `run-${randomUUID()}`;
560
- const initialSnapshot = {
561
- status: 'running',
562
- context: {
563
- stepResults: {},
564
- attempts: {},
565
- triggerData: { type: 'manual' },
566
- },
567
- };
568
-
569
- await store.persistWorkflowSnapshot({
570
- workflowName,
571
- runId,
572
- snapshot: initialSnapshot as any,
573
- });
574
-
575
- const updatedSnapshot = {
576
- status: 'completed',
577
- context: {
578
- stepResults: {
579
- 'step-1': { status: 'success', result: { data: 'test' } },
580
- },
581
- attempts: { 'step-1': 1 },
582
- triggerData: { type: 'manual' },
583
- },
584
- } as any;
585
-
586
- await store.persistWorkflowSnapshot({
587
- workflowName,
588
- runId,
589
- snapshot: updatedSnapshot,
590
- });
591
-
592
- const loadedSnapshot = await store.loadWorkflowSnapshot({
593
- workflowName,
594
- runId,
595
- });
596
-
597
- expect(loadedSnapshot).toEqual(updatedSnapshot);
598
- });
599
-
600
- it('should handle complex workflow state', async () => {
601
- const workflowName = 'complex-workflow';
602
- const runId = `run-${randomUUID()}`;
603
- const complexSnapshot = {
604
- value: { currentState: 'running' },
605
- context: {
606
- stepResults: {
607
- 'step-1': {
608
- status: 'success',
609
- result: {
610
- nestedData: {
611
- array: [1, 2, 3],
612
- object: { key: 'value' },
613
- date: new Date().toISOString(),
614
- },
615
- },
616
- },
617
- 'step-2': {
618
- status: 'waiting',
619
- dependencies: ['step-3', 'step-4'],
620
- },
621
- },
622
- attempts: { 'step-1': 1, 'step-2': 0 },
623
- triggerData: {
624
- type: 'scheduled',
625
- metadata: {
626
- schedule: '0 0 * * *',
627
- timezone: 'UTC',
628
- },
629
- },
630
- },
631
- activePaths: [
632
- {
633
- stepPath: ['step-1'],
634
- stepId: 'step-1',
635
- status: 'success',
636
- },
637
- {
638
- stepPath: ['step-2'],
639
- stepId: 'step-2',
640
- status: 'waiting',
641
- },
642
- ],
643
- serializedStepGraph: [],
644
- runId: runId,
645
- status: 'running',
646
- timestamp: Date.now(),
647
- };
648
-
649
- await store.persistWorkflowSnapshot({
650
- workflowName,
651
- runId,
652
- snapshot: complexSnapshot as unknown as WorkflowRunState,
653
- });
654
-
655
- const loadedSnapshot = await store.loadWorkflowSnapshot({
656
- workflowName,
657
- runId,
658
- });
659
-
660
- expect(loadedSnapshot).toEqual(complexSnapshot);
661
- });
662
- });
663
-
664
- describe('getWorkflowRuns', () => {
665
- it('returns empty array when no workflows exist', async () => {
666
- const test = new Test(store).build();
667
- await test.clearTables();
668
-
669
- const { runs, total } = await store.getWorkflowRuns();
670
- expect(runs).toEqual([]);
671
- expect(total).toBe(0);
672
- });
673
-
674
- it('returns all workflows by default', async () => {
675
- const test = new Test(store).build();
676
- await test.clearTables();
677
-
678
- const workflowName1 = 'default_test_1';
679
- const workflowName2 = 'default_test_2';
680
-
681
- const {
682
- snapshot: workflow1,
683
- runId: runId1,
684
- stepId: stepId1,
685
- } = test.generateSampleWorkflowSnapshot({ status: 'completed' });
686
- const {
687
- snapshot: workflow2,
688
- runId: runId2,
689
- stepId: stepId2,
690
- } = test.generateSampleWorkflowSnapshot({ status: 'running' });
691
-
692
- await store.persistWorkflowSnapshot({ workflowName: workflowName1, runId: runId1, snapshot: workflow1 });
693
- await new Promise(resolve => setTimeout(resolve, 10)); // Small delay to ensure different timestamps
694
- await store.persistWorkflowSnapshot({ workflowName: workflowName2, runId: runId2, snapshot: workflow2 });
695
-
696
- const { runs, total } = await store.getWorkflowRuns();
697
- expect(runs).toHaveLength(2);
698
- expect(total).toBe(2);
699
- expect(runs[0]!.workflowName).toBe(workflowName2); // Most recent first
700
- expect(runs[1]!.workflowName).toBe(workflowName1);
701
- const firstSnapshot = runs[0]!.snapshot as WorkflowRunState;
702
- const secondSnapshot = runs[1]!.snapshot as WorkflowRunState;
703
- expect(firstSnapshot.context?.[stepId2]?.status).toBe('running');
704
- expect(secondSnapshot.context?.[stepId1]?.status).toBe('completed');
705
- });
706
-
707
- it('filters by workflow name', async () => {
708
- const test = new Test(store).build();
709
- await test.clearTables();
710
- const workflowName1 = 'filter_test_1';
711
- const workflowName2 = 'filter_test_2';
712
-
713
- const {
714
- snapshot: workflow1,
715
- runId: runId1,
716
- stepId: stepId1,
717
- } = test.generateSampleWorkflowSnapshot({ status: 'completed' });
718
- const { snapshot: workflow2, runId: runId2 } = test.generateSampleWorkflowSnapshot({ status: 'failed' });
719
-
720
- await store.persistWorkflowSnapshot({ workflowName: workflowName1, runId: runId1, snapshot: workflow1 });
721
- await new Promise(resolve => setTimeout(resolve, 10)); // Small delay to ensure different timestamps
722
- await store.persistWorkflowSnapshot({ workflowName: workflowName2, runId: runId2, snapshot: workflow2 });
723
-
724
- const { runs, total } = await store.getWorkflowRuns({ workflowName: workflowName1 });
725
- expect(runs).toHaveLength(1);
726
- expect(total).toBe(1);
727
- expect(runs[0]!.workflowName).toBe(workflowName1);
728
- const snapshot = runs[0]!.snapshot as WorkflowRunState;
729
- expect(snapshot.context?.[stepId1]?.status).toBe('completed');
730
- });
731
-
732
- it('filters by date range', async () => {
733
- const test = new Test(store).build();
734
- await test.clearTables();
735
- const now = new Date();
736
- const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000);
737
- const twoDaysAgo = new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000);
738
- const workflowName1 = 'date_test_1';
739
- const workflowName2 = 'date_test_2';
740
- const workflowName3 = 'date_test_3';
741
-
742
- const { snapshot: workflow1, runId: runId1 } = test.generateSampleWorkflowSnapshot({ status: 'completed' });
743
- const {
744
- snapshot: workflow2,
745
- runId: runId2,
746
- stepId: stepId2,
747
- } = test.generateSampleWorkflowSnapshot({ status: 'running' });
748
- const {
749
- snapshot: workflow3,
750
- runId: runId3,
751
- stepId: stepId3,
752
- } = test.generateSampleWorkflowSnapshot({ status: 'waiting' });
753
-
754
- await store.insert({
755
- tableName: TABLE_WORKFLOW_SNAPSHOT,
756
- record: {
757
- workflow_name: workflowName1,
758
- run_id: runId1,
759
- snapshot: workflow1,
760
- createdAt: twoDaysAgo,
761
- updatedAt: twoDaysAgo,
762
- },
763
- });
764
- await store.insert({
765
- tableName: TABLE_WORKFLOW_SNAPSHOT,
766
- record: {
767
- workflow_name: workflowName2,
768
- run_id: runId2,
769
- snapshot: workflow2,
770
- createdAt: yesterday,
771
- updatedAt: yesterday,
772
- },
773
- });
774
- await store.insert({
775
- tableName: TABLE_WORKFLOW_SNAPSHOT,
776
- record: {
777
- workflow_name: workflowName3,
778
- run_id: runId3,
779
- snapshot: workflow3,
780
- createdAt: now,
781
- updatedAt: now,
782
- },
783
- });
784
-
785
- const { runs } = await store.getWorkflowRuns({
786
- fromDate: yesterday,
787
- toDate: now,
788
- });
789
-
790
- expect(runs).toHaveLength(2);
791
- expect(runs[0]!.workflowName).toBe(workflowName3);
792
- expect(runs[1]!.workflowName).toBe(workflowName2);
793
- const firstSnapshot = runs[0]!.snapshot as WorkflowRunState;
794
- const secondSnapshot = runs[1]!.snapshot as WorkflowRunState;
795
- expect(firstSnapshot.context?.[stepId3]?.status).toBe('waiting');
796
- expect(secondSnapshot.context?.[stepId2]?.status).toBe('running');
797
- });
798
-
799
- it('handles pagination', async () => {
800
- const test = new Test(store).build();
801
- await test.clearTables();
802
- const workflowName1 = 'page_test_1';
803
- const workflowName2 = 'page_test_2';
804
- const workflowName3 = 'page_test_3';
805
-
806
- const {
807
- snapshot: workflow1,
808
- runId: runId1,
809
- stepId: stepId1,
810
- } = test.generateSampleWorkflowSnapshot({ status: 'completed' });
811
- const {
812
- snapshot: workflow2,
813
- runId: runId2,
814
- stepId: stepId2,
815
- } = test.generateSampleWorkflowSnapshot({ status: 'running' });
816
- const {
817
- snapshot: workflow3,
818
- runId: runId3,
819
- stepId: stepId3,
820
- } = test.generateSampleWorkflowSnapshot({ status: 'waiting' });
821
-
822
- await store.persistWorkflowSnapshot({ workflowName: workflowName1, runId: runId1, snapshot: workflow1 });
823
- await new Promise(resolve => setTimeout(resolve, 10)); // Small delay to ensure different timestamps
824
- await store.persistWorkflowSnapshot({ workflowName: workflowName2, runId: runId2, snapshot: workflow2 });
825
- await new Promise(resolve => setTimeout(resolve, 10)); // Small delay to ensure different timestamps
826
- await store.persistWorkflowSnapshot({ workflowName: workflowName3, runId: runId3, snapshot: workflow3 });
827
-
828
- // Get first page
829
- const page1 = await store.getWorkflowRuns({ limit: 2, offset: 0 });
830
- expect(page1.runs).toHaveLength(2);
831
- expect(page1.total).toBe(3); // Total count of all records
832
- expect(page1.runs[0]!.workflowName).toBe(workflowName3);
833
- expect(page1.runs[1]!.workflowName).toBe(workflowName2);
834
- const firstSnapshot = page1.runs[0]!.snapshot as WorkflowRunState;
835
- const secondSnapshot = page1.runs[1]!.snapshot as WorkflowRunState;
836
- expect(firstSnapshot.context?.[stepId3]?.status).toBe('waiting');
837
- expect(secondSnapshot.context?.[stepId2]?.status).toBe('running');
838
-
839
- // Get second page
840
- const page2 = await store.getWorkflowRuns({ limit: 2, offset: 2 });
841
- expect(page2.runs).toHaveLength(1);
842
- expect(page2.total).toBe(3);
843
- expect(page2.runs[0]!.workflowName).toBe(workflowName1);
844
- const snapshot = page2.runs[0]!.snapshot as WorkflowRunState;
845
- expect(snapshot.context?.[stepId1]?.status).toBe('completed');
846
41
  });
847
- });
848
-
849
- describe('Eval Operations', () => {
850
- it('should retrieve evals by agent name', async () => {
851
- const test = new Test(store).build();
852
- await test.clearTables();
853
- const agentName = `test-agent-${randomUUID()}`;
854
-
855
- // Create sample evals
856
- const liveEval = test.generateSampleEval(false, { agentName });
857
- const testEval = test.generateSampleEval(true, { agentName });
858
- const otherAgentEval = test.generateSampleEval(false, { agentName: `other-agent-${randomUUID()}` });
859
-
860
- // Insert evals
861
- await store.insert({
862
- tableName: TABLE_EVALS,
863
- record: {
864
- agent_name: liveEval.agentName,
865
- input: liveEval.input,
866
- output: liveEval.output,
867
- result: liveEval.result,
868
- metric_name: liveEval.metricName,
869
- instructions: liveEval.instructions,
870
- test_info: null,
871
- global_run_id: liveEval.globalRunId,
872
- run_id: liveEval.runId,
873
- created_at: liveEval.createdAt,
874
- createdAt: new Date(liveEval.createdAt),
875
- },
876
- });
877
-
878
- await store.insert({
879
- tableName: TABLE_EVALS,
880
- record: {
881
- agent_name: testEval.agentName,
882
- input: testEval.input,
883
- output: testEval.output,
884
- result: testEval.result,
885
- metric_name: testEval.metricName,
886
- instructions: testEval.instructions,
887
- test_info: JSON.stringify(testEval.testInfo),
888
- global_run_id: testEval.globalRunId,
889
- run_id: testEval.runId,
890
- created_at: testEval.createdAt,
891
- createdAt: new Date(testEval.createdAt),
892
- },
893
- });
894
-
895
- await store.insert({
896
- tableName: TABLE_EVALS,
897
- record: {
898
- agent_name: otherAgentEval.agentName,
899
- input: otherAgentEval.input,
900
- output: otherAgentEval.output,
901
- result: otherAgentEval.result,
902
- metric_name: otherAgentEval.metricName,
903
- instructions: otherAgentEval.instructions,
904
- test_info: null,
905
- global_run_id: otherAgentEval.globalRunId,
906
- run_id: otherAgentEval.runId,
907
- created_at: otherAgentEval.createdAt,
908
- createdAt: new Date(otherAgentEval.createdAt),
909
- },
910
- });
911
42
 
912
- // Test getting all evals for the agent
913
- const allEvals = await store.getEvalsByAgentName(agentName);
914
- expect(allEvals).toHaveLength(2);
915
- expect(allEvals.map(e => e.runId)).toEqual(expect.arrayContaining([liveEval.runId, testEval.runId]));
916
-
917
- // Test getting only live evals
918
- const liveEvals = await store.getEvalsByAgentName(agentName, 'live');
919
- expect(liveEvals).toHaveLength(1);
920
- expect(liveEvals[0]!.runId).toBe(liveEval.runId);
921
-
922
- // Test getting only test evals
923
- const testEvals = await store.getEvalsByAgentName(agentName, 'test');
924
- expect(testEvals).toHaveLength(1);
925
- expect(testEvals[0]!.runId).toBe(testEval.runId);
926
- expect(testEvals[0]!.testInfo).toEqual(testEval.testInfo);
927
-
928
- // Test getting evals for non-existent agent
929
- const nonExistentEvals = await store.getEvalsByAgentName('non-existent-agent');
930
- expect(nonExistentEvals).toHaveLength(0);
931
- });
932
- });
933
-
934
- describe('alterTable (no-op/schemaless)', () => {
935
- const TEST_TABLE = 'test_alter_table'; // Use "table" or "collection" as appropriate
936
- beforeEach(async () => {
937
- await store.clearTable({ tableName: TEST_TABLE as TABLE_NAMES });
938
- });
939
-
940
- afterEach(async () => {
941
- await store.clearTable({ tableName: TEST_TABLE as TABLE_NAMES });
942
- });
943
-
944
- it('allows inserting records with new fields without alterTable', async () => {
945
- await store.insert({
946
- tableName: TEST_TABLE as TABLE_NAMES,
947
- record: { id: '1', name: 'Alice' },
948
- });
949
- await store.insert({
950
- tableName: TEST_TABLE as TABLE_NAMES,
951
- record: { id: '2', name: 'Bob', newField: 123 },
952
- });
953
-
954
- const row = await store.load<{ id: string; name: string; newField?: number }[]>({
955
- tableName: TEST_TABLE as TABLE_NAMES,
956
- keys: { id: '2' },
957
- });
958
- expect(row?.[0]?.newField).toBe(123);
959
- });
960
-
961
- it('does not throw when calling alterTable (no-op)', async () => {
962
- await expect(
963
- store.alterTable({
964
- tableName: TEST_TABLE as TABLE_NAMES,
965
- schema: {
966
- id: { type: 'text', primaryKey: true, nullable: false },
967
- name: { type: 'text', nullable: true },
968
- extra: { type: 'integer', nullable: true },
969
- },
970
- ifNotExists: ['extra'],
971
- }),
972
- ).resolves.not.toThrow();
973
- });
974
-
975
- it('can add multiple new fields at write time', async () => {
976
- await store.insert({
977
- tableName: TEST_TABLE as TABLE_NAMES,
978
- record: { id: '3', name: 'Charlie', age: 30, city: 'Paris' },
979
- });
980
- const row = await store.load<{ id: string; name: string; age?: number; city?: string }[]>({
981
- tableName: TEST_TABLE as TABLE_NAMES,
982
- keys: { id: '3' },
983
- });
984
- expect(row?.[0]?.age).toBe(30);
985
- expect(row?.[0]?.city).toBe('Paris');
43
+ it('not throws if dbName is missing or empty', () => {
44
+ expect(() => new MongoDBStore({ ...validWithConnectionHandlerConfig, dbName: '' })).not.toThrow(
45
+ /dbName must be provided and cannot be empty/,
46
+ );
47
+ const { dbName, ...rest } = validWithConnectionHandlerConfig as any;
48
+ expect(() => new MongoDBStore(rest as any)).not.toThrow(/dbName must be provided and cannot be empty/);
986
49
  });
987
50
 
988
- it('can retrieve all fields, including dynamically added ones', async () => {
989
- await store.insert({
990
- tableName: TEST_TABLE as TABLE_NAMES,
991
- record: { id: '4', name: 'Dana', hobby: 'skiing' },
992
- });
993
- const row = await store.load<{ id: string; name: string; hobby?: string }[]>({
994
- tableName: TEST_TABLE as TABLE_NAMES,
995
- keys: { id: '4' },
996
- });
997
- expect(row?.[0]?.hobby).toBe('skiing');
51
+ it('does not throw on valid config', () => {
52
+ expect(() => new MongoDBStore(validWithConnectionHandlerConfig)).not.toThrow();
998
53
  });
999
54
 
1000
- it('does not restrict or error on arbitrary new fields', async () => {
1001
- await expect(
1002
- store.insert({
1003
- tableName: TEST_TABLE as TABLE_NAMES,
1004
- record: { id: '5', weirdField: { nested: true }, another: [1, 2, 3] },
1005
- }),
1006
- ).resolves.not.toThrow();
1007
-
1008
- const row = await store.load<{ id: string; weirdField?: any; another?: any }[]>({
1009
- tableName: TEST_TABLE as TABLE_NAMES,
1010
- keys: { id: '5' },
1011
- });
1012
- expect(row?.[0]?.weirdField).toEqual({ nested: true });
1013
- expect(row?.[0]?.another).toEqual([1, 2, 3]);
55
+ it('should initialize the stores correctly', () => {
56
+ const store = new MongoDBStore(validWithConnectionHandlerConfig);
57
+ expect(Object.keys(store.stores)).not.toHaveLength(0);
1014
58
  });
1015
59
  });
1016
-
1017
- afterAll(async () => {
1018
- try {
1019
- await store.close();
1020
- } catch (error) {
1021
- console.warn('Error closing store:', error);
1022
- }
1023
- });
1024
60
  });
61
+
62
+ createTestSuite(new MongoDBStore(TEST_CONFIG));