|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "testing" |
| 7 | + |
| 8 | + requestctx "github.com/tae2089/code-context-graph/internal/ctx" |
| 9 | + "github.com/tae2089/code-context-graph/internal/domain/graph" |
| 10 | +) |
| 11 | + |
| 12 | +// seedCrossNamespaceDeps stores web.Login -> (cross ref) -> auth.ValidateToken -> auth.RecordAudit. |
| 13 | +func seedCrossNamespaceDeps(t *testing.T, deps *Deps) (webLogin, authValidate, authAudit uint) { |
| 14 | + t.Helper() |
| 15 | + st := testGraphStoreFor(deps) |
| 16 | + seed := func(namespace string, node graph.Node) uint { |
| 17 | + t.Helper() |
| 18 | + ctx := requestctx.WithNamespace(context.Background(), namespace) |
| 19 | + if err := st.UpsertNodes(ctx, []graph.Node{node}); err != nil { |
| 20 | + t.Fatalf("seed node: %v", err) |
| 21 | + } |
| 22 | + stored, err := st.GetNode(ctx, node.QualifiedName) |
| 23 | + if err != nil || stored == nil { |
| 24 | + t.Fatalf("load node %s: %v", node.QualifiedName, err) |
| 25 | + } |
| 26 | + return stored.ID |
| 27 | + } |
| 28 | + webLogin = seed("web", graph.Node{QualifiedName: "web.Login", Kind: graph.NodeKindFunction, Name: "Login", FilePath: "internal/web/login.go", StartLine: 5, EndLine: 25}) |
| 29 | + authValidate = seed("auth-svc", graph.Node{QualifiedName: "auth.ValidateToken", Kind: graph.NodeKindFunction, Name: "ValidateToken", FilePath: "internal/auth/token.go", StartLine: 10, EndLine: 20}) |
| 30 | + authAudit = seed("auth-svc", graph.Node{QualifiedName: "auth.RecordAudit", Kind: graph.NodeKindFunction, Name: "RecordAudit", FilePath: "internal/audit/audit.go", StartLine: 3, EndLine: 9}) |
| 31 | + |
| 32 | + authCtx := requestctx.WithNamespace(context.Background(), "auth-svc") |
| 33 | + if err := st.UpsertEdges(authCtx, []graph.Edge{{ |
| 34 | + FromNodeID: authValidate, ToNodeID: authAudit, Kind: graph.EdgeKindCalls, |
| 35 | + FilePath: "internal/auth/token.go", Line: 15, Fingerprint: "calls:internal/auth/token.go:RecordAudit:15", |
| 36 | + }}); err != nil { |
| 37 | + t.Fatalf("seed edge: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + resolved := authValidate |
| 41 | + if err := st.ReplaceCrossRefsFrom(context.Background(), "web", []graph.CrossRef{{ |
| 42 | + FromNamespace: "web", FromNodeID: webLogin, Raw: "ccg://auth-svc/internal/auth/token.go#ValidateToken", |
| 43 | + ToNamespace: "auth-svc", ToPath: "internal/auth/token.go", ToSymbol: "ValidateToken", |
| 44 | + ResolvedNodeID: &resolved, Status: graph.CrossRefStatusResolved, Source: graph.CrossRefSourceAnnotation, |
| 45 | + }}); err != nil { |
| 46 | + t.Fatalf("seed cross ref: %v", err) |
| 47 | + } |
| 48 | + return webLogin, authValidate, authAudit |
| 49 | +} |
| 50 | + |
| 51 | +func TestGetImpactRadius_CrossNamespace(t *testing.T) { |
| 52 | + deps := setupTestDeps(t) |
| 53 | + seedCrossNamespaceDeps(t, deps) |
| 54 | + |
| 55 | + result := callTool(t, deps, "get_impact_radius", map[string]any{ |
| 56 | + "qualified_name": "web.Login", |
| 57 | + "namespace": "web", |
| 58 | + "depth": 2, |
| 59 | + "cross_namespace": true, |
| 60 | + }) |
| 61 | + var payload struct { |
| 62 | + Nodes []struct { |
| 63 | + QualifiedName string `json:"qualified_name"` |
| 64 | + Namespace string `json:"namespace"` |
| 65 | + } `json:"nodes"` |
| 66 | + } |
| 67 | + if err := json.Unmarshal([]byte(resultTextOf(t, result)), &payload); err != nil { |
| 68 | + t.Fatalf("unmarshal: %v", err) |
| 69 | + } |
| 70 | + seen := map[string]string{} |
| 71 | + for _, n := range payload.Nodes { |
| 72 | + seen[n.QualifiedName] = n.Namespace |
| 73 | + } |
| 74 | + if seen["auth.ValidateToken"] != "auth-svc" { |
| 75 | + t.Fatalf("impact nodes = %v, want auth.ValidateToken labeled auth-svc", seen) |
| 76 | + } |
| 77 | + if seen["auth.RecordAudit"] != "auth-svc" { |
| 78 | + t.Fatalf("impact nodes = %v, want depth-2 traversal to continue inside auth-svc", seen) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestGetImpactRadius_CrossNamespaceReverse(t *testing.T) { |
| 83 | + deps := setupTestDeps(t) |
| 84 | + seedCrossNamespaceDeps(t, deps) |
| 85 | + |
| 86 | + result := callTool(t, deps, "get_impact_radius", map[string]any{ |
| 87 | + "qualified_name": "auth.ValidateToken", |
| 88 | + "namespace": "auth-svc", |
| 89 | + "depth": 1, |
| 90 | + "cross_namespace": true, |
| 91 | + }) |
| 92 | + var payload struct { |
| 93 | + Nodes []struct { |
| 94 | + QualifiedName string `json:"qualified_name"` |
| 95 | + Namespace string `json:"namespace"` |
| 96 | + } `json:"nodes"` |
| 97 | + } |
| 98 | + if err := json.Unmarshal([]byte(resultTextOf(t, result)), &payload); err != nil { |
| 99 | + t.Fatalf("unmarshal: %v", err) |
| 100 | + } |
| 101 | + found := false |
| 102 | + for _, n := range payload.Nodes { |
| 103 | + if n.QualifiedName == "web.Login" && n.Namespace == "web" { |
| 104 | + found = true |
| 105 | + } |
| 106 | + } |
| 107 | + if !found { |
| 108 | + t.Fatalf("reverse impact = %+v, want web.Login from web namespace", payload.Nodes) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestGetImpactRadius_DefaultStaysNamespaceScoped(t *testing.T) { |
| 113 | + deps := setupTestDeps(t) |
| 114 | + seedCrossNamespaceDeps(t, deps) |
| 115 | + |
| 116 | + result := callTool(t, deps, "get_impact_radius", map[string]any{ |
| 117 | + "qualified_name": "web.Login", |
| 118 | + "namespace": "web", |
| 119 | + "depth": 2, |
| 120 | + }) |
| 121 | + var payload struct { |
| 122 | + Nodes []struct { |
| 123 | + QualifiedName string `json:"qualified_name"` |
| 124 | + } `json:"nodes"` |
| 125 | + } |
| 126 | + if err := json.Unmarshal([]byte(resultTextOf(t, result)), &payload); err != nil { |
| 127 | + t.Fatalf("unmarshal: %v", err) |
| 128 | + } |
| 129 | + for _, n := range payload.Nodes { |
| 130 | + if n.QualifiedName == "auth.ValidateToken" { |
| 131 | + t.Fatal("default impact crossed namespaces without cross_namespace flag") |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestTraceFlow_CrossNamespace(t *testing.T) { |
| 137 | + deps := setupTestDeps(t) |
| 138 | + _, authValidate, authAudit := seedCrossNamespaceDeps(t, deps) |
| 139 | + |
| 140 | + result := callTool(t, deps, "trace_flow", map[string]any{ |
| 141 | + "qualified_name": "web.Login", |
| 142 | + "namespace": "web", |
| 143 | + "cross_namespace": true, |
| 144 | + }) |
| 145 | + var payload struct { |
| 146 | + Members []struct { |
| 147 | + NodeID uint `json:"node_id"` |
| 148 | + } `json:"members"` |
| 149 | + } |
| 150 | + if err := json.Unmarshal([]byte(resultTextOf(t, result)), &payload); err != nil { |
| 151 | + t.Fatalf("unmarshal: %v", err) |
| 152 | + } |
| 153 | + got := map[uint]bool{} |
| 154 | + for _, m := range payload.Members { |
| 155 | + got[m.NodeID] = true |
| 156 | + } |
| 157 | + if !got[authValidate] || !got[authAudit] { |
| 158 | + t.Fatalf("flow members = %+v, want continuation into auth-svc nodes %d and %d", payload.Members, authValidate, authAudit) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestListCrossRefs_Directions(t *testing.T) { |
| 163 | + deps := setupTestDeps(t) |
| 164 | + webLogin, authValidate, _ := seedCrossNamespaceDeps(t, deps) |
| 165 | + |
| 166 | + outbound := callTool(t, deps, "list_cross_refs", map[string]any{"namespace": "web", "direction": "outbound"}) |
| 167 | + var outPayload struct { |
| 168 | + Refs []struct { |
| 169 | + FromNamespace string `json:"from_namespace"` |
| 170 | + FromNodeID uint `json:"from_node_id"` |
| 171 | + ToNamespace string `json:"to_namespace"` |
| 172 | + ResolvedNodeID *uint `json:"resolved_node_id"` |
| 173 | + Status string `json:"status"` |
| 174 | + } `json:"refs"` |
| 175 | + } |
| 176 | + if err := json.Unmarshal([]byte(resultTextOf(t, outbound)), &outPayload); err != nil { |
| 177 | + t.Fatalf("unmarshal outbound: %v", err) |
| 178 | + } |
| 179 | + if len(outPayload.Refs) != 1 { |
| 180 | + t.Fatalf("outbound refs = %d, want 1", len(outPayload.Refs)) |
| 181 | + } |
| 182 | + ref := outPayload.Refs[0] |
| 183 | + if ref.FromNodeID != webLogin || ref.ToNamespace != "auth-svc" || ref.Status != "resolved" || ref.ResolvedNodeID == nil || *ref.ResolvedNodeID != authValidate { |
| 184 | + t.Fatalf("outbound ref = %+v, want resolved web -> auth-svc link", ref) |
| 185 | + } |
| 186 | + |
| 187 | + inbound := callTool(t, deps, "list_cross_refs", map[string]any{"namespace": "auth-svc", "direction": "inbound"}) |
| 188 | + var inPayload struct { |
| 189 | + Refs []struct { |
| 190 | + FromNamespace string `json:"from_namespace"` |
| 191 | + } `json:"refs"` |
| 192 | + } |
| 193 | + if err := json.Unmarshal([]byte(resultTextOf(t, inbound)), &inPayload); err != nil { |
| 194 | + t.Fatalf("unmarshal inbound: %v", err) |
| 195 | + } |
| 196 | + if len(inPayload.Refs) != 1 || inPayload.Refs[0].FromNamespace != "web" { |
| 197 | + t.Fatalf("inbound refs = %+v, want one ref from web", inPayload.Refs) |
| 198 | + } |
| 199 | +} |
0 commit comments