tsz-solver 0.1.8

TypeScript type solver for the tsz compiler
Documentation
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
use super::*;
use crate::freshness::{is_fresh_object_type, widen_freshness};
use crate::intern::PROPERTY_MAP_THRESHOLD;
use tsz_binder::SymbolId;

#[test]
fn test_interner_intrinsics() {
    let interner = TypeInterner::new();

    // Intrinsics should be pre-registered
    assert!(interner.lookup(TypeId::STRING).is_some());
    assert!(interner.lookup(TypeId::NUMBER).is_some());
    assert!(interner.lookup(TypeId::ANY).is_some());
}

#[test]
fn test_interner_deduplication() {
    let interner = TypeInterner::new();

    // Same structure should get same TypeId
    let id1 = interner.literal_string("hello");
    let id2 = interner.literal_string("hello");
    let id3 = interner.literal_string("world");

    assert_eq!(id1, id2);
    assert_ne!(id1, id3);
}

#[test]
fn test_interner_fresh_object_distinct_from_non_fresh() {
    let interner = TypeInterner::new();
    let prop = PropertyInfo::new(interner.intern_string("x"), TypeId::NUMBER);

    let fresh = interner.object_fresh(vec![prop.clone()]);
    let non_fresh = interner.object(vec![prop]);

    assert_ne!(fresh, non_fresh);
    assert!(is_fresh_object_type(&interner, fresh));
    assert!(!is_fresh_object_type(&interner, non_fresh));
    assert_eq!(widen_freshness(&interner, fresh), non_fresh);
}

#[test]
fn test_interner_bigint_literal() {
    let interner = TypeInterner::new();

    let id = interner.literal_bigint("123");
    let key = interner
        .lookup(id)
        .expect("bigint literal should be interned");

    match key {
        TypeData::Literal(LiteralValue::BigInt(atom)) => {
            assert_eq!(interner.resolve_atom(atom), "123");
        }
        _ => panic!("Expected bigint literal, got {key:?}"),
    }
}

#[test]
fn test_interner_union_normalization() {
    let interner = TypeInterner::new();

    // Union with single member should return that member
    let single = interner.union(vec![TypeId::STRING]);
    assert_eq!(single, TypeId::STRING);

    // Union with `any` should be `any`
    let with_any = interner.union(vec![TypeId::STRING, TypeId::ANY]);
    assert_eq!(with_any, TypeId::ANY);

    // Union with `never` should exclude `never`
    let with_never = interner.union(vec![TypeId::STRING, TypeId::NEVER]);
    assert_eq!(with_never, TypeId::STRING);

    // Empty union is `never`
    let empty = interner.union(vec![]);
    assert_eq!(empty, TypeId::NEVER);

    // Union with `error` should be `error`
    let with_error = interner.union(vec![TypeId::STRING, TypeId::ERROR]);
    assert_eq!(with_error, TypeId::ERROR);
}

#[test]
fn test_interner_union_unknown_dominates() {
    let interner = TypeInterner::new();

    let with_unknown = interner.union(vec![TypeId::STRING, TypeId::UNKNOWN]);
    assert_eq!(with_unknown, TypeId::UNKNOWN);

    let only_unknown = interner.union(vec![TypeId::UNKNOWN]);
    assert_eq!(only_unknown, TypeId::UNKNOWN);
}

#[test]
fn test_interner_union_any_beats_unknown() {
    let interner = TypeInterner::new();

    let any_and_unknown = interner.union(vec![TypeId::ANY, TypeId::UNKNOWN]);
    assert_eq!(any_and_unknown, TypeId::ANY);
}

#[test]
fn test_interner_union_dedups_and_flattens() {
    let interner = TypeInterner::new();

    let nested = interner.union(vec![TypeId::STRING, TypeId::NUMBER]);
    let flattened = interner.union(vec![TypeId::STRING, nested, TypeId::STRING]);
    let expected = interner.union(vec![TypeId::STRING, TypeId::NUMBER]);

    assert_eq!(flattened, expected);
}

