Add test for filtered servers (#426)
Claude wrote this extra test while I was doing some other work experimenting with stuff, seems useful to have! Also deleted the unnecessary expectedServers, because we basically never assert on it.
This commit is contained in:
parent
17eb56d4c7
commit
256c730198
1 changed files with 95 additions and 43 deletions
|
|
@ -25,7 +25,6 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
queryParams string
|
queryParams string
|
||||||
setupRegistryService func(service.RegistryService)
|
setupRegistryService func(service.RegistryService)
|
||||||
expectedStatus int
|
expectedStatus int
|
||||||
expectedServers []apiv0.ServerJSON
|
|
||||||
expectedMeta *apiv0.Metadata
|
expectedMeta *apiv0.Metadata
|
||||||
expectedError string
|
expectedError string
|
||||||
}{
|
}{
|
||||||
|
|
@ -56,8 +55,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
_, _ = registry.Publish(server1)
|
_, _ = registry.Publish(server1)
|
||||||
_, _ = registry.Publish(server2)
|
_, _ = registry.Publish(server2)
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: nil, // Will be verified differently since IDs are dynamic
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "successful list with cursor and limit",
|
name: "successful list with cursor and limit",
|
||||||
|
|
@ -75,8 +73,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
}
|
}
|
||||||
_, _ = registry.Publish(server)
|
_, _ = registry.Publish(server)
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: nil, // Will be verified differently since IDs are dynamic
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "successful list with limit capping at 100",
|
name: "successful list with limit capping at 100",
|
||||||
|
|
@ -118,8 +115,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
setupRegistryService: func(_ service.RegistryService) {
|
setupRegistryService: func(_ service.RegistryService) {
|
||||||
// Test empty registry - empty setup
|
// Test empty registry - empty setup
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: []apiv0.ServerJSON{},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "successful search by name substring",
|
name: "successful search by name substring",
|
||||||
|
|
@ -148,8 +144,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
_, _ = registry.Publish(server1)
|
_, _ = registry.Publish(server1)
|
||||||
_, _ = registry.Publish(server2)
|
_, _ = registry.Publish(server2)
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: nil, // Will verify in test that only matching server is returned
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "successful updated_since filter with RFC3339",
|
name: "successful updated_since filter with RFC3339",
|
||||||
|
|
@ -167,8 +162,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
}
|
}
|
||||||
_, _ = registry.Publish(server)
|
_, _ = registry.Publish(server)
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: nil, // Will verify server is returned since it was updated after 2020
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "successful version=latest filter",
|
name: "successful version=latest filter",
|
||||||
|
|
@ -197,8 +191,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
_, _ = registry.Publish(server1)
|
_, _ = registry.Publish(server1)
|
||||||
_, _ = registry.Publish(server2) // This will be marked as latest
|
_, _ = registry.Publish(server2) // This will be marked as latest
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: nil, // Will verify only latest server is returned
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "combined search and updated_since filter",
|
name: "combined search and updated_since filter",
|
||||||
|
|
@ -227,8 +220,7 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
_, _ = registry.Publish(server1)
|
_, _ = registry.Publish(server1)
|
||||||
_, _ = registry.Publish(server2)
|
_, _ = registry.Publish(server2)
|
||||||
},
|
},
|
||||||
expectedStatus: http.StatusOK,
|
expectedStatus: http.StatusOK,
|
||||||
expectedServers: nil, // Will verify only matching server is returned
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid updated_since format",
|
name: "invalid updated_since format",
|
||||||
|
|
@ -237,6 +229,58 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
expectedStatus: http.StatusBadRequest,
|
expectedStatus: http.StatusBadRequest,
|
||||||
expectedError: "Invalid updated_since format: expected RFC3339",
|
expectedError: "Invalid updated_since format: expected RFC3339",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "comprehensive query with all parameters",
|
||||||
|
queryParams: "?search=filesystem&updated_since=2020-01-01T00:00:00Z&version=latest&limit=50&cursor=",
|
||||||
|
setupRegistryService: func(registry service.RegistryService) {
|
||||||
|
// Create multiple versions of servers with different names
|
||||||
|
server1v1 := apiv0.ServerJSON{
|
||||||
|
Name: "io.example/filesystem-server",
|
||||||
|
Description: "Filesystem operations server v1",
|
||||||
|
Repository: model.Repository{
|
||||||
|
URL: "https://github.com/example/filesystem",
|
||||||
|
Source: "github",
|
||||||
|
ID: "example/filesystem",
|
||||||
|
},
|
||||||
|
Version: "1.0.0",
|
||||||
|
}
|
||||||
|
server1v2 := apiv0.ServerJSON{
|
||||||
|
Name: "io.example/filesystem-server",
|
||||||
|
Description: "Filesystem operations server v2 (latest)",
|
||||||
|
Repository: model.Repository{
|
||||||
|
URL: "https://github.com/example/filesystem",
|
||||||
|
Source: "github",
|
||||||
|
ID: "example/filesystem",
|
||||||
|
},
|
||||||
|
Version: "2.0.0",
|
||||||
|
}
|
||||||
|
server2 := apiv0.ServerJSON{
|
||||||
|
Name: "com.example/database-server",
|
||||||
|
Description: "Database operations server",
|
||||||
|
Repository: model.Repository{
|
||||||
|
URL: "https://github.com/example/database",
|
||||||
|
Source: "github",
|
||||||
|
ID: "example/database",
|
||||||
|
},
|
||||||
|
Version: "1.0.0",
|
||||||
|
}
|
||||||
|
server3 := apiv0.ServerJSON{
|
||||||
|
Name: "org.another/filesystem-tools",
|
||||||
|
Description: "Filesystem tools and utilities",
|
||||||
|
Repository: model.Repository{
|
||||||
|
URL: "https://github.com/another/filesystem-tools",
|
||||||
|
Source: "github",
|
||||||
|
ID: "another/filesystem-tools",
|
||||||
|
},
|
||||||
|
Version: "3.0.0",
|
||||||
|
}
|
||||||
|
_, _ = registry.Publish(server1v1)
|
||||||
|
_, _ = registry.Publish(server1v2)
|
||||||
|
_, _ = registry.Publish(server2)
|
||||||
|
_, _ = registry.Publish(server3)
|
||||||
|
},
|
||||||
|
expectedStatus: http.StatusOK,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
|
@ -273,36 +317,44 @@ func TestServersListEndpoint(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Check the response data
|
// Check the response data
|
||||||
if tc.expectedServers != nil {
|
switch tc.name {
|
||||||
assert.Equal(t, tc.expectedServers, resp.Servers)
|
case "successful search by name substring":
|
||||||
} else {
|
assert.Len(t, resp.Servers, 1, "Expected exactly one matching server")
|
||||||
// For tests with dynamic data, check structure and count
|
assert.Contains(t, resp.Servers[0].Name, "test-server", "Server name should contain search term")
|
||||||
// Special handling for filter test cases
|
case "successful updated_since filter with RFC3339":
|
||||||
switch tc.name {
|
assert.Len(t, resp.Servers, 1, "Expected one server updated after 2020")
|
||||||
case "successful search by name substring":
|
assert.Contains(t, resp.Servers[0].Name, "recent-server")
|
||||||
assert.Len(t, resp.Servers, 1, "Expected exactly one matching server")
|
case "successful version=latest filter":
|
||||||
assert.Contains(t, resp.Servers[0].Name, "test-server", "Server name should contain search term")
|
assert.Len(t, resp.Servers, 1, "Expected one latest server")
|
||||||
case "successful updated_since filter with RFC3339":
|
assert.Contains(t, resp.Servers[0].Description, "latest")
|
||||||
assert.Len(t, resp.Servers, 1, "Expected one server updated after 2020")
|
case "combined search and updated_since filter":
|
||||||
assert.Contains(t, resp.Servers[0].Name, "recent-server")
|
assert.Len(t, resp.Servers, 1, "Expected one server matching both filters")
|
||||||
case "successful version=latest filter":
|
assert.Contains(t, resp.Servers[0].Name, "combined", "Server name should contain search term")
|
||||||
assert.Len(t, resp.Servers, 1, "Expected one latest server")
|
case "empty registry returns success":
|
||||||
assert.Contains(t, resp.Servers[0].Description, "latest")
|
assert.Empty(t, resp.Servers, "Expected empty server list for empty registry")
|
||||||
case "combined search and updated_since filter":
|
case "comprehensive query with all parameters":
|
||||||
assert.Len(t, resp.Servers, 1, "Expected one server matching both filters")
|
// Should return only latest versions of servers matching "filesystem" search term
|
||||||
assert.Contains(t, resp.Servers[0].Name, "combined", "Server name should contain search term")
|
// Expected: 2 servers (filesystem-server v2.0.0 and filesystem-tools v3.0.0)
|
||||||
default:
|
assert.Len(t, resp.Servers, 2, "Expected two servers matching all filters")
|
||||||
assert.NotEmpty(t, resp.Servers, "Expected at least one server")
|
|
||||||
}
|
|
||||||
|
|
||||||
// General structure validation
|
|
||||||
for _, server := range resp.Servers {
|
for _, server := range resp.Servers {
|
||||||
assert.NotEmpty(t, server.Name)
|
assert.Contains(t, server.Name, "filesystem", "Server name should contain 'filesystem'")
|
||||||
assert.NotEmpty(t, server.Description)
|
|
||||||
assert.NotNil(t, server.Meta)
|
|
||||||
assert.NotNil(t, server.Meta.Official)
|
|
||||||
assert.NotEmpty(t, server.Meta.Official.ID)
|
|
||||||
}
|
}
|
||||||
|
// Verify the limit parameter worked (should be at most 50, but we only have 2)
|
||||||
|
assert.LessOrEqual(t, len(resp.Servers), 50, "Should respect limit parameter")
|
||||||
|
// Verify response includes metadata
|
||||||
|
assert.NotNil(t, resp.Metadata, "Expected metadata to be present")
|
||||||
|
assert.Equal(t, 2, resp.Metadata.Count, "Metadata count should match returned servers")
|
||||||
|
default:
|
||||||
|
assert.NotEmpty(t, resp.Servers, "Expected at least one server")
|
||||||
|
}
|
||||||
|
|
||||||
|
// General structure validation
|
||||||
|
for _, server := range resp.Servers {
|
||||||
|
assert.NotEmpty(t, server.Name)
|
||||||
|
assert.NotEmpty(t, server.Description)
|
||||||
|
assert.NotNil(t, server.Meta)
|
||||||
|
assert.NotNil(t, server.Meta.Official)
|
||||||
|
assert.NotEmpty(t, server.Meta.Official.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check metadata if expected
|
// Check metadata if expected
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue