perl-lsp 0.3.0

A Perl LSP server built on tree-sitter-perl and tower-lsp
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
use super::*;
use tree_sitter::Point;

/// Helper: build a minimal FileAnalysis with a single file scope and given type constraints.
/// Constraints are pushed via `push_type_constraint` so the witness bag carries them —
/// the bag is the sole store post-D4-followup.
fn fa_with_constraints(constraints: Vec<TypeConstraint>) -> FileAnalysis {
    let mut fa = FileAnalysis::new(
        vec![Scope {
            id: ScopeId(0),
            parent: None,
            kind: ScopeKind::File,
            span: Span {
                start: Point::new(0, 0),
                end: Point::new(10, 0),
            },
            package: None,
        }],
        vec![],
        vec![],
        vec![],
        vec![],
        vec![],
        HashMap::new(),
        vec![],
        HashSet::new(),
        vec![],
        vec![],
        vec![],
        HashMap::new(),
        HashMap::new(),
        vec![],
    );
    for tc in constraints {
        fa.push_type_constraint(tc);
    }
    fa
}

fn constraint(var: &str, row: usize, inferred_type: InferredType) -> TypeConstraint {
    TypeConstraint {
        variable: var.to_string(),
        scope: ScopeId(0),
        constraint_span: Span {
            start: Point::new(row, 0),
            end: Point::new(row, 20),
        },
        inferred_type,
    }
}

// ---- Type constraint resolution tests ----

#[test]
fn test_resolve_hashref_type() {
    let fa = fa_with_constraints(vec![constraint("$href", 0, InferredType::HashRef)]);
    assert_eq!(
        fa.inferred_type_via_bag("$href", Point::new(1, 0)),
        Some(InferredType::HashRef)
    );
}

#[test]
fn test_resolve_arrayref_type() {
    let fa = fa_with_constraints(vec![constraint("$aref", 0, InferredType::ArrayRef)]);
    assert_eq!(
        fa.inferred_type_via_bag("$aref", Point::new(1, 0)),
        Some(InferredType::ArrayRef)
    );
}

#[test]
fn test_resolve_coderef_type() {
    let fa = fa_with_constraints(vec![constraint(
        "$cref",
        0,
        InferredType::CodeRef { return_edge: None },
    )]);
    assert_eq!(
        fa.inferred_type_via_bag("$cref", Point::new(1, 0)),
        Some(InferredType::CodeRef { return_edge: None })
    );
}

#[test]
fn test_resolve_regexp_type() {
    let fa = fa_with_constraints(vec![constraint("$re", 0, InferredType::Regexp)]);
    assert_eq!(
        fa.inferred_type_via_bag("$re", Point::new(1, 0)),
        Some(InferredType::Regexp)
    );
}

#[test]
fn test_resolve_numeric_type() {
    let fa = fa_with_constraints(vec![constraint("$n", 0, InferredType::Numeric)]);
    assert_eq!(
        fa.inferred_type_via_bag("$n", Point::new(1, 0)),
        Some(InferredType::Numeric)
    );
}

#[test]
fn test_resolve_string_type() {
    let fa = fa_with_constraints(vec![constraint("$s", 0, InferredType::String)]);
    assert_eq!(
        fa.inferred_type_via_bag("$s", Point::new(1, 0)),
        Some(InferredType::String)
    );
}

#[test]
fn test_resolve_reassignment_changes_type() {
    let fa = fa_with_constraints(vec![
        constraint("$x", 0, InferredType::HashRef),
        constraint("$x", 3, InferredType::ArrayRef),
    ]);
    assert_eq!(
        fa.inferred_type_via_bag("$x", Point::new(1, 0)),
        Some(InferredType::HashRef)
    );
    assert_eq!(
        fa.inferred_type_via_bag("$x", Point::new(4, 0)),
        Some(InferredType::ArrayRef)
    );
}

#[test]
fn test_resolve_sub_return_type() {
    use crate::witnesses::{Witness, WitnessAttachment, WitnessPayload, WitnessSource};
    // Hand-craft a FileAnalysis with a sub that has a return type.
    // Post-D3, return types live on `Symbol(_)` only — seed
    // `Symbol(0)` directly so the bag-routed query picks it up.
    let mut fa = FileAnalysis::new(
        vec![Scope {
            id: ScopeId(0),
            parent: None,
            kind: ScopeKind::File,
            span: Span {
                start: Point::new(0, 0),
                end: Point::new(10, 0),
            },
            package: None,
        }],
        vec![Symbol {
            id: SymbolId(0),
            name: "get_config".to_string(),
            kind: SymKind::Sub,
            span: Span {
                start: Point::new(0, 0),
                end: Point::new(2, 1),
            },
            selection_span: Span {
                start: Point::new(0, 4),
                end: Point::new(0, 14),
            },
            scope: ScopeId(0),
            package: None,
            detail: SymbolDetail::Sub {
                params: vec![],
                is_method: false,
                doc: None,
                display: None,
                hide_in_outline: false,
                opaque_return: false,
            },
            namespace: Namespace::Language,
            outline_label: None,
        }],
        vec![],
        vec![],
        vec![],
        vec![],
        HashMap::new(),
        vec![],
        HashSet::new(),
        vec![],
        vec![],
        vec![],
        HashMap::new(),
        HashMap::new(),
        vec![],
    );
    let zero_span = Span {
        start: Point::new(0, 0),
        end: Point::new(0, 0),
    };
    fa.witnesses.push(Witness {
        attachment: WitnessAttachment::Symbol(SymbolId(0)),
        source: WitnessSource::Builder("local_return".into()),
        payload: WitnessPayload::InferredType(InferredType::HashRef),
        span: zero_span,
    });
    assert_eq!(
        fa.sub_return_type_at_arity("get_config", None),
        Some(InferredType::HashRef)
    );
    assert_eq!(fa.sub_return_type_at_arity("nonexistent", None), None);
}