#[test]
fn test_interner_intersection_normalization() {
    let interner = TypeInterner::new();

    // Intersection with single member should return that member
    let single = interner.intersection(vec![TypeId::STRING]);
    assert_eq!(single, TypeId::STRING);

    // Intersection with `never` should be `never`
    let with_never = interner.intersection(vec![TypeId::STRING, TypeId::NEVER]);
    assert_eq!(with_never, TypeId::NEVER);

    // Empty intersection is `unknown`
    let empty = interner.intersection(vec![]);
    assert_eq!(empty, TypeId::UNKNOWN);

    // Intersection with `any` should be `any`
    let with_any = interner.intersection(vec![TypeId::STRING, TypeId::ANY]);
    assert_eq!(with_any, TypeId::ANY);

    // Intersection with `error` should be `error`
    let with_error = interner.intersection(vec![TypeId::STRING, TypeId::ERROR]);
    assert_eq!(with_error, TypeId::ERROR);
}

#[test]
fn test_interner_intersection_unknown_identity() {
    let interner = TypeInterner::new();

    let with_unknown = interner.intersection(vec![TypeId::STRING, TypeId::UNKNOWN]);
    assert_eq!(with_unknown, TypeId::STRING);

    let only_unknown = interner.intersection(vec![TypeId::UNKNOWN]);
    assert_eq!(only_unknown, TypeId::UNKNOWN);
}

#[test]
fn test_interner_intersection_any_over_unknown() {
    let interner = TypeInterner::new();

    let any_and_unknown = interner.intersection(vec![TypeId::ANY, TypeId::UNKNOWN]);
    assert_eq!(any_and_unknown, TypeId::ANY);
}

#[test]
fn test_interner_intersection_flattens_and_dedups() {
    let interner = TypeInterner::new();

    let obj_a = interner.object(vec![PropertyInfo::new(
        interner.intern_string("a"),
        TypeId::NUMBER,
    )]);
    let obj_b = interner.object(vec![PropertyInfo::new(
        interner.intern_string("b"),
        TypeId::STRING,
    )]);

    let inner = interner.intersection(vec![obj_a, obj_b]);
    let outer = interner.intersection(vec![inner, obj_a]);
    let dup = interner.intersection(vec![obj_b, obj_a, obj_a]);

    assert_eq!(outer, inner);
    assert_eq!(dup, inner);
}

#[test]
fn test_interner_intersection_disjoint_primitives() {
    let interner = TypeInterner::new();

    let disjoint = interner.intersection(vec![TypeId::STRING, TypeId::NUMBER]);
    assert_eq!(disjoint, TypeId::NEVER);

    let literal = interner.literal_string("a");
    let disjoint_literal = interner.intersection(vec![literal, TypeId::BOOLEAN]);
    assert_eq!(disjoint_literal, TypeId::NEVER);
}

#[test]
fn test_interner_intersection_disjoint_object_literals() {
    let interner = TypeInterner::new();

    let kind = interner.intern_string("kind");
    let obj_a = interner.object(vec![PropertyInfo::new(kind, interner.literal_string("a"))]);
    let obj_b = interner.object(vec![PropertyInfo::new(kind, interner.literal_string("b"))]);

    let disjoint = interner.intersection(vec![obj_a, obj_b]);
    assert_eq!(disjoint, TypeId::NEVER);
}

#[test]
fn test_interner_intersection_disjoint_object_literal_union() {
    let interner = TypeInterner::new();

    let kind = interner.intern_string("kind");
    let union = interner.union(vec![
        interner.literal_string("a"),
        interner.literal_string("b"),
    ]);
    let obj_union = interner.object(vec![PropertyInfo::new(kind, union)]);
    let obj_c = interner.object(vec![PropertyInfo::new(kind, interner.literal_string("c"))]);

    let disjoint = interner.intersection(vec![obj_union, obj_c]);
    assert_eq!(disjoint, TypeId::NEVER);
}

#[test]
fn test_interner_intersection_optional_object_literals_not_reduced() {
    let interner = TypeInterner::new();

    let kind = interner.intern_string("kind");
    let obj_a = interner.object(vec![PropertyInfo::opt(kind, interner.literal_string("a"))]);
    let obj_b = interner.object(vec![PropertyInfo::opt(kind, interner.literal_string("b"))]);

    let intersection = interner.intersection(vec![obj_a, obj_b]);
    assert_ne!(intersection, TypeId::NEVER);
}

#[test]
fn test_interner_object_sorting() {
    let interner = TypeInterner::new();

    // Properties in different order should produce same TypeId
    let props1 = vec![
        PropertyInfo::new(interner.intern_string("a"), TypeId::STRING),
        PropertyInfo::new(interner.intern_string("b"), TypeId::NUMBER),
    ];
    let props2 = vec![
        PropertyInfo::new(interner.intern_string("b"), TypeId::NUMBER),
        PropertyInfo::new(interner.intern_string("a"), TypeId::STRING),
    ];

    let id1 = interner.object(props1);
    let id2 = interner.object(props2);

    assert_eq!(id1, id2);
}

