Files
zzet--gortex/internal/mcp/tools_walk_community_test.go
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

125 lines
3.7 KiB
Go

package mcp
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zzet/gortex/internal/analysis"
"github.com/zzet/gortex/internal/graph"
)
// seedWalkCommunityGraph injects a two-community call chain:
//
// x.go::X (comm "alpha") -> y.go::Y (comm "alpha") -> z.go::Z (comm "beta")
func seedWalkCommunityGraph(t *testing.T, srv *Server) {
t.Helper()
g := srv.graph
add := func(id, name, file string) {
g.AddNode(&graph.Node{ID: id, Kind: graph.KindFunction, Name: name, FilePath: file, Language: "go"})
}
add("x.go::X", "X", "x.go")
add("y.go::Y", "Y", "y.go")
add("z.go::Z", "Z", "z.go")
g.AddEdge(&graph.Edge{From: "x.go::X", To: "y.go::Y", Kind: graph.EdgeCalls, FilePath: "x.go", Line: 1})
g.AddEdge(&graph.Edge{From: "y.go::Y", To: "z.go::Z", Kind: graph.EdgeCalls, FilePath: "y.go", Line: 1})
srv.analysisMu.Lock()
srv.communities = &analysis.CommunityResult{
Communities: []analysis.Community{
{ID: "c-alpha", Label: "alpha"},
{ID: "c-beta", Label: "beta"},
},
NodeToComm: map[string]string{
"x.go::X": "c-alpha",
"y.go::Y": "c-alpha",
"z.go::Z": "c-beta",
},
}
srv.analysisMu.Unlock()
}
func walkResultIDs(t *testing.T, out map[string]any) map[string]bool {
t.Helper()
ids := map[string]bool{}
nodes, ok := out["nodes"].([]any)
require.True(t, ok, "walk result must carry nodes")
for _, n := range nodes {
row := n.(map[string]any)
if id, ok := row["id"].(string); ok {
ids[id] = true
}
}
return ids
}
func TestWalkGraph_CommunityFilter_ByLabel(t *testing.T) {
srv, _ := setupTestServer(t)
seedWalkCommunityGraph(t, srv)
out := extractTextResult(t, callTool(t, srv, "walk_graph", map[string]any{
"id": "x.go::X",
"edge_kinds": "calls",
"direction": "out",
"community": "alpha", // label, resolved to c-alpha
}))
ids := walkResultIDs(t, out)
assert.True(t, ids["x.go::X"])
assert.True(t, ids["y.go::Y"])
// z is in c-beta -> dropped.
assert.False(t, ids["z.go::Z"], "cross-community node must be dropped")
}
func TestWalkGraph_CommunityFilter_ByID(t *testing.T) {
srv, _ := setupTestServer(t)
seedWalkCommunityGraph(t, srv)
out := extractTextResult(t, callTool(t, srv, "walk_graph", map[string]any{
"id": "x.go::X",
"edge_kinds": "calls",
"direction": "out",
"community": "c-alpha", // raw ID
}))
ids := walkResultIDs(t, out)
assert.True(t, ids["y.go::Y"])
assert.False(t, ids["z.go::Z"])
}
func TestWalkGraph_CommunityFilter_NoArgIncludesAll(t *testing.T) {
srv, _ := setupTestServer(t)
seedWalkCommunityGraph(t, srv)
out := extractTextResult(t, callTool(t, srv, "walk_graph", map[string]any{
"id": "x.go::X",
"edge_kinds": "calls",
"direction": "out",
}))
ids := walkResultIDs(t, out)
// No community filter -> the whole reachable chain is returned.
assert.True(t, ids["y.go::Y"])
assert.True(t, ids["z.go::Z"])
}
func TestWalkGraph_CommunityFilter_NilCommunitiesNoOp(t *testing.T) {
srv, _ := setupTestServer(t)
// Seed the graph but NOT the communities — getCommunities() is nil.
g := srv.graph
g.AddNode(&graph.Node{ID: "x.go::X", Kind: graph.KindFunction, Name: "X", FilePath: "x.go", Language: "go"})
g.AddNode(&graph.Node{ID: "y.go::Y", Kind: graph.KindFunction, Name: "Y", FilePath: "y.go", Language: "go"})
g.AddEdge(&graph.Edge{From: "x.go::X", To: "y.go::Y", Kind: graph.EdgeCalls, FilePath: "x.go", Line: 1})
srv.analysisMu.Lock()
srv.communities = nil
srv.analysisMu.Unlock()
out := extractTextResult(t, callTool(t, srv, "walk_graph", map[string]any{
"id": "x.go::X",
"edge_kinds": "calls",
"direction": "out",
"community": "alpha",
}))
ids := walkResultIDs(t, out)
// Filter no-ops when communities are nil -> Y still present.
assert.True(t, ids["y.go::Y"])
}