// ---- Return type resolution tests (decoupled, pure function) ----

#[test]
fn test_resolve_return_type_all_agree() {
    assert_eq!(
        resolve_return_type(&[InferredType::HashRef, InferredType::HashRef]),
        Some(InferredType::HashRef),
    );
}

#[test]
fn test_resolve_return_type_disagreement() {
    assert_eq!(
        resolve_return_type(&[InferredType::HashRef, InferredType::ArrayRef]),
        None,
    );
}

#[test]
fn test_resolve_return_type_empty() {
    assert_eq!(resolve_return_type(&[]), None);
}

#[test]
fn test_resolve_return_type_object_subsumes_hashref() {
    // Object + HashRef → Object wins (overloaded objects)
    assert_eq!(
        resolve_return_type(&[InferredType::ClassName("Foo".into()), InferredType::HashRef,]),
        Some(InferredType::ClassName("Foo".into())),
    );
    // Order shouldn't matter
    assert_eq!(
        resolve_return_type(&[InferredType::HashRef, InferredType::ClassName("Foo".into()),]),
        Some(InferredType::ClassName("Foo".into())),
    );
}

#[test]
fn test_resolve_return_type_object_does_not_subsume_arrayref() {
    // Object + ArrayRef → disagreement
    assert_eq!(
        resolve_return_type(&[
            InferredType::ClassName("Foo".into()),
            InferredType::ArrayRef,
        ]),
        None,
    );
}

#[test]
fn test_resolve_return_type_single() {
    assert_eq!(
        resolve_return_type(&[InferredType::CodeRef { return_edge: None }]),
        Some(InferredType::CodeRef { return_edge: None }),
    );
}

#[test]
fn test_class_name_helper() {
    assert_eq!(
        InferredType::ClassName("Foo".into()).class_name(),
        Some("Foo")
    );
    assert_eq!(
        InferredType::FirstParam {
            package: "Bar".into()
        }
        .class_name(),
        Some("Bar")
    );
    assert_eq!(InferredType::HashRef.class_name(), None);
    assert_eq!(InferredType::ArrayRef.class_name(), None);
    assert_eq!(InferredType::CodeRef { return_edge: None }.class_name(), None);
    assert_eq!(InferredType::Regexp.class_name(), None);
    assert_eq!(InferredType::Numeric.class_name(), None);
    assert_eq!(InferredType::String.class_name(), None);
}

// `test_sub_return_type_imported_fallback` and
// `test_enrich_imported_types_pushes_constraints` were removed in
// D3: the imported-returns path no longer rides a name-keyed bag
// attachment. Cross-file imports resolve through
// `query_sub_return_type` walking `module_index.find_exporters` and
// recursing into the cached module's `Symbol(_)` witness; the
// integration tests under `symbols_tests.rs` (e.g.
// `test_demo_chain_empirical_truth_table`) and
// `builder_tests.rs::enrichment_twice_does_not_crash_on_stale_indices`
// exercise the new path end-to-end against a real `ModuleIndex`.

// ---- Phase 5: refs_by_target index + eager HashKeyAccess resolution ----

fn build_fa_from_source(source: &str) -> FileAnalysis {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&ts_parser_perl::LANGUAGE.into())
        .unwrap();
    let tree = parser.parse(source, None).unwrap();
    crate::builder::build(&tree, source.as_bytes())
}

/// Dynamic method dispatch: `my $m = 'get_config'; __PACKAGE__->$m()`.
/// Constant folding recovers the method name "get_config" from the
/// string assignment, so the MethodCallBinding fires and $c picks up
/// the return type. The access $c->{host} then links to the HashKeyDef.
/// This path already worked for method-call types in the old builder;
/// the phase 5 tests pin it so we don't regress when we refactor the
/// fixup.
#[test]
fn test_phase5_dynamic_method_call_via_constant_folding() {
    let fa = build_fa_from_source(
        r#"
            package Demo::dyn;
            sub get_config { return { host => 'localhost', port => 5432 } }
            my $method = 'get_config';
            my $c = __PACKAGE__->$method();
            my $h = $c->{host};
        "#,
    );

    let def = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "host"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { name, .. }, ..
                } if name == "get_config")
        })
        .expect("HashKeyDef host owned by get_config");

    let indexed = fa.refs_to_symbol(def.id);
    assert!(
        !indexed.is_empty(),
        "dynamic method call via $method='get_config' should link $c->{{host}} back to the def",
    );
}

/// Cross-file consumer-side resolution (the phase-5 follow-up).
/// Consumer has `my $c = Imported::get_config(); $c->{host}`. At build
/// time we don't know get_config returns HashRef. Enrichment injects
/// synthetic HashKeyDefs and now ALSO retries the HashKeyAccess owner
/// fixup + rebuilds refs_by_target. Result: the access links to the
/// synthetic def, so rename / references / refs_by_target all work.
#[test]
fn test_phase5_consumer_side_cross_file_resolves_via_enrichment() {
    use crate::module_index::ModuleIndex;
    use std::sync::Arc;

    let mut fa = build_fa_from_source(
        r#"
            use TestExporter qw(get_config);
            my $c = get_config();
            my $h = $c->{host};
        "#,
    );

    // Sanity: before enrichment the access has no link (there's no
    // local HashKeyDef for "host" and no local get_config sub).
    let hka_before = fa
        .refs
        .iter()
        .find(|r| matches!(r.kind, RefKind::HashKeyAccess { .. }) && r.target_name == "host")
        .expect("HashKeyAccess host");
    assert!(
        hka_before.resolves_to.is_none(),
        "pre-enrichment, access should not be resolved"
    );

    // Stub `TestExporter` with `get_config` returning a hashref whose
    // keys (host, port, name) the builder synthesizes as HashKeyDefs.
    // Enrichment reads these via `cached.sub_info("get_config").hash_keys()`
    // and injects synthetic local HashKeyDefs against the consumer.
    let test_exporter_pm = r#"
package TestExporter;
use Exporter 'import';
our @EXPORT_OK = qw(get_config);
sub get_config { return { host => 'h', port => 1, name => 'n' }; }
1;
"#;
    let idx = ModuleIndex::new_for_test();
    idx.register_workspace_module(
        std::path::PathBuf::from("/tmp/TestExporter.pm"),
        Arc::new(build_fa_from_source(test_exporter_pm)),
    );
    fa.enrich_imported_types_with_keys(Some(&idx));

    // The synthetic HashKeyDef `host` owned by Sub{None, "get_config"}
    // must now have the access indexed under it.
    let def = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "host"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { package: None, name }, ..
                } if name == "get_config")
        })
        .expect("synthetic HashKeyDef host");

    let indexed = fa.refs_to_symbol(def.id);
    assert!(
            !indexed.is_empty(),
            "consumer-side $c->{{host}} access should link to synthetic HashKeyDef after enrichment, got empty refs_by_target entry",
        );
}