#[test]
fn test_interner_object_property_lookup_cache() {
    let interner = TypeInterner::new();

    let mut props = Vec::with_capacity(PROPERTY_MAP_THRESHOLD + 2);
    for i in 0..(PROPERTY_MAP_THRESHOLD + 2) {
        let name = format!("prop{i}");
        props.push(PropertyInfo::new(
            interner.intern_string(&name),
            TypeId::NUMBER,
        ));
    }

    let obj = interner.object(props);
    let shape_id = match interner.lookup(obj) {
        Some(TypeData::Object(shape_id)) => shape_id,
        other => panic!("expected object type, got {other:?}"),
    };

    let target_name = format!("prop{}", PROPERTY_MAP_THRESHOLD / 2);
    let target_atom = interner.intern_string(&target_name);
    match interner.object_property_index(shape_id, target_atom) {
        PropertyLookup::Found(idx) => {
            let shape = interner.object_shape(shape_id);
            assert_eq!(shape.properties[idx].name, target_atom);
        }
        other => panic!("expected cached lookup, got {other:?}"),
    }

    let missing = interner.intern_string("missing");
    assert_eq!(
        interner.object_property_index(shape_id, missing),
        PropertyLookup::NotFound
    );

    let small = interner.object(vec![PropertyInfo::new(
        interner.intern_string("only"),
        TypeId::STRING,
    )]);
    let small_shape_id = match interner.lookup(small) {
        Some(TypeData::Object(shape_id)) => shape_id,
        other => panic!("expected object type, got {other:?}"),
    };
    assert_eq!(
        interner.object_property_index(small_shape_id, interner.intern_string("only")),
        PropertyLookup::Uncached
    );
}

#[test]
fn test_interner_application_deduplication() {
    let interner = TypeInterner::new();

    let base = interner.lazy(DefId(1));
    let app1 = interner.application(base, vec![TypeId::STRING]);
    let app2 = interner.application(base, vec![TypeId::STRING]);
    let app3 = interner.application(base, vec![TypeId::NUMBER]);

    assert_eq!(app1, app2);
    assert_ne!(app1, app3);
}

#[test]
fn test_tuple_list_interning_deduplication() {
    use std::sync::Arc;

    let interner = TypeInterner::new();
    let elements = vec![
        TupleElement {
            type_id: TypeId::STRING,
            name: None,
            optional: false,
            rest: false,
        },
        TupleElement {
            type_id: TypeId::NUMBER,
            name: None,
            optional: false,
            rest: false,
        },
    ];

    let tuple_a = interner.tuple(elements.clone());
    let tuple_b = interner.tuple(elements);

    let Some(TypeData::Tuple(list_a)) = interner.lookup(tuple_a) else {
        panic!("Expected tuple type");
    };
    let Some(TypeData::Tuple(list_b)) = interner.lookup(tuple_b) else {
        panic!("Expected tuple type");
    };

    assert_eq!(list_a, list_b);
    let elems_a = interner.tuple_list(list_a);
    let elems_b = interner.tuple_list(list_b);
    assert!(Arc::ptr_eq(&elems_a, &elems_b));
    assert_eq!(elems_a.len(), 2);
}

#[test]
fn test_template_literal_list_interning_deduplication() {
    use std::sync::Arc;

    let interner = TypeInterner::new();
    let spans = vec![
        TemplateSpan::Text(interner.intern_string("prefix")),
        TemplateSpan::Type(TypeId::STRING),
        TemplateSpan::Text(interner.intern_string("suffix")),
    ];

    let template_a = interner.template_literal(spans.clone());
    let template_b = interner.template_literal(spans);

    let Some(TypeData::TemplateLiteral(list_a)) = interner.lookup(template_a) else {
        panic!("Expected template literal type");
    };
    let Some(TypeData::TemplateLiteral(list_b)) = interner.lookup(template_b) else {
        panic!("Expected template literal type");
    };

    assert_eq!(list_a, list_b);
    let spans_a = interner.template_list(list_a);
    let spans_b = interner.template_list(list_b);
    assert!(Arc::ptr_eq(&spans_a, &spans_b));
    assert_eq!(spans_a.len(), 3);
}

