mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
A global config syncs every mcp.projects root in the same plan, but mcp check only looked at the global list, so a project server with a missing variable, an unresolvable command, or an entry out of sync went unreported. Check now covers each root's servers in global mode, rendering client rules in that root and reading sync state from the plan's changes with the same Root. Each result carries the root in a new project field (empty for global servers), and name filtering and the unknown-name error span every scope.
287 lines
10 KiB
Go
287 lines
10 KiB
Go
package mcp
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func checkService(t *testing.T, config string) *Service {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
if err := os.WriteFile(path, []byte(config), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &Service{ConfigPath: path, Home: dir, StateDir: filepath.Join(dir, "state"), Platform: "linux"}
|
|
}
|
|
|
|
// offlineCheck resolves every command and host and finds no variables, unless a test says otherwise.
|
|
func offlineCheck(names ...string) CheckOptions {
|
|
return CheckOptions{
|
|
Names: names,
|
|
LookupEnv: func(string) (string, bool) { return "", false },
|
|
LookPath: func(name string) (string, error) { return "/usr/bin/" + name, nil },
|
|
LookupHost: func(context.Context, string) ([]string, error) { return []string{"192.0.2.1"}, nil },
|
|
}
|
|
}
|
|
|
|
func findings(t *testing.T, report *CheckReport, name string) []CheckFinding {
|
|
t.Helper()
|
|
for _, server := range report.Servers {
|
|
if server.Name == name {
|
|
return server.Findings
|
|
}
|
|
}
|
|
t.Fatalf("no result for %s in %+v", name, report.Servers)
|
|
return nil
|
|
}
|
|
|
|
// subjectOf is the subject of the first finding of that check, or "" when there is none.
|
|
func subjectOf(list []CheckFinding, check string) string {
|
|
for _, f := range list {
|
|
if f.Check == check {
|
|
return f.Subject
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func hasFinding(list []CheckFinding, level, check, target string) bool {
|
|
for _, f := range list {
|
|
if f.Level == level && f.Check == check && f.Target == target {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func TestCheckMissingEnvIsErrorWithoutValue(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n gh:\n url: https://api.example.com/mcp\n bearerToken: {fromEnv: GH_TOKEN}\n headers:\n X-Team: {fromEnv: TEAM_ID}\n")
|
|
opts := offlineCheck()
|
|
opts.LookupEnv = func(name string) (string, bool) {
|
|
if name == "TEAM_ID" {
|
|
return "secret-team", true
|
|
}
|
|
return "", false
|
|
}
|
|
report, err := s.Check(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list := findings(t, report, "gh")
|
|
if !hasFinding(list, "error", "env", "") || report.Servers[0].OK {
|
|
t.Fatalf("missing GH_TOKEN must be an error: %+v", list)
|
|
}
|
|
if got := subjectOf(list, "env"); got != "GH_TOKEN" {
|
|
t.Fatalf("subject = %q, want the variable name", got)
|
|
}
|
|
for _, f := range list {
|
|
if strings.Contains(f.Message, "TEAM_ID") || strings.Contains(f.Message, "secret-team") {
|
|
t.Fatalf("a set variable was reported: %+v", f)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCheckCommandNotOnPath(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n local:\n command: no-such-mcp-binary\n")
|
|
opts := offlineCheck()
|
|
opts.LookPath = func(string) (string, error) { return "", errors.New("not found") }
|
|
report, err := s.Check(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !hasFinding(findings(t, report, "local"), "error", "command", "") {
|
|
t.Fatalf("unresolved command must be an error: %+v", report.Servers)
|
|
}
|
|
if got := subjectOf(findings(t, report, "local"), "command"); got != "no-such-mcp-binary" {
|
|
t.Fatalf("subject = %q, want the command", got)
|
|
}
|
|
}
|
|
|
|
func TestCheckUnresolvedHostIsWarningAndSkippable(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n docs:\n url: https://mcp.skillshare.invalid/mcp\n")
|
|
opts := offlineCheck()
|
|
opts.LookupHost = nil // the real resolver: .invalid never resolves
|
|
report, err := s.Check(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !hasFinding(findings(t, report, "docs"), "warning", "dns", "") || !report.Servers[0].OK {
|
|
t.Fatalf("unresolved host must be a warning only: %+v", report.Servers)
|
|
}
|
|
if got := subjectOf(findings(t, report, "docs"), "dns"); got != "mcp.skillshare.invalid" {
|
|
t.Fatalf("subject = %q, want the host", got)
|
|
}
|
|
opts.SkipDNS = true
|
|
opts.LookupHost = func(context.Context, string) ([]string, error) {
|
|
t.Fatal("--no-dns must not look up hosts")
|
|
return nil, nil
|
|
}
|
|
if report, err = s.Check(opts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hasFinding(findings(t, report, "docs"), "warning", "dns", "") {
|
|
t.Fatal("DNS finding reported with SkipDNS")
|
|
}
|
|
}
|
|
|
|
func TestCheckSyncState(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude, cursor]\n servers:\n docs:\n url: https://example.com/mcp\n")
|
|
report, err := s.Check(offlineCheck())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list := findings(t, report, "docs")
|
|
if !hasFinding(list, "warning", "sync", "claude") || report.Summary.Warnings != 2 {
|
|
t.Fatalf("unsynced entries must warn: %+v", list)
|
|
}
|
|
p, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(p.Revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(s.Home, ".claude.json"), []byte(`{"mcpServers":{"docs":{"type":"http","url":"https://changed.example/mcp"}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if report, err = s.Check(offlineCheck()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list = findings(t, report, "docs")
|
|
if !hasFinding(list, "error", "sync", "claude") || !hasFinding(list, "info", "sync", "cursor") || report.Summary.Errors != 1 {
|
|
t.Fatalf("an edited entry must be a conflict and the other in sync: %+v", list)
|
|
}
|
|
}
|
|
|
|
func TestCheckClientRuleNamesTarget(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude, cursor]\n servers:\n workspace:\n url: https://example.com/mcp\n docs:\n url: https://example.com/docs\n")
|
|
report, err := s.Check(offlineCheck())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list := findings(t, report, "workspace")
|
|
if !hasFinding(list, "error", "client-rule", "claude") || hasFinding(list, "error", "client-rule", "cursor") {
|
|
t.Fatalf("Claude's reserved name must be refused for claude only: %+v", list)
|
|
}
|
|
if !hasFinding(list, "warning", "sync", "cursor") || hasFinding(list, "warning", "sync", "claude") {
|
|
t.Fatalf("the accepted target must still report its sync state: %+v", list)
|
|
}
|
|
if !hasFinding(findings(t, report, "docs"), "warning", "sync", "claude") {
|
|
t.Fatal("a refused server must not hide the others' sync state")
|
|
}
|
|
if report, err = s.Check(offlineCheck("docs")); err != nil || report.Summary.Errors != 0 {
|
|
t.Fatalf("an unselected refused server must not fail the check: %v %+v", err, report)
|
|
}
|
|
}
|
|
|
|
func TestCheckServerWithoutTargets(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n parked:\n url: https://example.com/mcp\n targets: []\n")
|
|
report, err := s.Check(offlineCheck())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list := findings(t, report, "parked")
|
|
if len(list) != 1 || !hasFinding(list, "info", "targets", "") || !report.Servers[0].OK {
|
|
t.Fatalf("a server without targets is kept in Skillshare only: %+v", list)
|
|
}
|
|
}
|
|
|
|
func TestCheckUnknownNameListsKnown(t *testing.T) {
|
|
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n docs:\n url: https://example.com/mcp\n")
|
|
_, err := s.Check(offlineCheck("missing"))
|
|
if err == nil || !strings.Contains(err.Error(), "docs") {
|
|
t.Fatalf("unknown name must list known servers, got %v", err)
|
|
}
|
|
}
|
|
|
|
// projectCheckService declares docs globally and, under one mcp.projects root, a docs of its
|
|
// own and a local server that needs a variable and a command.
|
|
func projectCheckService(t *testing.T) (*Service, string) {
|
|
t.Helper()
|
|
root := filepath.Join(t.TempDir(), "app")
|
|
if err := os.MkdirAll(root, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := checkService(t, "mcp:\n targets: [claude]\n servers:\n docs:\n url: https://example.com/mcp\n projects:\n "+root+":\n servers:\n docs:\n url: https://example.com/app-docs\n local:\n command: no-such-mcp-binary\n env:\n API_KEY: {fromEnv: APP_TOKEN}\n")
|
|
return s, root
|
|
}
|
|
|
|
func checkResult(t *testing.T, report *CheckReport, project, name string) CheckServer {
|
|
t.Helper()
|
|
for _, server := range report.Servers {
|
|
if server.Project == project && server.Name == name {
|
|
return server
|
|
}
|
|
}
|
|
t.Fatalf("no result for %s in project %q: %+v", name, project, report.Servers)
|
|
return CheckServer{}
|
|
}
|
|
|
|
func TestCheckProjectServerEnvAndCommand(t *testing.T) {
|
|
s, root := projectCheckService(t)
|
|
opts := offlineCheck()
|
|
opts.LookPath = func(string) (string, error) { return "", errors.New("not found") }
|
|
report, err := s.Check(opts)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := checkResult(t, report, root, "local")
|
|
if local.OK || !hasFinding(local.Findings, "error", "env", "") || !hasFinding(local.Findings, "error", "command", "") {
|
|
t.Fatalf("a project server's variable and command must be checked: %+v", local)
|
|
}
|
|
if len(report.Servers) != 3 || checkResult(t, report, "", "docs").Project != "" {
|
|
t.Fatalf("want the global docs plus both project servers: %+v", report.Servers)
|
|
}
|
|
}
|
|
|
|
func TestCheckProjectSyncStateFromItsRoot(t *testing.T) {
|
|
s, root := projectCheckService(t)
|
|
p, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(p.Revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, ".mcp.json"), []byte(`{"mcpServers":{"docs":{"type":"http","url":"https://changed.example/mcp"}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
report, err := s.Check(offlineCheck("docs"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !hasFinding(checkResult(t, report, root, "docs").Findings, "error", "sync", "claude") {
|
|
t.Fatalf("the edited project entry must be a conflict: %+v", report.Servers)
|
|
}
|
|
if !hasFinding(checkResult(t, report, "", "docs").Findings, "info", "sync", "claude") {
|
|
t.Fatalf("the global docs must stay in sync: %+v", report.Servers)
|
|
}
|
|
}
|
|
|
|
func TestCheckNameMatchesGlobalAndProjects(t *testing.T) {
|
|
s, root := projectCheckService(t)
|
|
report, err := s.Check(offlineCheck("docs"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(report.Servers) != 2 || report.Servers[0].Project != "" || report.Servers[1].Project != root {
|
|
t.Fatalf("docs must match the global server and the project's: %+v", report.Servers)
|
|
}
|
|
if report, err = s.Check(offlineCheck("local")); err != nil || len(report.Servers) != 1 || report.Servers[0].Project != root {
|
|
t.Fatalf("a project-only name must select that server: %v %+v", err, report)
|
|
}
|
|
}
|
|
|
|
func TestCheckUnknownNameListsProjectServers(t *testing.T) {
|
|
s, _ := projectCheckService(t)
|
|
_, err := s.Check(offlineCheck("missing"))
|
|
if err == nil || !strings.Contains(err.Error(), "known servers: docs, local") {
|
|
t.Fatalf("unknown name must list project servers once each, got %v", err)
|
|
}
|
|
}
|