@mastra/libsql 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1702 @@
1
+ import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest';
2
+
3
+ import { LibSQLVector } from './index.js';
4
+
5
+ describe('LibSQLVector', () => {
6
+ let vectorDB: LibSQLVector;
7
+ const testIndexName = 'test_vectors';
8
+ // const testIndexName2 = 'test_vectors1';
9
+
10
+ beforeAll(async () => {
11
+ vectorDB = new LibSQLVector({
12
+ connectionUrl: 'file::memory:?cache=shared',
13
+ });
14
+ });
15
+
16
+ afterAll(async () => {
17
+ // Clean up test tables
18
+ await vectorDB.deleteIndex(testIndexName);
19
+ });
20
+
21
+ // Index Management Tests
22
+ describe('Index Management', () => {
23
+ describe('createIndex', () => {
24
+ it('should create a new vector table with specified dimensions', async () => {
25
+ await vectorDB.createIndex({ indexName: testIndexName, dimension: 3 });
26
+
27
+ const stats = await vectorDB.describeIndex(testIndexName);
28
+ expect(stats?.dimension).toBe(3);
29
+ expect(stats?.count).toBe(0);
30
+ });
31
+
32
+ // it('should create index with specified metric', async () => {
33
+ // await vectorDB.createIndex(testIndexName2, 3, 'euclidean');
34
+ //
35
+ // const stats = await vectorDB.describeIndex(testIndexName2);
36
+ //
37
+ // expect(stats.metric).toBe('euclidean');
38
+ // });
39
+
40
+ it('should throw error if dimension is invalid', async () => {
41
+ await expect(vectorDB.createIndex({ indexName: `testIndexNameFail`, dimension: 0 })).rejects.toThrow();
42
+ });
43
+ });
44
+
45
+ describe('listIndexes', () => {
46
+ const indexName = 'test_query_3';
47
+ beforeAll(async () => {
48
+ await vectorDB.createIndex({ indexName, dimension: 3 });
49
+ });
50
+
51
+ afterAll(async () => {
52
+ await vectorDB.deleteIndex(indexName);
53
+ });
54
+
55
+ it('should list all vector tables', async () => {
56
+ const indexes = await vectorDB.listIndexes();
57
+ expect(indexes).toContain(indexName);
58
+ });
59
+
60
+ it('should not return created index in list if it is deleted', async () => {
61
+ await vectorDB.deleteIndex(indexName);
62
+ const indexes = await vectorDB.listIndexes();
63
+ expect(indexes).not.toContain(indexName);
64
+ });
65
+ });
66
+
67
+ describe('describeIndex', () => {
68
+ const indexName = 'test_query_4';
69
+ beforeAll(async () => {
70
+ await vectorDB.createIndex({ indexName, dimension: 3 });
71
+ });
72
+
73
+ afterAll(async () => {
74
+ await vectorDB.deleteIndex(indexName);
75
+ });
76
+
77
+ it('should return correct index stats', async () => {
78
+ await vectorDB.createIndex({ indexName, dimension: 3, metric: 'cosine' });
79
+ const vectors = [
80
+ [1, 2, 3],
81
+ [4, 5, 6],
82
+ ];
83
+ await vectorDB.upsert({ indexName, vectors });
84
+
85
+ const stats = await vectorDB.describeIndex(indexName);
86
+ expect(stats).toEqual({
87
+ dimension: 3,
88
+ count: 2,
89
+ metric: 'cosine',
90
+ });
91
+ });
92
+
93
+ it('should throw error for non-existent index', async () => {
94
+ await expect(vectorDB.describeIndex('non_existent')).rejects.toThrow();
95
+ });
96
+ });
97
+ });
98
+
99
+ // Vector Operations Tests
100
+ describe('Vector Operations', () => {
101
+ describe('upsert', () => {
102
+ beforeEach(async () => {
103
+ await vectorDB.createIndex({ indexName: testIndexName, dimension: 3 });
104
+ });
105
+
106
+ afterEach(async () => {
107
+ await vectorDB.deleteIndex(testIndexName);
108
+ });
109
+
110
+ it('should insert new vectors', async () => {
111
+ const vectors = [
112
+ [1, 2, 3],
113
+ [4, 5, 6],
114
+ ];
115
+ const ids = await vectorDB.upsert({ indexName: testIndexName, vectors });
116
+
117
+ expect(ids).toHaveLength(2);
118
+ const stats = await vectorDB.describeIndex(testIndexName);
119
+ expect(stats.count).toBe(2);
120
+ });
121
+
122
+ it('should update existing vectors', async () => {
123
+ const vectors = [[1, 2, 3]];
124
+ const metadata = [{ test: 'initial' }];
125
+ const [id] = await vectorDB.upsert({ indexName: testIndexName, vectors, metadata });
126
+
127
+ const updatedVectors = [[4, 5, 6]];
128
+ const updatedMetadata = [{ test: 'updated' }];
129
+ await vectorDB.upsert({
130
+ indexName: testIndexName,
131
+ vectors: updatedVectors,
132
+ metadata: updatedMetadata,
133
+ ids: [id!],
134
+ });
135
+
136
+ const results = await vectorDB.query({ indexName: testIndexName, queryVector: [4, 5, 6], topK: 1 });
137
+ expect(results[0]?.id).toBe(id);
138
+ expect(results[0]?.metadata).toEqual({ test: 'updated' });
139
+ });
140
+
141
+ it('should handle metadata correctly', async () => {
142
+ const vectors = [[1, 2, 3]];
143
+ const metadata = [{ test: 'value', num: 123 }];
144
+
145
+ await vectorDB.upsert({ indexName: testIndexName, vectors, metadata });
146
+ const results = await vectorDB.query({ indexName: testIndexName, queryVector: [1, 2, 3], topK: 1 });
147
+
148
+ expect(results[0]?.metadata).toEqual(metadata[0]);
149
+ });
150
+
151
+ it('should throw error if vector dimensions dont match', async () => {
152
+ const vectors = [[1, 2, 3, 4]]; // 4D vector for 3D index
153
+ await expect(vectorDB.upsert({ indexName: testIndexName, vectors })).rejects.toThrow(
154
+ `Vector dimension mismatch: Index "${testIndexName}" expects 3 dimensions but got 4 dimensions. ` +
155
+ `Either use a matching embedding model or delete and recreate the index with the new dimension.`,
156
+ );
157
+ });
158
+
159
+ it('should delete the vector by id', async () => {
160
+ const vectors = [
161
+ [1, 2, 3],
162
+ [4, 5, 6],
163
+ ];
164
+ const ids = await vectorDB.upsert({ indexName: testIndexName, vectors });
165
+ expect(ids).toHaveLength(2);
166
+ const id = ids[1];
167
+
168
+ await vectorDB.deleteIndexById(testIndexName, ids[0]);
169
+
170
+ const results = await vectorDB.query({ indexName: testIndexName, queryVector: [1, 2, 3] });
171
+ expect(results).toHaveLength(1);
172
+ expect(results[0]?.id).toBe(id);
173
+ });
174
+
175
+ it('should update the vector by id', async () => {
176
+ const vectors = [[1, 2, 3]];
177
+ const metadata = [{ test: 'initial' }];
178
+ const [id] = await vectorDB.upsert({ indexName: testIndexName, vectors, metadata });
179
+
180
+ const update = {
181
+ vector: [4, 5, 6],
182
+ metadata: { test: 'updated' },
183
+ };
184
+ await vectorDB.updateIndexById(testIndexName, id, update);
185
+
186
+ const results = await vectorDB.query({
187
+ indexName: testIndexName,
188
+ queryVector: [4, 5, 6],
189
+ topK: 1,
190
+ includeVector: true,
191
+ });
192
+ expect(results[0]?.id).toBe(id);
193
+ expect(results[0]?.metadata).toEqual({ test: 'updated' });
194
+ expect(results[0]?.vector).toEqual([4, 5, 6]);
195
+ });
196
+
197
+ it('should update only metadata by id', async () => {
198
+ const vectors = [[1, 2, 3]];
199
+ const metadata = [{ test: 'initial' }];
200
+ const [id] = await vectorDB.upsert({ indexName: testIndexName, vectors, metadata });
201
+
202
+ const update = {
203
+ metadata: { test: 'updated' },
204
+ };
205
+ await vectorDB.updateIndexById(testIndexName, id, update);
206
+
207
+ const results = await vectorDB.query({ indexName: testIndexName, queryVector: [1, 2, 3], topK: 1 });
208
+ expect(results[0]?.id).toBe(id);
209
+ expect(results[0]?.metadata).toEqual({ test: 'updated' });
210
+ expect(results[0]?.vector).toBeUndefined();
211
+ });
212
+
213
+ it('should update only vector by id', async () => {
214
+ const vectors = [[1, 2, 3]];
215
+ const metadata = [{ test: 'initial' }];
216
+ const [id] = await vectorDB.upsert({ indexName: testIndexName, vectors, metadata });
217
+
218
+ const update = {
219
+ vector: [4, 5, 6],
220
+ };
221
+ await vectorDB.updateIndexById(testIndexName, id, update);
222
+
223
+ const results = await vectorDB.query({
224
+ indexName: testIndexName,
225
+ queryVector: [4, 5, 6],
226
+ topK: 1,
227
+ includeVector: true,
228
+ });
229
+ expect(results[0]?.id).toBe(id);
230
+ expect(results[0]?.metadata).toEqual({ test: 'initial' });
231
+ expect(results[0]?.vector).toEqual([4, 5, 6]);
232
+ });
233
+
234
+ it('should throw error if no updates are provided', async () => {
235
+ const vectors = [[1, 2, 3]];
236
+ const metadata = [{ test: 'initial' }];
237
+ const [id] = await vectorDB.upsert({ indexName: testIndexName, vectors, metadata });
238
+
239
+ await expect(vectorDB.updateIndexById(testIndexName, id, {})).rejects.toThrow('No updates provided');
240
+ });
241
+ });
242
+
243
+ describe('Basic Query Operations', () => {
244
+ const indexName = 'test_query_2';
245
+ beforeEach(async () => {
246
+ await vectorDB.createIndex({ indexName, dimension: 3 });
247
+ const vectors = [
248
+ [1, 0, 0],
249
+ [0.8, 0.2, 0],
250
+ [0, 1, 0],
251
+ ];
252
+ const metadata = [
253
+ { type: 'a', value: 1 },
254
+ { type: 'b', value: 2 },
255
+ { type: 'a', value: 3 },
256
+ ];
257
+ await vectorDB.upsert({ indexName, vectors, metadata });
258
+ });
259
+
260
+ afterEach(async () => {
261
+ await vectorDB.deleteIndex(indexName);
262
+ });
263
+
264
+ it('should return closest vectors', async () => {
265
+ const results = await vectorDB.query({ indexName, queryVector: [1, 0, 0], topK: 1 });
266
+ expect(results).toHaveLength(1);
267
+ expect(results[0]?.vector).toBe(undefined);
268
+ expect(results[0]?.score).toBeCloseTo(1, 5);
269
+ });
270
+
271
+ it('should return vector with result', async () => {
272
+ const results = await vectorDB.query({ indexName, queryVector: [1, 0, 0], topK: 1, includeVector: true });
273
+ expect(results).toHaveLength(1);
274
+ expect(results[0]?.vector).toStrictEqual([1, 0, 0]);
275
+ });
276
+
277
+ it('should respect topK parameter', async () => {
278
+ const results = await vectorDB.query({ indexName, queryVector: [1, 0, 0], topK: 2 });
279
+ expect(results).toHaveLength(2);
280
+ });
281
+
282
+ it('should handle filters correctly', async () => {
283
+ const results = await vectorDB.query({ indexName, queryVector: [1, 0, 0], topK: 10, filter: { type: 'a' } });
284
+
285
+ expect(results).toHaveLength(1);
286
+ results.forEach(result => {
287
+ expect(result?.metadata?.type).toBe('a');
288
+ });
289
+ });
290
+ });
291
+ });
292
+
293
+ // Advanced Query and Filter Tests
294
+ describe('Advanced Query and Filter Operations', () => {
295
+ const indexName = 'test_query_filters';
296
+
297
+ beforeEach(async () => {
298
+ await vectorDB.createIndex({ indexName, dimension: 3 });
299
+ const vectors = [
300
+ [1, 0.1, 0],
301
+ [0.9, 0.2, 0],
302
+ [0.95, 0.1, 0],
303
+ [0.85, 0.2, 0],
304
+ [0.9, 0.1, 0],
305
+ ];
306
+
307
+ const metadata = [
308
+ {
309
+ category: 'electronics',
310
+ price: 100,
311
+ tags: ['new', 'premium'],
312
+ active: true,
313
+ ratings: [4.5, 4.8, 4.2], // Array of numbers
314
+ stock: [
315
+ { location: 'A', count: 25 },
316
+ { location: 'B', count: 15 },
317
+ ], // Array of objects
318
+ reviews: [
319
+ { user: 'alice', score: 5, verified: true },
320
+ { user: 'bob', score: 4, verified: true },
321
+ { user: 'charlie', score: 3, verified: false },
322
+ ], // Complex array objects
323
+ },
324
+ {
325
+ category: 'books',
326
+ price: 50,
327
+ tags: ['used'],
328
+ active: true,
329
+ ratings: [3.8, 4.0, 4.1],
330
+ stock: [
331
+ { location: 'A', count: 10 },
332
+ { location: 'C', count: 30 },
333
+ ],
334
+ reviews: [
335
+ { user: 'dave', score: 4, verified: true },
336
+ { user: 'eve', score: 5, verified: false },
337
+ ],
338
+ },
339
+ { category: 'electronics', price: 75, tags: ['refurbished'], active: false },
340
+ { category: 'books', price: 25, tags: ['used', 'sale'], active: true },
341
+ { category: 'clothing', price: 60, tags: ['new'], active: true },
342
+ ];
343
+
344
+ await vectorDB.upsert({ indexName, vectors, metadata });
345
+ });
346
+
347
+ afterEach(async () => {
348
+ await vectorDB.deleteIndex(indexName);
349
+ });
350
+
351
+ // Numeric Comparison Tests
352
+ describe('Comparison Operators', () => {
353
+ it('should handle numeric string comparisons', async () => {
354
+ // Insert a record with numeric string
355
+ await vectorDB.upsert({ indexName, vectors: [[1, 0.1, 0]], metadata: [{ numericString: '123' }] });
356
+
357
+ const results = await vectorDB.query({
358
+ indexName,
359
+ queryVector: [1, 0, 0],
360
+ filter: { numericString: { $gt: '100' } }, // Compare strings numerically
361
+ });
362
+ expect(results.length).toBeGreaterThan(0);
363
+ expect(results[0]?.metadata?.numericString).toBe('123');
364
+ });
365
+
366
+ it('should filter with $gt operator', async () => {
367
+ const results = await vectorDB.query({
368
+ indexName,
369
+ queryVector: [1, 0, 0],
370
+ filter: { price: { $gt: 75 } },
371
+ });
372
+ expect(results).toHaveLength(1);
373
+ expect(results[0]?.metadata?.price).toBe(100);
374
+ });
375
+
376
+ it('should filter with $lte operator', async () => {
377
+ const results = await vectorDB.query({
378
+ indexName,
379
+ queryVector: [1, 0, 0],
380
+ filter: { price: { $lte: 50 } },
381
+ });
382
+ expect(results).toHaveLength(2);
383
+ results.forEach(result => {
384
+ expect(result.metadata?.price).toBeLessThanOrEqual(50);
385
+ });
386
+ });
387
+
388
+ it('should filter with lt operator', async () => {
389
+ const results = await vectorDB.query({
390
+ indexName,
391
+ queryVector: [1, 0, 0],
392
+ filter: { price: { $lt: 60 } },
393
+ });
394
+ expect(results).toHaveLength(2);
395
+ results.forEach(result => {
396
+ expect(result.metadata?.price).toBeLessThan(60);
397
+ });
398
+ });
399
+
400
+ it('should filter with gte operator', async () => {
401
+ const results = await vectorDB.query({
402
+ indexName,
403
+ queryVector: [1, 0, 0],
404
+ filter: { price: { $gte: 75 } },
405
+ });
406
+ expect(results).toHaveLength(2);
407
+ results.forEach(result => {
408
+ expect(result.metadata?.price).toBeGreaterThanOrEqual(75);
409
+ });
410
+ });
411
+
412
+ it('should filter with ne operator', async () => {
413
+ const results = await vectorDB.query({
414
+ indexName,
415
+ queryVector: [1, 0, 0],
416
+ filter: { category: { $ne: 'electronics' } },
417
+ });
418
+ expect(results.length).toBeGreaterThan(0);
419
+ results.forEach(result => {
420
+ expect(result.metadata?.category).not.toBe('electronics');
421
+ });
422
+ });
423
+
424
+ it('should filter with $gt and $lte operator', async () => {
425
+ const results = await vectorDB.query({
426
+ indexName,
427
+ queryVector: [1, 0, 0],
428
+ filter: { price: { $gt: 70, $lte: 100 } },
429
+ });
430
+ expect(results).toHaveLength(2);
431
+ results.forEach(result => {
432
+ expect(result.metadata?.price).toBeGreaterThan(70);
433
+ expect(result.metadata?.price).toBeLessThanOrEqual(100);
434
+ });
435
+ });
436
+ });
437
+
438
+ // Array Operator Tests
439
+ describe('Array Operators', () => {
440
+ it('should filter with $in operator', async () => {
441
+ const results = await vectorDB.query({
442
+ indexName,
443
+ queryVector: [1, 0, 0],
444
+ filter: { category: { $in: ['electronics', 'clothing'] } },
445
+ });
446
+ expect(results).toHaveLength(3);
447
+ results.forEach(result => {
448
+ expect(['electronics', 'clothing']).toContain(result.metadata?.category);
449
+ });
450
+ });
451
+
452
+ it('should filter with $nin operator', async () => {
453
+ const results = await vectorDB.query({
454
+ indexName,
455
+ queryVector: [1, 0, 0],
456
+ filter: { category: { $nin: ['electronics', 'books'] } },
457
+ });
458
+ expect(results.length).toBeGreaterThan(0);
459
+ results.forEach(result => {
460
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
461
+ });
462
+ });
463
+
464
+ it('should handle empty arrays in in/nin operators', async () => {
465
+ // Should return no results for empty IN
466
+ const resultsIn = await vectorDB.query({
467
+ indexName,
468
+ queryVector: [1, 0, 0],
469
+ filter: { category: { $in: [] } },
470
+ });
471
+ expect(resultsIn).toHaveLength(0);
472
+
473
+ // Should return all results for empty NIN
474
+ const resultsNin = await vectorDB.query({
475
+ indexName,
476
+ queryVector: [1, 0, 0],
477
+ filter: { category: { $nin: [] } },
478
+ });
479
+ expect(resultsNin.length).toBeGreaterThan(0);
480
+ });
481
+
482
+ it('should filter with array $contains operator', async () => {
483
+ const results = await vectorDB.query({
484
+ indexName,
485
+ queryVector: [1, 0.1, 0],
486
+ filter: { tags: { $contains: ['new'] } },
487
+ });
488
+ expect(results.length).toBeGreaterThan(0);
489
+ results.forEach(result => {
490
+ expect(result.metadata?.tags).toContain('new');
491
+ });
492
+ });
493
+
494
+ it('should filter with $elemMatch operator', async () => {
495
+ const results = await vectorDB.query({
496
+ indexName,
497
+ queryVector: [1, 0, 0],
498
+ filter: {
499
+ tags: {
500
+ $elemMatch: {
501
+ $in: ['new', 'premium'],
502
+ },
503
+ },
504
+ },
505
+ });
506
+ expect(results.length).toBeGreaterThan(0);
507
+ results.forEach(result => {
508
+ expect(result.metadata?.tags.some(tag => ['new', 'premium'].includes(tag))).toBe(true);
509
+ });
510
+ });
511
+
512
+ it('should filter with $elemMatch using equality', async () => {
513
+ const results = await vectorDB.query({
514
+ indexName,
515
+ queryVector: [1, 0, 0],
516
+ filter: {
517
+ tags: {
518
+ $elemMatch: {
519
+ $eq: 'sale',
520
+ },
521
+ },
522
+ },
523
+ });
524
+ expect(results).toHaveLength(1);
525
+ expect(results[0]?.metadata?.tags).toContain('sale');
526
+ });
527
+
528
+ it('should filter with $elemMatch using multiple conditions', async () => {
529
+ const results = await vectorDB.query({
530
+ indexName,
531
+ queryVector: [1, 0, 0],
532
+ filter: {
533
+ ratings: {
534
+ $elemMatch: {
535
+ $gt: 4,
536
+ $lt: 4.5,
537
+ },
538
+ },
539
+ },
540
+ });
541
+ expect(results.length).toBeGreaterThan(0);
542
+ results.forEach(result => {
543
+ expect(Array.isArray(result.metadata?.ratings)).toBe(true);
544
+ expect(result.metadata?.ratings.some(rating => rating > 4 && rating < 4.5)).toBe(true);
545
+ });
546
+ });
547
+
548
+ it('should handle complex $elemMatch conditions', async () => {
549
+ const results = await vectorDB.query({
550
+ indexName,
551
+ queryVector: [1, 0, 0],
552
+ filter: {
553
+ stock: {
554
+ $elemMatch: {
555
+ location: 'A',
556
+ count: { $gt: 20 },
557
+ },
558
+ },
559
+ },
560
+ });
561
+ expect(results.length).toBeGreaterThan(0);
562
+ results.forEach(result => {
563
+ const matchingStock = result.metadata?.stock.find(s => s.location === 'A' && s.count > 20);
564
+ expect(matchingStock).toBeDefined();
565
+ });
566
+ });
567
+
568
+ it('should filter with $elemMatch on nested numeric fields', async () => {
569
+ const results = await vectorDB.query({
570
+ indexName,
571
+ queryVector: [1, 0, 0],
572
+ filter: {
573
+ reviews: {
574
+ $elemMatch: {
575
+ score: { $gt: 4 },
576
+ },
577
+ },
578
+ },
579
+ });
580
+ expect(results.length).toBeGreaterThan(0);
581
+ results.forEach(result => {
582
+ expect(result.metadata?.reviews.some(r => r.score > 4)).toBe(true);
583
+ });
584
+ });
585
+
586
+ it('should filter with $elemMatch on multiple nested fields', async () => {
587
+ const results = await vectorDB.query({
588
+ indexName,
589
+ queryVector: [1, 0, 0],
590
+ filter: {
591
+ reviews: {
592
+ $elemMatch: {
593
+ score: { $gte: 4 },
594
+ verified: true,
595
+ },
596
+ },
597
+ },
598
+ });
599
+ expect(results.length).toBeGreaterThan(0);
600
+ results.forEach(result => {
601
+ expect(result.metadata?.reviews.some(r => r.score >= 4 && r.verified)).toBe(true);
602
+ });
603
+ });
604
+
605
+ it('should filter with $elemMatch on exact string match', async () => {
606
+ const results = await vectorDB.query({
607
+ indexName,
608
+ queryVector: [1, 0, 0],
609
+ filter: {
610
+ reviews: {
611
+ $elemMatch: {
612
+ user: 'alice',
613
+ },
614
+ },
615
+ },
616
+ });
617
+ expect(results).toHaveLength(1);
618
+ expect(results[0].metadata?.reviews.some(r => r.user === 'alice')).toBe(true);
619
+ });
620
+
621
+ it('should handle $elemMatch with no matches', async () => {
622
+ const results = await vectorDB.query({
623
+ indexName,
624
+ queryVector: [1, 0, 0],
625
+ filter: {
626
+ reviews: {
627
+ $elemMatch: {
628
+ score: 10, // No review has score 10
629
+ },
630
+ },
631
+ },
632
+ });
633
+ expect(results).toHaveLength(0);
634
+ });
635
+
636
+ it('should filter with $all operator', async () => {
637
+ const results = await vectorDB.query({
638
+ indexName,
639
+ queryVector: [1, 0, 0],
640
+ filter: { tags: { $all: ['used', 'sale'] } },
641
+ });
642
+ expect(results).toHaveLength(1);
643
+ results.forEach(result => {
644
+ expect(result.metadata?.tags).toContain('used');
645
+ expect(result.metadata?.tags).toContain('sale');
646
+ });
647
+ });
648
+
649
+ it('should filter with $all using single value', async () => {
650
+ const results = await vectorDB.query({
651
+ indexName,
652
+ queryVector: [1, 0, 0],
653
+ filter: { tags: { $all: ['new'] } },
654
+ });
655
+ expect(results.length).toBeGreaterThan(0);
656
+ results.forEach(result => {
657
+ expect(result.metadata?.tags).toContain('new');
658
+ });
659
+ });
660
+
661
+ it('should handle empty array for $all', async () => {
662
+ const results = await vectorDB.query({
663
+ indexName,
664
+ queryVector: [1, 0, 0],
665
+ filter: { tags: { $all: [] } },
666
+ });
667
+ expect(results).toHaveLength(0);
668
+ });
669
+
670
+ it('should handle non-array field $all', async () => {
671
+ // First insert a record with non-array field
672
+ await vectorDB.upsert({
673
+ indexName,
674
+ vectors: [[1, 0.1, 0]],
675
+ metadata: [{ tags: 'not-an-array' }],
676
+ });
677
+
678
+ const results = await vectorDB.query({
679
+ indexName,
680
+ queryVector: [1, 0, 0],
681
+ filter: { tags: { $all: ['value'] } },
682
+ });
683
+ expect(results).toHaveLength(0);
684
+ });
685
+
686
+ // Contains Operator Tests
687
+ it('should filter with contains operator for exact field match', async () => {
688
+ const results = await vectorDB.query({
689
+ indexName,
690
+ queryVector: [1, 0.1, 0],
691
+ filter: { category: { $contains: 'electronics' } },
692
+ });
693
+ expect(results.length).toBeGreaterThan(0);
694
+ results.forEach(result => {
695
+ expect(result.metadata?.category).toBe('electronics');
696
+ });
697
+ });
698
+
699
+ it('should filter with $contains operator for nested objects', async () => {
700
+ // First insert a record with nested object
701
+ await vectorDB.upsert({
702
+ indexName,
703
+ vectors: [[1, 0.1, 0]],
704
+ metadata: [
705
+ {
706
+ details: { color: 'red', size: 'large' },
707
+ category: 'clothing',
708
+ },
709
+ ],
710
+ });
711
+
712
+ const results = await vectorDB.query({
713
+ indexName,
714
+ queryVector: [1, 0.1, 0],
715
+ filter: { details: { $contains: { color: 'red' } } },
716
+ });
717
+ expect(results.length).toBeGreaterThan(0);
718
+ results.forEach(result => {
719
+ expect(result.metadata?.details.color).toBe('red');
720
+ });
721
+ });
722
+
723
+ // String Pattern Tests
724
+ it('should handle exact string matches', async () => {
725
+ const results = await vectorDB.query({
726
+ indexName,
727
+ queryVector: [1, 0, 0],
728
+ filter: { category: 'electronics' },
729
+ });
730
+ expect(results).toHaveLength(2);
731
+ });
732
+
733
+ it('should handle case-sensitive string matches', async () => {
734
+ const results = await vectorDB.query({
735
+ indexName,
736
+ queryVector: [1, 0, 0],
737
+ filter: { category: 'ELECTRONICS' },
738
+ });
739
+ expect(results).toHaveLength(0);
740
+ });
741
+ it('should filter arrays by size', async () => {
742
+ const results = await vectorDB.query({
743
+ indexName,
744
+ queryVector: [1, 0, 0],
745
+ filter: { ratings: { $size: 3 } },
746
+ });
747
+ expect(results.length).toBeGreaterThan(0);
748
+ results.forEach(result => {
749
+ expect(result.metadata?.ratings).toHaveLength(3);
750
+ });
751
+
752
+ const noResults = await vectorDB.query({
753
+ indexName,
754
+ queryVector: [1, 0, 0],
755
+ filter: { ratings: { $size: 10 } },
756
+ });
757
+ expect(noResults).toHaveLength(0);
758
+ });
759
+
760
+ it('should handle $size with nested arrays', async () => {
761
+ await vectorDB.upsert({
762
+ indexName,
763
+ vectors: [[1, 0.1, 0]],
764
+ metadata: [{ nested: { array: [1, 2, 3, 4] } }],
765
+ });
766
+ const results = await vectorDB.query({
767
+ indexName,
768
+ queryVector: [1, 0, 0],
769
+ filter: { 'nested.array': { $size: 4 } },
770
+ });
771
+ expect(results.length).toBeGreaterThan(0);
772
+ results.forEach(result => {
773
+ expect(result.metadata?.nested.array).toHaveLength(4);
774
+ });
775
+ });
776
+ });
777
+
778
+ // Logical Operator Tests
779
+ describe('Logical Operators', () => {
780
+ it('should handle AND filter conditions', async () => {
781
+ const results = await vectorDB.query({
782
+ indexName,
783
+ queryVector: [1, 0, 0],
784
+ filter: { $and: [{ category: { $eq: 'electronics' } }, { price: { $gt: 75 } }] },
785
+ });
786
+ expect(results).toHaveLength(1);
787
+ expect(results[0]?.metadata?.category).toBe('electronics');
788
+ expect(results[0]?.metadata?.price).toBeGreaterThan(75);
789
+ });
790
+
791
+ it('should handle OR filter conditions', async () => {
792
+ const results = await vectorDB.query({
793
+ indexName,
794
+ queryVector: [1, 0, 0],
795
+ filter: { $or: [{ category: { $eq: 'electronics' } }, { category: { $eq: 'books' } }] },
796
+ });
797
+ expect(results.length).toBeGreaterThan(1);
798
+ results.forEach(result => {
799
+ expect(['electronics', 'books']).toContain(result?.metadata?.category);
800
+ });
801
+ });
802
+
803
+ it('should handle $not operator', async () => {
804
+ const results = await vectorDB.query({
805
+ indexName,
806
+ queryVector: [1, 0, 0],
807
+ filter: { $not: { category: 'electronics' } },
808
+ });
809
+ expect(results.length).toBeGreaterThan(0);
810
+ results.forEach(result => {
811
+ expect(result.metadata?.category).not.toBe('electronics');
812
+ });
813
+ });
814
+
815
+ it('should handle $nor operator', async () => {
816
+ const results = await vectorDB.query({
817
+ indexName,
818
+ queryVector: [1, 0, 0],
819
+ filter: { $nor: [{ category: 'electronics' }, { category: 'books' }] },
820
+ });
821
+ expect(results.length).toBeGreaterThan(0);
822
+ results.forEach(result => {
823
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
824
+ });
825
+ });
826
+
827
+ it('should handle nested $not with $or', async () => {
828
+ const results = await vectorDB.query({
829
+ indexName,
830
+ queryVector: [1, 0, 0],
831
+ filter: { $not: { $or: [{ category: 'electronics' }, { category: 'books' }] } },
832
+ });
833
+ expect(results.length).toBeGreaterThan(0);
834
+ results.forEach(result => {
835
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
836
+ });
837
+ });
838
+
839
+ it('should handle $not with comparison operators', async () => {
840
+ const results = await vectorDB.query({
841
+ indexName,
842
+ queryVector: [1, 0, 0],
843
+ filter: { price: { $not: { $gt: 100 } } },
844
+ });
845
+ expect(results.length).toBeGreaterThan(0);
846
+ results.forEach(result => {
847
+ expect(Number(result.metadata?.price)).toBeLessThanOrEqual(100);
848
+ });
849
+ });
850
+
851
+ it('should handle $not with $in operator', async () => {
852
+ const results = await vectorDB.query({
853
+ indexName,
854
+ queryVector: [1, 0, 0],
855
+ filter: { category: { $not: { $in: ['electronics', 'books'] } } },
856
+ });
857
+ expect(results.length).toBeGreaterThan(0);
858
+ results.forEach(result => {
859
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
860
+ });
861
+ });
862
+
863
+ it('should handle $not with multiple nested conditions', async () => {
864
+ const results = await vectorDB.query({
865
+ indexName,
866
+ queryVector: [1, 0, 0],
867
+ filter: { $not: { $and: [{ category: 'electronics' }, { price: { $gt: 50 } }] } },
868
+ });
869
+ expect(results.length).toBeGreaterThan(0);
870
+ results.forEach(result => {
871
+ expect(result.metadata?.category !== 'electronics' || result.metadata?.price <= 50).toBe(true);
872
+ });
873
+ });
874
+
875
+ it('should handle $not with $exists operator', async () => {
876
+ const results = await vectorDB.query({
877
+ indexName,
878
+ queryVector: [1, 0, 0],
879
+ filter: { tags: { $not: { $exists: true } } },
880
+ });
881
+ expect(results.length).toBe(0); // All test data has tags
882
+ });
883
+
884
+ it('should handle $not with array operators', async () => {
885
+ const results = await vectorDB.query({
886
+ indexName,
887
+ queryVector: [1, 0, 0],
888
+ filter: { tags: { $not: { $all: ['new', 'premium'] } } },
889
+ });
890
+ expect(results.length).toBeGreaterThan(0);
891
+ results.forEach(result => {
892
+ expect(!result.metadata?.tags.includes('new') || !result.metadata?.tags.includes('premium')).toBe(true);
893
+ });
894
+ });
895
+
896
+ it('should handle $not with complex nested conditions', async () => {
897
+ const results = await vectorDB.query({
898
+ indexName,
899
+ queryVector: [1, 0, 0],
900
+ filter: {
901
+ $not: {
902
+ $or: [
903
+ { $and: [{ category: 'electronics' }, { price: { $gt: 90 } }] },
904
+ { $and: [{ category: 'books' }, { price: { $lt: 30 } }] },
905
+ ],
906
+ },
907
+ },
908
+ });
909
+ expect(results.length).toBeGreaterThan(0);
910
+ results.forEach(result => {
911
+ const notExpensiveElectronics = !(result.metadata?.category === 'electronics' && result.metadata?.price > 90);
912
+ const notCheapBooks = !(result.metadata?.category === 'books' && result.metadata?.price < 30);
913
+ expect(notExpensiveElectronics && notCheapBooks).toBe(true);
914
+ });
915
+ });
916
+
917
+ it('should handle $not with empty arrays', async () => {
918
+ const results = await vectorDB.query({
919
+ indexName,
920
+ queryVector: [1, 0, 0],
921
+ filter: { tags: { $not: { $in: [] } } },
922
+ });
923
+ expect(results.length).toBeGreaterThan(0); // Should match all records
924
+ });
925
+
926
+ it('should handle $not with null values', async () => {
927
+ // First insert a record with null value
928
+ await vectorDB.upsert({
929
+ indexName,
930
+ vectors: [[1, 0.1, 0]],
931
+ metadata: [{ category: null, price: 0 }],
932
+ });
933
+
934
+ const results = await vectorDB.query({
935
+ indexName,
936
+ queryVector: [1, 0, 0],
937
+ filter: { category: { $not: { $eq: null } } },
938
+ });
939
+ expect(results.length).toBeGreaterThan(0);
940
+ results.forEach(result => {
941
+ expect(result.metadata?.category).not.toBeNull();
942
+ });
943
+ });
944
+
945
+ it('should handle $not with boolean values', async () => {
946
+ const results = await vectorDB.query({
947
+ indexName,
948
+ queryVector: [1, 0, 0],
949
+ filter: { active: { $not: { $eq: true } } },
950
+ });
951
+ expect(results.length).toBeGreaterThan(0);
952
+ results.forEach(result => {
953
+ expect(result.metadata?.active).not.toBe(true);
954
+ });
955
+ });
956
+
957
+ it('should handle $not with multiple conditions', async () => {
958
+ const results = await vectorDB.query({
959
+ indexName,
960
+ queryVector: [1, 0, 0],
961
+ filter: { $not: { category: 'electronics', price: { $gt: 50 } } },
962
+ });
963
+ expect(results.length).toBeGreaterThan(0);
964
+ });
965
+
966
+ it('should handle $not with $not operator', async () => {
967
+ const results = await vectorDB.query({
968
+ indexName,
969
+ queryVector: [1, 0, 0],
970
+ filter: { $not: { $not: { category: 'electronics' } } },
971
+ });
972
+ expect(results.length).toBeGreaterThan(0);
973
+ });
974
+
975
+ it('should handle $not in nested fields', async () => {
976
+ await vectorDB.upsert({
977
+ indexName,
978
+ vectors: [[1, 0.1, 0]],
979
+ metadata: [{ user: { profile: { price: 10 } } }],
980
+ });
981
+ const results = await vectorDB.query({
982
+ indexName,
983
+ queryVector: [1, 0, 0],
984
+ filter: { 'user.profile.price': { $not: { $gt: 25 } } },
985
+ });
986
+ expect(results.length).toBe(1);
987
+ });
988
+
989
+ it('should handle $not with multiple operators', async () => {
990
+ const results = await vectorDB.query({
991
+ indexName,
992
+ queryVector: [1, 0, 0],
993
+ filter: { price: { $not: { $gte: 30, $lte: 70 } } },
994
+ });
995
+ expect(results.length).toBeGreaterThan(0);
996
+ results.forEach(result => {
997
+ const price = Number(result.metadata?.price);
998
+ expect(price < 30 || price > 70).toBe(true);
999
+ });
1000
+ });
1001
+
1002
+ it('should handle $not with comparison operators', async () => {
1003
+ const results = await vectorDB.query({
1004
+ indexName,
1005
+ queryVector: [1, 0, 0],
1006
+ filter: { price: { $not: { $gt: 100 } } },
1007
+ });
1008
+ expect(results.length).toBeGreaterThan(0);
1009
+ results.forEach(result => {
1010
+ expect(Number(result.metadata?.price)).toBeLessThanOrEqual(100);
1011
+ });
1012
+ });
1013
+
1014
+ it('should handle $not with $and', async () => {
1015
+ const results = await vectorDB.query({
1016
+ indexName,
1017
+ queryVector: [1, 0, 0],
1018
+ filter: { $not: { $and: [{ category: 'electronics' }, { price: { $gt: 50 } }] } },
1019
+ });
1020
+ expect(results.length).toBeGreaterThan(0);
1021
+ results.forEach(result => {
1022
+ expect(result.metadata?.category !== 'electronics' || result.metadata?.price <= 50).toBe(true);
1023
+ });
1024
+ });
1025
+
1026
+ it('should handle $nor with $or', async () => {
1027
+ const results = await vectorDB.query({
1028
+ indexName,
1029
+ queryVector: [1, 0, 0],
1030
+ filter: { $nor: [{ $or: [{ category: 'electronics' }, { category: 'books' }] }, { price: { $gt: 75 } }] },
1031
+ });
1032
+ expect(results.length).toBeGreaterThan(0);
1033
+ results.forEach(result => {
1034
+ expect(['electronics', 'books']).not.toContain(result.metadata?.category);
1035
+ expect(result.metadata?.price).toBeLessThanOrEqual(75);
1036
+ });
1037
+ });
1038
+
1039
+ it('should handle $nor with nested $and conditions', async () => {
1040
+ const results = await vectorDB.query({
1041
+ indexName,
1042
+ queryVector: [1, 0, 0],
1043
+ filter: {
1044
+ $nor: [
1045
+ { $and: [{ category: 'electronics' }, { active: true }] },
1046
+ { $and: [{ category: 'books' }, { price: { $lt: 30 } }] },
1047
+ ],
1048
+ },
1049
+ });
1050
+ expect(results.length).toBeGreaterThan(0);
1051
+ results.forEach(result => {
1052
+ const notElectronicsActive = !(
1053
+ result.metadata?.category === 'electronics' && result.metadata?.active === true
1054
+ );
1055
+ const notBooksLowPrice = !(result.metadata?.category === 'books' && result.metadata?.price < 30);
1056
+ expect(notElectronicsActive && notBooksLowPrice).toBe(true);
1057
+ });
1058
+ });
1059
+
1060
+ it('should handle nested $and with $or and $not', async () => {
1061
+ const results = await vectorDB.query({
1062
+ indexName,
1063
+ queryVector: [1, 0, 0],
1064
+ filter: {
1065
+ $and: [{ $or: [{ category: 'electronics' }, { category: 'books' }] }, { $not: { price: { $lt: 50 } } }],
1066
+ },
1067
+ });
1068
+ expect(results.length).toBeGreaterThan(0);
1069
+ results.forEach(result => {
1070
+ expect(['electronics', 'books']).toContain(result.metadata?.category);
1071
+ expect(result.metadata?.price).toBeGreaterThanOrEqual(50);
1072
+ });
1073
+ });
1074
+
1075
+ it('should handle $or with multiple $not conditions', async () => {
1076
+ const results = await vectorDB.query({
1077
+ indexName,
1078
+ queryVector: [1, 0, 0],
1079
+ filter: { $or: [{ $not: { category: 'electronics' } }, { $not: { price: { $gt: 50 } } }] },
1080
+ });
1081
+ expect(results.length).toBeGreaterThan(0);
1082
+ results.forEach(result => {
1083
+ expect(result.metadata?.category !== 'electronics' || result.metadata?.price <= 50).toBe(true);
1084
+ });
1085
+ });
1086
+ });
1087
+
1088
+ // Edge Cases and Special Values
1089
+ describe('Edge Cases and Special Values', () => {
1090
+ it('should handle empty result sets with valid filters', async () => {
1091
+ const results = await vectorDB.query({
1092
+ indexName,
1093
+ queryVector: [1, 0, 0],
1094
+ filter: { price: { $gt: 1000 } },
1095
+ });
1096
+ expect(results).toHaveLength(0);
1097
+ });
1098
+
1099
+ it('should throw error for invalid operator', async () => {
1100
+ await expect(
1101
+ vectorDB.query({
1102
+ indexName,
1103
+ queryVector: [1, 0, 0],
1104
+ filter: { price: { $invalid: 100 } },
1105
+ }),
1106
+ ).rejects.toThrow('Unsupported operator: $invalid');
1107
+ });
1108
+
1109
+ it('should handle empty filter object', async () => {
1110
+ const results = await vectorDB.query({
1111
+ indexName,
1112
+ queryVector: [1, 0, 0],
1113
+ filter: {},
1114
+ });
1115
+ expect(results.length).toBeGreaterThan(0);
1116
+ });
1117
+
1118
+ it('should handle numeric string comparisons', async () => {
1119
+ await vectorDB.upsert({
1120
+ indexName,
1121
+ vectors: [[1, 0.1, 0]],
1122
+ metadata: [{ numericString: '123' }],
1123
+ });
1124
+ const results = await vectorDB.query({
1125
+ indexName,
1126
+ queryVector: [1, 0, 0],
1127
+ filter: { numericString: { $gt: '100' } },
1128
+ });
1129
+ expect(results.length).toBeGreaterThan(0);
1130
+ expect(results[0]?.metadata?.numericString).toBe('123');
1131
+ });
1132
+ });
1133
+
1134
+ // Score Threshold Tests
1135
+ describe('Score Threshold', () => {
1136
+ it('should respect minimum score threshold', async () => {
1137
+ const results = await vectorDB.query({
1138
+ indexName,
1139
+ queryVector: [1, 0, 0],
1140
+ filter: { category: 'electronics' },
1141
+ includeVector: false,
1142
+ minScore: 0.9,
1143
+ });
1144
+ expect(results.length).toBeGreaterThan(0);
1145
+ results.forEach(result => {
1146
+ expect(result.score).toBeGreaterThan(0.9);
1147
+ });
1148
+ });
1149
+ });
1150
+
1151
+ describe('Edge Cases and Special Values', () => {
1152
+ // Additional Edge Cases
1153
+ it('should handle empty result sets with valid filters', async () => {
1154
+ const results = await vectorDB.query({
1155
+ indexName,
1156
+ queryVector: [1, 0, 0],
1157
+ filter: { price: { $gt: 1000 } },
1158
+ });
1159
+ expect(results).toHaveLength(0);
1160
+ });
1161
+
1162
+ it('should handle empty filter object', async () => {
1163
+ const results = await vectorDB.query({
1164
+ indexName,
1165
+ queryVector: [1, 0, 0],
1166
+ filter: {},
1167
+ });
1168
+ expect(results.length).toBeGreaterThan(0);
1169
+ });
1170
+
1171
+ it('should handle non-existent field', async () => {
1172
+ const results = await vectorDB.query({
1173
+ indexName,
1174
+ queryVector: [1, 0, 0],
1175
+ filter: { nonexistent: { $elemMatch: { $eq: 'value' } } },
1176
+ });
1177
+ expect(results).toHaveLength(0);
1178
+ });
1179
+
1180
+ it('should handle non-existent values', async () => {
1181
+ const results = await vectorDB.query({
1182
+ indexName,
1183
+ queryVector: [1, 0, 0],
1184
+ filter: { tags: { $elemMatch: { $eq: 'nonexistent-tag' } } },
1185
+ });
1186
+ expect(results).toHaveLength(0);
1187
+ });
1188
+ // Empty Conditions Tests
1189
+ it('should handle empty conditions in logical operators', async () => {
1190
+ const results = await vectorDB.query({
1191
+ indexName,
1192
+ queryVector: [1, 0, 0],
1193
+ filter: { $and: [], category: 'electronics' },
1194
+ });
1195
+ expect(results.length).toBeGreaterThan(0);
1196
+ results.forEach(result => {
1197
+ expect(result.metadata?.category).toBe('electronics');
1198
+ });
1199
+ });
1200
+
1201
+ it('should handle empty $and conditions', async () => {
1202
+ const results = await vectorDB.query({
1203
+ indexName,
1204
+ queryVector: [1, 0, 0],
1205
+ filter: { $and: [], category: 'electronics' },
1206
+ });
1207
+ expect(results.length).toBeGreaterThan(0);
1208
+ results.forEach(result => {
1209
+ expect(result.metadata?.category).toBe('electronics');
1210
+ });
1211
+ });
1212
+
1213
+ it('should handle empty $or conditions', async () => {
1214
+ const results = await vectorDB.query({
1215
+ indexName,
1216
+ queryVector: [1, 0, 0],
1217
+ filter: { $or: [], category: 'electronics' },
1218
+ });
1219
+ expect(results).toHaveLength(0);
1220
+ });
1221
+
1222
+ it('should handle empty $nor conditions', async () => {
1223
+ const results = await vectorDB.query({
1224
+ indexName,
1225
+ queryVector: [1, 0, 0],
1226
+ filter: { $nor: [], category: 'electronics' },
1227
+ });
1228
+ expect(results.length).toBeGreaterThan(0);
1229
+ results.forEach(result => {
1230
+ expect(result.metadata?.category).toBe('electronics');
1231
+ });
1232
+ });
1233
+
1234
+ it('should handle empty $not conditions', async () => {
1235
+ await expect(
1236
+ vectorDB.query({
1237
+ indexName,
1238
+ queryVector: [1, 0, 0],
1239
+ filter: { $not: {}, category: 'electronics' },
1240
+ }),
1241
+ ).rejects.toThrow('$not operator cannot be empty');
1242
+ });
1243
+
1244
+ it('should handle multiple empty logical operators', async () => {
1245
+ const results = await vectorDB.query({
1246
+ indexName,
1247
+ queryVector: [1, 0, 0],
1248
+ filter: { $and: [], $or: [], $nor: [], category: 'electronics' },
1249
+ });
1250
+ expect(results).toHaveLength(0);
1251
+ });
1252
+
1253
+ // Nested Field Tests
1254
+ it('should handle deeply nested metadata paths', async () => {
1255
+ await vectorDB.upsert({
1256
+ indexName,
1257
+ vectors: [[1, 0.1, 0]],
1258
+ metadata: [
1259
+ {
1260
+ level1: {
1261
+ level2: {
1262
+ level3: 'deep value',
1263
+ },
1264
+ },
1265
+ },
1266
+ ],
1267
+ });
1268
+
1269
+ const results = await vectorDB.query({
1270
+ indexName,
1271
+ queryVector: [1, 0, 0],
1272
+ filter: { 'level1.level2.level3': 'deep value' },
1273
+ });
1274
+ expect(results).toHaveLength(1);
1275
+ expect(results[0]?.metadata?.level1?.level2?.level3).toBe('deep value');
1276
+ });
1277
+
1278
+ it('should handle non-existent nested paths', async () => {
1279
+ const results = await vectorDB.query({
1280
+ indexName,
1281
+ queryVector: [1, 0, 0],
1282
+ filter: { 'nonexistent.path': 'value' },
1283
+ });
1284
+ expect(results).toHaveLength(0);
1285
+ });
1286
+
1287
+ // Score Threshold Tests
1288
+ it('should respect minimum score threshold', async () => {
1289
+ const results = await vectorDB.query({
1290
+ indexName,
1291
+ queryVector: [1, 0, 0],
1292
+ filter: { category: 'electronics' },
1293
+ includeVector: false,
1294
+ minScore: 0.9,
1295
+ });
1296
+ expect(results.length).toBeGreaterThan(0);
1297
+ results.forEach(result => {
1298
+ expect(result.score).toBeGreaterThan(0.9);
1299
+ });
1300
+ });
1301
+
1302
+ // Complex Nested Operators Test
1303
+ it('should handle deeply nested logical operators', async () => {
1304
+ const results = await vectorDB.query({
1305
+ indexName,
1306
+ queryVector: [1, 0, 0],
1307
+ filter: {
1308
+ $and: [
1309
+ { $or: [{ category: 'electronics' }, { $and: [{ category: 'books' }, { price: { $lt: 30 } }] }] },
1310
+ { $not: { $or: [{ active: false }, { price: { $gt: 100 } }] } },
1311
+ ],
1312
+ },
1313
+ });
1314
+ expect(results.length).toBeGreaterThan(0);
1315
+ results.forEach(result => {
1316
+ // First condition: electronics OR (books AND price < 30)
1317
+ const firstCondition =
1318
+ result.metadata?.category === 'electronics' ||
1319
+ (result.metadata?.category === 'books' && result.metadata?.price < 30);
1320
+
1321
+ // Second condition: NOT (active = false OR price > 100)
1322
+ const secondCondition = result.metadata?.active !== false && result.metadata?.price <= 100;
1323
+
1324
+ expect(firstCondition && secondCondition).toBe(true);
1325
+ });
1326
+ });
1327
+
1328
+ it('should throw error for invalid operator', async () => {
1329
+ await expect(
1330
+ vectorDB.query({
1331
+ indexName,
1332
+ queryVector: [1, 0, 0],
1333
+ filter: { price: { $invalid: 100 } },
1334
+ }),
1335
+ ).rejects.toThrow('Unsupported operator: $invalid');
1336
+ });
1337
+
1338
+ it('should handle multiple logical operators at root level', async () => {
1339
+ const results = await vectorDB.query({
1340
+ indexName,
1341
+ queryVector: [1, 0, 0],
1342
+ filter: {
1343
+ $and: [{ category: 'electronics' }],
1344
+ $or: [{ price: { $lt: 100 } }, { price: { $gt: 20 } }],
1345
+ $nor: [],
1346
+ },
1347
+ });
1348
+ expect(results.length).toBeGreaterThan(0);
1349
+ results.forEach(result => {
1350
+ expect(result.metadata?.category).toBe('electronics');
1351
+ expect(result.metadata?.price < 100 || result.metadata?.price > 20).toBe(true);
1352
+ });
1353
+ });
1354
+
1355
+ it('should handle non-array field with $elemMatch', async () => {
1356
+ // First insert a record with non-array field
1357
+ await vectorDB.upsert({
1358
+ indexName,
1359
+ vectors: [[1, 0.1, 0]],
1360
+ metadata: [{ tags: 'not-an-array' }],
1361
+ });
1362
+
1363
+ const results = await vectorDB.query({
1364
+ indexName,
1365
+ queryVector: [1, 0, 0],
1366
+ filter: { tags: { $elemMatch: { $eq: 'value' } } },
1367
+ });
1368
+ expect(results).toHaveLength(0); // Should return no results for non-array field
1369
+ });
1370
+ it('should handle undefined filter', async () => {
1371
+ const results1 = await vectorDB.query({
1372
+ indexName,
1373
+ queryVector: [1, 0, 0],
1374
+ filter: undefined,
1375
+ });
1376
+ const results2 = await vectorDB.query({
1377
+ indexName,
1378
+ queryVector: [1, 0, 0],
1379
+ });
1380
+ expect(results1).toEqual(results2);
1381
+ expect(results1.length).toBeGreaterThan(0);
1382
+ });
1383
+
1384
+ it('should handle empty object filter', async () => {
1385
+ const results = await vectorDB.query({
1386
+ indexName,
1387
+ queryVector: [1, 0, 0],
1388
+ filter: {},
1389
+ });
1390
+ const results2 = await vectorDB.query({
1391
+ indexName,
1392
+ queryVector: [1, 0, 0],
1393
+ });
1394
+ expect(results).toEqual(results2);
1395
+ expect(results.length).toBeGreaterThan(0);
1396
+ });
1397
+
1398
+ it('should handle null filter', async () => {
1399
+ const results = await vectorDB.query({
1400
+ indexName,
1401
+ queryVector: [1, 0, 0],
1402
+ filter: null,
1403
+ });
1404
+ const results2 = await vectorDB.query({
1405
+ indexName,
1406
+ queryVector: [1, 0, 0],
1407
+ });
1408
+ expect(results).toEqual(results2);
1409
+ expect(results.length).toBeGreaterThan(0);
1410
+ });
1411
+ });
1412
+
1413
+ // Regex Operator Tests
1414
+ // describe('Regex Operators', () => {
1415
+ // // // it('should handle $not with regex', async () => {
1416
+ // // // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1417
+ // // // category: { $not: { $regex: '^elect' } },
1418
+ // // // });
1419
+ // // // expect(results.length).toBeGreaterThan(0);
1420
+ // // // results.forEach(result => {
1421
+ // // // expect(result.metadata?.category).not.toMatch(/^elect/);
1422
+ // // // });
1423
+ // // // });
1424
+ // // Regex operator tests
1425
+ // // it('should handle basic regex patterns', async () => {
1426
+ // // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1427
+ // // category: { $regex: 'elect.*' },
1428
+ // // });
1429
+ // // expect(results).toHaveLength(2);
1430
+ // // });
1431
+ // // it('should handle case sensitivity', async () => {
1432
+ // // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1433
+ // // category: { $regex: 'ELECTRONICS' },
1434
+ // // });
1435
+ // // expect(results).toHaveLength(0); // Case sensitive by default
1436
+ // // const iResults = await vectorDB.query(indexName, [1, 0, 0], 10, {
1437
+ // // category: { $regex: 'ELECTRONICS', $options: 'i' },
1438
+ // // });
1439
+ // // expect(iResults).toHaveLength(2); // Case insensitive
1440
+ // // });
1441
+ // // it('should handle start/end anchors', async () => {
1442
+ // // const startResults = await vectorDB.query(indexName, [1, 0, 0], 10, {
1443
+ // // category: { $regex: '^elect' },
1444
+ // // });
1445
+ // // expect(startResults).toHaveLength(2);
1446
+ // // const endResults = await vectorDB.query(indexName, [1, 0, 0], 10, {
1447
+ // // category: { $regex: 'nics$' },
1448
+ // // });
1449
+ // // expect(endResults).toHaveLength(2);
1450
+ // // });
1451
+ // // it('should handle multiline flag', async () => {
1452
+ // // // First insert a record with multiline text
1453
+ // // await vectorDB.upsert(
1454
+ // // // indexName,
1455
+ // // // [[1, 0.1, 0]],
1456
+ // // // [
1457
+ // // // {
1458
+ // // // description: 'First line\nSecond line\nThird line',
1459
+ // // // },
1460
+ // // // ],
1461
+ // // // );
1462
+ // // // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1463
+ // // // description: { $regex: '^Second', $options: 'm' },
1464
+ // // // });
1465
+ // // // expect(results).toHaveLength(1);
1466
+ // // // });
1467
+ // // it('should handle multiline regex patterns', async () => {
1468
+ // // await vectorDB.upsert(
1469
+ // // // indexName,
1470
+ // // // [[1, 0.1, 0]],
1471
+ // // // [
1472
+ // // // {
1473
+ // // // description: 'First line\nSecond line\nThird line',
1474
+ // // // },
1475
+ // // // ],
1476
+ // // // );
1477
+ // // // // Test without multiline flag
1478
+ // // // const withoutM = await vectorDB.query(indexName, [1, 0, 0], 10, {
1479
+ // // // description: { $regex: '^Second' },
1480
+ // // // });
1481
+ // // // expect(withoutM).toHaveLength(0); // Won't match "Second" at start of line
1482
+ // // // Test with multiline flag
1483
+ // // // const withM = await vectorDB.query(indexName, [1, 0, 0], 10, {
1484
+ // // // description: { $regex: '^Second', $options: 'm' },
1485
+ // // // });
1486
+ // // // expect(withM).toHaveLength(1); // Will match "Second" at start of any line
1487
+ // // // });
1488
+ // // });
1489
+ // // it('should handle dotall flag', async () => {
1490
+ // // await vectorDB.upsert(
1491
+ // // indexName,
1492
+ // // [[1, 0.1, 0]],
1493
+ // // [
1494
+ // // {
1495
+ // // description: 'First\nSecond\nThird',
1496
+ // // },
1497
+ // // ],
1498
+ // // );
1499
+ // // // Test with a more complex pattern that demonstrates s flag behavior
1500
+ // // const withoutS = await vectorDB.query(indexName, [1, 0, 0], 10, {
1501
+ // // // description: { $regex: 'First[^\\n]*Third' },
1502
+ // // // });
1503
+ // // // expect(withoutS).toHaveLength(0); // Won't match across lines without s flag
1504
+ // // // const withS = await vectorDB.query(indexName, [1, 0, 0], 10, {
1505
+ // // // description: { $regex: 'First.*Third', $options: 's' },
1506
+ // // // });
1507
+ // // // expect(withS).toHaveLength(1); // Matches across lines with s flag
1508
+ // // // });
1509
+ // // });
1510
+ // // it('should handle extended flag', async () => {
1511
+ // // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1512
+ // // category: { $regex: 'elect # start\nronics # end', $options: 'x' },
1513
+ // // });
1514
+ // // expect(results).toHaveLength(2); // x flag allows comments and whitespace
1515
+ // // });
1516
+ // // it('should handle flag combinations', async () => {
1517
+ // // await vectorDB.upsert(
1518
+ // // indexName,
1519
+ // // [[1, 0.1, 0]],
1520
+ // // [
1521
+ // // {
1522
+ // // description: 'FIRST line\nSECOND line',
1523
+ // // },
1524
+ // // ],
1525
+ // // );
1526
+ // // // Test case-insensitive and multiline flags together
1527
+ // // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1528
+ // // // description: {
1529
+ // // // $regex: '^first',
1530
+ // // // $options: 'im', // Case-insensitive and multiline
1531
+ // // // },
1532
+ // // // });
1533
+ // // // expect(results).toHaveLength(1);
1534
+ // // // Test with second line
1535
+ // // const secondResults = await vectorDB.query(indexName, [1, 0, 0], 10, {
1536
+ // // // description: {
1537
+ // // // $regex: '^second',
1538
+ // // // $options: 'im',
1539
+ // // // },
1540
+ // // // });
1541
+ // // // expect(secondResults).toHaveLength(1);
1542
+ // // // });
1543
+ // it('should handle case insensitive flag (i)', async () => {
1544
+ // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1545
+ // category: { $regex: 'ELECTRONICS', $options: 'i' },
1546
+ // });
1547
+ // expect(results.length).toBeGreaterThan(0);
1548
+ // });
1549
+
1550
+ // it('should handle multiline flag (m)', async () => {
1551
+ // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1552
+ // description: { $regex: '^start', $options: 'm' },
1553
+ // });
1554
+ // expect(results.length).toBeGreaterThan(0);
1555
+ // });
1556
+
1557
+ // it('should handle extended flag (x)', async () => {
1558
+ // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1559
+ // category: {
1560
+ // $regex: 'elect # match electronics\nronics',
1561
+ // $options: 'x',
1562
+ // },
1563
+ // });
1564
+ // expect(results.length).toBeGreaterThan(0);
1565
+ // });
1566
+
1567
+ // it('should handle multiple flags', async () => {
1568
+ // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1569
+ // category: {
1570
+ // $regex: 'ELECTRONICS\nITEM',
1571
+ // $options: 'im',
1572
+ // },
1573
+ // });
1574
+ // expect(results.length).toBeGreaterThan(0);
1575
+ // });
1576
+ // it('should handle special regex characters as literals', async () => {
1577
+ // await vectorDB.upsert(
1578
+ // indexName,
1579
+ // [[1, 0.1, 0]],
1580
+ // [
1581
+ // {
1582
+ // special: 'text.with*special(chars)',
1583
+ // },
1584
+ // ],
1585
+ // );
1586
+
1587
+ // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1588
+ // special: 'text.with*special(chars)',
1589
+ // });
1590
+ // expect(results).toHaveLength(1); // Exact match, not regex
1591
+ // });
1592
+ // it('should handle $not with $regex operator', async () => {
1593
+ // const results = await vectorDB.query(indexName, [1, 0, 0], 10, {
1594
+ // category: { $not: { $regex: '^elect' } },
1595
+ // });
1596
+ // expect(results.length).toBeGreaterThan(0);
1597
+ // results.forEach(result => {
1598
+ // expect(result.metadata?.category).not.toMatch(/^elect/);
1599
+ // });
1600
+ // });
1601
+
1602
+ // });
1603
+ });
1604
+
1605
+ describe('Deprecation Warnings', () => {
1606
+ const indexName = 'testdeprecationwarnings';
1607
+
1608
+ const indexName2 = 'testdeprecationwarnings2';
1609
+
1610
+ let warnSpy;
1611
+
1612
+ beforeAll(async () => {
1613
+ await vectorDB.createIndex({ indexName: indexName, dimension: 3 });
1614
+ });
1615
+
1616
+ afterAll(async () => {
1617
+ await vectorDB.deleteIndex(indexName);
1618
+ await vectorDB.deleteIndex(indexName2);
1619
+ });
1620
+
1621
+ beforeEach(async () => {
1622
+ warnSpy = vi.spyOn(vectorDB['logger'], 'warn');
1623
+ });
1624
+
1625
+ afterEach(async () => {
1626
+ warnSpy.mockRestore();
1627
+ await vectorDB.deleteIndex(indexName2);
1628
+ });
1629
+
1630
+ it('should show deprecation warning when using individual args for createIndex', async () => {
1631
+ await vectorDB.createIndex(indexName2, 3, 'cosine');
1632
+
1633
+ expect(warnSpy).toHaveBeenCalledWith(
1634
+ expect.stringContaining('Deprecation Warning: Passing individual arguments to createIndex() is deprecated'),
1635
+ );
1636
+ });
1637
+
1638
+ it('should show deprecation warning when using individual args for upsert', async () => {
1639
+ await vectorDB.upsert(indexName, [[1, 2, 3]], [{ test: 'data' }]);
1640
+
1641
+ expect(warnSpy).toHaveBeenCalledWith(
1642
+ expect.stringContaining('Deprecation Warning: Passing individual arguments to upsert() is deprecated'),
1643
+ );
1644
+ });
1645
+
1646
+ it('should show deprecation warning when using individual args for query', async () => {
1647
+ await vectorDB.query(indexName, [1, 2, 3], 5);
1648
+
1649
+ expect(warnSpy).toHaveBeenCalledWith(
1650
+ expect.stringContaining('Deprecation Warning: Passing individual arguments to query() is deprecated'),
1651
+ );
1652
+ });
1653
+
1654
+ it('should not show deprecation warning when using object param for query', async () => {
1655
+ await vectorDB.query({
1656
+ indexName,
1657
+ queryVector: [1, 2, 3],
1658
+ topK: 5,
1659
+ });
1660
+
1661
+ expect(warnSpy).not.toHaveBeenCalled();
1662
+ });
1663
+
1664
+ it('should not show deprecation warning when using object param for createIndex', async () => {
1665
+ await vectorDB.createIndex({
1666
+ indexName: indexName2,
1667
+ dimension: 3,
1668
+ metric: 'cosine',
1669
+ });
1670
+
1671
+ expect(warnSpy).not.toHaveBeenCalled();
1672
+ });
1673
+
1674
+ it('should not show deprecation warning when using object param for upsert', async () => {
1675
+ await vectorDB.upsert({
1676
+ indexName,
1677
+ vectors: [[1, 2, 3]],
1678
+ metadata: [{ test: 'data' }],
1679
+ });
1680
+
1681
+ expect(warnSpy).not.toHaveBeenCalled();
1682
+ });
1683
+
1684
+ it('should maintain backward compatibility with individual args', async () => {
1685
+ // Query
1686
+ const queryResults = await vectorDB.query(indexName, [1, 2, 3], 5);
1687
+ expect(Array.isArray(queryResults)).toBe(true);
1688
+
1689
+ // CreateIndex
1690
+ await expect(vectorDB.createIndex(indexName2, 3, 'cosine')).resolves.not.toThrow();
1691
+
1692
+ // Upsert
1693
+ const upsertResults = await vectorDB.upsert({
1694
+ indexName,
1695
+ vectors: [[1, 2, 3]],
1696
+ metadata: [{ test: 'data' }],
1697
+ });
1698
+ expect(Array.isArray(upsertResults)).toBe(true);
1699
+ expect(upsertResults).toHaveLength(1);
1700
+ });
1701
+ });
1702
+ });