@voidhash/mimic 0.0.6 → 0.0.8

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.
@@ -554,6 +554,352 @@ describe("StructPrimitive", () => {
554
554
  expect(operations[0]!.path.toTokens()).toEqual(["users", "0", "name"]);
555
555
  });
556
556
  });
557
+ describe("extend", () => {
558
+ it("extends struct with additional fields", () => {
559
+ const basePrimitive = Primitive.Struct({
560
+ name: Primitive.String().required(),
561
+ });
562
+
563
+ const extendedPrimitive = basePrimitive.extend({
564
+ email: Primitive.String().required(),
565
+ age: Primitive.Number(),
566
+ });
567
+
568
+ // Verify fields exist
569
+ expect(extendedPrimitive.fields).toHaveProperty("name");
570
+ expect(extendedPrimitive.fields).toHaveProperty("email");
571
+ expect(extendedPrimitive.fields).toHaveProperty("age");
572
+ });
573
+
574
+ it("extended struct generates correct operations", () => {
575
+ const operations: Operation.Operation<any, any, any>[] = [];
576
+ const env = ProxyEnvironment.make((op) => {
577
+ operations.push(op);
578
+ });
579
+
580
+ const basePrimitive = Primitive.Struct({
581
+ name: Primitive.String(),
582
+ });
583
+
584
+ const extendedPrimitive = basePrimitive.extend({
585
+ email: Primitive.String(),
586
+ });
587
+
588
+ const proxy = extendedPrimitive._internal.createProxy(env, OperationPath.make(""));
589
+
590
+ proxy.name.set("John");
591
+ proxy.email.set("john@example.com");
592
+
593
+ expect(operations).toHaveLength(2);
594
+ expect(operations[0]!.kind).toBe("string.set");
595
+ expect(operations[0]!.payload).toBe("John");
596
+ expect(operations[0]!.path.toTokens()).toEqual(["name"]);
597
+ expect(operations[1]!.kind).toBe("string.set");
598
+ expect(operations[1]!.payload).toBe("john@example.com");
599
+ expect(operations[1]!.path.toTokens()).toEqual(["email"]);
600
+ });
601
+
602
+ it("extended struct set() works with all fields", () => {
603
+ const operations: Operation.Operation<any, any, any>[] = [];
604
+ const env = ProxyEnvironment.make((op) => {
605
+ operations.push(op);
606
+ });
607
+
608
+ const basePrimitive = Primitive.Struct({
609
+ name: Primitive.String(),
610
+ });
611
+
612
+ const extendedPrimitive = basePrimitive.extend({
613
+ email: Primitive.String(),
614
+ });
615
+
616
+ const proxy = extendedPrimitive._internal.createProxy(env, OperationPath.make(""));
617
+
618
+ proxy.set({ name: "John", email: "john@example.com" });
619
+
620
+ expect(operations).toHaveLength(1);
621
+ expect(operations[0]!.kind).toBe("struct.set");
622
+ expect(operations[0]!.payload).toEqual({ name: "John", email: "john@example.com" });
623
+ });
624
+
625
+ it("extended struct preserves required status", () => {
626
+ const basePrimitive = Primitive.Struct({
627
+ name: Primitive.String(),
628
+ }).required();
629
+
630
+ const extendedPrimitive = basePrimitive.extend({
631
+ email: Primitive.String(),
632
+ });
633
+
634
+ // The extended struct should still be required - verify via type system
635
+ // Compile-time check: if the type doesn't match, this would be a type error
636
+ type ExtendedTRequired = typeof extendedPrimitive._TRequired;
637
+ const _typeCheck: ExtendedTRequired = true as const;
638
+ expect(_typeCheck).toBe(true);
639
+ });
640
+
641
+ it("extended struct applyOperation works for both base and new fields", () => {
642
+ const basePrimitive = Primitive.Struct({
643
+ name: Primitive.String(),
644
+ });
645
+
646
+ const extendedPrimitive = basePrimitive.extend({
647
+ email: Primitive.String(),
648
+ });
649
+
650
+ const nameOperation: Operation.Operation<any, any, any> = {
651
+ kind: "string.set",
652
+ path: OperationPath.make("name"),
653
+ payload: "John",
654
+ };
655
+
656
+ const emailOperation: Operation.Operation<any, any, any> = {
657
+ kind: "string.set",
658
+ path: OperationPath.make("email"),
659
+ payload: "john@example.com",
660
+ };
661
+
662
+ let state = extendedPrimitive._internal.applyOperation(undefined, nameOperation);
663
+ state = extendedPrimitive._internal.applyOperation(state, emailOperation);
664
+
665
+ expect(state).toEqual({ name: "John", email: "john@example.com" });
666
+ });
667
+
668
+ it("can chain multiple extend calls", () => {
669
+ const basePrimitive = Primitive.Struct({
670
+ id: Primitive.String(),
671
+ });
672
+
673
+ const extendedOnce = basePrimitive.extend({
674
+ name: Primitive.String(),
675
+ });
676
+
677
+ const extendedTwice = extendedOnce.extend({
678
+ email: Primitive.String(),
679
+ });
680
+
681
+ expect(extendedTwice.fields).toHaveProperty("id");
682
+ expect(extendedTwice.fields).toHaveProperty("name");
683
+ expect(extendedTwice.fields).toHaveProperty("email");
684
+ });
685
+
686
+ it("extend with nested struct works correctly", () => {
687
+ const operations: Operation.Operation<any, any, any>[] = [];
688
+ const env = ProxyEnvironment.make((op) => {
689
+ operations.push(op);
690
+ });
691
+
692
+ const basePrimitive = Primitive.Struct({
693
+ name: Primitive.String(),
694
+ });
695
+
696
+ const addressPrimitive = Primitive.Struct({
697
+ city: Primitive.String(),
698
+ zip: Primitive.String(),
699
+ });
700
+
701
+ const extendedPrimitive = basePrimitive.extend({
702
+ address: addressPrimitive,
703
+ });
704
+
705
+ const proxy = extendedPrimitive._internal.createProxy(env, OperationPath.make(""));
706
+
707
+ proxy.address.city.set("New York");
708
+
709
+ expect(operations).toHaveLength(1);
710
+ expect(operations[0]!.kind).toBe("string.set");
711
+ expect(operations[0]!.payload).toBe("New York");
712
+ expect(operations[0]!.path.toTokens()).toEqual(["address", "city"]);
713
+ });
714
+ });
715
+
716
+ describe("partial", () => {
717
+ it("makes all fields optional", () => {
718
+ const structPrimitive = Primitive.Struct({
719
+ name: Primitive.String().required(),
720
+ email: Primitive.String().required(),
721
+ age: Primitive.Number().required(),
722
+ });
723
+
724
+ const partialPrimitive = structPrimitive.partial();
725
+
726
+ // All fields should now be optional (not required)
727
+ expect(partialPrimitive.fields).toHaveProperty("name");
728
+ expect(partialPrimitive.fields).toHaveProperty("email");
729
+ expect(partialPrimitive.fields).toHaveProperty("age");
730
+ });
731
+
732
+ it("partial struct allows empty set()", () => {
733
+ const operations: Operation.Operation<any, any, any>[] = [];
734
+ const env = ProxyEnvironment.make((op) => {
735
+ operations.push(op);
736
+ });
737
+
738
+ const structPrimitive = Primitive.Struct({
739
+ name: Primitive.String().required(),
740
+ email: Primitive.String().required(),
741
+ });
742
+
743
+ const partialPrimitive = structPrimitive.partial();
744
+
745
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
746
+
747
+ // This should work without providing any fields since all are optional
748
+ proxy.set({});
749
+
750
+ expect(operations).toHaveLength(1);
751
+ expect(operations[0]!.kind).toBe("struct.set");
752
+ });
753
+
754
+ it("partial struct allows partial set()", () => {
755
+ const operations: Operation.Operation<any, any, any>[] = [];
756
+ const env = ProxyEnvironment.make((op) => {
757
+ operations.push(op);
758
+ });
759
+
760
+ const structPrimitive = Primitive.Struct({
761
+ name: Primitive.String().required(),
762
+ email: Primitive.String().required(),
763
+ age: Primitive.Number().required(),
764
+ });
765
+
766
+ const partialPrimitive = structPrimitive.partial();
767
+
768
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
769
+
770
+ // Only provide some fields
771
+ proxy.set({ name: "John" });
772
+
773
+ expect(operations).toHaveLength(1);
774
+ expect(operations[0]!.kind).toBe("struct.set");
775
+ expect(operations[0]!.payload).toHaveProperty("name", "John");
776
+ });
777
+
778
+ it("partial struct field access still works", () => {
779
+ const operations: Operation.Operation<any, any, any>[] = [];
780
+ const env = ProxyEnvironment.make((op) => {
781
+ operations.push(op);
782
+ });
783
+
784
+ const structPrimitive = Primitive.Struct({
785
+ name: Primitive.String().required(),
786
+ email: Primitive.String().required(),
787
+ });
788
+
789
+ const partialPrimitive = structPrimitive.partial();
790
+
791
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
792
+
793
+ proxy.name.set("John");
794
+
795
+ expect(operations).toHaveLength(1);
796
+ expect(operations[0]!.kind).toBe("string.set");
797
+ expect(operations[0]!.payload).toBe("John");
798
+ expect(operations[0]!.path.toTokens()).toEqual(["name"]);
799
+ });
800
+
801
+ it("partial struct preserves required/default status of struct itself", () => {
802
+ const structPrimitive = Primitive.Struct({
803
+ name: Primitive.String().required(),
804
+ }).required();
805
+
806
+ const partialPrimitive = structPrimitive.partial();
807
+ // The struct itself should still be required - verify via type system
808
+ // Compile-time check: if the type doesn't match, this would be a type error
809
+ type PartialTRequired = typeof partialPrimitive._TRequired;
810
+ const _typeCheck: PartialTRequired = true as const;
811
+ expect(_typeCheck).toBe(true);
812
+ });
813
+
814
+ it("partial struct applyOperation works correctly", () => {
815
+ const structPrimitive = Primitive.Struct({
816
+ name: Primitive.String().required(),
817
+ email: Primitive.String().required(),
818
+ });
819
+
820
+ const partialPrimitive = structPrimitive.partial();
821
+
822
+ const operation: Operation.Operation<any, any, any> = {
823
+ kind: "string.set",
824
+ path: OperationPath.make("name"),
825
+ payload: "John",
826
+ };
827
+
828
+ const result = partialPrimitive._internal.applyOperation(undefined, operation);
829
+
830
+ expect(result).toEqual({ name: "John" });
831
+ });
832
+
833
+ it("partial can be combined with extend", () => {
834
+ const basePrimitive = Primitive.Struct({
835
+ id: Primitive.String().required(),
836
+ name: Primitive.String().required(),
837
+ });
838
+
839
+ // First extend, then partial
840
+ const extendedPartial = basePrimitive
841
+ .extend({
842
+ email: Primitive.String().required(),
843
+ })
844
+ .partial();
845
+
846
+ expect(extendedPartial.fields).toHaveProperty("id");
847
+ expect(extendedPartial.fields).toHaveProperty("name");
848
+ expect(extendedPartial.fields).toHaveProperty("email");
849
+ });
850
+
851
+ it("partial works with nested structs", () => {
852
+ const addressPrimitive = Primitive.Struct({
853
+ city: Primitive.String().required(),
854
+ zip: Primitive.String().required(),
855
+ });
856
+
857
+ const personPrimitive = Primitive.Struct({
858
+ name: Primitive.String().required(),
859
+ address: addressPrimitive.required(),
860
+ });
861
+
862
+ const partialPrimitive = personPrimitive.partial();
863
+
864
+ const operations: Operation.Operation<any, any, any>[] = [];
865
+ const env = ProxyEnvironment.make((op) => {
866
+ operations.push(op);
867
+ });
868
+
869
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
870
+
871
+ // Nested struct access should still work
872
+ proxy.address.city.set("New York");
873
+
874
+ expect(operations).toHaveLength(1);
875
+ expect(operations[0]!.kind).toBe("string.set");
876
+ expect(operations[0]!.payload).toBe("New York");
877
+ expect(operations[0]!.path.toTokens()).toEqual(["address", "city"]);
878
+ });
879
+
880
+ it("partial struct update() works correctly", () => {
881
+ const operations: Operation.Operation<any, any, any>[] = [];
882
+ const env = ProxyEnvironment.make((op) => {
883
+ operations.push(op);
884
+ });
885
+
886
+ const structPrimitive = Primitive.Struct({
887
+ name: Primitive.String().required(),
888
+ email: Primitive.String().required(),
889
+ age: Primitive.Number().required(),
890
+ });
891
+
892
+ const partialPrimitive = structPrimitive.partial();
893
+
894
+ const proxy = partialPrimitive._internal.createProxy(env, OperationPath.make(""));
895
+
896
+ proxy.update({ name: "Jane" });
897
+
898
+ expect(operations).toHaveLength(1);
899
+ expect(operations[0]!.kind).toBe("string.set");
900
+ expect(operations[0]!.payload).toBe("Jane");
901
+ });
902
+ });
557
903
  });
