@mastra/pg 0.0.0-commonjs-20250227130920

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.
@@ -0,0 +1,1302 @@
1
+ import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
2
+
3
+ import { PgVector } from '.';
4
+
5
+ describe('PgVector', () => {
6
+ let vectorDB: PgVector;
7
+ const testIndexName = 'test_vectors';
8
+ const testIndexName2 = 'test_vectors1';
9
+ const connectionString = process.env.DB_URL || 'postgresql://postgres:postgres@localhost:5434/mastra';
10
+
11
+ beforeAll(async () => {
12
+ // Initialize PgVector
13
+ vectorDB = new PgVector(connectionString);
14
+ });
15
+
16
+ afterAll(async () => {
17
+ // Clean up test tables
18
+ await vectorDB.deleteIndex(testIndexName);
19
+ await vectorDB.disconnect();
20
+ });
21
+
22
+ // Index Management Tests
23
+ describe('Index Management', () => {
24
+ describe('createIndex', () => {
25
+ afterAll(async () => {
26
+ await vectorDB.deleteIndex(testIndexName2);
27
+ });
28
+
29
+ it('should create a new vector table with specified dimensions', async () => {
30
+ await vectorDB.createIndex(testIndexName, 3);
31
+ const stats = await vectorDB.describeIndex(testIndexName);
32
+ expect(stats?.dimension).toBe(3);
33
+ expect(stats?.count).toBe(0);
34
+ });
35
+
36
+ it('should create index with specified metric', async () => {
37
+ await vectorDB.createIndex(testIndexName2, 3, 'euclidean');
38
+ const stats = await vectorDB.describeIndex(testIndexName2);
39
+ expect(stats.metric).toBe('euclidean');
40
+ });
41
+
42
+ it('should throw error if dimension is invalid', async () => {
43
+ await expect(vectorDB.createIndex(`testIndexNameFail`, 0)).rejects.toThrow();
44
+ });
45
+
46
+ it('should create index with flat type', async () => {
47
+ await vectorDB.createIndex(testIndexName2, 3, 'cosine', { type: 'flat' });
48
+ const stats = await vectorDB.describeIndex(testIndexName2);
49
+ expect(stats.type).toBe('flat');
50
+ });
51
+
52
+ it('should create index with hnsw type', async () => {
53
+ await vectorDB.createIndex(testIndexName2, 3, 'cosine', {
54
+ type: 'hnsw',
55
+ hnsw: { m: 16, efConstruction: 64 }, // Any reasonable values work
56
+ });
57
+ const stats = await vectorDB.describeIndex(testIndexName2);
58
+ expect(stats.type).toBe('hnsw');
59
+ expect(stats.config.m).toBe(16);
60
+ });
61
+
62
+ it('should create index with ivfflat type and lists', async () => {
63
+ await vectorDB.createIndex(testIndexName2, 3, 'cosine', {
64
+ type: 'ivfflat',
65
+ ivf: { lists: 100 },
66
+ });
67
+ const stats = await vectorDB.describeIndex(testIndexName2);
68
+ expect(stats.type).toBe('ivfflat');
69
+ expect(stats.config.lists).toBe(100);
70
+ });
71
+ });
72
+
73
+ describe('listIndexes', () => {
74
+ const indexName = 'test_query_3';
75
+ beforeAll(async () => {
76
+ await vectorDB.createIndex(indexName, 3);
77
+ });
78
+
79
+ afterAll(async () => {
80
+ await vectorDB.deleteIndex(indexName);
81
+ });
82
+
83
+ it('should list all vector tables', async () => {
84
+ const indexes = await vectorDB.listIndexes();
85
+ expect(indexes).toContain(indexName);
86
+ });
87
+
88
+ it('should not return created index in list if it is deleted', async () => {
89
+ await vectorDB.deleteIndex(indexName);
90
+ const indexes = await vectorDB.listIndexes();
91
+ expect(indexes).not.toContain(indexName);
92
+ });
93
+ });
94
+
95
+ describe('describeIndex', () => {
96
+ const indexName = 'test_query_4';
97
+ beforeAll(async () => {
98
+ await vectorDB.createIndex(indexName, 3);
99
+ });
100
+
101
+ afterAll(async () => {
102
+ await vectorDB.deleteIndex(indexName);
103
+ });
104
+
105
+ it('should return correct index stats', async () => {
106
+ await vectorDB.createIndex(indexName, 3, 'cosine');
107
+ const vectors = [
108
+ [1, 2, 3],
109
+ [4, 5, 6],
110
+ ];
111
+ await vectorDB.upsert(indexName, vectors);
112
+
113
+ const stats = await vectorDB.describeIndex(indexName);
114
+ expect(stats).toEqual({
115
+ type: 'ivfflat',
116
+ config: {
117
+ lists: 100,
118
+ },
119
+ dimension: 3,
120
+ count: 2,
121
+ metric: 'cosine',
122
+ });
123
+ });
124
+
125
+ it('should throw error for non-existent index', async () => {
126
+ await expect(vectorDB.describeIndex('non_existent')).rejects.toThrow();
127
+ });
128
+ });
129
+ });
130
+
131
+ // Vector Operations Tests
132
+ describe('Vector Operations', () => {
133
+ describe('upsert', () => {
134
+ beforeEach(async () => {
135
+ await vectorDB.createIndex(testIndexName, 3);
136
+ });
137
+
138
+ afterEach(async () => {
139
+ await vectorDB.deleteIndex(testIndexName);
140
+ });
141
+
142
+ it('should insert new vectors', async () => {
143
+ const vectors = [
144
+ [1, 2, 3],
145
+ [4, 5, 6],
146
+ ];
147
+ const ids = await vectorDB.upsert(testIndexName, vectors);
148
+
149
+ expect(ids).toHaveLength(2);
150
+ const stats = await vectorDB.describeIndex(testIndexName);
151
+ expect(stats.count).toBe(2);
152
+ });
153
+
154
+ it('should update existing vectors', async () => {
155
+ const vectors = [[1, 2, 3]];
156
+ const metadata = [{ test: 'initial' }];
157
+ const [id] = await vectorDB.upsert(testIndexName, vectors, metadata);
158
+
159
+ const updatedVectors = [[4, 5, 6]];
160
+ const updatedMetadata = [{ test: 'updated' }];
161
+ await vectorDB.upsert(testIndexName, updatedVectors, updatedMetadata, [id!]);
162
+
163
+ const results = await vectorDB.query(testIndexName, [4, 5, 6], 1);
164
+ expect(results[0]?.id).toBe(id);
165
+ expect(results[0]?.metadata).toEqual({ test: 'updated' });
166
+ });
167
+
168
+ it('should handle metadata correctly', async () => {
169
+ const vectors = [[1, 2, 3]];
170
+ const metadata = [{ test: 'value', num: 123 }];
171
+
172
+ await vectorDB.upsert(testIndexName, vectors, metadata);
173
+ const results = await vectorDB.query(testIndexName, [1, 2, 3], 1);
174
+
175
+ expect(results[0]?.metadata).toEqual(metadata[0]);
176
+ });
177
+
178
+ it('should throw error if vector dimensions dont match', async () => {
179
+ const vectors = [[1, 2, 3, 4]]; // 4D vector for 3D index
180
+ await expect(vectorDB.upsert(testIndexName, vectors)).rejects.toThrow();
181
+ });
182
+ });
183
+
184
+ describe('Basic Query Operations', () => {
185
+ ['flat', 'hnsw', 'ivfflat'].forEach(indexType => {
186
+ const indexName = `test_query_2_${indexType}`;
187
+ beforeAll(async () => {
188
+ try {
189
+ await vectorDB.deleteIndex(indexName);
190
+ } catch {
191
+ // Ignore if doesn't exist
192
+ }
193
+ await vectorDB.createIndex(indexName, 3);
194
+ });
195
+
196
+ beforeEach(async () => {
197
+ await vectorDB.truncateIndex(indexName);
198
+ const vectors = [
199
+ [1, 0, 0],
200
+ [0.8, 0.2, 0],
201
+ [0, 1, 0],
202
+ ];
203
+ const metadata = [
204
+ { type: 'a', value: 1 },
205
+ { type: 'b', value: 2 },
206
+ { type: 'a', value: 3 },
207
+ ];
208
+ await vectorDB.upsert(indexName, vectors, metadata);
209
+ });
210
+
211
+ afterAll(async () => {
212
+ await vectorDB.deleteIndex(indexName);
213
+ });
214
+
215
+ it('should return closest vectors', async () => {
216
+ const results = await vectorDB.query(indexName, [1, 0, 0], 1);
217
+ expect(results).toHaveLength(1);
218
+ expect(results[0]?.vector).toBe(undefined);
219
+ expect(results[0]?.score).toBeCloseTo(1, 5);
220
+ });
221
+
222
+ it('should return vector with result', async () => {
223
+ const results = await vectorDB.query(indexName, [1, 0, 0], 1, undefined, true);
224
+ expect(results).toHaveLength(1);
225
+ expect(results[0]?.vector).toStrictEqual([1, 0, 0]);
226
+ });
227
+
228
+ it('should respect topK parameter', async () => {
229
+ const results = await vectorDB.query(indexName, [1, 0, 0], 2);
230
+ expect(results).toHaveLength(2);
231
+ });
232
+
233
+ it('should handle filters correctly', async () => {
234
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, { type: 'a' });
235
+
236
+ expect(results).toHaveLength(1);
237
+ results.forEach(result => {
238
+ expect(result?.metadata?.type).toBe('a');
239
+ });
240
+ });
241
+ });
242
+ });
243
+ });
244
+
245
+ // Advanced Query and Filter Tests
246
+ describe('Advanced Query and Filter Operations', () => {
247
+ const indexName = 'test_query_filters';
248
+ beforeAll(async () => {
249
+ try {
250
+ await vectorDB.deleteIndex(indexName);
251
+ } catch {
252
+ // Ignore if doesn't exist
253
+ }
254
+ await vectorDB.createIndex(indexName, 3);
255
+ });
256
+
257
+ beforeEach(async () => {
258
+ await vectorDB.truncateIndex(indexName);
259
+ const vectors = [
260
+ [1, 0.1, 0],
261
+ [0.9, 0.2, 0],
262
+ [0.95, 0.1, 0],
263
+ [0.85, 0.2, 0],
264
+ [0.9, 0.1, 0],
265
+ ];
266
+
267
+ const metadata = [
268
+ {
269
+ category: 'electronics',
270
+ price: 100,
271
+ tags: ['new', 'premium'],
272
+ active: true,
273
+ ratings: [4.5, 4.8, 4.2], // Array of numbers
274
+ stock: [
275
+ { location: 'A', count: 25 },
276
+ { location: 'B', count: 15 },
277
+ ], // Array of objects
278
+ reviews: [
279
+ { user: 'alice', score: 5, verified: true },
280
+ { user: 'bob', score: 4, verified: true },
281
+ { user: 'charlie', score: 3, verified: false },
282
+ ], // Complex array objects
283
+ },
284
+ {
285
+ category: 'books',
286
+ price: 50,
287
+ tags: ['used'],
288
+ active: true,
289
+ ratings: [3.8, 4.0, 4.1],
290
+ stock: [
291
+ { location: 'A', count: 10 },
292
+ { location: 'C', count: 30 },
293
+ ],
294
+ reviews: [
295
+ { user: 'dave', score: 4, verified: true },
296
+ { user: 'eve', score: 5, verified: false },
297
+ ],
298
+ },
299
+ { category: 'electronics', price: 75, tags: ['refurbished'], active: false },
300
+ { category: 'books', price: 25, tags: ['used', 'sale'], active: true },
301
+ { category: 'clothing', price: 60, tags: ['new'], active: true },
302
+ ];
303
+
304
+ await vectorDB.upsert(indexName, vectors, metadata);
305
+ });
306
+
307
+ afterAll(async () => {
308
+ await vectorDB.deleteIndex(indexName);
309
+ });
310
+
311
+ // Numeric Comparison Tests
312
+ describe('Comparison Operators', () => {
313
+ it('should handle numeric string comparisons', async () => {
314
+ // Insert a record with numeric string
315
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ numericString: '123' }]);
316
+
317
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
318
+ numericString: { $gt: '100' }, // Compare strings numerically
319
+ });
320
+ expect(results.length).toBeGreaterThan(0);
321
+ expect(results[0]?.metadata?.numericString).toBe('123');
322
+ });
323
+
324
+ it('should filter with $gt operator', async () => {
325
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
326
+ price: { $gt: 75 },
327
+ });
328
+ expect(results).toHaveLength(1);
329
+ expect(results[0]?.metadata?.price).toBe(100);
330
+ });
331
+
332
+ it('should filter with $lte operator', async () => {
333
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
334
+ price: { $lte: 50 },
335
+ });
336
+ expect(results).toHaveLength(2);
337
+ results.forEach(result => {
338
+ expect(result.metadata?.price).toBeLessThanOrEqual(50);
339
+ });
340
+ });
341
+
342
+ it('should filter with lt operator', async () => {
343
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
344
+ price: { $lt: 60 },
345
+ });
346
+ expect(results).toHaveLength(2);
347
+ results.forEach(result => {
348
+ expect(result.metadata?.price).toBeLessThan(60);
349
+ });
350
+ });
351
+
352
+ it('should filter with gte operator', async () => {
353
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
354
+ price: { $gte: 75 },
355
+ });
356
+ expect(results).toHaveLength(2);
357
+ results.forEach(result => {
358
+ expect(result.metadata?.price).toBeGreaterThanOrEqual(75);
359
+ });
360
+ });
361
+
362
+ it('should filter with ne operator', async () => {
363
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
364
+ category: { $ne: 'electronics' },
365
+ });
366
+ expect(results.length).toBeGreaterThan(0);
367
+ results.forEach(result => {
368
+ expect(result.metadata?.category).not.toBe('electronics');
369
+ });
370
+ });
371
+
372
+ it('should filter with $gt and $lte operator', async () => {
373
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
374
+ price: { $gt: 70, $lte: 100 },
375
+ });
376
+ expect(results).toHaveLength(2);
377
+ results.forEach(result => {
378
+ expect(result.metadata?.price).toBeGreaterThan(70);
379
+ expect(result.metadata?.price).toBeLessThanOrEqual(100);
380
+ });
381
+ });
382
+ });
383
+
384
+ // Array Operator Tests
385
+ describe('Array Operators', () => {
386
+ it('should filter with $in operator', async () => {
387
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
388
+ category: { $in: ['electronics', 'clothing'] },
389
+ });
390
+ expect(results).toHaveLength(3);
391
+ results.forEach(result => {
392
+ expect(['electronics', 'clothing']).toContain(result.metadata?.category);
393
+ });
394
+ });
395
+
396
+ it('should filter with $nin operator', async () => {
397
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
398
+ category: { $nin: ['electronics', 'books'] },
399
+ });
400
+ expect(results.length).toBeGreaterThan(0);
401
+ results.forEach(result => {
402
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
403
+ });
404
+ });
405
+
406
+ it('should handle empty arrays in in/nin operators', async () => {
407
+ // Should return no results for empty IN
408
+ const resultsIn = await vectorDB.query(indexName, [1, 0, 0], 10, {
409
+ category: { $in: [] },
410
+ });
411
+ expect(resultsIn).toHaveLength(0);
412
+
413
+ // Should return all results for empty NIN
414
+ const resultsNin = await vectorDB.query(indexName, [1, 0, 0], 10, {
415
+ category: { $nin: [] },
416
+ });
417
+ expect(resultsNin.length).toBeGreaterThan(0);
418
+ });
419
+
420
+ it('should filter with array $contains operator', async () => {
421
+ const results = await vectorDB.query(indexName, [1, 0.1, 0], 10, {
422
+ tags: { $contains: ['new'] },
423
+ });
424
+ expect(results.length).toBeGreaterThan(0);
425
+ results.forEach(result => {
426
+ expect(result.metadata?.tags).toContain('new');
427
+ });
428
+ });
429
+
430
+ it('should filter with $elemMatch operator', async () => {
431
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
432
+ tags: {
433
+ $elemMatch: {
434
+ $in: ['new', 'premium'],
435
+ },
436
+ },
437
+ });
438
+ expect(results.length).toBeGreaterThan(0);
439
+ results.forEach(result => {
440
+ expect(result.metadata?.tags.some(tag => ['new', 'premium'].includes(tag))).toBe(true);
441
+ });
442
+ });
443
+
444
+ it('should filter with $elemMatch using equality', async () => {
445
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
446
+ tags: {
447
+ $elemMatch: {
448
+ $eq: 'sale',
449
+ },
450
+ },
451
+ });
452
+ expect(results).toHaveLength(1);
453
+ expect(results[0]?.metadata?.tags).toContain('sale');
454
+ });
455
+
456
+ it('should filter with $elemMatch using multiple conditions', async () => {
457
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
458
+ ratings: {
459
+ $elemMatch: {
460
+ $gt: 4,
461
+ $lt: 4.5,
462
+ },
463
+ },
464
+ });
465
+ expect(results.length).toBeGreaterThan(0);
466
+ results.forEach(result => {
467
+ expect(Array.isArray(result.metadata?.ratings)).toBe(true);
468
+ expect(result.metadata?.ratings.some(rating => rating > 4 && rating < 4.5)).toBe(true);
469
+ });
470
+ });
471
+
472
+ it('should handle complex $elemMatch conditions', async () => {
473
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
474
+ stock: {
475
+ $elemMatch: {
476
+ location: 'A',
477
+ count: { $gt: 20 },
478
+ },
479
+ },
480
+ });
481
+ expect(results.length).toBeGreaterThan(0);
482
+ results.forEach(result => {
483
+ const matchingStock = result.metadata?.stock.find(s => s.location === 'A' && s.count > 20);
484
+ expect(matchingStock).toBeDefined();
485
+ });
486
+ });
487
+
488
+ it('should filter with $elemMatch on nested numeric fields', async () => {
489
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
490
+ reviews: {
491
+ $elemMatch: {
492
+ score: { $gt: 4 },
493
+ },
494
+ },
495
+ });
496
+ expect(results.length).toBeGreaterThan(0);
497
+ results.forEach(result => {
498
+ expect(result.metadata?.reviews.some(r => r.score > 4)).toBe(true);
499
+ });
500
+ });
501
+
502
+ it('should filter with $elemMatch on multiple nested fields', async () => {
503
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
504
+ reviews: {
505
+ $elemMatch: {
506
+ score: { $gte: 4 },
507
+ verified: true,
508
+ },
509
+ },
510
+ });
511
+ expect(results.length).toBeGreaterThan(0);
512
+ results.forEach(result => {
513
+ expect(result.metadata?.reviews.some(r => r.score >= 4 && r.verified)).toBe(true);
514
+ });
515
+ });
516
+
517
+ it('should filter with $elemMatch on exact string match', async () => {
518
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
519
+ reviews: {
520
+ $elemMatch: {
521
+ user: 'alice',
522
+ },
523
+ },
524
+ });
525
+ expect(results).toHaveLength(1);
526
+ expect(results[0].metadata?.reviews.some(r => r.user === 'alice')).toBe(true);
527
+ });
528
+
529
+ it('should handle $elemMatch with no matches', async () => {
530
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
531
+ reviews: {
532
+ $elemMatch: {
533
+ score: 10, // No review has score 10
534
+ },
535
+ },
536
+ });
537
+ expect(results).toHaveLength(0);
538
+ });
539
+
540
+ it('should filter with $all operator', async () => {
541
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
542
+ tags: { $all: ['used', 'sale'] },
543
+ });
544
+ expect(results).toHaveLength(1);
545
+ results.forEach(result => {
546
+ expect(result.metadata?.tags).toContain('used');
547
+ expect(result.metadata?.tags).toContain('sale');
548
+ });
549
+ });
550
+
551
+ it('should filter with $all using single value', async () => {
552
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
553
+ tags: { $all: ['new'] },
554
+ });
555
+ expect(results.length).toBeGreaterThan(0);
556
+ results.forEach(result => {
557
+ expect(result.metadata?.tags).toContain('new');
558
+ });
559
+ });
560
+
561
+ it('should handle empty array for $all', async () => {
562
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
563
+ tags: { $all: [] },
564
+ });
565
+ expect(results).toHaveLength(0);
566
+ });
567
+
568
+ it('should handle non-array field $all', async () => {
569
+ // First insert a record with non-array field
570
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ tags: 'not-an-array' }]);
571
+
572
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
573
+ tags: { $all: ['value'] },
574
+ });
575
+ expect(results).toHaveLength(0);
576
+ });
577
+
578
+ // Contains Operator Tests
579
+ it('should filter with contains operator for exact field match', async () => {
580
+ const results = await vectorDB.query(indexName, [1, 0.1, 0], 10, {
581
+ category: { $contains: 'electronics' },
582
+ });
583
+ expect(results.length).toBeGreaterThan(0);
584
+ results.forEach(result => {
585
+ expect(result.metadata?.category).toBe('electronics');
586
+ });
587
+ });
588
+
589
+ it('should filter with $contains operator for nested objects', async () => {
590
+ // First insert a record with nested object
591
+ await vectorDB.upsert(
592
+ indexName,
593
+ [[1, 0.1, 0]],
594
+ [
595
+ {
596
+ details: { color: 'red', size: 'large' },
597
+ category: 'clothing',
598
+ },
599
+ ],
600
+ );
601
+
602
+ const results = await vectorDB.query(indexName, [1, 0.1, 0], 10, {
603
+ details: { $contains: { color: 'red' } },
604
+ });
605
+ expect(results.length).toBeGreaterThan(0);
606
+ results.forEach(result => {
607
+ expect(result.metadata?.details.color).toBe('red');
608
+ });
609
+ });
610
+
611
+ // String Pattern Tests
612
+ it('should handle exact string matches', async () => {
613
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
614
+ category: 'electronics',
615
+ });
616
+ expect(results).toHaveLength(2);
617
+ });
618
+
619
+ it('should handle case-sensitive string matches', async () => {
620
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
621
+ category: 'ELECTRONICS',
622
+ });
623
+ expect(results).toHaveLength(0);
624
+ });
625
+ it('should filter arrays by size', async () => {
626
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
627
+ ratings: { $size: 3 },
628
+ });
629
+ expect(results.length).toBeGreaterThan(0);
630
+ results.forEach(result => {
631
+ expect(result.metadata?.ratings).toHaveLength(3);
632
+ });
633
+
634
+ const noResults = await vectorDB.query(indexName, [1, 0, 0], 10, {
635
+ ratings: { $size: 10 },
636
+ });
637
+ expect(noResults).toHaveLength(0);
638
+ });
639
+
640
+ it('should handle $size with nested arrays', async () => {
641
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ nested: { array: [1, 2, 3, 4] } }]);
642
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
643
+ 'nested.array': { $size: 4 },
644
+ });
645
+ expect(results.length).toBeGreaterThan(0);
646
+ results.forEach(result => {
647
+ expect(result.metadata?.nested.array).toHaveLength(4);
648
+ });
649
+ });
650
+ });
651
+
652
+ // Logical Operator Tests
653
+ describe('Logical Operators', () => {
654
+ it('should handle AND filter conditions', async () => {
655
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
656
+ $and: [{ category: { $eq: 'electronics' } }, { price: { $gt: 75 } }],
657
+ });
658
+ expect(results).toHaveLength(1);
659
+ expect(results[0]?.metadata?.category).toBe('electronics');
660
+ expect(results[0]?.metadata?.price).toBeGreaterThan(75);
661
+ });
662
+
663
+ it('should handle OR filter conditions', async () => {
664
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
665
+ $or: [{ category: { $eq: 'electronics' } }, { category: { $eq: 'books' } }],
666
+ });
667
+ expect(results.length).toBeGreaterThan(1);
668
+ results.forEach(result => {
669
+ expect(['electronics', 'books']).toContain(result?.metadata?.category);
670
+ });
671
+ });
672
+
673
+ it('should handle $not operator', async () => {
674
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
675
+ $not: { category: 'electronics' },
676
+ });
677
+ expect(results.length).toBeGreaterThan(0);
678
+ results.forEach(result => {
679
+ expect(result.metadata?.category).not.toBe('electronics');
680
+ });
681
+ });
682
+
683
+ it('should handle $nor operator', async () => {
684
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
685
+ $nor: [{ category: 'electronics' }, { category: 'books' }],
686
+ });
687
+ expect(results.length).toBeGreaterThan(0);
688
+ results.forEach(result => {
689
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
690
+ });
691
+ });
692
+
693
+ it('should handle nested $not with $or', async () => {
694
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
695
+ $not: { $or: [{ category: 'electronics' }, { category: 'books' }] },
696
+ });
697
+ expect(results.length).toBeGreaterThan(0);
698
+ results.forEach(result => {
699
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
700
+ });
701
+ });
702
+
703
+ it('should handle $not with comparison operators', async () => {
704
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
705
+ price: { $not: { $gt: 100 } },
706
+ });
707
+ expect(results.length).toBeGreaterThan(0);
708
+ results.forEach(result => {
709
+ expect(Number(result.metadata?.price)).toBeLessThanOrEqual(100);
710
+ });
711
+ });
712
+
713
+ it('should handle $not with $in operator', async () => {
714
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
715
+ category: { $not: { $in: ['electronics', 'books'] } },
716
+ });
717
+ expect(results.length).toBeGreaterThan(0);
718
+ results.forEach(result => {
719
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
720
+ });
721
+ });
722
+
723
+ it('should handle $not with multiple nested conditions', async () => {
724
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
725
+ $not: { $and: [{ category: 'electronics' }, { price: { $gt: 50 } }] },
726
+ });
727
+ expect(results.length).toBeGreaterThan(0);
728
+ results.forEach(result => {
729
+ expect(result.metadata?.category !== 'electronics' || result.metadata?.price <= 50).toBe(true);
730
+ });
731
+ });
732
+
733
+ it('should handle $not with $exists operator', async () => {
734
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
735
+ tags: { $not: { $exists: true } },
736
+ });
737
+ expect(results.length).toBe(0); // All test data has tags
738
+ });
739
+
740
+ it('should handle $not with array operators', async () => {
741
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
742
+ tags: { $not: { $all: ['new', 'premium'] } },
743
+ });
744
+ expect(results.length).toBeGreaterThan(0);
745
+ results.forEach(result => {
746
+ expect(!result.metadata?.tags.includes('new') || !result.metadata?.tags.includes('premium')).toBe(true);
747
+ });
748
+ });
749
+
750
+ it('should handle $not with complex nested conditions', async () => {
751
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
752
+ $not: {
753
+ $or: [
754
+ {
755
+ $and: [{ category: 'electronics' }, { price: { $gt: 90 } }],
756
+ },
757
+ {
758
+ $and: [{ category: 'books' }, { price: { $lt: 30 } }],
759
+ },
760
+ ],
761
+ },
762
+ });
763
+ expect(results.length).toBeGreaterThan(0);
764
+ results.forEach(result => {
765
+ const notExpensiveElectronics = !(result.metadata?.category === 'electronics' && result.metadata?.price > 90);
766
+ const notCheapBooks = !(result.metadata?.category === 'books' && result.metadata?.price < 30);
767
+ expect(notExpensiveElectronics && notCheapBooks).toBe(true);
768
+ });
769
+ });
770
+
771
+ it('should handle $not with empty arrays', async () => {
772
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
773
+ tags: { $not: { $in: [] } },
774
+ });
775
+ expect(results.length).toBeGreaterThan(0); // Should match all records
776
+ });
777
+
778
+ it('should handle $not with null values', async () => {
779
+ // First insert a record with null value
780
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ category: null, price: 0 }]);
781
+
782
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
783
+ category: { $not: { $eq: null } },
784
+ });
785
+ expect(results.length).toBeGreaterThan(0);
786
+ results.forEach(result => {
787
+ expect(result.metadata?.category).not.toBeNull();
788
+ });
789
+ });
790
+
791
+ it('should handle $not with boolean values', async () => {
792
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
793
+ active: { $not: { $eq: true } },
794
+ });
795
+ expect(results.length).toBeGreaterThan(0);
796
+ results.forEach(result => {
797
+ expect(result.metadata?.active).not.toBe(true);
798
+ });
799
+ });
800
+
801
+ it('should handle $not with multiple conditions', async () => {
802
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
803
+ $not: { category: 'electronics', price: { $gt: 50 } },
804
+ });
805
+ expect(results.length).toBeGreaterThan(0);
806
+ });
807
+
808
+ it('should handle $not with $not operator', async () => {
809
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
810
+ $not: { $not: { category: 'electronics' } },
811
+ });
812
+ expect(results.length).toBeGreaterThan(0);
813
+ });
814
+
815
+ it('should handle $not in nested fields', async () => {
816
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ user: { profile: { price: 10 } } }]);
817
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
818
+ 'user.profile.price': { $not: { $gt: 25 } },
819
+ });
820
+ expect(results.length).toBe(1);
821
+ });
822
+
823
+ it('should handle $not with multiple operators', async () => {
824
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
825
+ price: { $not: { $gte: 30, $lte: 70 } },
826
+ });
827
+ expect(results.length).toBeGreaterThan(0);
828
+ results.forEach(result => {
829
+ const price = Number(result.metadata?.price);
830
+ expect(price < 30 || price > 70).toBe(true);
831
+ });
832
+ });
833
+
834
+ it('should handle $not with comparison operators', async () => {
835
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
836
+ price: { $not: { $gt: 100 } },
837
+ });
838
+ expect(results.length).toBeGreaterThan(0);
839
+ results.forEach(result => {
840
+ expect(Number(result.metadata?.price)).toBeLessThanOrEqual(100);
841
+ });
842
+ });
843
+
844
+ it('should handle $not with $and', async () => {
845
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
846
+ $not: {
847
+ $and: [{ category: 'electronics' }, { price: { $gt: 50 } }],
848
+ },
849
+ });
850
+ expect(results.length).toBeGreaterThan(0);
851
+ results.forEach(result => {
852
+ expect(result.metadata?.category !== 'electronics' || result.metadata?.price <= 50).toBe(true);
853
+ });
854
+ });
855
+
856
+ it('should handle $nor with $or', async () => {
857
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
858
+ $nor: [{ $or: [{ category: 'electronics' }, { category: 'books' }] }, { price: { $gt: 75 } }],
859
+ });
860
+ expect(results.length).toBeGreaterThan(0);
861
+ results.forEach(result => {
862
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
863
+ expect(result.metadata?.price).toBeLessThanOrEqual(75);
864
+ });
865
+ });
866
+
867
+ it('should handle $nor with nested $and conditions', async () => {
868
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
869
+ $nor: [
870
+ { $and: [{ category: 'electronics' }, { active: true }] },
871
+ { $and: [{ category: 'books' }, { price: { $lt: 30 } }] },
872
+ ],
873
+ });
874
+ expect(results.length).toBeGreaterThan(0);
875
+ results.forEach(result => {
876
+ const notElectronicsActive = !(
877
+ result.metadata?.category === 'electronics' && result.metadata?.active === true
878
+ );
879
+ const notBooksLowPrice = !(result.metadata?.category === 'books' && result.metadata?.price < 30);
880
+ expect(notElectronicsActive && notBooksLowPrice).toBe(true);
881
+ });
882
+ });
883
+
884
+ it('should handle nested $and with $or and $not', async () => {
885
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
886
+ $and: [{ $or: [{ category: 'electronics' }, { category: 'books' }] }, { $not: { price: { $lt: 50 } } }],
887
+ });
888
+ expect(results.length).toBeGreaterThan(0);
889
+ results.forEach(result => {
890
+ expect(['electronics', 'books']).toContain(result.metadata?.category);
891
+ expect(result.metadata?.price).toBeGreaterThanOrEqual(50);
892
+ });
893
+ });
894
+
895
+ it('should handle $or with multiple $not conditions', async () => {
896
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
897
+ $or: [{ $not: { category: 'electronics' } }, { $not: { price: { $gt: 50 } } }],
898
+ });
899
+ expect(results.length).toBeGreaterThan(0);
900
+ results.forEach(result => {
901
+ expect(result.metadata?.category !== 'electronics' || result.metadata?.price <= 50).toBe(true);
902
+ });
903
+ });
904
+ });
905
+
906
+ // Edge Cases and Special Values
907
+ describe('Edge Cases and Special Values', () => {
908
+ it('should handle empty result sets with valid filters', async () => {
909
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
910
+ price: { $gt: 1000 },
911
+ });
912
+ expect(results).toHaveLength(0);
913
+ });
914
+
915
+ it('should throw error for invalid operator', async () => {
916
+ await expect(
917
+ vectorDB.query(indexName, [1, 0, 0], 10, {
918
+ price: { $invalid: 100 },
919
+ }),
920
+ ).rejects.toThrow('Unsupported operator: $invalid');
921
+ });
922
+
923
+ it('should handle empty filter object', async () => {
924
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {});
925
+ expect(results.length).toBeGreaterThan(0);
926
+ });
927
+
928
+ it('should handle numeric string comparisons', async () => {
929
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ numericString: '123' }]);
930
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
931
+ numericString: { $gt: '100' },
932
+ });
933
+ expect(results.length).toBeGreaterThan(0);
934
+ expect(results[0]?.metadata?.numericString).toBe('123');
935
+ });
936
+ });
937
+
938
+ // Score Threshold Tests
939
+ describe('Score Threshold', () => {
940
+ it('should respect minimum score threshold', async () => {
941
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, { category: 'electronics' }, false, 0.9);
942
+ expect(results.length).toBeGreaterThan(0);
943
+ results.forEach(result => {
944
+ expect(result.score).toBeGreaterThan(0.9);
945
+ });
946
+ });
947
+ });
948
+
949
+ describe('Edge Cases and Special Values', () => {
950
+ // Additional Edge Cases
951
+ it('should handle empty result sets with valid filters', async () => {
952
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
953
+ price: { $gt: 1000 },
954
+ });
955
+ expect(results).toHaveLength(0);
956
+ });
957
+
958
+ it('should handle empty filter object', async () => {
959
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {});
960
+ expect(results.length).toBeGreaterThan(0);
961
+ });
962
+
963
+ it('should handle non-existent field', async () => {
964
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
965
+ nonexistent: {
966
+ $elemMatch: {
967
+ $eq: 'value',
968
+ },
969
+ },
970
+ });
971
+ expect(results).toHaveLength(0);
972
+ });
973
+
974
+ it('should handle non-existent values', async () => {
975
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
976
+ tags: {
977
+ $elemMatch: {
978
+ $eq: 'nonexistent-tag',
979
+ },
980
+ },
981
+ });
982
+ expect(results).toHaveLength(0);
983
+ });
984
+ // Empty Conditions Tests
985
+ it('should handle empty conditions in logical operators', async () => {
986
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
987
+ $and: [],
988
+ category: 'electronics',
989
+ });
990
+ expect(results.length).toBeGreaterThan(0);
991
+ results.forEach(result => {
992
+ expect(result.metadata?.category).toBe('electronics');
993
+ });
994
+ });
995
+
996
+ it('should handle empty $and conditions', async () => {
997
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
998
+ $and: [],
999
+ category: 'electronics',
1000
+ });
1001
+ expect(results.length).toBeGreaterThan(0);
1002
+ results.forEach(result => {
1003
+ expect(result.metadata?.category).toBe('electronics');
1004
+ });
1005
+ });
1006
+
1007
+ it('should handle empty $or conditions', async () => {
1008
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1009
+ $or: [],
1010
+ category: 'electronics',
1011
+ });
1012
+ expect(results).toHaveLength(0);
1013
+ });
1014
+
1015
+ it('should handle empty $nor conditions', async () => {
1016
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1017
+ $nor: [],
1018
+ category: 'electronics',
1019
+ });
1020
+ expect(results.length).toBeGreaterThan(0);
1021
+ results.forEach(result => {
1022
+ expect(result.metadata?.category).toBe('electronics');
1023
+ });
1024
+ });
1025
+
1026
+ it('should handle empty $not conditions', async () => {
1027
+ await expect(
1028
+ vectorDB.query(indexName, [1, 0, 0], 10, {
1029
+ $not: {},
1030
+ category: 'electronics',
1031
+ }),
1032
+ ).rejects.toThrow('$not operator cannot be empty');
1033
+ });
1034
+
1035
+ it('should handle multiple empty logical operators', async () => {
1036
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1037
+ $and: [],
1038
+ $or: [],
1039
+ $nor: [],
1040
+ category: 'electronics',
1041
+ });
1042
+ expect(results).toHaveLength(0);
1043
+ });
1044
+
1045
+ // Nested Field Tests
1046
+ it('should handle deeply nested metadata paths', async () => {
1047
+ await vectorDB.upsert(
1048
+ indexName,
1049
+ [[1, 0.1, 0]],
1050
+ [
1051
+ {
1052
+ level1: {
1053
+ level2: {
1054
+ level3: 'deep value',
1055
+ },
1056
+ },
1057
+ },
1058
+ ],
1059
+ );
1060
+
1061
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1062
+ 'level1.level2.level3': 'deep value',
1063
+ });
1064
+ expect(results).toHaveLength(1);
1065
+ expect(results[0]?.metadata?.level1?.level2?.level3).toBe('deep value');
1066
+ });
1067
+
1068
+ it('should handle non-existent nested paths', async () => {
1069
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1070
+ 'nonexistent.path': 'value',
1071
+ });
1072
+ expect(results).toHaveLength(0);
1073
+ });
1074
+
1075
+ // Score Threshold Tests
1076
+ it('should respect minimum score threshold', async () => {
1077
+ const results = await vectorDB.query(
1078
+ indexName,
1079
+ [1, 0, 0],
1080
+ 10,
1081
+ { category: 'electronics' },
1082
+ false,
1083
+ 0.9, // minScore
1084
+ );
1085
+ expect(results.length).toBeGreaterThan(0);
1086
+ results.forEach(result => {
1087
+ expect(result.score).toBeGreaterThan(0.9);
1088
+ });
1089
+ });
1090
+
1091
+ // Complex Nested Operators Test
1092
+ it('should handle deeply nested logical operators', async () => {
1093
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1094
+ $and: [
1095
+ {
1096
+ $or: [{ category: 'electronics' }, { $and: [{ category: 'books' }, { price: { $lt: 30 } }] }],
1097
+ },
1098
+ {
1099
+ $not: {
1100
+ $or: [{ active: false }, { price: { $gt: 100 } }],
1101
+ },
1102
+ },
1103
+ ],
1104
+ });
1105
+ expect(results.length).toBeGreaterThan(0);
1106
+ results.forEach(result => {
1107
+ // First condition: electronics OR (books AND price < 30)
1108
+ const firstCondition =
1109
+ result.metadata?.category === 'electronics' ||
1110
+ (result.metadata?.category === 'books' && result.metadata?.price < 30);
1111
+
1112
+ // Second condition: NOT (active = false OR price > 100)
1113
+ const secondCondition = result.metadata?.active !== false && result.metadata?.price <= 100;
1114
+
1115
+ expect(firstCondition && secondCondition).toBe(true);
1116
+ });
1117
+ });
1118
+
1119
+ it('should throw error for invalid operator', async () => {
1120
+ await expect(
1121
+ vectorDB.query(indexName, [1, 0, 0], 10, {
1122
+ price: { $invalid: 100 },
1123
+ }),
1124
+ ).rejects.toThrow('Unsupported operator: $invalid');
1125
+ });
1126
+
1127
+ it('should handle multiple logical operators at root level', async () => {
1128
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1129
+ $and: [{ category: 'electronics' }],
1130
+ $or: [{ price: { $lt: 100 } }, { price: { $gt: 20 } }],
1131
+ });
1132
+ expect(results.length).toBeGreaterThan(0);
1133
+ results.forEach(result => {
1134
+ expect(result.metadata?.category).toBe('electronics');
1135
+ expect(result.metadata?.price < 100 || result.metadata?.price > 20).toBe(true);
1136
+ });
1137
+ });
1138
+
1139
+ it('should handle non-array field with $elemMatch', async () => {
1140
+ // First insert a record with non-array field
1141
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ tags: 'not-an-array' }]);
1142
+
1143
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1144
+ tags: {
1145
+ $elemMatch: {
1146
+ $eq: 'value',
1147
+ },
1148
+ },
1149
+ });
1150
+ expect(results).toHaveLength(0); // Should return no results for non-array field
1151
+ });
1152
+ it('should handle undefined filter', async () => {
1153
+ const results1 = await vectorDB.query(indexName, [1, 0, 0], 10, undefined);
1154
+ const results2 = await vectorDB.query(indexName, [1, 0, 0], 10);
1155
+ expect(results1).toEqual(results2);
1156
+ expect(results1.length).toBeGreaterThan(0);
1157
+ });
1158
+
1159
+ it('should handle empty object filter', async () => {
1160
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {});
1161
+ const results2 = await vectorDB.query(indexName, [1, 0, 0], 10);
1162
+ expect(results).toEqual(results2);
1163
+ expect(results.length).toBeGreaterThan(0);
1164
+ });
1165
+
1166
+ it('should handle null filter', async () => {
1167
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, null as any);
1168
+ const results2 = await vectorDB.query(indexName, [1, 0, 0], 10);
1169
+ expect(results).toEqual(results2);
1170
+ expect(results.length).toBeGreaterThan(0);
1171
+ });
1172
+ });
1173
+
1174
+ // Regex Operator Tests
1175
+ describe('Regex Operators', () => {
1176
+ it('should handle $regex with case sensitivity', async () => {
1177
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1178
+ category: { $regex: 'ELECTRONICS' },
1179
+ });
1180
+ expect(results).toHaveLength(0);
1181
+ });
1182
+
1183
+ it('should handle $regex with case insensitivity', async () => {
1184
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1185
+ category: { $regex: 'ELECTRONICS', $options: 'i' },
1186
+ });
1187
+ expect(results).toHaveLength(2);
1188
+ });
1189
+
1190
+ it('should handle $regex with start anchor', async () => {
1191
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1192
+ category: { $regex: '^elect' },
1193
+ });
1194
+ expect(results).toHaveLength(2);
1195
+ });
1196
+
1197
+ it('should handle $regex with end anchor', async () => {
1198
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1199
+ category: { $regex: 'nics$' },
1200
+ });
1201
+ expect(results).toHaveLength(2);
1202
+ });
1203
+
1204
+ it('should handle multiline flag', async () => {
1205
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ description: 'First line\nSecond line\nThird line' }]);
1206
+
1207
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1208
+ description: { $regex: '^Second', $options: 'm' },
1209
+ });
1210
+ expect(results).toHaveLength(1);
1211
+ });
1212
+
1213
+ it('should handle dotall flag', async () => {
1214
+ await vectorDB.upsert(indexName, [[1, 0.1, 0]], [{ description: 'First\nSecond\nThird' }]);
1215
+
1216
+ const withoutS = await vectorDB.query(indexName, [1, 0, 0], 10, {
1217
+ description: { $regex: 'First[^\\n]*Third' },
1218
+ });
1219
+ expect(withoutS).toHaveLength(0);
1220
+
1221
+ const withS = await vectorDB.query(indexName, [1, 0, 0], 10, {
1222
+ description: { $regex: 'First.*Third', $options: 's' },
1223
+ });
1224
+ expect(withS).toHaveLength(1);
1225
+ });
1226
+ it('should handle $not with $regex operator', async () => {
1227
+ const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1228
+ category: { $not: { $regex: '^elect' } },
1229
+ });
1230
+ expect(results.length).toBeGreaterThan(0);
1231
+ results.forEach(result => {
1232
+ expect(result.metadata?.category).not.toMatch(/^elect/);
1233
+ });
1234
+ });
1235
+ });
1236
+ });
1237
+
1238
+ describe('Search Parameters', () => {
1239
+ const indexName = 'test_search_params';
1240
+ const vectors = [
1241
+ [1, 0, 0], // Query vector will be closest to this
1242
+ [0.8, 0.2, 0], // Second closest
1243
+ [0, 1, 0], // Third (much further)
1244
+ ];
1245
+
1246
+ describe('HNSW Parameters', () => {
1247
+ beforeAll(async () => {
1248
+ await vectorDB.createIndex(indexName, 3, 'cosine', {
1249
+ type: 'hnsw',
1250
+ hnsw: { m: 16, efConstruction: 64 },
1251
+ });
1252
+ await vectorDB.upsert(indexName, vectors);
1253
+ });
1254
+
1255
+ afterAll(async () => {
1256
+ await vectorDB.deleteIndex(indexName);
1257
+ });
1258
+
1259
+ it('should use default ef value', async () => {
1260
+ const results = await vectorDB.query(indexName, [1, 0, 0], 2);
1261
+ expect(results).toHaveLength(2);
1262
+ expect(results[0]?.score).toBeCloseTo(1, 5);
1263
+ expect(results[1]?.score).toBeGreaterThan(0.9); // Second vector should be close
1264
+ });
1265
+
1266
+ it('should respect custom ef value', async () => {
1267
+ const results = await vectorDB.query(indexName, [1, 0, 0], 2, undefined, undefined, undefined, { ef: 100 });
1268
+ expect(results).toHaveLength(2);
1269
+ expect(results[0]?.score).toBeCloseTo(1, 5);
1270
+ expect(results[1]?.score).toBeGreaterThan(0.9);
1271
+ });
1272
+ });
1273
+
1274
+ describe('IVF Parameters', () => {
1275
+ beforeAll(async () => {
1276
+ await vectorDB.createIndex(indexName, 3, 'cosine', {
1277
+ type: 'ivfflat',
1278
+ ivf: { lists: 2 }, // Small number for test data
1279
+ });
1280
+ await vectorDB.upsert(indexName, vectors);
1281
+ });
1282
+
1283
+ afterAll(async () => {
1284
+ await vectorDB.deleteIndex(indexName);
1285
+ });
1286
+
1287
+ it('should use default probe value', async () => {
1288
+ const results = await vectorDB.query(indexName, [1, 0, 0], 2);
1289
+ expect(results).toHaveLength(2);
1290
+ expect(results[0]?.score).toBeCloseTo(1, 5);
1291
+ expect(results[1]?.score).toBeGreaterThan(0.9);
1292
+ });
1293
+
1294
+ it('should respect custom probe value', async () => {
1295
+ const results = await vectorDB.query(indexName, [1, 0, 0], 2, undefined, undefined, undefined, { probes: 2 });
1296
+ expect(results).toHaveLength(2);
1297
+ expect(results[0]?.score).toBeCloseTo(1, 5);
1298
+ expect(results[1]?.score).toBeGreaterThan(0.9);
1299
+ });
1300
+ });
1301
+ });
1302
+ });