Repository navigation
Expand file tree
/
Copy pathdata-engine.test.ts
More file actions
1346 lines (1160 loc) · 45.3 KB
/
Copy pathdata-engine.test.ts
File metadata and controls
1346 lines (1160 loc) · 45.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect } from 'vitest';
import { z } from 'zod';
import {
DataEngineFilterSchema,
DataEngineSortSchema,
DataEngineQueryOptionsSchema,
DataEngineInsertOptionsSchema,
DataEngineUpdateOptionsSchema,
DataEngineDeleteOptionsSchema,
DataEngineAggregateOptionsSchema,
DataEngineCountOptionsSchema,
EngineQueryOptionsSchema,
EngineUpdateOptionsSchema,
EngineDeleteOptionsSchema,
EngineAggregateOptionsSchema,
EngineCountOptionsSchema,
DataEngineFindRequestSchema,
DataEngineFindOneRequestSchema,
DataEngineInsertRequestSchema,
DataEngineUpdateRequestSchema,
DataEngineDeleteRequestSchema,
DataEngineCountRequestSchema,
DataEngineAggregateRequestSchema,
DataEngineExecuteRequestSchema,
DataEngineVectorFindRequestSchema,
DataEngineRequestSchema,
DroppedFieldsEventSchema,
QueryTransportParamsSchema,
} from './data-engine.zod';
import { QuerySchema } from './query.zod';
describe('DataEngineFilterSchema', () => {
it('should accept simple key-value filter', () => {
const filter = DataEngineFilterSchema.parse({
status: 'active',
category: 'premium',
});
expect(filter.status).toBe('active');
expect(filter.category).toBe('premium');
});
it('should accept complex filter expressions', () => {
const filter = DataEngineFilterSchema.parse({
operator: 'and',
conditions: [
{ field: 'status', operator: 'eq', value: 'active' },
{ field: 'revenue', operator: 'gt', value: 100000 },
],
});
expect(filter.operator).toBe('and');
expect(filter.conditions).toHaveLength(2);
});
it('should accept nested filters', () => {
const filter = DataEngineFilterSchema.parse({
'address.city': 'New York',
'address.state': 'NY',
});
expect(filter['address.city']).toBe('New York');
});
});
describe('DataEngineSortSchema', () => {
it('should accept string-based sort', () => {
const sort = DataEngineSortSchema.parse({
name: 'asc',
created_at: 'desc',
});
expect(sort.name).toBe('asc');
expect(sort.created_at).toBe('desc');
});
it('should accept number-based sort', () => {
const sort = DataEngineSortSchema.parse({
name: 1,
created_at: -1,
});
expect(sort.name).toBe(1);
expect(sort.created_at).toBe(-1);
});
it('should accept array-based sort', () => {
const sort = DataEngineSortSchema.parse([
{ field: 'name', order: 'asc' },
{ field: 'created_at', order: 'desc' },
]);
expect(sort).toHaveLength(2);
});
});
describe('DataEngineQueryOptionsSchema', () => {
it('should accept minimal options', () => {
const options = DataEngineQueryOptionsSchema.parse({});
expect(options).toBeDefined();
});
it('should accept filter option', () => {
const options = DataEngineQueryOptionsSchema.parse({
filter: { status: 'active' },
});
expect(options.filter).toBeDefined();
});
it('should accept select option', () => {
const options = DataEngineQueryOptionsSchema.parse({
select: ['id', 'name', 'email'],
});
expect(options.select).toHaveLength(3);
});
it('should accept sort option', () => {
const options = DataEngineQueryOptionsSchema.parse({
sort: { name: 'asc' },
});
expect(options.sort).toBeDefined();
});
it('should accept pagination with limit/skip', () => {
const options = DataEngineQueryOptionsSchema.parse({
limit: 10,
skip: 20,
});
expect(options.limit).toBe(10);
expect(options.skip).toBe(20);
});
it('should accept pagination with top', () => {
const options = DataEngineQueryOptionsSchema.parse({
top: 25,
skip: 50,
});
expect(options.top).toBe(25);
expect(options.skip).toBe(50);
});
it('should accept populate option', () => {
const options = DataEngineQueryOptionsSchema.parse({
populate: ['owner', 'contacts'],
});
expect(options.populate).toHaveLength(2);
});
it('should accept complete query options', () => {
const options = DataEngineQueryOptionsSchema.parse({
filter: { status: 'active' },
select: ['id', 'name', 'email'],
sort: { name: 'asc' },
limit: 50,
skip: 0,
populate: ['owner'],
});
expect(options.filter).toBeDefined();
expect(options.select).toHaveLength(3);
expect(options.limit).toBe(50);
expect(options.populate).toHaveLength(1);
});
});
describe('DataEngineInsertOptionsSchema', () => {
it('should accept returning option', () => {
const options = DataEngineInsertOptionsSchema.parse({
returning: true,
});
expect(options.returning).toBe(true);
});
it('should accept custom returning', () => {
const options = DataEngineInsertOptionsSchema.parse({
returning: false,
});
expect(options.returning).toBe(false);
});
});
describe('DataEngineUpdateOptionsSchema', () => {
it('should accept empty options', () => {
const options = DataEngineUpdateOptionsSchema.parse({});
expect(options).toBeDefined();
});
it('REJECTS the retired `upsert` flag, with the fix in the message', () => {
// Tombstoned, not deleted: the schema is not `.strict()`, so a plain
// deletion would silently strip the key — the exact accepted-and-dropped
// silence #8057 removes. `retiredKey()` makes the removal audible.
expect(() =>
DataEngineUpdateOptionsSchema.parse({
upsert: true,
}),
).toThrow(/`update\.options\.upsert` was removed .*ADR-0049.*Delete the key/s);
});
it('parses cleanly without the retired flag, and never produces the key', () => {
const options = DataEngineUpdateOptionsSchema.parse({ multi: true });
expect(options.multi).toBe(true);
expect(options).not.toHaveProperty('upsert');
});
it('should accept multi-update', () => {
const options = DataEngineUpdateOptionsSchema.parse({
multi: true,
returning: true,
});
expect(options.multi).toBe(true);
expect(options.returning).toBe(true);
});
it('should accept update with filter', () => {
const options = DataEngineUpdateOptionsSchema.parse({
filter: { status: 'inactive' },
multi: true,
});
expect(options.filter).toBeDefined();
expect(options.multi).toBe(true);
});
});
describe('DataEngineDeleteOptionsSchema', () => {
it('should accept empty options', () => {
const options = DataEngineDeleteOptionsSchema.parse({});
expect(options).toBeDefined();
});
it('should accept multi-delete', () => {
const options = DataEngineDeleteOptionsSchema.parse({
multi: true,
});
expect(options.multi).toBe(true);
});
it('should accept delete with filter', () => {
const options = DataEngineDeleteOptionsSchema.parse({
filter: { status: 'archived' },
multi: true,
});
expect(options.filter).toBeDefined();
});
});
describe('DataEngineAggregateOptionsSchema', () => {
it('should accept group by', () => {
const options = DataEngineAggregateOptionsSchema.parse({
groupBy: ['status', 'category'],
});
expect(options.groupBy).toHaveLength(2);
});
it('should accept aggregations', () => {
const options = DataEngineAggregateOptionsSchema.parse({
aggregations: [
{ field: 'revenue', method: 'sum', alias: 'total_revenue' },
{ field: 'id', method: 'count', alias: 'total_count' },
],
});
expect(options.aggregations).toHaveLength(2);
});
it('should accept all aggregation methods', () => {
const methods = ['count', 'sum', 'avg', 'min', 'max', 'count_distinct'] as const;
methods.forEach(method => {
const options = DataEngineAggregateOptionsSchema.parse({
aggregations: [
{ field: 'value', method },
],
});
expect(options.aggregations![0].method).toBe(method);
});
});
it('should accept aggregation with filter and groupBy', () => {
const options = DataEngineAggregateOptionsSchema.parse({
filter: { status: 'active' },
groupBy: ['category'],
aggregations: [
{ field: 'revenue', method: 'sum' },
{ field: 'revenue', method: 'avg' },
],
});
expect(options.filter).toBeDefined();
expect(options.groupBy).toHaveLength(1);
expect(options.aggregations).toHaveLength(2);
});
});
describe('DataEngineCountOptionsSchema', () => {
it('should accept empty options', () => {
const options = DataEngineCountOptionsSchema.parse({});
expect(options).toBeDefined();
});
it('should accept count with filter', () => {
const options = DataEngineCountOptionsSchema.parse({
filter: { status: 'active' },
});
expect(options.filter).toBeDefined();
});
});
// ==========================================================================
// NEW: QueryAST-aligned Engine Options Schemas
// ==========================================================================
describe('EngineQueryOptionsSchema', () => {
it('should accept minimal options', () => {
const options = EngineQueryOptionsSchema.parse({});
expect(options).toBeDefined();
});
it('should accept where (standard QueryAST filter)', () => {
const options = EngineQueryOptionsSchema.parse({
where: { status: 'active' },
});
expect(options.where).toBeDefined();
});
it('should accept fields (standard QueryAST select)', () => {
const options = EngineQueryOptionsSchema.parse({
fields: ['id', 'name', 'email'],
});
expect(options.fields).toHaveLength(3);
});
it('should accept orderBy (standard QueryAST sort)', () => {
const options = EngineQueryOptionsSchema.parse({
orderBy: [{ field: 'name', order: 'asc' }],
});
expect(options.orderBy).toHaveLength(1);
});
it('should accept pagination with limit/offset', () => {
const options = EngineQueryOptionsSchema.parse({
limit: 10,
offset: 20,
});
expect(options.limit).toBe(10);
expect(options.offset).toBe(20);
});
it('should accept expand (standard QueryAST relation loading)', () => {
const options = EngineQueryOptionsSchema.parse({
expand: {
owner: { object: 'user', fields: ['name', 'email'] },
},
});
expect(options.expand).toBeDefined();
expect(options.expand!.owner.object).toBe('user');
});
it('should accept complete QueryAST-style options', () => {
const options = EngineQueryOptionsSchema.parse({
where: { status: 'active' },
fields: ['id', 'name', 'email'],
orderBy: [{ field: 'name', order: 'asc' }],
limit: 50,
offset: 0,
expand: { owner: { object: 'user' } },
});
expect(options.where).toBeDefined();
expect(options.fields).toHaveLength(3);
expect(options.limit).toBe(50);
expect(options.expand!.owner.object).toBe('user');
});
// ── `search`: both spellings, canonical one first (#7178) ────────────
it('accepts the BARE query string — the canonical ADR-0061 D1 spelling', () => {
// This is the pin that was RED before #7178: the schema declared only the
// structured form, so the spelling the executor actually serves, every
// surface sends, and `BaseQuerySchema.search` already accepts was rejected
// here — and every engine caller wanting it had to `as any` the whole query.
const options = EngineQueryOptionsSchema.parse({ search: 'acme corp' });
expect(options.search).toBe('acme corp');
});
it('still accepts the structured FullTextSearch form — the Tier-2 knobs', () => {
const options = EngineQueryOptionsSchema.parse({
search: { query: 'acme corp', fields: ['name', 'industry'] },
});
expect(typeof options.search).toBe('object');
expect((options.search as { query: string }).query).toBe('acme corp');
expect((options.search as { fields?: string[] }).fields).toEqual(['name', 'industry']);
});
it('accepts search alongside searchFields, in both spellings', () => {
expect(EngineQueryOptionsSchema.parse({
search: 'acme', searchFields: ['name'],
}).searchFields).toEqual(['name']);
expect(EngineQueryOptionsSchema.parse({
search: { query: 'acme' }, searchFields: ['name'],
}).searchFields).toEqual(['name']);
});
it('rejects a search that is neither a string nor a FullTextSearch', () => {
// The union widens the accept face by exactly one spelling — it does not
// open the key to anything.
expect(() => EngineQueryOptionsSchema.parse({ search: 42 })).toThrow();
expect(() => EngineQueryOptionsSchema.parse({ search: { fields: ['name'] } })).toThrow();
});
it('matches BaseQuerySchema.search — the two sibling schemas agree', () => {
// The whole point of the repair: what QuerySchema accepts for `search`,
// the engine options schema accepts too. `DriverQuery` (= Omit<QueryAST,
// 'object'>) is assignable to `EngineQueryOptionsParsed` again because of
// this, which is what lets `database-loader`'s engine branch drop its casts.
for (const search of ['acme corp', { query: 'acme corp', fields: ['name'] }]) {
expect(QuerySchema.parse({ object: 'crm_account', search })).toBeDefined();
expect(EngineQueryOptionsSchema.parse({ search })).toBeDefined();
}
});
it('rejects the removed cursor/distinct keys with the query.* prescriptions', () => {
expect(() => EngineQueryOptionsSchema.parse({ cursor: { id: 'x' } }))
.toThrow(/query\.cursor.*removed/s);
expect(() => EngineQueryOptionsSchema.parse({ distinct: true }))
.toThrow(/query\.distinct.*removed/s);
});
it('should accept context', () => {
const options = EngineQueryOptionsSchema.parse({
where: { status: 'active' },
context: { userId: 'user_123', tenantId: 'org_456' },
});
expect(options.context?.userId).toBe('user_123');
});
});
describe('EngineUpdateOptionsSchema', () => {
it('should accept where instead of filter', () => {
const options = EngineUpdateOptionsSchema.parse({
where: { status: 'inactive' },
multi: true,
});
expect(options.where).toBeDefined();
expect(options.multi).toBe(true);
});
it('REJECTS the retired `upsert` flag with the same tombstone as the legacy schema', () => {
expect(() =>
EngineUpdateOptionsSchema.parse({
where: { status: 'inactive' },
upsert: true,
}),
).toThrow(/`update\.options\.upsert` was removed .*ADR-0049.*Delete the key/s);
});
it('never produces the retired key on a clean parse', () => {
const options = EngineUpdateOptionsSchema.parse({ where: { status: 'inactive' } });
expect(options).not.toHaveProperty('upsert');
});
});
describe('EngineDeleteOptionsSchema', () => {
it('should accept where instead of filter', () => {
const options = EngineDeleteOptionsSchema.parse({
where: { status: 'archived' },
multi: true,
});
expect(options.where).toBeDefined();
expect(options.multi).toBe(true);
});
});
describe('EngineAggregateOptionsSchema', () => {
it('should accept where and standard AggregationNodeSchema', () => {
const options = EngineAggregateOptionsSchema.parse({
where: { status: 'active' },
groupBy: ['category'],
aggregations: [
{ function: 'sum', field: 'revenue', alias: 'total_revenue' },
{ function: 'avg', field: 'revenue', alias: 'avg_revenue' },
],
});
expect(options.where).toBeDefined();
expect(options.groupBy).toHaveLength(1);
expect(options.aggregations).toHaveLength(2);
});
// #8032 — `groupBy` is the standard GroupByNodeSchema union, the same
// vocabulary as `QuerySchema.groupBy`. The engine has always read the
// structured `{ field, dateGranularity }` bucket form (date bucketing,
// and the credential-aggregation guard walks it); the declaration used to
// say `string[]`, so every correct caller had to cast. These pin the
// declaration to the enforced contract, in both directions.
it('accepts structured { field, dateGranularity, alias } groupBy buckets', () => {
const result = EngineAggregateOptionsSchema.safeParse({
groupBy: ['region', { field: 'closed_at', dateGranularity: 'quarter' }, { field: 'owner_id', alias: 'owner' }],
aggregations: [{ function: 'sum', field: 'amount', alias: 'total' }],
timezone: 'Asia/Shanghai',
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.groupBy).toEqual([
'region',
{ field: 'closed_at', dateGranularity: 'quarter' },
{ field: 'owner_id', alias: 'owner' },
]);
}
});
it('keeps the plain-string groupBy form validating byte-identically', () => {
const result = EngineAggregateOptionsSchema.safeParse({
groupBy: ['status', 'category'],
aggregations: [{ function: 'count', alias: 'n' }],
});
expect(result.success).toBe(true);
if (result.success) expect(result.data.groupBy).toEqual(['status', 'category']);
});
it('rejects a groupBy bucket whose dateGranularity is not in the vocabulary', () => {
const result = EngineAggregateOptionsSchema.safeParse({
groupBy: [{ field: 'closed_at', dateGranularity: 'decade' }],
aggregations: [{ function: 'count', alias: 'n' }],
});
expect(result.success).toBe(false);
});
it('rejects a groupBy bucket that names no field', () => {
const result = EngineAggregateOptionsSchema.safeParse({
groupBy: [{ dateGranularity: 'day' }],
aggregations: [{ function: 'count', alias: 'n' }],
});
expect(result.success).toBe(false);
});
// ADR-0061 `search` on the aggregate verb. `QuerySchema.search` sits beside
// `groupBy` / `aggregations` with no carve-out, so the aggregate options
// declare the two keys the query options declare, and the engine expands them
// through the same expander. Before, a parse STRIPPED them (this schema is not
// strict) — the declaration-side half of a grouped answer under a search
// coming back unsearched.
it('keeps `search` (both forms) and `searchFields` through a parse', () => {
const bare = EngineAggregateOptionsSchema.parse({
groupBy: ['business_unit'],
aggregations: [{ function: 'count', alias: 'count' }],
search: 'harbour',
searchFields: ['business_unit'],
});
expect(bare.search).toBe('harbour');
expect(bare.searchFields).toEqual(['business_unit']);
const structured = EngineAggregateOptionsSchema.parse({
aggregations: [{ function: 'count', alias: 'count' }],
search: { query: 'harbour', fields: ['business_unit'] },
});
expect(structured.search).toMatchObject({ query: 'harbour', fields: ['business_unit'] });
});
it('refuses a `search` / `searchFields` value the query options refuse', () => {
for (const bad of [{ search: 42 }, { searchFields: 'business_unit' }, { searchFields: [1] }]) {
const agg = EngineAggregateOptionsSchema.safeParse({ aggregations: [{ function: 'count', alias: 'n' }], ...bad });
const find = EngineQueryOptionsSchema.safeParse(bad);
expect(agg.success, JSON.stringify(bad)).toBe(false);
expect(find.success, JSON.stringify(bad)).toBe(false);
}
});
it('declares `search` / `searchFields` exactly as EngineQueryOptionsSchema does — one contract on both read verbs', () => {
const aggShape = (EngineAggregateOptionsSchema as unknown as z.ZodObject<z.ZodRawShape>).shape;
const findShape = (EngineQueryOptionsSchema as unknown as z.ZodObject<z.ZodRawShape>).shape;
for (const key of ['search', 'searchFields'] as const) {
expect(aggShape[key], key).toBeDefined();
expect(z.toJSONSchema(aggShape[key] as z.ZodType, { io: 'input' }), key)
.toEqual(z.toJSONSchema(findShape[key] as z.ZodType, { io: 'input' }));
}
});
});
describe('EngineCountOptionsSchema', () => {
it('should accept where instead of filter', () => {
const options = EngineCountOptionsSchema.parse({
where: { status: 'active' },
});
expect(options.where).toBeDefined();
});
// #22790 — a count under a search counts the searched rows (a paged list's
// `total`), so the count options declare the pair exactly as `find` does.
it('keeps `search` / `searchFields`, declared exactly as EngineQueryOptionsSchema declares them', () => {
const parsed = EngineCountOptionsSchema.parse({ search: { query: 'acme', fields: ['name'] }, searchFields: ['name'] });
expect(parsed.search).toMatchObject({ query: 'acme', fields: ['name'] });
expect(parsed.searchFields).toEqual(['name']);
const countShape = (EngineCountOptionsSchema as unknown as z.ZodObject<z.ZodRawShape>).shape;
const findShape = (EngineQueryOptionsSchema as unknown as z.ZodObject<z.ZodRawShape>).shape;
for (const key of ['search', 'searchFields'] as const) {
expect(z.toJSONSchema(countShape[key] as z.ZodType, { io: 'input' }), key)
.toEqual(z.toJSONSchema(findShape[key] as z.ZodType, { io: 'input' }));
}
});
});
describe('DataEngineFindRequestSchema', () => {
it('should accept minimal find request', () => {
const request = DataEngineFindRequestSchema.parse({
method: 'find',
object: 'account',
});
expect(request.method).toBe('find');
expect(request.object).toBe('account');
});
it('should accept find with query', () => {
const request = DataEngineFindRequestSchema.parse({
method: 'find',
object: 'account',
query: {
where: { status: 'active' },
limit: 10,
},
});
expect(request.query?.where).toBeDefined();
});
it('folds legacy params into their canonical keys and drops the aliases', () => {
const request = DataEngineFindRequestSchema.parse({
method: 'find',
object: 'account',
query: {
filter: { status: 'active' },
select: ['id', 'name'],
sort: [{ field: 'name', order: 'asc' }],
skip: 10,
populate: ['owner'],
top: 5,
},
});
// Legacy input stays accepted (backward compat) — but the parsed output
// speaks canonical QueryAST only, so no consumer re-implements precedence.
expect(request.query?.where).toEqual({ status: 'active' });
expect(request.query?.fields).toEqual(['id', 'name']);
expect(request.query?.orderBy).toEqual([{ field: 'name', order: 'asc' }]);
expect(request.query?.offset).toBe(10);
expect(request.query?.expand).toEqual({ owner: { object: 'owner' } });
expect(request.query?.limit).toBe(5);
for (const alias of ['filter', 'select', 'sort', 'skip', 'populate', 'top']) {
expect(request.query && alias in request.query).toBe(false);
}
});
it('lowers the record sort spellings while folding', () => {
const named = DataEngineFindRequestSchema.parse({
method: 'find',
object: 'account',
query: { sort: { name: 'desc' } },
});
expect(named.query?.orderBy).toEqual([{ field: 'name', order: 'desc' }]);
const numeric = DataEngineFindRequestSchema.parse({
method: 'find',
object: 'account',
query: { sort: { created_at: 1, name: -1 } },
});
expect(numeric.query?.orderBy).toEqual([
{ field: 'created_at', order: 'asc' },
{ field: 'name', order: 'desc' },
]);
});
it('tolerates a redundant identical spelling, refuses a conflicting one', () => {
const redundant = DataEngineFindRequestSchema.parse({
method: 'find',
object: 'account',
query: { fields: ['id'], select: ['id'] },
});
expect(redundant.query?.fields).toEqual(['id']);
expect(redundant.query && 'select' in redundant.query).toBe(false);
const conflict = DataEngineFindRequestSchema.safeParse({
method: 'find',
object: 'account',
query: { fields: ['id'], select: ['name'] },
});
expect(conflict.success).toBe(false);
if (!conflict.success) {
expect(conflict.error.issues[0].message).toMatch(/Send exactly one/);
}
});
it('refuses a conflicting value on every alias pair', () => {
const cases: Array<Record<string, unknown>> = [
{ filter: { a: 1 }, where: { b: 2 } },
{ select: ['a'], fields: ['b'] },
{ sort: [{ field: 'a', order: 'asc' }], orderBy: [{ field: 'b', order: 'asc' }] },
{ skip: 1, offset: 2 },
{ populate: ['a'], expand: { b: { object: 'b' } } },
{ top: 1, limit: 3 },
];
for (const query of cases) {
const result = DataEngineFindRequestSchema.safeParse({ method: 'find', object: 'account', query });
expect(result.success, `${Object.keys(query).join('+')} must be refused`).toBe(false);
}
});
});
describe('DataEngineFindOneRequestSchema', () => {
it('should accept find one request', () => {
const request = DataEngineFindOneRequestSchema.parse({
method: 'findOne',
object: 'account',
query: {
where: { id: '123' },
},
});
expect(request.method).toBe('findOne');
});
it('folds legacy params into canonical keys (backward compat input)', () => {
const request = DataEngineFindOneRequestSchema.parse({
method: 'findOne',
object: 'account',
query: {
filter: { id: '123' },
select: ['id', 'name'],
},
});
expect(request.query?.where).toEqual({ id: '123' });
expect(request.query?.fields).toEqual(['id', 'name']);
expect(request.query && 'filter' in request.query).toBe(false);
});
});
describe('DataEngineInsertRequestSchema', () => {
it('should accept single record insert', () => {
const request = DataEngineInsertRequestSchema.parse({
method: 'insert',
object: 'account',
data: { name: 'Test Account' },
});
expect(request.method).toBe('insert');
expect(request.data.name).toBe('Test Account');
});
it('should accept multiple records insert', () => {
const request = DataEngineInsertRequestSchema.parse({
method: 'insert',
object: 'account',
data: [
{ name: 'Account 1' },
{ name: 'Account 2' },
],
});
expect(request.data).toHaveLength(2);
});
it('should accept insert with options', () => {
const request = DataEngineInsertRequestSchema.parse({
method: 'insert',
object: 'account',
data: { name: 'Test' },
options: {
returning: true,
},
});
expect(request.options?.returning).toBe(true);
});
});
describe('DataEngineUpdateRequestSchema', () => {
it('should accept update with id', () => {
const request = DataEngineUpdateRequestSchema.parse({
method: 'update',
object: 'account',
id: '123',
data: { status: 'active' },
});
expect(request.id).toBe('123');
});
it('folds a legacy filter into where (backward compat input)', () => {
const request = DataEngineUpdateRequestSchema.parse({
method: 'update',
object: 'account',
data: { status: 'active' },
options: {
filter: { category: 'premium' },
multi: true,
},
});
expect(request.options?.where).toEqual({ category: 'premium' });
expect(request.options && 'filter' in request.options).toBe(false);
expect(request.options?.multi).toBe(true);
});
it('should accept update with new where (preferred)', () => {
const request = DataEngineUpdateRequestSchema.parse({
method: 'update',
object: 'account',
data: { status: 'active' },
options: {
where: { category: 'premium' },
multi: true,
},
});
expect(request.options?.where).toEqual({ category: 'premium' });
expect(request.options?.multi).toBe(true);
});
});
describe('DataEngineDeleteRequestSchema', () => {
it('should accept delete with id', () => {
const request = DataEngineDeleteRequestSchema.parse({
method: 'delete',
object: 'account',
id: '123',
});
expect(request.id).toBe('123');
});
it('folds a legacy filter into where (backward compat input)', () => {
const request = DataEngineDeleteRequestSchema.parse({
method: 'delete',
object: 'account',
options: {
filter: { status: 'archived' },
multi: true,
},
});
expect(request.options?.where).toEqual({ status: 'archived' });
expect(request.options && 'filter' in request.options).toBe(false);
expect(request.options?.multi).toBe(true);
});
it('should accept delete with new where (preferred)', () => {
const request = DataEngineDeleteRequestSchema.parse({
method: 'delete',
object: 'account',
options: {
where: { status: 'archived' },
multi: true,
},
});
expect(request.options?.where).toEqual({ status: 'archived' });
expect(request.options?.multi).toBe(true);
});
});
describe('DataEngineCountRequestSchema', () => {
it('should accept count request', () => {
const request = DataEngineCountRequestSchema.parse({
method: 'count',
object: 'account',
});
expect(request.method).toBe('count');
});
it('should accept count with where (preferred)', () => {
const request = DataEngineCountRequestSchema.parse({
method: 'count',
object: 'account',
query: {
where: { status: 'active' },
},
});
expect(request.query?.where).toBeDefined();
});
it('folds a legacy filter into where (backward compat input)', () => {
const request = DataEngineCountRequestSchema.parse({
method: 'count',
object: 'account',
query: {
filter: { status: 'active' },
},
});
expect(request.query?.where).toEqual({ status: 'active' });
expect(request.query && 'filter' in request.query).toBe(false);
});
it('refuses filter + where with different values', () => {
const result = DataEngineCountRequestSchema.safeParse({
method: 'count',
object: 'account',
query: { filter: { a: 1 }, where: { b: 2 } },
});
expect(result.success).toBe(false);
});
});
describe('DataEngineAggregateRequestSchema', () => {
it('should accept aggregate request', () => {
const request = DataEngineAggregateRequestSchema.parse({
method: 'aggregate',
object: 'account',
query: {
groupBy: ['status'],
aggregations: [
{ function: 'sum', field: 'revenue', alias: 'total_revenue' },
],
},
});
expect(request.method).toBe('aggregate');
});
});
describe('DataEngineExecuteRequestSchema', () => {
it('should accept execute with SQL command', () => {
const request = DataEngineExecuteRequestSchema.parse({
method: 'execute',
command: 'SELECT * FROM accounts WHERE status = ?',
});
expect(request.method).toBe('execute');
});
it('should accept execute with object command', () => {
const request = DataEngineExecuteRequestSchema.parse({
method: 'execute',
command: {
operation: 'custom_query',
params: { limit: 10 },
},
});
expect(request.command.operation).toBe('custom_query');
});
it('should accept execute with options', () => {
const request = DataEngineExecuteRequestSchema.parse({
method: 'execute',
command: 'CUSTOM QUERY',
options: {
timeout: 5000,
},
});
expect(request.options?.timeout).toBe(5000);
});
});
describe('DataEngineVectorFindRequestSchema', () => {
it('should accept vector find request', () => {
const request = DataEngineVectorFindRequestSchema.parse({
method: 'vectorFind',
object: 'documents',
vector: [0.1, 0.2, 0.3, 0.4],
});
expect(request.method).toBe('vectorFind');
expect(request.vector).toHaveLength(4);
});
it('should accept custom limit', () => {
const request = DataEngineVectorFindRequestSchema.parse({
method: 'vectorFind',
object: 'documents',
vector: [0.1, 0.2, 0.3],
limit: 10,
});
expect(request.limit).toBe(10);
});
it('should accept vector find with filter', () => {
const request = DataEngineVectorFindRequestSchema.parse({
method: 'vectorFind',
object: 'documents',