#[test]
fn test_intersection_visibility_merging() {
    let interner = TypeInterner::new();

    // Create object { x: number } with private visibility
    let obj_private = interner.object(vec![PropertyInfo {
        name: interner.intern_string("x"),
        type_id: TypeId::NUMBER,
        write_type: TypeId::NUMBER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Private,
        parent_id: None,
    }]);

    // Create object { x: string } with public visibility
    let obj_public = interner.object(vec![PropertyInfo::new(
        interner.intern_string("x"),
        TypeId::STRING,
    )]);

    // Intersection should merge visibility (Private > Public = Private)
    let intersection = interner.intersection2(obj_private, obj_public);

    if let Some(TypeData::Object(shape_id)) = interner.lookup(intersection) {
        let shape = interner.object_shape(shape_id);
        assert_eq!(shape.properties.len(), 1);
        assert_eq!(shape.properties[0].visibility, Visibility::Private);
    } else {
        panic!("Expected object type");
    }
}

#[test]
fn test_intersection_disjoint_literals() {
    let interner = TypeInterner::new();

    // Test: 1 & 2 should be NEVER (disjoint number literals)
    let lit1 = interner.literal_number(1.0);
    let lit2 = interner.literal_number(2.0);
    let intersection = interner.intersection2(lit1, lit2);

    assert_eq!(intersection, TypeId::NEVER);
}

#[test]
fn test_intersection_object_merging() {
    let interner = TypeInterner::new();

    // Test: { a: 1 } & { b: 2 } should merge to { a: 1, b: 2 }
    let obj1 = interner.object(vec![PropertyInfo::new(
        interner.intern_string("a"),
        TypeId::NUMBER,
    )]);

    let obj2 = interner.object(vec![PropertyInfo::new(
        interner.intern_string("b"),
        TypeId::NUMBER,
    )]);

    let intersection = interner.intersection2(obj1, obj2);

    if let Some(TypeData::Object(shape_id)) = interner.lookup(intersection) {
        let shape = interner.object_shape(shape_id);
        assert_eq!(shape.properties.len(), 2);
        let prop_names: Vec<_> = shape.properties.iter().map(|p| p.name.0).collect();
        let atom_a = interner.intern_string("a").0;
        let atom_b = interner.intern_string("b").0;
        assert!(prop_names.contains(&atom_a));
        assert!(prop_names.contains(&atom_b));
    } else {
        panic!("Expected object type");
    }
}

#[test]
fn test_intersection_disjoint_property_types() {
    let interner = TypeInterner::new();

    // Test: { a: 1 } & { a: 2 } should reduce to NEVER (disjoint property types)
    let lit1 = interner.literal_number(1.0);
    let lit2 = interner.literal_number(2.0);

    let obj1 = interner.object(vec![PropertyInfo::new(interner.intern_string("a"), lit1)]);

    let obj2 = interner.object(vec![PropertyInfo::new(interner.intern_string("a"), lit2)]);

    let intersection = interner.intersection2(obj1, obj2);

    // Objects with disjoint property types should reduce to NEVER
    // This is detected in intersection_has_disjoint_primitives
    assert_eq!(intersection, TypeId::NEVER);
}

#[test]
fn test_visibility_interning_distinct_shape_ids() {
    let interner = TypeInterner::new();

    // Create two objects with identical structure but different visibility
    let obj_public = interner.object(vec![PropertyInfo::new(
        interner.intern_string("x"),
        TypeId::NUMBER,
    )]);

    let obj_private = interner.object(vec![PropertyInfo {
        name: interner.intern_string("x"),
        type_id: TypeId::NUMBER,
        write_type: TypeId::NUMBER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Private,
        parent_id: None,
    }]);

    // These should have different TypeIds because visibility differs
    assert_ne!(
        obj_public, obj_private,
        "Objects with different visibility should have different TypeIds"
    );

    // They should also have different ObjectShapeIds
    let shape_public = match interner.lookup(obj_public) {
        Some(TypeData::Object(shape_id)) => shape_id,
        other => panic!("Expected object type, got {other:?}"),
    };

    let shape_private = match interner.lookup(obj_private) {
        Some(TypeData::Object(shape_id)) => shape_id,
        other => panic!("Expected object type, got {other:?}"),
    };

    assert_ne!(
        shape_public, shape_private,
        "Objects with different visibility should have different ObjectShapeIds"
    );
}

