<!-- Provide a brief summary of your changes --> Adding validation for version and identifier for packages. ## Motivation and Context While debugging a bug, I found out that we need exact version to be there in package to fetch from npm, otherwise we won't be able to parse the response as it returns all versions. Even if we do, we still need to go and figure out which version have the mcpname associated with while iterating over all versions. So, we should make mandatory to have identifier and version present in the given package object if we want to validate package association with registries. ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> UTs. ## Breaking Changes <!-- Will users need to update their code or configurations? --> ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [ ] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [ ] My code follows the repository's style guidelines - [ ] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> Co-authored-by: Avish Porwal <avishporwal@microsoft.com>
114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
package registries_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/modelcontextprotocol/registry/internal/validators/registries"
|
|
"github.com/modelcontextprotocol/registry/pkg/model"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestValidateNPM_RealPackages(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
tests := []struct {
|
|
name string
|
|
packageName string
|
|
version string
|
|
serverName string
|
|
expectError bool
|
|
errorMessage string
|
|
}{
|
|
{
|
|
name: "empty package identifier should fail",
|
|
packageName: "",
|
|
version: "1.0.0",
|
|
serverName: "com.example/test",
|
|
expectError: true,
|
|
errorMessage: "package identifier is required for NPM packages",
|
|
},
|
|
{
|
|
name: "empty package version should fail",
|
|
packageName: "test-package",
|
|
version: "",
|
|
serverName: "com.example/test",
|
|
expectError: true,
|
|
errorMessage: "package version is required for NPM packages",
|
|
},
|
|
{
|
|
name: "both empty identifier and version should fail with identifier error first",
|
|
packageName: "",
|
|
version: "",
|
|
serverName: "com.example/test",
|
|
expectError: true,
|
|
errorMessage: "package identifier is required for NPM packages",
|
|
},
|
|
{
|
|
name: "non-existent package should fail",
|
|
packageName: generateRandomPackageName(),
|
|
version: "1.0.0",
|
|
serverName: "com.example/test",
|
|
expectError: true,
|
|
errorMessage: "not found",
|
|
},
|
|
{
|
|
name: "real package without mcpName should fail",
|
|
packageName: "express", // Popular package without mcpName field
|
|
version: "4.18.2",
|
|
serverName: "com.example/test",
|
|
expectError: true,
|
|
errorMessage: "missing required 'mcpName' field",
|
|
},
|
|
{
|
|
name: "real package without mcpName should fail",
|
|
packageName: "lodash", // Another popular package
|
|
version: "4.17.21",
|
|
serverName: "com.example/completely-different-name",
|
|
expectError: true,
|
|
errorMessage: "missing required 'mcpName' field",
|
|
},
|
|
{
|
|
name: "real package without mcpName should fail",
|
|
packageName: "airtable-mcp-server",
|
|
version: "1.5.0",
|
|
serverName: "io.github.domdomegg/airtable-mcp-server",
|
|
expectError: true,
|
|
errorMessage: "missing required 'mcpName' field",
|
|
},
|
|
{
|
|
name: "real package with incorrect mcpName should fail",
|
|
packageName: "airtable-mcp-server",
|
|
version: "1.7.2",
|
|
serverName: "io.github.not-domdomegg/airtable-mcp-server",
|
|
expectError: true,
|
|
errorMessage: "Expected mcpName 'io.github.not-domdomegg/airtable-mcp-server', got 'io.github.domdomegg/airtable-mcp-server'",
|
|
},
|
|
{
|
|
name: "real package with correct mcpName should pass",
|
|
packageName: "airtable-mcp-server",
|
|
version: "1.7.2",
|
|
serverName: "io.github.domdomegg/airtable-mcp-server",
|
|
expectError: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
pkg := model.Package{
|
|
RegistryType: model.RegistryTypeNPM,
|
|
Identifier: tt.packageName,
|
|
Version: tt.version,
|
|
}
|
|
|
|
err := registries.ValidateNPM(ctx, pkg, tt.serverName)
|
|
|
|
if tt.expectError {
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), tt.errorMessage)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|