/// End-to-end demo shape: mirrors `test_files/phase5_demo.pl` so a
/// regression in the demo surfaces here first. Three access sites all
/// flow through the qualified `Demo::phase5::get_config()` binding.
#[test]
fn test_phase5_demo_file_shape_resolves_all_access_sites() {
    let fa = build_fa_from_source(
        r#"
            package Demo::phase5;
            sub get_config {
                return { host => 'localhost', port => 5432, name => 'mydb' };
            }
            my $c = Demo::phase5::get_config();
            my $h1 = $c->{host};
            my $h2 = $c->{host};
            my $p  = $c->{port};
        "#,
    );

    let def_host = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "host"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { package: Some(p), name }, ..
                } if p == "Demo::phase5" && name == "get_config")
        })
        .expect("Demo::phase5::get_config's HashKeyDef `host`");

    let indexed = fa.refs_to_symbol(def_host.id);
    // Two $c->{host} accesses — both should resolve to the same def.
    assert_eq!(
        indexed.len(),
        2,
        "expected 2 $c->{{host}} accesses to link via refs_by_target, got {} (indexes: {:?})",
        indexed.len(),
        indexed,
    );

    // The port access must NOT be under host's target.
    let def_port = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "port"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { name, .. }, ..
                } if name == "get_config")
        })
        .expect("HashKeyDef port");
    let port_refs = fa.refs_to_symbol(def_port.id);
    assert_eq!(
        port_refs.len(),
        1,
        "expected exactly one $c->{{port}} access"
    );
}

/// Qualified call: `Demo::phase5::get_config()` should link $c->{host}
/// to the HashKeyDef emitted inside `sub get_config` in the same package.
/// Previously broken: the binding fixup looked up `return_types` by the
/// qualified func_name ("Demo::phase5::get_config"), missed, and left
/// $c->{host} with owner=None. Now the fixup strips the package prefix.
#[test]
fn test_phase5_qualified_sub_call_links_hash_key_access() {
    let fa = build_fa_from_source(
        r#"
            package Demo::phase5;
            sub get_config { return { host => 'localhost', port => 5432 } }
            my $c = Demo::phase5::get_config();
            my $h = $c->{host};
        "#,
    );

    let def = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "host"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { package: Some(p), name }, ..
                } if p == "Demo::phase5" && name == "get_config")
        })
        .expect("HashKeyDef host owned by Sub{Some(Demo::phase5), get_config}");

    let indexed = fa.refs_to_symbol(def.id);
    assert!(
            !indexed.is_empty(),
            "qualified call `Demo::phase5::get_config()` should link $c->{{host}} to the def via refs_by_target",
        );
}

/// Chain through an intermediate sub: `sub chain { return get_config() }`
/// with `$c = chain(); $c->{host}`. The hash key ownership must flow
/// through chain to get_config, where the actual HashKeyDefs live.
/// Previously broken: $c's owner was Sub{_, "chain"}, which has no
/// HashKeyDefs, so refs_by_target matched nothing.
#[test]
fn test_phase5_hash_key_owner_flows_through_intermediate_sub() {
    let fa = build_fa_from_source(
        r#"
            package Demo::phase5;
            sub get_config { return { host => 'localhost', port => 5432 } }
            sub chain_helper { return get_config() }
            my $c = chain_helper();
            my $h = $c->{host};
        "#,
    );

    // The HashKeyDef for `host` lives under get_config, not chain_helper.
    let def = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "host"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { name, .. }, ..
                } if name == "get_config")
        })
        .expect("host HashKeyDef owned by get_config");

    // The access $c->{host} must link to this def, even though $c was
    // bound to chain_helper() (the intermediate sub with no keys).
    let indexed = fa.refs_to_symbol(def.id);
    assert!(
        !indexed.is_empty(),
        "chain_helper → get_config delegation should flow $c->{{host}} to get_config's HashKeyDef",
    );
}

/// Previously broken: references on a hash key owned by a sub's return
/// value didn't flow through refs_by_target because resolves_to was never
/// set on HashKeyAccess refs. Phase 5 links HashKeyAccess.resolves_to to
/// the matching HashKeyDef at build time via (target_name, owner) lookup.
#[test]
fn test_phase5_hash_key_access_links_to_def_symbol() {
    let fa = build_fa_from_source(
        r#"
            sub get_config { return { host => 'localhost', port => 5432 } }
            my $cfg = get_config();
            my $h = $cfg->{host};
        "#,
    );

    // Find the HashKeyDef "host" owned by Sub { name: "get_config", .. }.
    let def = fa.symbols.iter().find(|s| {
        s.name == "host"
            && s.kind == SymKind::HashKeyDef
            && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { name, .. }, ..
                } if name == "get_config")
    });
    assert!(
        def.is_some(),
        "HashKeyDef 'host' owned by Sub get_config should exist"
    );
    let def_id = def.unwrap().id;

    // The `$cfg->{host}` access should be indexed under this symbol.
    let indexed = fa.refs_to_symbol(def_id);
    assert!(
        !indexed.is_empty(),
        "refs_by_target should index $cfg->{{host}} access under the HashKeyDef, got empty"
    );
    let access = &fa.refs[indexed[0]];
    assert!(matches!(access.kind, RefKind::HashKeyAccess { .. }));
    assert_eq!(access.target_name, "host");
}