#[test]
fn test_parent_id_interning_distinct_shape_ids() {
    let interner = TypeInterner::new();

    // Create two objects with identical structure but different parent_id
    // This tests nominal property identity (different declaring classes)
    let obj_class1 = interner.object(vec![PropertyInfo {
        name: interner.intern_string("x"),
        type_id: TypeId::NUMBER,
        write_type: TypeId::NUMBER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Public,
        parent_id: Some(SymbolId(1)),
    }]);

    let obj_class2 = interner.object(vec![PropertyInfo {
        name: interner.intern_string("x"),
        type_id: TypeId::NUMBER,
        write_type: TypeId::NUMBER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Public,
        parent_id: Some(SymbolId(2)),
    }]);

    // These should have different TypeIds because parent_id differs
    assert_ne!(
        obj_class1, obj_class2,
        "Objects with different parent_id should have different TypeIds"
    );

    // They should also have different ObjectShapeIds
    let shape_class1 = match interner.lookup(obj_class1) {
        Some(TypeData::Object(shape_id)) => shape_id,
        other => panic!("Expected object type, got {other:?}"),
    };

    let shape_class2 = match interner.lookup(obj_class2) {
        Some(TypeData::Object(shape_id)) => shape_id,
        other => panic!("Expected object type, got {other:?}"),
    };

    assert_ne!(
        shape_class1, shape_class2,
        "Objects with different parent_id should have different ObjectShapeIds"
    );
}

// Task #42: Test union order independence (canonicalization)
#[test]
fn test_union_order_independence() {
    let interner = TypeInterner::new();

    // Create two literal types
    let type_a = interner.literal_string("a");
    let type_b = interner.literal_string("b");

    // Create union A | B
    let union_ab = interner.union(vec![type_a, type_b]);

    // Create union B | A (reverse order)
    let union_ba = interner.union(vec![type_b, type_a]);

    // They should have the same TypeId (order independence)
    assert_eq!(
        union_ab, union_ba,
        "Unions should be order-independent: A | B == B | A"
    );

    // Also test with more members
    let type_c = interner.literal_string("c");
    let union_abc = interner.union(vec![type_a, type_b, type_c]);
    let union_cba = interner.union(vec![type_c, type_b, type_a]);

    assert_eq!(
        union_abc, union_cba,
        "Unions with 3+ members should be order-independent"
    );
}

// Task #42: Test intersection order independence
#[test]
fn test_intersection_order_independence() {
    let interner = TypeInterner::new();

    // Create two literal types (non-callable for simplicity)
    let type_a = interner.literal_string("a");
    let type_b = interner.literal_string("b");

    // Create intersection A & B
    let inter_ab = interner.intersection(vec![type_a, type_b]);

    // Create intersection B & A (reverse order)
    let inter_ba = interner.intersection(vec![type_b, type_a]);

    // They should have the same TypeId (order independence for non-callables)
    assert_eq!(
        inter_ab, inter_ba,
        "Intersections should be order-independent: A & B == B & A"
    );
}

// Task #42: Test union redundancy elimination
#[test]
fn test_union_redundancy_elimination() {
    let interner = TypeInterner::new();

    let type_a = interner.literal_string("a");

    // A | A should simplify to A
    let union_aa = interner.union(vec![type_a, type_a]);

    assert_eq!(union_aa, type_a, "Union of A | A should simplify to A");
}

// Task #42: Test intersection redundancy elimination
#[test]
fn test_intersection_redundancy_elimination() {
    let interner = TypeInterner::new();

    let type_a = interner.literal_string("a");

    // A & A should simplify to A
    let inter_aa = interner.intersection(vec![type_a, type_a]);

    assert_eq!(
        inter_aa, type_a,
        "Intersection of A & A should simplify to A"
    );
}

