Skip to content

Commit 256c730

Browse files
authored
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.
1 parent 17eb56d commit 256c730

File tree

1 file changed

+95
-43
lines changed

1 file changed

+95
-43
lines changed

internal/api/handlers/v0/servers_test.go

Lines changed: 95 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func TestServersListEndpoint(t *testing.T) {
2525
queryParams string
2626
setupRegistryService func(service.RegistryService)
2727
expectedStatus int
28-
expectedServers []apiv0.ServerJSON
2928
expectedMeta *apiv0.Metadata
3029
expectedError string
3130
}{
@@ -56,8 +55,7 @@ func TestServersListEndpoint(t *testing.T) {
5655
_, _ = registry.Publish(server1)
5756
_, _ = registry.Publish(server2)
5857
},
59-
expectedStatus: http.StatusOK,
60-
expectedServers: nil, // Will be verified differently since IDs are dynamic
58+
expectedStatus: http.StatusOK,
6159
},
6260
{
6361
name: "successful list with cursor and limit",
@@ -75,8 +73,7 @@ func TestServersListEndpoint(t *testing.T) {
7573
}
7674
_, _ = registry.Publish(server)
7775
},
78-
expectedStatus: http.StatusOK,
79-
expectedServers: nil, // Will be verified differently since IDs are dynamic
76+
expectedStatus: http.StatusOK,
8077
},
8178
{
8279
name: "successful list with limit capping at 100",
@@ -118,8 +115,7 @@ func TestServersListEndpoint(t *testing.T) {
118115
setupRegistryService: func(_ service.RegistryService) {
119116
// Test empty registry - empty setup
120117
},
121-
expectedStatus: http.StatusOK,
122-
expectedServers: []apiv0.ServerJSON{},
118+
expectedStatus: http.StatusOK,
123119
},
124120
{
125121
name: "successful search by name substring",
@@ -148,8 +144,7 @@ func TestServersListEndpoint(t *testing.T) {
148144
_, _ = registry.Publish(server1)
149145
_, _ = registry.Publish(server2)
150146
},
151-
expectedStatus: http.StatusOK,
152-
expectedServers: nil, // Will verify in test that only matching server is returned
147+
expectedStatus: http.StatusOK,
153148
},
154149
{
155150
name: "successful updated_since filter with RFC3339",
@@ -167,8 +162,7 @@ func TestServersListEndpoint(t *testing.T) {
167162
}
168163
_, _ = registry.Publish(server)
169164
},
170-
expectedStatus: http.StatusOK,
171-
expectedServers: nil, // Will verify server is returned since it was updated after 2020
165+
expectedStatus: http.StatusOK,
172166
},
173167
{
174168
name: "successful version=latest filter",
@@ -197,8 +191,7 @@ func TestServersListEndpoint(t *testing.T) {
197191
_, _ = registry.Publish(server1)
198192
_, _ = registry.Publish(server2) // This will be marked as latest
199193
},
200-
expectedStatus: http.StatusOK,
201-
expectedServers: nil, // Will verify only latest server is returned
194+
expectedStatus: http.StatusOK,
202195
},
203196
{
204197
name: "combined search and updated_since filter",
@@ -227,8 +220,7 @@ func TestServersListEndpoint(t *testing.T) {
227220
_, _ = registry.Publish(server1)
228221
_, _ = registry.Publish(server2)
229222
},
230-
expectedStatus: http.StatusOK,
231-
expectedServers: nil, // Will verify only matching server is returned
223+
expectedStatus: http.StatusOK,
232224
},
233225
{
234226
name: "invalid updated_since format",
@@ -237,6 +229,58 @@ func TestServersListEndpoint(t *testing.T) {
237229
expectedStatus: http.StatusBadRequest,
238230
expectedError: "Invalid updated_since format: expected RFC3339",
239231
},
232+
{
233+
name: "comprehensive query with all parameters",
234+
queryParams: "?search=filesystem&updated_since=2020-01-01T00:00:00Z&version=latest&limit=50&cursor=",
235+
setupRegistryService: func(registry service.RegistryService) {
236+
// Create multiple versions of servers with different names
237+
server1v1 := apiv0.ServerJSON{
238+
Name: "io.example/filesystem-server",
239+
Description: "Filesystem operations server v1",
240+
Repository: model.Repository{
241+
URL: "https://github.com/example/filesystem",
242+
Source: "github",
243+
ID: "example/filesystem",
244+
},
245+
Version: "1.0.0",
246+
}
247+
server1v2 := apiv0.ServerJSON{
248+
Name: "io.example/filesystem-server",
249+
Description: "Filesystem operations server v2 (latest)",
250+
Repository: model.Repository{
251+
URL: "https://github.com/example/filesystem",
252+
Source: "github",
253+
ID: "example/filesystem",
254+
},
255+
Version: "2.0.0",
256+
}
257+
server2 := apiv0.ServerJSON{
258+
Name: "com.example/database-server",
259+
Description: "Database operations server",
260+
Repository: model.Repository{
261+
URL: "https://github.com/example/database",
262+
Source: "github",
263+
ID: "example/database",
264+
},
265+
Version: "1.0.0",
266+
}
267+
server3 := apiv0.ServerJSON{
268+
Name: "org.another/filesystem-tools",
269+
Description: "Filesystem tools and utilities",
270+
Repository: model.Repository{
271+
URL: "https://github.com/another/filesystem-tools",
272+
Source: "github",
273+
ID: "another/filesystem-tools",
274+
},
275+
Version: "3.0.0",
276+
}
277+
_, _ = registry.Publish(server1v1)
278+
_, _ = registry.Publish(server1v2)
279+
_, _ = registry.Publish(server2)
280+
_, _ = registry.Publish(server3)
281+
},
282+
expectedStatus: http.StatusOK,
283+
},
240284
}
241285

