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

77 lines
2.8 KiB
Go

package mcp
import (
"context"
"encoding/json"
"path/filepath"
"strings"
"testing"
mcplib "github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/require"
)
func TestScopeStore_PersistsRoundTrip(t *testing.T) {
path := filepath.Join(t.TempDir(), "scopes.json")
st := newScopeStore(path)
require.NoError(t, st.put(SavedScope{Name: "backend", Description: "be", Repos: []string{"api", "core"}}))
// A fresh store over the same file sees the persisted scope.
got, ok := newScopeStore(path).get("backend")
require.True(t, ok, "a saved scope must survive a store reload")
require.Equal(t, []string{"api", "core"}, got.Repos)
removed, err := st.remove("backend")
require.NoError(t, err)
require.True(t, removed)
_, stillThere := newScopeStore(path).get("backend")
require.False(t, stillThere, "a removed scope must not survive a reload")
}
// TestSavedScope_FiltersSearchResults drives save_scope + a scoped
// search_symbols end-to-end and confirms the scope confines results.
func TestSavedScope_FiltersSearchResults(t *testing.T) {
t.Setenv("GORTEX_SCOPES_PATH", filepath.Join(t.TempDir(), "scopes.json"))
srv, _, _ := newIsolationServer(t)
ctx := context.Background() // unbound session — the scope is the only filter
saveReq := mcplib.CallToolRequest{}
saveReq.Params.Name = "save_scope"
saveReq.Params.Arguments = map[string]any{"name": "alpha-only", "repos": "repo-a"}
saveRes, err := srv.handleSaveScope(ctx, saveReq)
require.NoError(t, err)
require.False(t, saveRes.IsError, "save_scope must succeed")
resp := scopedSearch(t, srv, ctx, "Thing", "alpha-only")
ids := resultIDs(resp)
require.NotEmpty(t, ids, "the scoped search should still find repo-a symbols")
for _, id := range ids {
require.NotContainsf(t, id, "repo-b", "scoped search leaked a repo-b result: %s", id)
}
require.Contains(t, strings.Join(ids, " "), "AlphaThing")
}
func TestSavedScope_UnknownScopeErrors(t *testing.T) {
t.Setenv("GORTEX_SCOPES_PATH", filepath.Join(t.TempDir(), "scopes.json"))
srv, _, _ := newIsolationServer(t)
req := mcplib.CallToolRequest{}
req.Params.Name = "search_symbols"
req.Params.Arguments = map[string]any{"query": "Thing", "scope": "no-such-scope"}
res, err := srv.handleSearchSymbols(context.Background(), req)
require.NoError(t, err)
require.True(t, res.IsError, "an unknown scope must surface an error")
}
func scopedSearch(t *testing.T, srv *Server, ctx context.Context, query, scope string) map[string]any {
t.Helper()
req := mcplib.CallToolRequest{}
req.Params.Name = "search_symbols"
req.Params.Arguments = map[string]any{"query": query, "scope": scope}
res, err := srv.handleSearchSymbols(ctx, req)
require.NoError(t, err)
require.Falsef(t, res.IsError, "scoped search %q errored", query)
var resp map[string]any
require.NoError(t, json.Unmarshal([]byte(res.Content[0].(mcplib.TextContent).Text), &resp))
return resp
}