mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
When Agents hold different definitions of one server, importing one Agent's version was refused with "unresolved conflicts prevented adoption", because any conflict in the plan blocked it, including another Agent's. The source was saved anyway, so the Agents that had matched the old definition now conflicted, and importing from them flipped the source back. Replace with source sent one resolution at a time, so with two conflicts the plan stayed blocked and the dialog could never apply. Gate adoption only on the resolved entries' own conflicts, in the API save and in the interactive CLI import. Replace with source now settles every conflict of that server at once. After a conflict import, a translated hint names the Agents in the same scope that still need Replace with source, and the remaining adoption error is translated.
315 lines
10 KiB
Go
315 lines
10 KiB
Go
package mcp
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func testService(t *testing.T) *Service {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yaml")
|
|
if err := os.WriteFile(path, []byte("mcp:\n targets: [claude, codex, cursor, vscode]\n servers:\n docs:\n url: https://example.com/mcp\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &Service{ConfigPath: path, Home: dir, StateDir: filepath.Join(dir, "state"), Platform: "linux"}
|
|
}
|
|
|
|
func TestSyncLifecycleAndDrift(t *testing.T) {
|
|
s := testService(t)
|
|
plan, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(plan.Changes) != 4 {
|
|
t.Fatalf("got %d changes", len(plan.Changes))
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err = s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, change := range plan.Changes {
|
|
if change.Action != "unchanged" {
|
|
t.Fatalf("not idempotent: %+v", change)
|
|
}
|
|
}
|
|
path := filepath.Join(s.Home, ".claude.json")
|
|
data, _ := os.ReadFile(path)
|
|
if err := os.WriteFile(path, []byte(strings.ReplaceAll(string(data), "https://example.com/mcp", "https://changed.example/mcp")), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err = s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !plan.Blocked {
|
|
t.Fatal("local edit must conflict")
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err == nil {
|
|
t.Fatal("must not apply conflicted plan")
|
|
}
|
|
}
|
|
|
|
func TestPreviewRejectsConcurrentChanges(t *testing.T) {
|
|
s := testService(t)
|
|
plan, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(s.Home, ".claude.json"), []byte(`{"mcpServers":{"docs":{"command":"mine"}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err == nil {
|
|
t.Fatal("stale preview accepted")
|
|
}
|
|
if _, err := os.Stat(filepath.Join(s.Home, ".codex", "config.toml")); !os.IsNotExist(err) {
|
|
t.Fatal("other target was written")
|
|
}
|
|
}
|
|
|
|
func TestApplyKeepsSettingsWrittenAfterPreview(t *testing.T) {
|
|
s := testService(t)
|
|
plan, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(s.Home, ".claude.json")
|
|
if err := os.WriteFile(path, []byte(`{"theme":"dark"}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err != nil {
|
|
t.Fatalf("unrelated Agent setting blocked apply: %v", err)
|
|
}
|
|
data, _ := os.ReadFile(path)
|
|
if !strings.Contains(string(data), `"theme"`) || !strings.Contains(string(data), "example.com") {
|
|
t.Fatalf("apply lost the Agent setting or the entry: %s", data)
|
|
}
|
|
}
|
|
|
|
// An Agent entry that already matches is taken over only by a sync the user runs: the
|
|
// preview lists it, the file stays as it is, and from then on the server's removal
|
|
// removes it too. Refs: #303.
|
|
func TestIdenticalUnmanagedEntryIsTakenOverBySync(t *testing.T) {
|
|
s := testService(t)
|
|
path := filepath.Join(s.Home, ".claude.json")
|
|
// Laid out as sync writes it, so the takeover leaves nothing else to do.
|
|
before := []byte("{\n \"mcpServers\": {\n \"docs\": {\n \"type\": \"http\",\n \"url\": \"https://example.com/mcp\"\n }\n }\n}\n")
|
|
if err := os.WriteFile(path, before, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := changeFor(plan, path, "docs"); c == nil || c.Action != "adopt" {
|
|
t.Fatalf("want adopt, got %+v", c)
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err != nil {
|
|
t.Fatalf("identical entry must not conflict: %v", err)
|
|
}
|
|
if data, _ := os.ReadFile(path); !bytes.Equal(data, before) {
|
|
t.Fatalf("taking over rewrote the entry: %s", data)
|
|
}
|
|
if plan, err = s.Preview(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := changeFor(plan, path, "docs"); c == nil || c.Action != "unchanged" {
|
|
t.Fatalf("a taken over entry is still pending: %+v", c)
|
|
}
|
|
if err := os.WriteFile(s.ConfigPath, []byte("mcp:\n targets: [claude, codex, cursor, vscode]\n servers: {}\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if data, _ := os.ReadFile(path); strings.Contains(string(data), "example.com") {
|
|
t.Fatal("removing the server left the entry Skillshare took over")
|
|
}
|
|
}
|
|
|
|
// Importing without the source Agent and ticking it later still takes the entry over,
|
|
// so unticking it again has something to remove. Refs: #303.
|
|
func TestTickingTheImportedAgentLaterTakesItsEntryOver(t *testing.T) {
|
|
s := testService(t)
|
|
path := filepath.Join(s.Home, ".claude.json")
|
|
if err := os.WriteFile(path, []byte(`{"mcpServers":{"mcp-test":{"command":"npx","args":["x"]}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
candidates, err := s.ImportClient("claude")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := candidates[0].Server
|
|
server.Targets = []string{"codex"}
|
|
if _, err := s.Mutate(Mutation{Name: "mcp-test", Server: &server}, "", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.Targets = []string{"claude"}
|
|
tick := Mutation{Name: "mcp-test", Server: &server, Replace: true}
|
|
plan, err := s.PreviewMutation(tick)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := changeFor(plan, path, "mcp-test"); c == nil || c.Action != "adopt" {
|
|
t.Fatalf("ticking claude plans nothing to sync: %+v", c)
|
|
}
|
|
if _, err := s.Mutate(tick, plan.Revision, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server.Targets = []string{}
|
|
if plan, err = s.PreviewMutation(Mutation{Name: "mcp-test", Server: &server, Replace: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := changeFor(plan, path, "mcp-test"); c == nil || c.Action != "remove" {
|
|
t.Fatalf("unticking claude plans nothing to sync: %+v", c)
|
|
}
|
|
}
|
|
|
|
// Two Agents hold different definitions of one server. Importing one of them takes that
|
|
// entry over even though the other is still a conflict, so the user can replace the other
|
|
// next instead of importing back and forth.
|
|
func TestImportAdoptsDespiteAnotherAgentsConflict(t *testing.T) {
|
|
s := testService(t)
|
|
claude := filepath.Join(s.Home, ".claude.json")
|
|
// Laid out as sync writes it, so taking it over leaves nothing else to do.
|
|
if err := os.WriteFile(claude, []byte("{\n \"mcpServers\": {\n \"docs\": {\n \"command\": \"a\"\n }\n }\n}\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
codex := filepath.Join(s.Home, ".codex", "config.toml")
|
|
if err := os.MkdirAll(filepath.Dir(codex), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(codex, []byte("[mcp_servers.docs]\ncommand = \"b\"\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := Server{Command: "a"}
|
|
adopt := []Resolution{{Target: "claude", Name: "docs", Action: "adopt"}}
|
|
if _, err := s.Mutate(Mutation{Name: "docs", Server: &server, Replace: true, Resolutions: adopt}, "", false); err != nil {
|
|
t.Fatalf("another Agent's conflict refused the import: %v", err)
|
|
}
|
|
plan, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := changeFor(plan, claude, "docs"); c == nil || c.Action != "unchanged" {
|
|
t.Fatalf("the imported entry was not taken over: %+v", c)
|
|
}
|
|
if c := changeFor(plan, codex, "docs"); c == nil || c.Action != "conflict" {
|
|
t.Fatalf("the other Agent's entry must stay a conflict: %+v", c)
|
|
}
|
|
}
|
|
|
|
func TestAgentDefaultsAreNotDrift(t *testing.T) {
|
|
s := testService(t)
|
|
if _, err := s.Apply(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Agents may rewrite an entry without the default type and with empty fields.
|
|
if err := os.WriteFile(filepath.Join(s.Home, ".claude.json"), []byte(`{"mcpServers":{"docs":{"url":"https://example.com/mcp","headers":{}}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err := s.Preview()
|
|
if err != nil || plan.Blocked {
|
|
t.Fatalf("equivalent entry reported as drift: %v %+v", err, plan)
|
|
}
|
|
}
|
|
|
|
func TestDisabledEntryIsDrift(t *testing.T) {
|
|
s := testService(t)
|
|
if _, err := s.Apply(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := s.ClientPaths()["cursor"]
|
|
data, _ := os.ReadFile(path)
|
|
if err := os.WriteFile(path, []byte(strings.Replace(string(data), `"url"`, `"disabled": true, "url"`, 1)), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err := s.Preview()
|
|
if err != nil || !plan.Blocked {
|
|
t.Fatalf("disabling a managed server was not reported: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPulledChangeConverges(t *testing.T) {
|
|
s := testService(t)
|
|
if _, err := s.Apply(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A teammate's commit changes the source and the Agent file together.
|
|
for _, path := range []string{s.ConfigPath, filepath.Join(s.Home, ".claude.json")} {
|
|
data, _ := os.ReadFile(path)
|
|
if err := os.WriteFile(path, []byte(strings.ReplaceAll(string(data), "example.com", "moved.example")), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
plan, err := s.Preview()
|
|
if err != nil || plan.Blocked {
|
|
t.Fatalf("pulled change must not conflict: %v %+v", err, plan)
|
|
}
|
|
}
|
|
|
|
func TestSyncKeepsAgentSpecificFields(t *testing.T) {
|
|
s := testService(t)
|
|
if _, err := s.Apply(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(s.Home, ".codex", "config.toml")
|
|
data, _ := os.ReadFile(path)
|
|
if err := os.WriteFile(path, append(data, "startup_timeout_sec = 30\n"...), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
config, _ := os.ReadFile(s.ConfigPath)
|
|
if err := os.WriteFile(s.ConfigPath, []byte(strings.ReplaceAll(string(config), "example.com", "moved.example")), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, _ = os.ReadFile(path)
|
|
if !strings.Contains(string(data), "startup_timeout_sec = 30") || !strings.Contains(string(data), "moved.example") {
|
|
t.Fatalf("Agent-specific field lost or entry not updated: %s", data)
|
|
}
|
|
}
|
|
|
|
func TestSyncLaysOutAnOwnedEntryThatIsStillOnOneLine(t *testing.T) {
|
|
s := testService(t)
|
|
plan, err := s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(s.Home, ".cursor", "mcp.json")
|
|
if err := os.WriteFile(path, []byte(`{"mcpServers":{"docs":{"url":"https://example.com/mcp"}}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan, err = s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Apply(plan.Revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := os.ReadFile(path)
|
|
if want := "{\n \"mcpServers\": {\n \"docs\": {\n \"url\": \"https://example.com/mcp\"\n }\n }\n}"; string(got) != want { // no newline at the end: the file had none
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
// Laid out once, it is left alone: Sync has nothing more to do.
|
|
plan, err = s.Preview()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, c := range plan.Changes {
|
|
if c.Action != "unchanged" {
|
|
t.Fatalf("%s %s is %q after the layout", c.Target, c.Name, c.Action)
|
|
}
|
|
}
|
|
}
|