/// Previously broken: find_references on the HashKeyDef required a tree
/// argument to match accesses (the HashKeyAccess.owner was resolved lazily
/// in the query via `resolve_hash_owner_from_tree`). Phase 5 links the ref
/// to the def at build time, so the query returns the access even when
/// the caller passes no tree.
#[test]
fn test_phase5_find_references_on_hash_key_def_without_tree() {
    let fa = build_fa_from_source(
        r#"
            sub get_config { return { host => 1, port => 2 } }
            my $cfg = get_config();
            my $h = $cfg->{host};
            my $p = $cfg->{port};
        "#,
    );
    let def_host = fa
        .symbols
        .iter()
        .find(|s| {
            s.name == "host"
                && s.kind == SymKind::HashKeyDef
                && matches!(&s.detail, SymbolDetail::HashKeyDef {
                    owner: HashKeyOwner::Sub { name, .. }, ..
                } if name == "get_config")
        })
        .expect("host HashKeyDef");

    // Cursor on the def — convention is that find_references returns the
    // *usages*, not the def itself. Previously this returned 0 without a
    // tree; phase 5 returns the access sites via refs_by_target.
    let point = def_host.selection_span.start;
    let refs = fa.find_references(point, None, None, None);
    assert!(
        !refs.is_empty(),
        "expected at least one access for 'host' via refs_by_target (no tree), got empty",
    );
    // Sanity: the access should point at `$cfg->{host}`.
    assert!(
        refs.iter().any(|s| s.start.row == 3),
        "expected to find the access on row 3 ($cfg->{{host}}), got {:?}",
        refs,
    );
}

/// Previously broken: refs_by_target was absent. Even variable refs
/// relied on a linear scan of `refs` per query. Phase 5 exposes an
/// O(1) lookup for any resolved ref.
/// Pin the exhaustive document outline for `test_files/plugin_mojo_demo.pl`.
///
/// Covers every plugin path in a single real file: mojo-helpers
/// (simple + dotted chains), mojo-routes (`->to`), mojo-lite (verb
/// DSL), minion tasks, mojo-events (`->on`). This is what a user
/// actually sees in `<leader>o` — each plugin-emitted entry carries
/// a leading kind word ("helper"/"route"/"task"/"event") and
/// non-invocant params so stacked registrations (GET vs POST on
/// the same path, three events on one emitter) are distinguishable
/// in clients that render only `name` (nvim's builtin
/// document_symbol handler does).
///
/// Internal plumbing — mojo-helpers' synthetic proxy classes and
/// their `[proxy]` namespaces, the intermediate segment Methods
/// that chain dotted helpers — must NOT show up. It's invisible
/// to the user but pins the no-duplicate-`helper users` invariant
/// that was the motivating bug.
#[test]
fn plugin_mojo_demo_outline_pinned() {
    let path =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("test_files/plugin_mojo_demo.pl");
    let src = std::fs::read_to_string(&path).expect("plugin_mojo_demo.pl is checked into the repo");
    let fa = build_fa_from_source(&src);
    let rendered = render_outline(&fa.document_symbols());
    let expected = "\
[NAMESPACE] MyApp @L34
[MODULE] use strict @L35
[MODULE] use warnings @L36
[MODULE] use Mojolicious::Lite @L37
[MODULE] use Mojolicious @L38
[VARIABLE] $app @L44
[FUNCTION] <helper> current_user ($fallback) @L45
[FUNCTION] <helper> users.create ($name, $email) @L52
[FUNCTION] <helper> users.delete ($id) @L56
[FUNCTION] <helper> admin.users.purge ($force) @L62
[VARIABLE] $r @L70
[FUNCTION] <action> Users#list @L71
[FUNCTION] <action> Users#create @L72
[FUNCTION] <action> Admin::Dashboard#index @L73
[FUNCTION] <route> GET /users/profile @L79
[FUNCTION] <route> POST /users/profile ($form_data) @L84
[FUNCTION] <route> ANY /fallback @L90
[MODULE] use Minion @L104
[VARIABLE] $minion @L105
[FUNCTION] <task> send_email ($to, $subject, $body) @L107
[FUNCTION] <task> resize_image ($path, $width, $height) @L113
[NAMESPACE] MyApp::Progress @L131
[MODULE] use parent @L132
[FUNCTION] new @L134
  [VARIABLE] $class @L135
  [VARIABLE] $self @L136
  [EVENT] <event> ready ($ctx) @L137
  [EVENT] <event> step ($n, $total) @L138
  [EVENT] <event> done ($result) @L139
[FUNCTION] tick @L143
  [VARIABLE] $self @L144
  [VARIABLE] $n @L144
";
    assert_eq!(
        rendered, expected,
        "\n---- ACTUAL ----\n{}\n---- EXPECTED ----\n{}",
        rendered, expected,
    );
}