242286
for _, tc := range testCases {
@@ -273,36 +317,44 @@ func TestServersListEndpoint(t *testing.T) {
273317
assert.NoError(t, err)
274318

275319
// Check the response data
276-
if tc.expectedServers != nil {
277-
assert.Equal(t, tc.expectedServers, resp.Servers)
278-
} else {
279-
// For tests with dynamic data, check structure and count
280-
// Special handling for filter test cases
281-
switch tc.name {
282-
case "successful search by name substring":
283-
assert.Len(t, resp.Servers, 1, "Expected exactly one matching server")
284-
assert.Contains(t, resp.Servers[0].Name, "test-server", "Server name should contain search term")
285-
case "successful updated_since filter with RFC3339":
286-
assert.Len(t, resp.Servers, 1, "Expected one server updated after 2020")
287-
assert.Contains(t, resp.Servers[0].Name, "recent-server")
288-
case "successful version=latest filter":
289-
assert.Len(t, resp.Servers, 1, "Expected one latest server")
290-
assert.Contains(t, resp.Servers[0].Description, "latest")
291-
case "combined search and updated_since filter":
292-
assert.Len(t, resp.Servers, 1, "Expected one server matching both filters")
293-
assert.Contains(t, resp.Servers[0].Name, "combined", "Server name should contain search term")
294-
default:
295-
assert.NotEmpty(t, resp.Servers, "Expected at least one server")
296-
}
297-
298-
// General structure validation
320+
switch tc.name {
321+
case "successful search by name substring":
322+
assert.Len(t, resp.Servers, 1, "Expected exactly one matching server")
323+
assert.Contains(t, resp.Servers[0].Name, "test-server", "Server name should contain search term")
324+
case "successful updated_since filter with RFC3339":
325+
assert.Len(t, resp.Servers, 1, "Expected one server updated after 2020")
326+
assert.Contains(t, resp.Servers[0].Name, "recent-server")
327+
case "successful version=latest filter":
328+
assert.Len(t, resp.Servers, 1, "Expected one latest server")
329+
assert.Contains(t, resp.Servers[0].Description, "latest")
330+
case "combined search and updated_since filter":
331+
assert.Len(t, resp.Servers, 1, "Expected one server matching both filters")
332+
assert.Contains(t, resp.Servers[0].Name, "combined", "Server name should contain search term")
333+
case "empty registry returns success":
334+
assert.Empty(t, resp.Servers, "Expected empty server list for empty registry")
335+
case "comprehensive query with all parameters":
336+
// Should return only latest versions of servers matching "filesystem" search term
337+
// Expected: 2 servers (filesystem-server v2.0.0 and filesystem-tools v3.0.0)
338+
assert.Len(t, resp.Servers, 2, "Expected two servers matching all filters")
299339
for _, server := range resp.Servers {
300-
assert.NotEmpty(t, server.Name)
301-
assert.NotEmpty(t, server.Description)
302-
assert.NotNil(t, server.Meta)
303-
assert.NotNil(t, server.Meta.Official)
304-
assert.NotEmpty(t, server.Meta.Official.ID)
340+
assert.Contains(t, server.Name, "filesystem", "Server name should contain 'filesystem'")
305341
}
342+
// Verify the limit parameter worked (should be at most 50, but we only have 2)
343+
assert.LessOrEqual(t, len(resp.Servers), 50, "Should respect limit parameter")
344+
// Verify response includes metadata
345+
assert.NotNil(t, resp.Metadata, "Expected metadata to be present")
346+
assert.Equal(t, 2, resp.Metadata.Count, "Metadata count should match returned servers")
347+
default:
348+
assert.NotEmpty(t, resp.Servers, "Expected at least one server")
349+
}
350+
351+
// General structure validation
352+
for _, server := range resp.Servers {
353+
assert.NotEmpty(t, server.Name)
354+
assert.NotEmpty(t, server.Description)
355+
assert.NotNil(t, server.Meta)
356+
assert.NotNil(t, server.Meta.Official)
357+
assert.NotEmpty(t, server.Meta.Official.ID)
306358
}
307359

308360
// Check metadata if expected

0 commit comments

Comments
 (0)