558
904
 
559
905
  // =============================================================================
@@ -155,6 +155,95 @@ describe("UnionPrimitive", () => {
155
155
  content: "default",
156
156
  });
157
157
  });
158
+
159
+ it("return correct defaults for nested structs when global default is set", () => {
160
+ const stringVariableTypeSchema = Primitive.Struct({
161
+ key: Primitive.Literal("string"),
162
+ value: Primitive.String(),
163
+ });
164
+
165
+ const numberVariableTypeSchema = Primitive.Struct({
166
+ key: Primitive.Literal("number"),
167
+ value: Primitive.Number(),
168
+ });
169
+
170
+
171
+ const variableTypeSchema = Primitive.Union({
172
+ discriminator: "key",
173
+ variants: {
174
+ string: stringVariableTypeSchema,
175
+ number: numberVariableTypeSchema,
176
+ },
177
+ }).default({
178
+ key: "string",
179
+ value: "",
180
+ });
181
+
182
+ expect(variableTypeSchema._internal.getInitialState()).toEqual({
183
+ key: "string",
184
+ value: "",
185
+ });
186
+ });
187
+
188
+ it("return correct defaults for nested structs when variant default is set", () => {
189
+ const stringVariableTypeSchema = Primitive.Struct({
190
+ key: Primitive.Literal("string"),
191
+ value: Primitive.String().default(""),
192
+ });
193
+
194
+ const numberVariableTypeSchema = Primitive.Struct({
195
+ key: Primitive.Literal("number"),
196
+ value: Primitive.Number().default(10),
197
+ });
198
+
199
+
200
+ const variableTypeSchema = Primitive.Union({
201
+ discriminator: "key",
202
+ variants: {
203
+ string: stringVariableTypeSchema,
204
+ number: numberVariableTypeSchema,
205
+ },
206
+ }).default({
207
+ key: "number",
208
+ });
209
+
210
+ expect(variableTypeSchema._internal.getInitialState()).toEqual({
211
+ key: "number",
212
+ value: 10,
213
+ });
214
+ });
215
+
216
+ it("set() applies defaults to generated operation payload", () => {
217
+ const stringVariableTypeSchema = Primitive.Struct({
218
+ key: Primitive.Literal("string"),
219
+ value: Primitive.String().default(""),
220
+ });
221
+
222
+ const numberVariableTypeSchema = Primitive.Struct({
223
+ key: Primitive.Literal("number"),
224
+ value: Primitive.Number().default(10),
225
+ });
226
+
227
+
228
+ const variableTypeSchema = Primitive.Union({
229
+ discriminator: "key",
230
+ variants: {
231
+ string: stringVariableTypeSchema,
232
+ number: numberVariableTypeSchema,
233
+ },
234
+ });
235
+
236
+ const operations: Operation.Operation<any, any, any>[] = [];
237
+ const proxy = variableTypeSchema._internal.createProxy(ProxyEnvironment.make((op) => operations.push(op)), OperationPath.make(""));
238
+ proxy.set({ key: "number" });
239
+
240
+ expect(operations).toHaveLength(1);
241
+ expect(operations[0]!.kind).toBe("union.set");
242
+ expect(operations[0]!.payload).toEqual({
243
+ key: "number",
244
+ value: 10,
245
+ });
246
+ });
158
247
  });