/// Serialize an outline tree to the `[LSP_KIND] name @L<line>`
/// form the pin test compares against. Indent = two spaces per
/// depth. LSP kind uses the same path symbols.rs takes for the
/// real documentSymbol response (`outline_lsp_kind`), so the
/// snapshot reflects what clients actually receive.
fn render_outline(items: &[OutlineSymbol]) -> String {
    fn walk(o: &OutlineSymbol, depth: usize, buf: &mut String) {
        let kind = crate::symbols::outline_lsp_kind(o);
        // lsp_types SymbolKind's Debug is verbose ("SymbolKind(12)");
        // map the handful of kinds we actually emit by hand.
        let name = lsp_kind_name(kind);
        let pad = "  ".repeat(depth);
        buf.push_str(&format!(
            "{}[{}] {} @L{}\n",
            pad,
            name,
            o.name,
            o.span.start.row + 1,
        ));
        for c in &o.children {
            walk(c, depth + 1, buf);
        }
    }
    let mut buf = String::new();
    for o in items {
        walk(o, 0, &mut buf);
    }
    buf
}

fn lsp_kind_name(k: tower_lsp::lsp_types::SymbolKind) -> &'static str {
    use tower_lsp::lsp_types::SymbolKind as K;
    match k {
        K::FUNCTION => "FUNCTION",
        K::METHOD => "METHOD",
        K::CLASS => "CLASS",
        K::NAMESPACE => "NAMESPACE",
        K::MODULE => "MODULE",
        K::VARIABLE => "VARIABLE",
        K::EVENT => "EVENT",
        K::FIELD => "FIELD",
        K::PROPERTY => "PROPERTY",
        K::CONSTANT => "CONSTANT",
        K::KEY => "KEY",
        _ => "OTHER",
    }
}

#[test]
fn test_phase5_refs_by_target_index_populated_for_variables() {
    let fa = build_fa_from_source(
        r#"
            my $x = 1;
            $x = 2;
            my $y = $x + 1;
        "#,
    );
    // Find the variable symbol $x.
    let x_sym = fa
        .symbols
        .iter()
        .find(|s| s.name == "$x" && s.kind == SymKind::Variable)
        .expect("$x variable symbol");
    let indexed = fa.refs_to_symbol(x_sym.id);
    // Two usages after decl: `$x = 2` (write) and `$x + 1` (read).
    assert!(
        indexed.len() >= 2,
        "expected >= 2 refs to $x via refs_by_target, got {}: {:?}",
        indexed.len(),
        indexed,
    );
}

// ---- Part 6/7 witness-bag integration ----

/// The spike's driving case: a Mojo::Base class whose method
/// reads `$self->{k}` (blessed-hashref). The legacy
/// `inferred_type` may flip to HashRef; the witness-based
/// `inferred_type_via_bag` must keep the class.
#[test]
fn test_witnesses_mojo_self_stays_class_on_hashref_access() {
    let fa = build_fa_from_source(
        r#"
package Mojolicious::Routes::Route;
use Mojo::Base -base;

sub name {
    my $self = shift;
    return $self->{name} unless @_;
    $self->{name} = shift;
    $self;
}
        "#,
    );

    // Framework mode was detected.
    assert_eq!(
        fa.package_framework.get("Mojolicious::Routes::Route"),
        Some(&crate::witnesses::FrameworkFact::MojoBase),
        "Mojo::Base package should record MojoBase framework"
    );

    // Witness bag has entries for $self.
    let self_wits: Vec<_> = fa
        .witnesses
        .all()
        .iter()
        .filter(|w| {
            matches!(&w.attachment,
                crate::witnesses::WitnessAttachment::Variable { name, .. } if name == "$self")
        })
        .collect();
    assert!(
        !self_wits.is_empty(),
        "expected witnesses for $self, got none (bag size: {})",
        fa.witnesses.len()
    );

    // Find the body of `sub name` and pick a point inside it.
    // The `$self->{name} unless @_;` line is row 5 (0-indexed).
    let point_in_body = Point { row: 5, column: 15 };
    let t = fa.inferred_type_via_bag("$self", point_in_body);
    assert!(
        matches!(t, Some(InferredType::ClassName(ref n)) if n == "Mojolicious::Routes::Route"),
        "via-bag type at body point should be ClassName(Route), got {:?}",
        t
    );
}

/// Part 7 fluent-chain: `$r->get('/x')->to('Y#z')` — the `->to`
/// call has an invocant expression that's itself a method call.
/// The bag lets us ask "what's the return type of ref[i]?" without
/// needing a named intermediate variable. Verifies the seed is
/// present and the bag's edge-chase materialization resolves the
/// chain hop to its target sub's return type.
#[test]
fn test_witnesses_fluent_chain_seeds_expression_witnesses() {
    let fa = build_fa_from_source(
        r#"
package MyApp::Route;
sub get  { my $self = shift; return $self; }
sub to   { my $self = shift; return $self; }

package main;
my $r = MyApp::Route->new;
$r->get('/x')->to('Y#z');
        "#,
    );

    // Expected: Expression-attached witnesses for each method
    // call. At minimum: one for `->new`, one for `->get`, one for
    // `->to`.
    let expr_wits: Vec<_> = fa
        .witnesses
        .all()
        .iter()
        .filter(|w| {
            matches!(
                w.attachment,
                crate::witnesses::WitnessAttachment::Expression(_)
            )
        })
        .collect();
    assert!(
        expr_wits.len() >= 3,
        "expected >= 3 Expression witnesses (new, get, to), got {}",
        expr_wits.len()
    );

    // Find the `->get` ref by name.
    let get_ref_idx = fa
        .refs
        .iter()
        .position(|r| matches!(r.kind, RefKind::MethodCall { .. }) && r.target_name == "get")
        .expect("->get ref should exist");

    // Querying the bag for the get-call's return should yield
    // MyApp::Route (since its return is `return $self`, which is
    // FirstParam → ClassName projection).
    let t = fa.method_call_return_type_via_bag(get_ref_idx, None);
    assert!(
        matches!(t, Some(InferredType::ClassName(ref n)) if n == "MyApp::Route"),
        "->get's return type via bag should be ClassName(MyApp::Route), got {:?}",
        t
    );
}

