registry/internal/database/import_test.go
Copilot 9c49c8b9b8
Simplify seed data configuration by removing command line flags and renaming environment variable (#175)
This PR simplifies the seed data import configuration based on
maintainer feedback by removing command line flags and using a clearer
environment variable name.

## Key Changes

### Configuration Simplification
- **Renamed**: `MCP_REGISTRY_SEED_FILE_PATH` → `MCP_REGISTRY_SEED_FROM`
- **Removed**: `--seed-file-path` command line flag to eliminate
configuration complexity

### Improved User Experience
Users now configure seed data import using a single, clear environment
variable:

```bash
# Before (multiple ways to configure)
MCP_REGISTRY_SEED_FILE_PATH=data/seed.json ./registry
./registry --seed-file-path=data/seed.json

# After (simplified single method)
MCP_REGISTRY_SEED_FROM=data/seed.json ./registry
```

### Registry Composability
The composability workflow is now simplified while maintaining all
functionality:

```bash
# Start first registry with seed data
MCP_REGISTRY_SEED_FROM=data/seed.json ./registry

# Start second registry importing from first via pagination
MCP_REGISTRY_SEED_FROM=http://localhost:8080 ./registry
```

## Technical Implementation
- Maintains full backward compatibility for import functionality
- All existing seed data sources continue to work (local files, HTTP
URLs, registry URLs)
- Automatic pagination through `/v0/servers` endpoint remains unchanged
- Configurable delays between requests preserved

## Benefits
- **Reduced complexity**: Single configuration method instead of
multiple
- **Clearer naming**: `SEED_FROM` better describes the source of seed
data
- **Easier maintenance**: Fewer configuration paths to test and document
- **Consistent patterns**: Aligns with other environment-variable-only
configuration

All existing functionality is preserved while providing a cleaner, more
maintainable configuration interface.

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Avinash Sridhar <sridharavinash@users.noreply.github.com>
Co-authored-by: Toby Padilla <toby@toby.sh>
Co-authored-by: toby <83556+toby@users.noreply.github.com>
2025-08-08 13:42:16 +01:00

188 lines
4.9 KiB
Go

package database_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/modelcontextprotocol/registry/internal/database"
"github.com/modelcontextprotocol/registry/internal/model"
"github.com/stretchr/testify/assert"
)
func TestReadSeedFile_LocalFile(t *testing.T) {
// Create a temporary seed file
tempFile := "/tmp/test_seed.json"
seedData := []model.ServerDetail{
{
Server: model.Server{
ID: "test-id-1",
Name: "test-server-1",
Description: "Test server 1",
Repository: model.Repository{
URL: "https://github.com/test/repo1",
Source: "github",
ID: "123",
},
VersionDetail: model.VersionDetail{
Version: "1.0.0",
ReleaseDate: "2023-01-01T00:00:00Z",
IsLatest: true,
},
},
},
}
// Write seed data to temp file
data, err := json.Marshal(seedData)
assert.NoError(t, err)
err = func() error {
f, err := os.Create(tempFile)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)
return err
}()
assert.NoError(t, err)
defer os.Remove(tempFile)
// Test reading the file
result, err := database.ReadSeedFile(context.Background(), tempFile)
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, "test-server-1", result[0].Name)
}
func TestReadSeedFile_DirectHTTPURL(t *testing.T) {
// Create a test HTTP server that serves seed JSON directly
seedData := []model.ServerDetail{
{
Server: model.Server{
ID: "test-id-1",
Name: "test-server-1",
Description: "Test server 1",
},
},
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(seedData); err != nil {
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
}
}))
defer server.Close()
// Test reading from HTTP URL ending in .json
result, err := database.ReadSeedFile(context.Background(), server.URL+"/seed.json")
assert.NoError(t, err)
assert.Len(t, result, 1)
assert.Equal(t, "test-server-1", result[0].Name)
}
func TestReadSeedFile_RegistryURL(t *testing.T) {
// Create mock registry servers
server1 := model.Server{
ID: "server-1",
Name: "Test Server 1",
Description: "First test server",
}
server2 := model.Server{
ID: "server-2",
Name: "Test Server 2",
Description: "Second test server",
}
serverDetail1 := model.ServerDetail{
Server: server1,
Packages: []model.Package{
{
Name: "test-package-1",
Version: "1.0.0",
},
},
}
serverDetail2 := model.ServerDetail{
Server: server2,
Packages: []model.Package{
{
Name: "test-package-2",
Version: "2.0.0",
},
},
}
// Create a test HTTP server that simulates the registry API
mux := http.NewServeMux()
// Handle /v0/servers endpoint (paginated)
mux.HandleFunc("/v0/servers", func(w http.ResponseWriter, r *http.Request) {
cursor := r.URL.Query().Get("cursor")
var response database.PaginatedResponse
switch cursor {
case "":
// First page
response = database.PaginatedResponse{
Data: []model.Server{server1},
Metadata: database.Metadata{
NextCursor: "next-cursor-1",
Count: 1,
},
}
case "next-cursor-1":
// Second page
response = database.PaginatedResponse{
Data: []model.Server{server2},
Metadata: database.Metadata{
Count: 1,
// No NextCursor means end of pagination
},
}
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
}
})
// Handle individual server detail endpoints
mux.HandleFunc("/v0/servers/server-1", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(serverDetail1); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
mux.HandleFunc("/v0/servers/server-2", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(serverDetail2); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
server := httptest.NewServer(mux)
defer server.Close()
// Test reading from registry root URL (this should trigger pagination)
result, err := database.ReadSeedFile(context.Background(), server.URL)
assert.NoError(t, err)
assert.Len(t, result, 2)
// Verify the servers were imported correctly
assert.Equal(t, "Test Server 1", result[0].Name)
assert.Equal(t, "Test Server 2", result[1].Name)
// Verify packages were included
assert.Len(t, result[0].Packages, 1)
assert.Equal(t, "test-package-1", result[0].Packages[0].Name)
assert.Len(t, result[1].Packages, 1)
assert.Equal(t, "test-package-2", result[1].Packages[0].Name)
}