// Task #43: Test partial object merging in mixed intersections
#[test]
fn test_partial_object_merging_in_intersection() {
    let interner = TypeInterner::new();

    // Create two object types
    let obj1 = interner.object(vec![PropertyInfo {
        name: interner.intern_string("a"),
        type_id: TypeId::STRING,
        write_type: TypeId::NEVER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Public,
        parent_id: None,
    }]);

    let obj2 = interner.object(vec![PropertyInfo {
        name: interner.intern_string("b"),
        type_id: TypeId::NUMBER,
        write_type: TypeId::NEVER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Public,
        parent_id: None,
    }]);

    // Create a primitive type
    let prim = TypeId::BOOLEAN;

    // Intersection: { a: string } & { b: number } & boolean
    // Expected: Merged object { a: string; b: number } & boolean
    let inter1 = interner.intersection(vec![obj1, obj2, prim]);
    let inter2 = interner.intersection(vec![obj2, obj1, prim]); // Different order

    // Order independence should still hold
    assert_eq!(
        inter1, inter2,
        "Partial object merging should be order-independent"
    );

    // The result should be an intersection of merged object and boolean
    if let Some(TypeData::Intersection(members)) = interner.lookup(inter1) {
        let member_list = interner.type_list(members);
        assert_eq!(
            member_list.len(),
            2,
            "Result should have 2 members: merged object + boolean"
        );
    } else {
        panic!("Expected intersection type");
    }
}

// Task #43: Test partial callable merging in mixed intersections
#[test]
fn test_partial_callable_merging_in_intersection() {
    let interner = TypeInterner::new();

    // Create two function types
    let func1 = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::unnamed(TypeId::STRING)],
        this_type: None,
        return_type: TypeId::VOID,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    let func2 = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::unnamed(TypeId::NUMBER)],
        this_type: None,
        return_type: TypeId::VOID,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    // Create a primitive type
    let prim = TypeId::BOOLEAN;

    // Intersection: (x: string) => void & (x: number) => void & boolean
    // Expected: Merged callable with 2 overloads & boolean
    let inter = interner.intersection(vec![func1, func2, prim]);

    // The result should be an intersection of merged callable and boolean
    if let Some(TypeData::Intersection(members)) = interner.lookup(inter) {
        let member_list = interner.type_list(members);
        assert_eq!(
            member_list.len(),
            2,
            "Result should have 2 members: merged callable + boolean"
        );
    } else {
        panic!("Expected intersection type");
    }

    // NOTE: Callable order IS significant in TypeScript, so different input
    // orders produce different results (different overload orders).
    // We do NOT test order independence for callables.
}

// Task #43: Test partial merging with both objects and callables
#[test]
fn test_partial_object_and_callable_merging() {
    let interner = TypeInterner::new();

    // Create object type
    let obj1 = interner.object(vec![PropertyInfo {
        name: interner.intern_string("a"),
        type_id: TypeId::STRING,
        write_type: TypeId::NEVER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Public,
        parent_id: None,
    }]);

    let obj2 = interner.object(vec![PropertyInfo {
        name: interner.intern_string("b"),
        type_id: TypeId::NUMBER,
        write_type: TypeId::NEVER,
        optional: false,
        readonly: false,
        is_method: false,
        visibility: Visibility::Public,
        parent_id: None,
    }]);

    // Create callable types
    let func1 = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::unnamed(TypeId::STRING)],
        this_type: None,
        return_type: TypeId::VOID,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    let func2 = interner.function(FunctionShape {
        type_params: vec![],
        params: vec![ParamInfo::unnamed(TypeId::NUMBER)],
        this_type: None,
        return_type: TypeId::VOID,
        type_predicate: None,
        is_constructor: false,
        is_method: false,
    });

    // Intersection: { a: string } & { b: number } & (x: string) => void & (x: number) => void
    // Expected: Merged object { a: string; b: number } & Merged callable (with 2 overloads)
    let inter = interner.intersection(vec![obj1, obj2, func1, func2]);

    // The result should be an intersection with 2 members: merged object + merged callable
    if let Some(TypeData::Intersection(members)) = interner.lookup(inter) {
        let member_list = interner.type_list(members);
        assert_eq!(
            member_list.len(),
            2,
            "Result should have 2 members: merged object + merged callable"
        );

        // First member should be the merged object
        if let Some(TypeData::Object(_) | TypeData::ObjectWithIndex(_)) =
            interner.lookup(member_list[0])
        {
            // OK
        } else {
            panic!("First member should be an object");
        }

        // Second member should be the merged callable
        if let Some(TypeData::Callable(shape_id)) = interner.lookup(member_list[1]) {
            // OK - verify it has 2 call signatures
            let callable = interner.callable_shape(shape_id);
            assert_eq!(
                callable.call_signatures.len(),
                2,
                "Merged callable should have 2 call signatures"
            );
        } else {
            panic!("Second member should be a callable");
        }
    } else {
        panic!("Expected intersection type");
    }

    // NOTE: Object order independence should be tested separately
}

// Task #47: Template Literal Canonicalization Tests