/// Part 1 — mutated keys on the class. Write-access HashKeyAccess
/// refs push mutation facts; the helper returns them for dynamic-
/// key completion.
#[test]
fn test_witnesses_mutated_keys_on_mojo_class() {
    let fa = build_fa_from_source(
        r#"
package Mojolicious::Routes::Route;
use Mojo::Base -base;

sub boot {
    my $self = shift;
    $self->{name}    = 'root';
    $self->{custom}  = 42;
    $self->{counter} = 0;
    return $self;
}
        "#,
    );

    let mut keys = fa.mutated_keys_on_class("Mojolicious::Routes::Route");
    keys.sort();
    // Depending on how owners resolve (Class vs Sub), we expect at
    // least two of the three writes to surface. Exact set is a
    // function of HashKeyOwner resolution, which isn't in scope
    // for this spike — just assert the helper plumbs through and
    // something lands.
    assert!(
        !keys.is_empty(),
        "expected some mutated keys for Mojolicious::Routes::Route, got {:?} \
             (hash-key witnesses in bag: {})",
        keys,
        fa.witnesses
            .all()
            .iter()
            .filter(|w| matches!(
                &w.attachment,
                crate::witnesses::WitnessAttachment::HashKey { .. }
            ))
            .count(),
    );
}

/// Fluent chain through `resolve_expression_type` public path:
/// `$r->get('/x')->to('Y#z')` — `to`'s invocant is a
/// method_call_expression. We walk the tree, recursively resolve
/// `$r->get('/x')`'s type; for that to work, `get`'s return type
/// must resolve. Inside `get`, `return $self` (where $self =
/// shift inside `sub get` of a class) is FirstParam → ClassName.
///
/// The witness wiring doesn't change this path's logic — it just
/// makes sure the `$self` inside `get` doesn't get flipped to
/// HashRef by intervening `$self->{k}` accesses (the Mojo bug).
#[test]
fn test_wiring_fluent_chain_resolves_through_expression_type() {
    let source = r#"
package MyApp::Route;
use Mojo::Base -base;

sub get {
    my $self = shift;
    $self->{_pattern} = shift;
    return $self;
}

sub to {
    my $self = shift;
    $self->{_target} = shift;
    return $self;
}

package main;
my $r = MyApp::Route->new;
$r->get('/x')->to('Y#z');
"#;

    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&ts_parser_perl::LANGUAGE.into())
        .unwrap();
    let tree = parser.parse(source, None).unwrap();
    let fa = crate::builder::build(&tree, source.as_bytes());

    // Find the `->to` method call node by walking the tree.
    fn find_to_node<'a>(node: tree_sitter::Node<'a>) -> Option<tree_sitter::Node<'a>> {
        if node.kind() == "method_call_expression" {
            if let Some(m) = node.child_by_field_name("method") {
                if m.utf8_text(&[]).ok() == Some("to") {
                    return Some(node);
                }
            }
        }
        for i in 0..node.named_child_count() {
            if let Some(c) = node.named_child(i) {
                if let Some(r) = find_to_node(c) {
                    return Some(r);
                }
            }
        }
        None
    }

    // The `->to`'s invocant is `$r->get('/x')`, a
    // method_call_expression. Grab its type — should be
    // ClassName(MyApp::Route) because `get` returns $self.
    //
    // We walk the tree looking for the method_call_expression
    // whose *invocant* is itself a method_call_expression — that's
    // our fluent chain site.
    let source_bytes = source.as_bytes();
    let to_node = {
        let mut stack: Vec<tree_sitter::Node> = vec![tree.root_node()];
        let mut found: Option<tree_sitter::Node> = None;
        while let Some(n) = stack.pop() {
            if n.kind() == "method_call_expression" {
                if let Some(m) = n.child_by_field_name("method") {
                    if m.utf8_text(source_bytes).ok() == Some("to") {
                        found = Some(n);
                        break;
                    }
                }
            }
            for i in 0..n.named_child_count() {
                if let Some(c) = n.named_child(i) {
                    stack.push(c);
                }
            }
        }
        found.expect("found ->to node")
    };

    let invocant = to_node.child_by_field_name("invocant").expect("invocant");
    let ty = fa.resolve_expression_type(invocant, source_bytes, None);
    let class = ty.as_ref().and_then(|t| t.class_name());
    assert_eq!(
        class,
        Some("MyApp::Route"),
        "fluent chain: invocant type of `->to` should resolve to \
             MyApp::Route (ClassName or FirstParam), got {:?}",
        ty
    );
    let _ = find_to_node; // silence unused warning in case we refactor
}

/// End-to-end wiring check: the public path
/// `resolve_invocant_class` now goes through the witness bag, so
/// the Mojo `$self->{k}` access inside a Mojo::Base method no
/// longer flips `$self` to HashRef — method chains downstream
/// still see the class.
#[test]
fn test_wiring_mojo_self_invocant_class_via_public_api() {
    let fa = build_fa_from_source(
        r#"
package Mojolicious::Routes::Route;
use Mojo::Base -base;

sub name {
    my $self = shift;
    return $self->{name} unless @_;
    $self->{name} = shift;
    $self;
}
        "#,
    );

    // At a point inside the method body where `$self` has been
    // assigned via `shift`, the flat `inferred_type` returns
    // HashRef (because the access flipped it). The witness-based
    // path — which is what `resolve_invocant_class` now calls —
    // should return the class.
    let point = Point { row: 5, column: 15 };

    // Walk up the scope chain to find the sub's scope.
    let scope = fa.scope_at(point).expect("scope");

    let resolved = fa.resolve_invocant_class_test("$self", scope, point);
    assert_eq!(
        resolved.as_deref(),
        Some("Mojolicious::Routes::Route"),
        "resolve_invocant_class (public path) should see $self as the class, \
             not HashRef — got {:?}",
        resolved
    );
}