159
248
 
160
249
  describe("custom discriminator", () => {
@@ -208,3 +297,258 @@ describe("UnionPrimitive", () => {
208
297
  // =============================================================================
209
298
  // Integration Tests - Complex Nested Structures
210
299
  // =============================================================================
300
+
301
+ describe("Union defaults in nested structures", () => {
302
+ // Schema matching the production use case
303
+ const stringVariableTypeSchema = Primitive.Struct({
304
+ key: Primitive.Literal("string"),
305
+ value: Primitive.String().default(""),
306
+ });
307
+
308
+ const numberVariableTypeSchema = Primitive.Struct({
309
+ key: Primitive.Literal("number"),
310
+ value: Primitive.Number().default(0),
311
+ });
312
+
313
+ const booleanVariableTypeSchema = Primitive.Struct({
314
+ key: Primitive.Literal("boolean"),
315
+ value: Primitive.Boolean().default(false),
316
+ });
317
+
318
+ const productVariableTypeSchema = Primitive.Struct({
319
+ key: Primitive.Literal("product"),
320
+ value: Primitive.Struct({
321
+ productId: Primitive.Either(
322
+ Primitive.String(),
323
+ Primitive.Literal(null),
324
+ ),
325
+ }).default({
326
+ productId: null,
327
+ }),
328
+ });
329
+
330
+ const variableTypeSchema = Primitive.Union({
331
+ discriminator: "key",
332
+ variants: {
333
+ string: stringVariableTypeSchema,
334
+ number: numberVariableTypeSchema,
335
+ boolean: booleanVariableTypeSchema,
336
+ product: productVariableTypeSchema,
337
+ },
338
+ });
339
+
340
+ const variableSchema = Primitive.Struct({
341
+ id: Primitive.String(),
342
+ name: Primitive.String(),
343
+ value: variableTypeSchema,
344
+ });
345
+
346
+ describe("Struct.set() with nested Union", () => {
347
+ it("applies Union variant defaults when setting partial Union value", () => {
348
+ const operations: Operation.Operation<any, any, any>[] = [];
349
+ const env = ProxyEnvironment.make((op) => operations.push(op));
350
+ const proxy = variableSchema._internal.createProxy(env, OperationPath.make(""));
351
+
352
+ proxy.set({
353
+ id: "var-1",
354
+ name: "test",
355
+ value: { key: "number" },
356
+ });
357
+
358
+ expect(operations).toHaveLength(1);
359
+ expect(operations[0]!.kind).toBe("struct.set");
360
+ expect(operations[0]!.payload).toEqual({
361
+ id: "var-1",
362
+ name: "test",
363
+ value: {
364
+ key: "number",
365
+ value: 0,
366
+ },
367
+ });
368
+ });
369
+
370
+ it("applies Union variant defaults for string type", () => {
371
+ const operations: Operation.Operation<any, any, any>[] = [];
372
+ const env = ProxyEnvironment.make((op) => operations.push(op));
373
+ const proxy = variableSchema._internal.createProxy(env, OperationPath.make(""));
374
+
375
+ proxy.set({
376
+ id: "var-1",
377
+ name: "test",
378
+ value: { key: "string" },
379
+ });
380
+
381
+ expect(operations[0]!.payload).toEqual({
382
+ id: "var-1",
383
+ name: "test",
384
+ value: {
385
+ key: "string",
386
+ value: "",
387
+ },
388
+ });
389
+ });
390
+
391
+ it("applies Union variant defaults for boolean type", () => {
392
+ const operations: Operation.Operation<any, any, any>[] = [];
393
+ const env = ProxyEnvironment.make((op) => operations.push(op));
394
+ const proxy = variableSchema._internal.createProxy(env, OperationPath.make(""));
395
+
396
+ proxy.set({
397
+ id: "var-1",
398
+ name: "test",
399
+ value: { key: "boolean" },
400
+ });
401
+
402
+ expect(operations[0]!.payload).toEqual({
403
+ id: "var-1",
404
+ name: "test",
405
+ value: {
406
+ key: "boolean",
407
+ value: false,
408
+ },
409
+ });
410
+ });
411
+
412
+ it("applies Union variant defaults for nested struct (product type)", () => {
413
+ const operations: Operation.Operation<any, any, any>[] = [];
414
+ const env = ProxyEnvironment.make((op) => operations.push(op));
415
+ const proxy = variableSchema._internal.createProxy(env, OperationPath.make(""));
416
+
417
+ proxy.set({
418
+ id: "var-1",
419
+ name: "test",
420
+ value: { key: "product" },
421
+ });
422
+
423
+ expect(operations[0]!.payload).toEqual({
424
+ id: "var-1",
425
+ name: "test",
426
+ value: {
427
+ key: "product",
428
+ value: {
429
+ productId: null,
430
+ },
431
+ },
432
+ });
433
+ });
434
+
435
+ it("preserves explicitly provided Union values", () => {
436
+ const operations: Operation.Operation<any, any, any>[] = [];
437
+ const env = ProxyEnvironment.make((op) => operations.push(op));
438
+ const proxy = variableSchema._internal.createProxy(env, OperationPath.make(""));
439
+
440
+ proxy.set({
441
+ id: "var-1",
442
+ name: "test",
443
+ value: { key: "number", value: 42 },
444
+ });
445
+
446
+ expect(operations[0]!.payload).toEqual({
447
+ id: "var-1",
448
+ name: "test",
449
+ value: {
450
+ key: "number",
451
+ value: 42,
452
+ },
453
+ });
454
+ });
455
+ });
456
+
457
+ describe("Array.push() with Struct containing Union", () => {
458
+ const variablesArraySchema = Primitive.Array(variableSchema);
459
+
460
+ it("applies Union variant defaults when pushing with partial Union value", () => {
461
+ const operations: Operation.Operation<any, any, any>[] = [];
462
+ const env = ProxyEnvironment.make((op) => operations.push(op));
463
+ const proxy = variablesArraySchema._internal.createProxy(env, OperationPath.make(""));
464
+
465
+ proxy.push({
466
+ id: "var-1",
467
+ name: "test",
468
+ value: { key: "number" },
469
+ });
470
+
471
+ expect(operations).toHaveLength(1);
472
+ expect(operations[0]!.kind).toBe("array.insert");
473
+ expect(operations[0]!.payload.value).toEqual({
474
+ id: "var-1",
475
+ name: "test",
476
+ value: {
477
+ key: "number",
478
+ value: 0,
479
+ },
480
+ });
481
+ });
482
+
483
+ it("applies Union variant defaults for all variant types", () => {
484
+ const operations: Operation.Operation<any, any, any>[] = [];
485
+ const env = ProxyEnvironment.make((op) => operations.push(op));
486
+ const proxy = variablesArraySchema._internal.createProxy(env, OperationPath.make(""));
487
+
488
+ proxy.push({ id: "var-1", name: "str", value: { key: "string" } });
489
+ proxy.push({ id: "var-2", name: "num", value: { key: "number" } });
490
+ proxy.push({ id: "var-3", name: "bool", value: { key: "boolean" } });
491
+ proxy.push({ id: "var-4", name: "prod", value: { key: "product" } });
492
+
493
+ expect(operations[0]!.payload.value.value).toEqual({ key: "string", value: "" });
494
+ expect(operations[1]!.payload.value.value).toEqual({ key: "number", value: 0 });
495
+ expect(operations[2]!.payload.value.value).toEqual({ key: "boolean", value: false });
496
+ expect(operations[3]!.payload.value.value).toEqual({ key: "product", value: { productId: null } });
497
+ });
498
+ });
499
+
500
+ describe("Array.set() with Struct containing Union", () => {
501
+ const variablesArraySchema = Primitive.Array(variableSchema);
502
+
503
+ it("applies Union variant defaults when setting array with partial Union values", () => {
504
+ const operations: Operation.Operation<any, any, any>[] = [];
505
+ const env = ProxyEnvironment.make((op) => operations.push(op));
506
+ const proxy = variablesArraySchema._internal.createProxy(env, OperationPath.make(""));
507
+
508
+ proxy.set([
509
+ { id: "var-1", name: "str", value: { key: "string" } },
510
+ { id: "var-2", name: "num", value: { key: "number" } },
511
+ ]);
512
+
513
+ expect(operations).toHaveLength(1);
514
+ expect(operations[0]!.kind).toBe("array.set");
515
+ expect(operations[0]!.payload[0].value).toEqual({
516
+ id: "var-1",
517
+ name: "str",
518
+ value: { key: "string", value: "" },
519
+ });
520
+ expect(operations[0]!.payload[1].value).toEqual({
521
+ id: "var-2",
522
+ name: "num",
523
+ value: { key: "number", value: 0 },
524
+ });
525
+ });
526
+ });
527
+
528
+ describe("Array.insertAt() with Struct containing Union", () => {
529
+ const variablesArraySchema = Primitive.Array(variableSchema);
530
+
531
+ it("applies Union variant defaults when inserting with partial Union value", () => {
532
+ const operations: Operation.Operation<any, any, any>[] = [];
533
+ const env = ProxyEnvironment.make((op) => operations.push(op));
534
+ const proxy = variablesArraySchema._internal.createProxy(env, OperationPath.make(""));
535
+
536
+ proxy.insertAt(0, {
537
+ id: "var-1",
538
+ name: "test",
539
+ value: { key: "boolean" },
540
+ });
541
+
542
+ expect(operations).toHaveLength(1);
543
+ expect(operations[0]!.kind).toBe("array.insert");
544
+ expect(operations[0]!.payload.value).toEqual({
545
+ id: "var-1",
546
+ name: "test",
547
+ value: {
548
+ key: "boolean",
549
+ value: false,
550
+ },
551
+ });
552
+ });
553
+ });
554
+ });