-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgraph_test.go
More file actions
979 lines (838 loc) · 29.2 KB
/
Copy pathgraph_test.go
File metadata and controls
979 lines (838 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
package facts
import (
"reflect"
"strings"
"testing"
)
// buildTestGraph creates a graph from a set of facts for testing.
// The topology is:
//
// A --calls--> B --calls--> C --calls--> D
// A --imports-> E
// E --calls--> C
// F (disconnected)
func buildTestGraph() (*Graph, *Store) {
s := NewStore()
s.Add(
Fact{Kind: KindSymbol, Name: "A", File: "a.go", Line: 1, Relations: []Relation{
{Kind: RelCalls, Target: "B"},
{Kind: RelImports, Target: "E"},
}},
Fact{Kind: KindSymbol, Name: "B", File: "b.go", Line: 10, Relations: []Relation{
{Kind: RelCalls, Target: "C"},
}},
Fact{Kind: KindModule, Name: "C", File: "c.go", Line: 20, Relations: []Relation{
{Kind: RelCalls, Target: "D"},
}},
Fact{Kind: KindSymbol, Name: "D", File: "d.go", Line: 30},
Fact{Kind: KindModule, Name: "E", File: "e.go", Line: 40, Relations: []Relation{
{Kind: RelCalls, Target: "C"},
}},
Fact{Kind: KindSymbol, Name: "F", File: "f.go", Line: 50}, // disconnected
)
s.BuildGraph()
return s.Graph(), s
}
// buildCyclicGraph creates a graph with a cycle: A -> B -> C -> A
func buildCyclicGraph() (*Graph, *Store) {
s := NewStore()
s.Add(
Fact{Kind: KindModule, Name: "A", File: "a.go", Relations: []Relation{
{Kind: RelImports, Target: "B"},
}},
Fact{Kind: KindModule, Name: "B", File: "b.go", Relations: []Relation{
{Kind: RelImports, Target: "C"},
}},
Fact{Kind: KindModule, Name: "C", File: "c.go", Relations: []Relation{
{Kind: RelImports, Target: "A"},
}},
)
s.BuildGraph()
return s.Graph(), s
}
func TestNewGraph_BuildsAdjacencyLists(t *testing.T) {
g, _ := buildTestGraph()
if g.NodeCount() != 6 {
t.Errorf("NodeCount = %d, want 6", g.NodeCount())
}
if g.EdgeCount() != 5 {
t.Errorf("EdgeCount = %d, want 5", g.EdgeCount())
}
// Check forward adjacency for A
fwd := g.Forward()
aEdges := fwd["A"]
if len(aEdges) != 2 {
t.Errorf("A forward edges = %d, want 2", len(aEdges))
}
// Check reverse adjacency for C (B and E both call C)
rev := g.Reverse()
cEdges := rev["C"]
if len(cEdges) != 2 {
t.Errorf("C reverse edges = %d, want 2", len(cEdges))
}
}
func TestTraverse_ForwardFromA(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("A", "forward", nil, nil, 10, 100)
// A -> B -> C -> D, A -> E -> C (C already visited)
// Should visit: A, B, C, D, E
if result.Stats.NodesVisited != 5 {
t.Errorf("NodesVisited = %d, want 5", result.Stats.NodesVisited)
}
// Nodes in result (including start)
names := nodeNames(result.Nodes)
for _, want := range []string{"A", "B", "C", "D", "E"} {
if !contains(names, want) {
t.Errorf("missing node %q in traverse result", want)
}
}
if contains(names, "F") {
t.Error("F should not be reachable from A")
}
}
func TestTraverse_ReverseFromD(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("D", "reverse", nil, nil, 10, 100)
// D is called by C, C is called by B and E, B is called by A, E is imported by A
// Reverse from D: D <- C <- B <- A, C <- E <- A (A already visited)
names := nodeNames(result.Nodes)
for _, want := range []string{"D", "C", "B", "A", "E"} {
if !contains(names, want) {
t.Errorf("missing node %q in reverse traverse from D", want)
}
}
}
func TestTraverse_DepthLimit(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("A", "forward", nil, nil, 1, 100)
// Depth 1: A -> B, A -> E (only direct neighbors)
names := nodeNames(result.Nodes)
if !contains(names, "A") || !contains(names, "B") || !contains(names, "E") {
t.Errorf("depth-1 should include A, B, E; got %v", names)
}
if contains(names, "C") || contains(names, "D") {
t.Errorf("depth-1 should NOT include C or D; got %v", names)
}
}
func TestTraverse_MaxNodesLimit(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("A", "forward", nil, nil, 10, 3)
// Should only return at most 3 nodes
if len(result.Nodes) > 3 {
t.Errorf("maxNodes=3 but got %d nodes", len(result.Nodes))
}
if !result.Stats.Truncated {
t.Error("should be truncated with maxNodes=3")
}
}
func TestTraverse_EdgesConsistentWhenTruncated(t *testing.T) {
g, _ := buildTestGraph()
// maxNodes=2 truncates the result; every returned edge must still reference
// only nodes present in result.Nodes (no dangling edges to capped-out nodes).
result := g.Traverse("A", "forward", nil, nil, 10, 2)
if !result.Stats.Truncated {
t.Fatal("expected truncation with maxNodes=2")
}
inSet := map[string]bool{}
for _, n := range result.Nodes {
inSet[n.Name] = true
}
for _, e := range result.Edges {
if !inSet[e.Source] || !inSet[e.Target] {
t.Errorf("edge %s -> %s references a node absent from result.Nodes %v", e.Source, e.Target, nodeNames(result.Nodes))
}
}
}
func TestTraverse_RelationKindFilter(t *testing.T) {
g, _ := buildTestGraph()
// Only follow "calls" relations from A
result := g.Traverse("A", "forward", []string{RelCalls}, nil, 10, 100)
names := nodeNames(result.Nodes)
// A --calls-> B --calls-> C --calls-> D (imports to E skipped)
for _, want := range []string{"A", "B", "C", "D"} {
if !contains(names, want) {
t.Errorf("calls-only traverse missing %q", want)
}
}
if contains(names, "E") {
t.Error("E should not be reachable via calls-only")
}
}
func TestTraverse_NodeKindFilter(t *testing.T) {
g, _ := buildTestGraph()
// Traverse from A but only include module-kind nodes in results
// C and E are modules, A/B/D are symbols
result := g.Traverse("A", "forward", nil, []string{KindModule}, 10, 100)
names := nodeNames(result.Nodes)
// Should traverse through symbols but only include modules in result
// A(sym) -> B(sym) -> C(mod) -> D(sym), A -> E(mod) -> C
if !contains(names, "C") || !contains(names, "E") {
t.Errorf("module filter should include C and E; got %v", names)
}
// Start node A is always included regardless of filter
if !contains(names, "A") {
t.Errorf("start node A should always be included; got %v", names)
}
}
func TestTraverse_CycleHandling(t *testing.T) {
g, _ := buildCyclicGraph()
result := g.Traverse("A", "forward", nil, nil, 20, 100)
// Should visit A, B, C without infinite loop
if result.Stats.NodesVisited != 3 {
t.Errorf("NodesVisited = %d, want 3 (cycle should be handled)", result.Stats.NodesVisited)
}
names := nodeNames(result.Nodes)
for _, want := range []string{"A", "B", "C"} {
if !contains(names, want) {
t.Errorf("cycle traverse missing %q", want)
}
}
}
func TestTraverse_DisconnectedNode(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("F", "forward", nil, nil, 10, 100)
// F is disconnected, should only return F itself
if len(result.Nodes) != 1 || result.Nodes[0].Name != "F" {
t.Errorf("disconnected traverse: got %v, want [F]", nodeNames(result.Nodes))
}
}
func TestTraverse_NonexistentStart(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("NONEXISTENT", "forward", nil, nil, 10, 100)
// Should still return the start node (with no metadata)
if len(result.Nodes) != 1 || result.Nodes[0].Name != "NONEXISTENT" {
t.Errorf("nonexistent start: got %v, want [NONEXISTENT]", nodeNames(result.Nodes))
}
}
func TestFindPath_DirectConnection(t *testing.T) {
g, _ := buildTestGraph()
result := g.FindPath("A", "B", nil, 10)
if !result.Found {
t.Fatal("path A->B should be found")
}
if len(result.Path) != 2 {
t.Errorf("path length = %d, want 2 (A, B)", len(result.Path))
}
if result.Path[0].Name != "A" || result.Path[1].Name != "B" {
t.Errorf("path = %v, want [A, B]", pathNames(result.Path))
}
}
func TestFindPath_MultiHop(t *testing.T) {
g, _ := buildTestGraph()
result := g.FindPath("A", "D", nil, 10)
if !result.Found {
t.Fatal("path A->D should be found")
}
// Shortest: A -> B -> C -> D
if len(result.Path) != 4 {
t.Errorf("path length = %d, want 4 (A, B, C, D); path = %v", len(result.Path), pathNames(result.Path))
}
// Check edges
if len(result.Edges) != 3 {
t.Errorf("edges = %d, want 3", len(result.Edges))
}
}
func TestFindPath_NoPath(t *testing.T) {
g, _ := buildTestGraph()
// F is disconnected
result := g.FindPath("A", "F", nil, 10)
if result.Found {
t.Error("path A->F should not exist")
}
if len(result.Path) != 0 {
t.Errorf("path should be empty, got %v", pathNames(result.Path))
}
}
func TestFindPath_SameNode(t *testing.T) {
g, _ := buildTestGraph()
result := g.FindPath("A", "A", nil, 10)
if !result.Found {
t.Fatal("path A->A should be found (trivial)")
}
if len(result.Path) != 1 {
t.Errorf("path length = %d, want 1 (just A)", len(result.Path))
}
}
func TestFindPath_DepthLimit(t *testing.T) {
g, _ := buildTestGraph()
// A -> D needs 3 hops, limit to 2
result := g.FindPath("A", "D", nil, 2)
if result.Found {
t.Error("path A->D should not be found with maxDepth=2")
}
}
func TestFindPath_RelationKindFilter(t *testing.T) {
g, _ := buildTestGraph()
// Only imports: A --imports-> E, but no path from E to D via imports
result := g.FindPath("A", "D", []string{RelImports}, 10)
if result.Found {
t.Error("path A->D via imports only should not exist")
}
}
func TestFindPath_WithCycle(t *testing.T) {
g, _ := buildCyclicGraph()
result := g.FindPath("A", "C", nil, 10)
if !result.Found {
t.Fatal("path A->C should be found")
}
// Shortest: A -> B -> C
if len(result.Path) != 3 {
t.Errorf("path length = %d, want 3; path = %v", len(result.Path), pathNames(result.Path))
}
}
func TestImpactSet_Basic(t *testing.T) {
g, _ := buildTestGraph()
result := g.ImpactSet("C", 10, 100, false)
if result.Target != "C" {
t.Errorf("Target = %q, want C", result.Target)
}
// Who depends on C? B calls C, E calls C, A calls B and imports E
// Reverse: C <- B <- A, C <- E <- A (A already counted)
// Depth 1: B, E
// Depth 2: A (via B), A (via E, already counted)
depth1 := result.ByDepth[1]
depth1Names := make([]string, len(depth1))
for i, n := range depth1 {
depth1Names[i] = n.Name
}
if len(depth1) != 2 {
t.Errorf("depth 1 = %d (%v), want 2 (B, E)", len(depth1), depth1Names)
}
if !contains(depth1Names, "B") || !contains(depth1Names, "E") {
t.Errorf("depth 1 should include B and E; got %v", depth1Names)
}
depth2 := result.ByDepth[2]
if len(depth2) != 1 || depth2[0].Name != "A" {
depth2Names := make([]string, len(depth2))
for i, n := range depth2 {
depth2Names[i] = n.Name
}
t.Errorf("depth 2 = %v, want [A]", depth2Names)
}
if result.Summary == "" {
t.Error("summary should not be empty")
}
}
func TestReachableCount(t *testing.T) {
g, _ := buildTestGraph()
// Reverse from C: B and E depend on C directly, A transitively. 3 total.
if got := g.reachableCount([]string{"C"}, "reverse", 10); got != 3 {
t.Errorf("reachableCount(C, reverse) = %d, want 3", got)
}
// Forward from A: B, E (direct), C, D (transitive). 4 total.
if got := g.reachableCount([]string{"A"}, "forward", 10); got != 4 {
t.Errorf("reachableCount(A, forward) = %d, want 4", got)
}
// Depth limit: reverse from C at depth 1 reaches only B and E.
if got := g.reachableCount([]string{"C"}, "reverse", 1); got != 2 {
t.Errorf("reachableCount(C, reverse, depth 1) = %d, want 2", got)
}
}
func TestReachableCount_Cyclic(t *testing.T) {
g, _ := buildCyclicGraph()
// A -> B -> C -> A. Reverse from A reaches C then B (A is the seed). 2 total,
// and the cycle must not loop forever.
if got := g.reachableCount([]string{"A"}, "reverse", 10); got != 2 {
t.Errorf("reachableCount(A, reverse) on cycle = %d, want 2", got)
}
}
func TestImpactSet_TotalDependents(t *testing.T) {
g, _ := buildTestGraph()
// Not truncated: total equals the shown dependents (B, E, A = 3).
result := g.ImpactSet("C", 10, 100, false)
if result.TotalDependents != 3 {
t.Errorf("TotalDependents = %d, want 3", result.TotalDependents)
}
if strings.Contains(result.Summary, "showing") {
t.Errorf("summary should not mention 'showing' when not truncated: %q", result.Summary)
}
}
func TestImpactSet_TotalDependents_Truncated(t *testing.T) {
g, _ := buildTestGraph()
// maxNodes=2 leaves room for the seed (C) plus one dependent, so the display
// is truncated but the total must still report all 3 dependents.
result := g.ImpactSet("C", 10, 2, false)
if result.TotalDependents != 3 {
t.Errorf("TotalDependents = %d, want 3 (accurate despite cap)", result.TotalDependents)
}
if !result.Stats.Truncated {
t.Error("expected Stats.Truncated with maxNodes=2")
}
if !strings.Contains(result.Summary, "3 total dependents (showing 1)") {
t.Errorf("summary should report accurate total and showing count; got %q", result.Summary)
}
}
func TestImpactSet_WithForward(t *testing.T) {
g, _ := buildTestGraph()
result := g.ImpactSet("C", 10, 100, true)
if result.Forward == nil {
t.Fatal("forward dependencies should be included")
}
// C -> D (forward)
names := nodeNames(result.Forward.Nodes)
if !contains(names, "D") {
t.Errorf("forward from C should include D; got %v", names)
}
}
func TestImpactSet_LeafNode(t *testing.T) {
g, _ := buildTestGraph()
// D has no dependents
result := g.ImpactSet("D", 10, 100, false)
totalDependents := 0
for _, nodes := range result.ByDepth {
totalDependents += len(nodes)
}
// C calls D, B calls C, E calls C, A calls B and imports E
if totalDependents < 1 {
t.Errorf("D should have at least C as direct dependent, got %d total", totalDependents)
}
}
func TestImpactSet_CycleHandling(t *testing.T) {
g, _ := buildCyclicGraph()
result := g.ImpactSet("A", 20, 100, false)
// In a cycle A->B->C->A, impact of A is: B (depth 1 reverse from A via C->A),
// Actually reverse: who points TO A? C points to A. Who points to C? B. Who points to B? A (already visited)
// So: depth 1: C, depth 2: B
totalDependents := 0
for _, nodes := range result.ByDepth {
totalDependents += len(nodes)
}
if totalDependents != 2 {
t.Errorf("cycle impact should have 2 dependents (B, C), got %d", totalDependents)
}
}
func TestBuildGraph_ViaStore(t *testing.T) {
s := NewStore()
s.Add(
Fact{Kind: KindSymbol, Name: "X", File: "x.go", Relations: []Relation{
{Kind: RelCalls, Target: "Y"},
}},
Fact{Kind: KindSymbol, Name: "Y", File: "y.go"},
)
// Before BuildGraph
if s.Graph() != nil {
t.Error("Graph should be nil before BuildGraph")
}
s.BuildGraph()
g := s.Graph()
if g == nil {
t.Fatal("Graph should not be nil after BuildGraph")
}
if g.NodeCount() != 2 {
t.Errorf("NodeCount = %d, want 2", g.NodeCount())
}
if g.EdgeCount() != 1 {
t.Errorf("EdgeCount = %d, want 1", g.EdgeCount())
}
}
func TestBuildGraph_ClearedByStoreClear(t *testing.T) {
s := NewStore()
s.Add(Fact{Kind: KindSymbol, Name: "X", File: "x.go"})
s.BuildGraph()
if s.Graph() == nil {
t.Fatal("Graph should exist after BuildGraph")
}
s.Clear()
if s.Graph() != nil {
t.Error("Graph should be nil after Clear")
}
}
func TestTraverse_DefaultParameters(t *testing.T) {
g, _ := buildTestGraph()
// Test with zero values (should use defaults)
result := g.Traverse("A", "forward", nil, nil, 0, 0)
// Default maxDepth=5, maxNodes=100
// Should still find all reachable nodes
names := nodeNames(result.Nodes)
if len(names) < 5 {
t.Errorf("default params should find all reachable nodes; got %d", len(names))
}
}
func TestFindPath_DefaultMaxDepth(t *testing.T) {
g, _ := buildTestGraph()
// maxDepth=0 should use default (10)
result := g.FindPath("A", "D", nil, 0)
if !result.Found {
t.Error("should find path A->D with default maxDepth")
}
}
func TestTraverse_EdgesAreRecorded(t *testing.T) {
g, _ := buildTestGraph()
result := g.Traverse("A", "forward", []string{RelCalls}, nil, 1, 100)
// A --calls-> B only (depth 1, calls only)
if len(result.Edges) != 1 {
t.Errorf("edges = %d, want 1", len(result.Edges))
}
if len(result.Edges) > 0 {
e := result.Edges[0]
if e.Source != "A" || e.Target != "B" || e.Kind != RelCalls {
t.Errorf("edge = %+v, want A->B calls", e)
}
}
}
func TestFindPath_EdgesHaveCorrectKinds(t *testing.T) {
g, _ := buildTestGraph()
result := g.FindPath("A", "C", nil, 10)
if !result.Found {
t.Fatal("path should be found")
}
// Shortest path A->B->C, edges should have their relation kinds
for _, e := range result.Edges {
if e.Kind == "" {
t.Error("edge kind should not be empty")
}
}
}
func TestNewGraph_DeduplicatesEdges(t *testing.T) {
s := NewStore()
// Two facts with identical relations (same source->kind->target).
s.Add(
Fact{Kind: KindDependency, Name: "dep1", File: "models/a.rb", Relations: []Relation{
{Kind: RelDependsOn, Target: "User"},
}},
Fact{Kind: KindDependency, Name: "dep2", File: "models/b.rb", Relations: []Relation{
{Kind: RelDependsOn, Target: "User"},
}},
Fact{Kind: KindSymbol, Name: "User", File: "models/user.rb"},
// A fact with a duplicate relation on itself.
Fact{Kind: KindModule, Name: "A", File: "a.rb", Relations: []Relation{
{Kind: RelImports, Target: "B"},
}},
// Another fact that also creates the same A->imports->B edge.
Fact{Kind: KindDependency, Name: "A -> B", File: "a.rb", Relations: []Relation{
{Kind: RelImports, Target: "B"},
}},
Fact{Kind: KindModule, Name: "B", File: "b.rb"},
)
s.BuildGraph()
g := s.Graph()
// dep1->User and dep2->User are distinct source nodes, so both edges exist.
// But A->imports->B should appear only once despite two facts creating it.
fwd := g.Forward()
aEdges := fwd["A"]
count := 0
for _, e := range aEdges {
if e.RelKind == RelImports && e.Target == "B" {
count++
}
}
if count != 1 {
t.Errorf("A->imports->B should appear exactly once, got %d", count)
}
// Reverse: B should have exactly one incoming imports edge from A.
rev := g.Reverse()
bEdges := rev["B"]
count = 0
for _, e := range bEdges {
if e.RelKind == RelImports && e.Target == "A" {
count++
}
}
if count != 1 {
t.Errorf("B reverse imports from A should appear exactly once, got %d", count)
}
}
// --- helpers ---
func nodeNames(nodes []TraversalNode) []string {
names := make([]string, len(nodes))
for i, n := range nodes {
names[i] = n.Name
}
return names
}
func pathNames(nodes []TraversalNode) []string {
return nodeNames(nodes)
}
func contains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
// TestNewGraph_CrossRepoCallNormalisation verifies that cross-repo call targets
// whose import paths include a known Go module path are normalised to the
// repo-relative fact name when building the graph.
func TestNewGraph_CrossRepoCallNormalisation(t *testing.T) {
// Simulate two repos loaded together:
// - go-auth repo: root module fact with modulePath prop, plus symbol facts
// - golf repo: symbol with a call target using the full external import path
facts := []Fact{
// go-auth root module fact — carries the Go module path
{
Kind: KindModule,
Name: ".",
Repo: "go-auth",
Props: map[string]any{
"package": "goauth",
"language": "go",
"modulePath": "github.com/dejo1307/go-auth",
},
},
// go-auth adapters module
{Kind: KindModule, Name: "adapters", Repo: "go-auth"},
// go-auth symbol: adapters.AuthHandler.Login
{
Kind: KindSymbol,
Name: "adapters.AuthHandler.Login",
Repo: "go-auth",
},
// golf symbol with an unresolved external call target
{
Kind: KindSymbol,
Name: "internal/auth.LoginWrapper.Login",
Repo: "golf",
Relations: []Relation{
{Kind: RelCalls, Target: "github.com/dejo1307/go-auth/adapters.AuthHandler.Login"},
},
},
}
g := NewGraph(facts)
// The forward edge from golf's LoginWrapper.Login should point to the
// normalised fact name "adapters.AuthHandler.Login", not the full import path.
edges := g.forward["internal/auth.LoginWrapper.Login"]
if len(edges) == 0 {
t.Fatal("expected at least one forward edge from LoginWrapper.Login")
}
found := false
for _, e := range edges {
if e.RelKind == RelCalls && e.Target == "adapters.AuthHandler.Login" {
found = true
}
}
if !found {
t.Errorf("expected normalised call edge to adapters.AuthHandler.Login; got edges: %v", edges)
}
}
// TestNormalizeExternalTarget verifies the helper directly.
func TestNormalizeExternalTarget(t *testing.T) {
mods := map[string]struct{}{"github.com/dejo1307/go-auth": {}}
cases := []struct {
target string
want string
}{
{"github.com/dejo1307/go-auth/adapters.Handler.Login", "adapters.Handler.Login"},
{"github.com/dejo1307/go-auth.SecurityHeaders", "..SecurityHeaders"},
{"github.com/other/lib/pkg.Type.Method", ""}, // no matching module
{"github.com/dejo1307/go-auth", ""}, // no separator after module path
}
for _, tc := range cases {
got := normalizeExternalTarget(tc.target, mods)
if got != tc.want {
t.Errorf("normalizeExternalTarget(%q) = %q, want %q", tc.target, got, tc.want)
}
}
}
// buildTypeMethodStore models a Go type with a method that makes a call, plus a
// dangling call into an unanalyzed package. The struct and method are separate
// sibling facts with no edge between them, mirroring the goextractor output.
func buildTypeMethodStore() (*Graph, *Store) {
s := NewStore()
s.Add(
Fact{Kind: KindSymbol, Name: "auth.AuthHandler", File: "auth/handler.go", Line: 1,
Props: map[string]any{"symbol_kind": SymbolStruct}},
Fact{Kind: KindSymbol, Name: "auth.AuthHandler.Login", File: "auth/handler.go", Line: 10,
Props: map[string]any{"symbol_kind": SymbolMethod},
Relations: []Relation{
{Kind: RelCalls, Target: "jwt.Sign"},
{Kind: RelCalls, Target: "external.Unknown"}, // no backing fact
}},
Fact{Kind: KindSymbol, Name: "jwt.Sign", File: "jwt/jwt.go", Line: 5,
Props: map[string]any{"symbol_kind": SymbolFunc}},
)
s.BuildGraph()
return s.Graph(), s
}
func TestNewGraph_StructToMethodEdges(t *testing.T) {
g, _ := buildTypeMethodStore()
var found bool
for _, e := range g.Forward()["auth.AuthHandler"] {
if e.RelKind == RelHasMethod && e.Target == "auth.AuthHandler.Login" {
found = true
}
}
if !found {
t.Fatalf("expected has_method edge auth.AuthHandler -> auth.AuthHandler.Login, got %+v", g.Forward()["auth.AuthHandler"])
}
// A package-level function whose owner ("jwt") is not a type must NOT get a
// has_method edge.
for _, e := range g.Forward()["jwt"] {
if e.RelKind == RelHasMethod {
t.Errorf("unexpected has_method edge from non-type owner: %+v", e)
}
}
}
func TestTraverse_ForwardFromStructSurfacesMethodCalls(t *testing.T) {
g, _ := buildTypeMethodStore()
result := g.Traverse("auth.AuthHandler", "forward", nil, nil, 5, 100)
names := nodeNames(result.Nodes)
for _, want := range []string{"auth.AuthHandler.Login", "jwt.Sign"} {
if !contains(names, want) {
t.Errorf("forward traverse from struct missing %q; got %v", want, names)
}
}
}
func TestTraverse_UnresolvedTargetMarked(t *testing.T) {
g, _ := buildTypeMethodStore()
result := g.Traverse("auth.AuthHandler.Login", "forward", nil, nil, 5, 100)
var sawUnresolved, sawResolved bool
for _, n := range result.Nodes {
switch n.Name {
case "external.Unknown":
sawUnresolved = true
if !n.Unresolved {
t.Error("external.Unknown should be marked Unresolved")
}
case "jwt.Sign":
sawResolved = true
if n.Unresolved {
t.Error("jwt.Sign is a real fact and must not be marked Unresolved")
}
}
}
if !sawUnresolved {
t.Error("expected an unresolved node external.Unknown in the result")
}
if !sawResolved {
t.Error("expected the resolved node jwt.Sign in the result")
}
}
// buildCrossRepoTypeStore models a go-auth library type (struct + method +
// constructor) consumed by a golf caller, mirroring how the cross-repo call
// normalisation lands golf's calls onto go-auth's local symbol names.
func buildCrossRepoTypeStore() (*Graph, *Store) {
s := NewStore()
s.Add(
// go-auth: the AuthHandler type, one method, and its constructor.
Fact{Kind: KindSymbol, Name: "adapters.AuthHandler", File: "adapters/h.go", Line: 1, Repo: "go-auth",
Props: map[string]any{"symbol_kind": SymbolStruct}},
Fact{Kind: KindSymbol, Name: "adapters.AuthHandler.Login", File: "adapters/h.go", Line: 10, Repo: "go-auth",
Props: map[string]any{"symbol_kind": SymbolMethod}},
Fact{Kind: KindSymbol, Name: "adapters.NewAuthHandler", File: "adapters/h.go", Line: 30, Repo: "go-auth",
Props: map[string]any{"symbol_kind": SymbolFunc}},
// golf: a setup function that calls the constructor and a method.
Fact{Kind: KindSymbol, Name: "pkg/auth.Setup", File: "pkg/auth/setup.go", Line: 28, Repo: "golf",
Props: map[string]any{"symbol_kind": SymbolFunc}, Relations: []Relation{
{Kind: RelCalls, Target: "adapters.NewAuthHandler"},
{Kind: RelCalls, Target: "adapters.AuthHandler.Login"},
}},
)
s.BuildGraph()
return s.Graph(), s
}
func TestImpactSet_TypeRollup(t *testing.T) {
g, _ := buildCrossRepoTypeStore()
// Impact on the bare struct must roll up its method + constructor and surface
// the cross-repo caller (previously this returned "no dependents").
res := g.ImpactSet("adapters.AuthHandler", 3, 100, false)
names := nodeNames(impactNodes(res))
if !contains(names, "pkg/auth.Setup") {
t.Errorf("type rollup did not surface cross-repo caller pkg/auth.Setup; got %v", names)
}
// Seeds (the type entity itself) must not appear as dependents.
for _, seed := range []string{"adapters.AuthHandler", "adapters.AuthHandler.Login", "adapters.NewAuthHandler"} {
if contains(names, seed) {
t.Errorf("seed %q should be excluded from dependents", seed)
}
}
if !reflect.DeepEqual(res.CrossRepoImpact, []string{"golf"}) {
t.Errorf("CrossRepoImpact = %v, want [golf]", res.CrossRepoImpact)
}
}
func TestImpactSet_CrossRepoImpactField(t *testing.T) {
g, _ := buildCrossRepoTypeStore()
// A plain function target (not a type) still reports cross-repo dependents and
// per-node repo.
res := g.ImpactSet("adapters.NewAuthHandler", 2, 100, false)
if !reflect.DeepEqual(res.CrossRepoImpact, []string{"golf"}) {
t.Fatalf("CrossRepoImpact = %v, want [golf]", res.CrossRepoImpact)
}
var sawRepo bool
for _, nodes := range res.ByDepth {
for _, n := range nodes {
if n.Name == "pkg/auth.Setup" {
sawRepo = true
if n.Repo != "golf" {
t.Errorf("dependent node repo = %q, want golf", n.Repo)
}
}
}
}
if !sawRepo {
t.Error("expected pkg/auth.Setup among dependents")
}
}
func TestImpactSet_NonTypeUnchanged(t *testing.T) {
// Single-repo graph (no Repo tags): rollup is inert and CrossRepoImpact stays nil.
g, _ := buildTestGraph()
res := g.ImpactSet("D", 5, 100, false)
if res.CrossRepoImpact != nil {
t.Errorf("CrossRepoImpact should be nil for same-repo graph, got %v", res.CrossRepoImpact)
}
// D is called by C (a module here) — at least one dependent is found, as before.
if len(impactNodes(res)) == 0 {
t.Error("expected at least one dependent for D")
}
}
// TestTraverseFrom_ReverseTypeRollup documents the #6 fix: reverse traversal of a
// bare type finds nothing (its reverse adjacency is empty — callers reference its
// methods/constructor, not the type), but seeding with RollupSeeds surfaces the
// cross-repo caller, matching impact_analysis.
func TestTraverseFrom_ReverseTypeRollup(t *testing.T) {
g, _ := buildCrossRepoTypeStore()
// Bare reverse traversal of the type: no callers reference the type directly.
bare := g.Traverse("adapters.AuthHandler", "reverse", nil, nil, 3, 100)
if got := nodeNames(bare.Nodes); contains(got, "pkg/auth.Setup") {
t.Errorf("bare reverse unexpectedly found the caller; got %v", got)
}
// Seeded with the type's methods + constructor: the cross-repo caller appears.
seeded := g.TraverseFrom(g.RollupSeeds("adapters.AuthHandler"), "reverse", nil, nil, 3, 100)
if got := nodeNames(seeded.Nodes); !contains(got, "pkg/auth.Setup") {
t.Errorf("seeded reverse did not surface cross-repo caller pkg/auth.Setup; got %v", got)
}
}
// impactNodes flattens an ImpactResult's depth buckets into a node slice.
func impactNodes(res ImpactResult) []TraversalNode {
var out []TraversalNode
for _, nodes := range res.ByDepth {
out = append(out, nodes...)
}
return out
}
// TestArchitecturalReverse_ExcludesReferenceKinds verifies that reference-only
// facts (test_ref/file_ref) are dropped from the architectural reverse index.
// Their RelCalls edges must not count as dependents — otherwise they inflate
// god-class fan-in and hotspots centrality and drift the outlier threshold
// (GAP-XL-15). The unfiltered Reverse() index must still surface them, since
// orphans/impact_analysis rely on seeing test/file references.
func TestArchitecturalReverse_ExcludesReferenceKinds(t *testing.T) {
s := NewStore()
s.Add(
Fact{Kind: KindSymbol, Name: "Prod", File: "app/prod.rb"},
Fact{Kind: KindSymbol, Name: "Caller", File: "app/caller.rb", Relations: []Relation{
{Kind: RelCalls, Target: "Prod"},
}},
Fact{Kind: KindTestRef, Name: "spec/prod_spec.rb", File: "spec/prod_spec.rb", Relations: []Relation{
{Kind: RelCalls, Target: "Prod"},
}},
Fact{Kind: KindFileRef, Name: "config/initializers/boot.rb", File: "config/initializers/boot.rb", Relations: []Relation{
{Kind: RelCalls, Target: "Prod"},
}},
)
s.BuildGraph()
g := s.Graph()
// Unfiltered: all three sources (symbol + test_ref + file_ref) are dependents.
if got := len(g.Reverse()["Prod"]); got != 3 {
t.Fatalf("Reverse()[Prod] = %d edges, want 3 (Caller + test_ref + file_ref)", got)
}
// Architectural: only the symbol dependent survives.
arch := g.ArchitecturalReverse()["Prod"]
if len(arch) != 1 {
t.Fatalf("ArchitecturalReverse()[Prod] = %d edges, want 1 (symbol only): %+v", len(arch), arch)
}
// In a reverse edge, Edge.Target holds the SOURCE fact name.
if arch[0].Target != "Caller" {
t.Errorf("surviving dependent = %q, want Caller (the symbol source)", arch[0].Target)
}
}