/// Ternary RETURN: `sub f { return $c ? $self : $self }` —
/// both arms agree on `$self` (FirstParam), so `f`'s return
/// type collapses to that class. Same reduction as the RHS-
/// ternary + explicit-if/else-return paths, just emitted from
/// the return-expression visit on a Symbol attachment.
#[test]
fn test_witnesses_ternary_return_on_sub_symbol() {
    let fa = build_fa_from_source(
        r#"
package MyApp::Widget;

sub choose {
    my $self = shift;
    my $flag = shift;
    return $flag ? $self : $self;
}

sub literal_mix {
    my ($flag) = @_;
    return $flag ? 1 : 2;
}

sub disagree {
    my ($flag) = @_;
    return $flag ? 1 : "two";
}
        "#,
    );

    let choose = fa
        .symbols
        .iter()
        .find(|s| s.name == "choose" && matches!(s.kind, SymKind::Sub))
        .expect("sub choose");
    let return_type = fa.symbol_return_type_via_bag(choose.id, None);
    assert!(
        matches!(&return_type, Some(InferredType::FirstParam { package }) if package == "MyApp::Widget")
            || matches!(&return_type, Some(InferredType::ClassName(n)) if n == "MyApp::Widget"),
        "choose's return: ternary arms both $self → class; got {:?}",
        return_type
    );

    let literal = fa
        .symbols
        .iter()
        .find(|s| s.name == "literal_mix" && matches!(s.kind, SymKind::Sub))
        .expect("sub literal_mix");
    let return_type = fa.symbol_return_type_via_bag(literal.id, None);
    assert_eq!(
        return_type,
        Some(InferredType::Numeric),
        "literal_mix: both arms Numeric"
    );

    let disagree = fa
        .symbols
        .iter()
        .find(|s| s.name == "disagree" && matches!(s.kind, SymKind::Sub))
        .expect("sub disagree");
    let return_type = fa.symbol_return_type_via_bag(disagree.id, None);
    assert_eq!(
        return_type, None,
        "disagree: arms Numeric vs String → None"
    );
}

/// Ternary: `my $n = $c ? 1 : 2;` → both arms are Numeric, so
/// the fold via the bag yields Numeric.
#[test]
fn test_witnesses_ternary_agreeing_arms_yield_type() {
    let fa = build_fa_from_source(
        r#"
my $c = 1;
my $n = $c ? 10 : 20;
my $s = $c ? "a" : "b";
my $x = $c ? 10 : "oops";
        "#,
    );

    // $n: both arms Numeric.
    let p_n = Point { row: 2, column: 22 };
    let t_n = fa.inferred_type_via_bag("$n", p_n);
    assert_eq!(t_n, Some(InferredType::Numeric), "agreeing numeric arms");

    // $s: both arms String.
    let p_s = Point { row: 3, column: 22 };
    let t_s = fa.inferred_type_via_bag("$s", p_s);
    assert_eq!(t_s, Some(InferredType::String), "agreeing string arms");

    // $x: disagreeing — reducer returns None. The legacy
    // fallback doesn't handle ternaries either, so overall None.
    let p_x = Point { row: 4, column: 22 };
    let t_x = fa.inferred_type_via_bag("$x", p_x);
    assert_eq!(
        t_x, None,
        "disagreeing arms: bag returns None, legacy has no answer"
    );
}

/// Explicit if/else return arms use the same BranchArm reduction
/// as ternary. `sub pick { if ($c) { return 10 } else { return 20 } }`
/// — both arms Numeric, so the reducer returns Numeric for the
/// sub's return type.
#[test]
fn test_witnesses_if_else_branch_arm_on_sub() {
    let fa = build_fa_from_source(
        r#"
sub pick {
    my $c = shift;
    if ($c) { return 10 }
    else    { return 20 }
}

sub mix {
    my $c = shift;
    if ($c) { return 10 }
    else    { return "nope" }
}
        "#,
    );

    // Find the `pick` sub's symbol.
    let pick_sym = fa
        .symbols
        .iter()
        .find(|s| s.name == "pick" && matches!(s.kind, SymKind::Sub))
        .expect("sub pick");
    let mix_sym = fa
        .symbols
        .iter()
        .find(|s| s.name == "mix" && matches!(s.kind, SymKind::Sub))
        .expect("sub mix");

    // Query the bag for the sub's Symbol attachment, expect the
    // BranchArmFold to produce Numeric for `pick` and None for
    // `mix` (disagreement).
    use crate::witnesses::{
        FrameworkFact, ReducedValue, ReducerQuery, ReducerRegistry, WitnessAttachment,
    };
    let reg = ReducerRegistry::with_defaults();

    let att_p = WitnessAttachment::Symbol(pick_sym.id);
    let q_p = ReducerQuery {
        attachment: &att_p,
        point: None,
        framework: FrameworkFact::Plain,
        arity_hint: None, receiver: None, context: None,
    };
    assert_eq!(
        reg.query(&fa.witnesses, &q_p),
        ReducedValue::Type(InferredType::Numeric),
        "pick: both arms Numeric → Numeric"
    );

    let att_m = WitnessAttachment::Symbol(mix_sym.id);
    let q_m = ReducerQuery {
        attachment: &att_m,
        point: None,
        framework: FrameworkFact::Plain,
        arity_hint: None, receiver: None, context: None,
    };
    assert_eq!(
        reg.query(&fa.witnesses, &q_m),
        ReducedValue::None,
        "mix: disagreement (Numeric vs String) → None"
    );
}

