|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// MCP-1049: the shipped default registry set is trimmed to exactly three |
| 12 | +// official/trusted entries, and former-default registries that were removed from |
| 13 | +// the shipped set are pruned from a user's persisted config on load (the merge in |
| 14 | +// registry_data.go keys by id and never prunes, so without this they live forever |
| 15 | +// in ~/.mcpproxy/config.json). |
| 16 | + |
| 17 | +func TestDefaultRegistries_ExactlyThree(t *testing.T) { |
| 18 | + defaults := DefaultRegistries() |
| 19 | + ids := make([]string, 0, len(defaults)) |
| 20 | + for _, r := range defaults { |
| 21 | + ids = append(ids, r.ID) |
| 22 | + } |
| 23 | + sort.Strings(ids) |
| 24 | + assert.Equal(t, []string{"docker-mcp-catalog", "official", "reference"}, ids, |
| 25 | + "the shipped default registry set must be exactly official/reference/docker-mcp-catalog") |
| 26 | +} |
| 27 | + |
| 28 | +func TestDefaultRegistries_DoesNotShipDeprecated(t *testing.T) { |
| 29 | + for _, r := range DefaultRegistries() { |
| 30 | + assert.Falsef(t, IsDeprecatedDefaultRegistry(r.ID), |
| 31 | + "shipped default %q must not also be in the deprecated former-default set", r.ID) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestIsDeprecatedDefaultRegistry(t *testing.T) { |
| 36 | + for _, id := range []string{"pulse", "smithery", "fleur", "azure-mcp-demo", "remote-mcp-servers"} { |
| 37 | + assert.Truef(t, IsDeprecatedDefaultRegistry(id), "%q must be a known deprecated former-default", id) |
| 38 | + } |
| 39 | + for _, id := range []string{"official", "reference", "docker-mcp-catalog", "my-custom-registry", ""} { |
| 40 | + assert.Falsef(t, IsDeprecatedDefaultRegistry(id), "%q must NOT be flagged deprecated", id) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestPruneDeprecatedRegistries_RemovesFormerDefaultsPreservesCustom(t *testing.T) { |
| 45 | + cfg := &Config{ |
| 46 | + Registries: []RegistryEntry{ |
| 47 | + {ID: "official", Name: "Official MCP Registry"}, |
| 48 | + {ID: "reference", Name: "Reference Servers"}, |
| 49 | + {ID: "docker-mcp-catalog", Name: "Docker MCP Catalog"}, |
| 50 | + {ID: "pulse", Name: "Pulse MCP"}, |
| 51 | + {ID: "smithery", Name: "Smithery"}, |
| 52 | + {ID: "fleur", Name: "Fleur"}, |
| 53 | + {ID: "azure-mcp-demo", Name: "Azure MCP Registry Demo"}, |
| 54 | + {ID: "remote-mcp-servers", Name: "Remote MCP Servers"}, |
| 55 | + {ID: "my-custom-registry", Name: "My Custom Registry"}, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + removed := PruneDeprecatedRegistries(cfg) |
| 60 | + assert.Equal(t, 5, removed, "all five deprecated former-defaults must be removed") |
| 61 | + |
| 62 | + ids := make([]string, 0, len(cfg.Registries)) |
| 63 | + for _, r := range cfg.Registries { |
| 64 | + ids = append(ids, r.ID) |
| 65 | + } |
| 66 | + sort.Strings(ids) |
| 67 | + assert.Equal(t, []string{"docker-mcp-catalog", "my-custom-registry", "official", "reference"}, ids, |
| 68 | + "prune must keep the 3 current defaults plus the genuinely user-added custom registry") |
| 69 | +} |
| 70 | + |
| 71 | +func TestPruneDeprecatedRegistries_Idempotent(t *testing.T) { |
| 72 | + cfg := &Config{ |
| 73 | + Registries: []RegistryEntry{ |
| 74 | + {ID: "official"}, |
| 75 | + {ID: "pulse"}, |
| 76 | + {ID: "my-custom-registry"}, |
| 77 | + }, |
| 78 | + } |
| 79 | + first := PruneDeprecatedRegistries(cfg) |
| 80 | + require.Equal(t, 1, first) |
| 81 | + second := PruneDeprecatedRegistries(cfg) |
| 82 | + assert.Equal(t, 0, second, "a second prune must be a no-op (idempotent)") |
| 83 | + assert.Len(t, cfg.Registries, 2) |
| 84 | +} |
| 85 | + |
| 86 | +func TestPruneDeprecatedRegistries_NilAndEmptySafe(t *testing.T) { |
| 87 | + assert.Equal(t, 0, PruneDeprecatedRegistries(nil)) |
| 88 | + assert.Equal(t, 0, PruneDeprecatedRegistries(&Config{})) |
| 89 | +} |
| 90 | + |
| 91 | +// initializeRegistries runs on every config load; it must prune the persisted |
| 92 | +// deprecated entries so an existing install converges to the trimmed set. |
| 93 | +func TestInitializeRegistries_PrunesDeprecatedOnLoad(t *testing.T) { |
| 94 | + cfg := &Config{ |
| 95 | + Registries: []RegistryEntry{ |
| 96 | + {ID: "official"}, |
| 97 | + {ID: "pulse"}, |
| 98 | + {ID: "smithery"}, |
| 99 | + {ID: "fleur"}, |
| 100 | + {ID: "azure-mcp-demo"}, |
| 101 | + {ID: "remote-mcp-servers"}, |
| 102 | + {ID: "my-custom-registry"}, |
| 103 | + }, |
| 104 | + } |
| 105 | + initializeRegistries(cfg) |
| 106 | + |
| 107 | + ids := make([]string, 0, len(cfg.Registries)) |
| 108 | + for _, r := range cfg.Registries { |
| 109 | + ids = append(ids, r.ID) |
| 110 | + } |
| 111 | + sort.Strings(ids) |
| 112 | + assert.Equal(t, []string{"my-custom-registry", "official"}, ids, |
| 113 | + "load must prune deprecated former-defaults from the persisted config") |
| 114 | +} |
0 commit comments