#[test]
fn test_template_never_absorption() {
    let interner = TypeInterner::new();

    // `` `${never}` `` should be never
    let template = interner.template_literal(vec![TemplateSpan::Type(TypeId::NEVER)]);
    assert_eq!(
        template,
        TypeId::NEVER,
        "Template with never should be never"
    );

    // `` `a${never}b` `` should be never
    let template2 = interner.template_literal(vec![
        TemplateSpan::Text(interner.intern_string("a")),
        TemplateSpan::Type(TypeId::NEVER),
        TemplateSpan::Text(interner.intern_string("b")),
    ]);
    assert_eq!(
        template2,
        TypeId::NEVER,
        "Template with never anywhere should be never"
    );
}

#[test]
fn test_template_empty_string_removal() {
    let interner = TypeInterner::new();

    // `` `${""}` `` should simplify to empty string literal
    let empty_lit = interner.literal_string("");
    let template = interner.template_literal(vec![TemplateSpan::Type(empty_lit)]);

    // Should be a literal empty string, not a template with empty type span
    match interner.lookup(template) {
        Some(TypeData::Literal(LiteralValue::String(s))) => {
            let s = interner.resolve_atom_ref(s);
            assert!(s.is_empty(), "Should be empty string literal");
        }
        _ => panic!("Expected empty string literal"),
    }

    // `` `a${""}b` `` should become `ab` (text spans merged)
    let template2 = interner.template_literal(vec![
        TemplateSpan::Text(interner.intern_string("a")),
        TemplateSpan::Type(empty_lit),
        TemplateSpan::Text(interner.intern_string("b")),
    ]);

    // Should be a literal "ab", not a template
    match interner.lookup(template2) {
        Some(TypeData::Literal(LiteralValue::String(s))) => {
            let s = interner.resolve_atom_ref(s);
            assert_eq!(s.to_string(), "ab", "Should be merged 'ab' literal");
        }
        _ => panic!("Expected 'ab' string literal"),
    }
}

#[test]
fn test_template_unknown_widening() {
    let interner = TypeInterner::new();

    // `` `${unknown}` `` should widen to string
    let template = interner.template_literal(vec![TemplateSpan::Type(TypeId::UNKNOWN)]);
    assert_eq!(
        template,
        TypeId::STRING,
        "Template with unknown should widen to string"
    );
}

#[test]
fn test_template_any_widening() {
    let interner = TypeInterner::new();

    // `` `${any}` `` should widen to string (any is infectious in templates)
    let template = interner.template_literal(vec![TemplateSpan::Type(TypeId::ANY)]);
    assert_eq!(
        template,
        TypeId::STRING,
        "Template with any should widen to string"
    );
}

#[test]
fn test_empty_object_rule_intersection() {
    let interner = TypeInterner::new();

    // Task #48: Empty Object Rule for Intersections
    // Primitives should absorb empty objects in intersections

    // Case 1: string & empty_object → string
    let empty_obj = interner.object(vec![]);
    let string_and_empty = interner.intersection(vec![TypeId::STRING, empty_obj]);
    assert_eq!(
        string_and_empty,
        TypeId::STRING,
        "string & empty_object should normalize to string"
    );

    // Case 2: number & empty_object → number
    let number_and_empty = interner.intersection(vec![TypeId::NUMBER, empty_obj]);
    assert_eq!(
        number_and_empty,
        TypeId::NUMBER,
        "number & empty_object should normalize to number"
    );

    // Case 3: (string | null) & empty_object
    // Due to distributivity: (string | null) & {} → (string & {}) | (null & {})
    // string & {} → string
    // null & {} → never (null is disjoint from objects)
    // Result: string | never → string
    let string_or_null = interner.union(vec![TypeId::STRING, TypeId::NULL]);
    let union_and_empty = interner.intersection(vec![string_or_null, empty_obj]);

    // The result should be string (null is filtered out by the empty object constraint)
    assert_eq!(
        union_and_empty,
        TypeId::STRING,
        "(string | null) & empty_object should normalize to string"
    );

    // Case 4: string & empty_object & number → never (disjoint primitives still detected)
    let string_and_empty_and_number =
        interner.intersection(vec![TypeId::STRING, empty_obj, TypeId::NUMBER]);
    assert_eq!(
        string_and_empty_and_number,
        TypeId::NEVER,
        "string & empty_object & number should be never (disjoint primitives)"
    );
}