/// Extended arity idioms (Part C): exact-N matching.
#[test]
fn test_witnesses_arity_exact_n_variants() {
    let fa = build_fa_from_source(
        r#"
sub postfix_eq {
    return "zero" unless @_;
    return "one"  if @_ == 1;
    return "two"  if @_ == 2;
    return "many";
}

sub postfix_scalar {
    return "zero" if !@_;
    return "one"  if scalar(@_) == 1;
    return "dflt";
}

sub explicit_if {
    my ($self) = @_;
    if (@_ == 1) { return "alpha" }
    if (@_ == 2) { return "beta"  }
    return "gamma";
}
        "#,
    );

    // postfix_eq: arity-indexed returns.
    assert_eq!(
        fa.sub_return_type_at_arity("postfix_eq", Some(0)),
        Some(InferredType::String),
        "postfix: arity 0 via `unless @_`"
    );
    assert_eq!(
        fa.sub_return_type_at_arity("postfix_eq", Some(1)),
        Some(InferredType::String),
        "postfix: arity 1 via `if @_ == 1`"
    );
    assert_eq!(
        fa.sub_return_type_at_arity("postfix_eq", Some(2)),
        Some(InferredType::String),
        "postfix: arity 2 via `if @_ == 2`"
    );
    assert_eq!(
        fa.sub_return_type_at_arity("postfix_eq", Some(3)),
        Some(InferredType::String),
        "postfix: arity 3 → default"
    );

    // postfix_scalar: `!@_` and `scalar(@_) == 1` variants.
    assert_eq!(
        fa.sub_return_type_at_arity("postfix_scalar", Some(0)),
        Some(InferredType::String),
        "postfix: arity 0 via `if !@_`"
    );
    assert_eq!(
        fa.sub_return_type_at_arity("postfix_scalar", Some(1)),
        Some(InferredType::String),
        "postfix: arity 1 via `if scalar(@_) == 1`"
    );

    // explicit_if: `if (@_ == N) { return … }`.
    assert_eq!(
        fa.sub_return_type_at_arity("explicit_if", Some(1)),
        Some(InferredType::String),
        "explicit: arity 1 via if (@_ == 1) return arm"
    );
    assert_eq!(
        fa.sub_return_type_at_arity("explicit_if", Some(2)),
        Some(InferredType::String),
        "explicit: arity 2"
    );
}

/// Part 6b — fluent arity dispatch. Two unambiguous return
/// types on either side of `unless @_` — arity 0 vs default
/// return different types. Verifies the reducer picks correctly.
#[test]
fn test_witnesses_fluent_arity_dispatch_literal_returns() {
    let fa = build_fa_from_source(
        r#"
package MyApp::Route;

sub name {
    my $self = shift;
    return "configured" unless @_;
    return 42;
}
        "#,
    );

    let t0 = fa.sub_return_type_at_arity("name", Some(0));
    let t1 = fa.sub_return_type_at_arity("name", Some(1));
    let td = fa.sub_return_type_at_arity("name", None);

    assert_eq!(
        t0,
        Some(InferredType::String),
        "arity=0 fires the `unless @_` branch (String return)"
    );
    assert_eq!(
        t1,
        Some(InferredType::Numeric),
        "arity=1 falls through to default branch (Numeric return)"
    );
    assert_eq!(
        td,
        Some(InferredType::Numeric),
        "no arity hint also falls through to default"
    );
}

/// Non-Mojo class without any framework mode: hashref-only access
/// with no class assertion should still fold to HashRef (the bag
/// mirror of the current flat behavior).
#[test]
fn test_witnesses_plain_hashref_without_class_evidence() {
    let fa = build_fa_from_source(
        r#"
my $cfg = { host => 'localhost' };
my $host = $cfg->{host};
        "#,
    );

    let point = Point { row: 2, column: 20 };
    let t = fa.inferred_type_via_bag("$cfg", point);
    assert_eq!(
        t,
        Some(InferredType::HashRef),
        "without class evidence, $cfg is plain HashRef"
    );
}

/// Inherited-method rename — pin the chain that the LSP rename
/// path walks. e2e `rename: process → execute` was the surface
/// failure: `rename_method_in_class` strict-matches both the
/// def's `package` and the call site's `invocant_class`, so a
/// rename starting on `$worker->process()` (invocant=MyWorker)
/// only catches the call sites in the child class — never
/// reaches `sub process` in `BaseWorker.pm`. The chain is
/// `[child, …, defining ancestor]`; backend renames in every
/// link and merges. Stops at the first defining ancestor so
/// overrides in unrelated branches aren't lumped in.
#[test]
fn red_pin_method_rename_chain_walks_to_defining_ancestor() {
    // Same-file inheritance — keeps the test free of the
    // module_index, which still gets exercised end-to-end via the
    // e2e suite.
    let src = "\
package Animal;
sub speak { return '...' }
sub breathe { return 1 }

package Dog;
our @ISA = ('Animal');
sub speak { return 'Woof!' }
# `breathe` inherited; no override

package main;
my $dog = bless {}, 'Dog';
$dog->breathe();
$dog->speak();
";
    let fa = build_fa_from_source(src);

    // Inherited (defined in Animal, not in Dog) — chain runs
    // child → defining ancestor and stops.
    let chain = fa.method_rename_chain("Dog", "breathe", None);
    assert_eq!(
        chain,
        vec!["Dog".to_string(), "Animal".to_string()],
        "inherited method: chain must reach the defining ancestor",
    );

    // Override (Dog defines `speak` itself) — chain stops at
    // child. Walking past the override into Animal would lump
    // two semantically distinct methods together in one rename.
    let chain = fa.method_rename_chain("Dog", "speak", None);
    assert_eq!(
        chain,
        vec!["Dog".to_string()],
        "overridden method: chain must stop at the defining (= child) class, \
             not include the parent's same-named method",
    );

    // Unknown method: degrade to the original class so the
    // backend's per-class rename still runs (no edits, no harm).
    let chain = fa.method_rename_chain("Dog", "nonexistent", None);
    assert_eq!(chain, vec!["Dog".to_string()]);
}