Files
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

113 lines
3.5 KiB
Go

package mcp
import (
"encoding/json"
"testing"
mcplib "github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zzet/gortex/internal/query"
)
// TestWalkGraph_FollowsCalls verifies walk_graph traverses the call
// graph from a start symbol and returns the reachable callees.
func TestWalkGraph_FollowsCalls(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{
"id": "main.go::main",
"edge_kinds": "calls",
"direction": "out",
})
require.False(t, result.IsError, "walk_graph errored: %s", toolResultText(result))
var sg query.SubGraph
require.NoError(t, json.Unmarshal([]byte(result.Content[0].(mcplib.TextContent).Text), &sg))
ids := make(map[string]bool)
for _, n := range sg.Nodes {
ids[n.ID] = true
}
assert.True(t, ids["main.go::main"], "seed must be in the result")
assert.True(t, ids["main.go::helper"], "callee helper must be reached")
}
// TestWalkGraph_MissingID surfaces a clear error for an absent symbol.
func TestWalkGraph_MissingID(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{
"id": "no/such::Symbol",
})
require.True(t, result.IsError)
assert.Contains(t, toolResultText(result), "symbol not found")
}
// TestWalkGraph_RequiresID rejects a call with no id.
func TestWalkGraph_RequiresID(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{})
require.True(t, result.IsError)
assert.Contains(t, toolResultText(result), "id is required")
}
// TestWalkGraph_BadEdgeKind rejects an unknown edge kind.
func TestWalkGraph_BadEdgeKind(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{
"id": "main.go::main",
"edge_kinds": "bogus_kind",
})
require.True(t, result.IsError)
assert.Contains(t, toolResultText(result), "bogus_kind")
}
// TestWalkGraph_BadDirection rejects an unknown direction.
func TestWalkGraph_BadDirection(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{
"id": "main.go::main",
"direction": "sideways",
})
require.True(t, result.IsError)
assert.Contains(t, toolResultText(result), "direction")
}
// TestWalkGraph_TokenBudgetHit checks that a tiny token budget truncates
// the walk and surfaces budget_hit on the JSON response.
func TestWalkGraph_TokenBudgetHit(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{
"id": "main.go::main",
"edge_kinds": "calls",
"token_budget": 1,
})
require.False(t, result.IsError, "walk_graph errored: %s", toolResultText(result))
var raw map[string]any
require.NoError(t, json.Unmarshal([]byte(result.Content[0].(mcplib.TextContent).Text), &raw))
// A 1-token budget cannot admit the callee, so the walk is truncated.
assert.Equal(t, true, raw["budget_hit"], "budget_hit must be set on a truncated walk")
}
// TestWalkGraph_GCXFormat verifies the GCX wire format is produced and
// carries the node table.
func TestWalkGraph_GCXFormat(t *testing.T) {
srv, _ := setupTestServer(t)
result := callTool(t, srv, "walk_graph", map[string]any{
"id": "main.go::main",
"edge_kinds": "calls",
"format": "gcx",
})
require.False(t, result.IsError)
text := toolResultText(result)
assert.Contains(t, text, "walk_graph.nodes", "GCX output must carry the nodes table")
}