Skip to content

Commit d81e783

Browse files
tadasantclaude
andcommitted
test: add comprehensive test coverage for server ID endpoints
Add missing test coverage for: - GET /v0/servers/{server_id}?version=X.X.X (specific version parameter) - GET /v0/servers/{server_id}/versions (all versions endpoint) - Service layer methods: GetByServerIDAndVersion, GetAllVersionsByServerID Tests cover: - Valid version retrieval scenarios - Version not found error cases - Server not found error cases - Invalid server ID format handling - Multi-version consistency validation - IsLatest flag verification 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent d9f0cf2 commit d81e783

File tree

2 files changed

+411
-10
lines changed

2 files changed

+411
-10
lines changed

internal/api/handlers/v0/servers_test.go

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,25 +380,53 @@ func TestServersDetailEndpoint(t *testing.T) {
380380
// Create mock registry service
381381
registryService := service.NewRegistryService(database.NewMemoryDB(), config.NewConfig())
382382

383-
testServer, err := registryService.Publish(apiv0.ServerJSON{
383+
// Publish multiple versions of the same server
384+
testServer1, err := registryService.Publish(apiv0.ServerJSON{
384385
Name: "com.example/test-server",
385386
Description: "A test server",
386387
Version: "1.0.0",
387388
})
388389
assert.NoError(t, err)
389390

391+
_, err = registryService.Publish(apiv0.ServerJSON{
392+
Name: "com.example/test-server",
393+
Description: "A test server updated",
394+
Version: "2.0.0",
395+
})
396+
assert.NoError(t, err)
397+
390398
testCases := []struct {
391399
name string
392400
serverID string
401+
version string
393402
expectedStatus int
394403
expectedServer *apiv0.ServerJSON
395404
expectedError string
396405
}{
397406
{
398-
name: "successful get server detail",
399-
serverID: testServer.Meta.Official.ServerID,
407+
name: "successful get server detail (latest)",
408+
serverID: testServer1.Meta.Official.ServerID,
409+
expectedStatus: http.StatusOK,
410+
},
411+
{
412+
name: "successful get server detail with specific version",
413+
serverID: testServer1.Meta.Official.ServerID,
414+
version: "1.0.0",
415+
expectedStatus: http.StatusOK,
416+
},
417+
{
418+
name: "successful get server detail with latest version",
419+
serverID: testServer1.Meta.Official.ServerID,
420+
version: "2.0.0",
400421
expectedStatus: http.StatusOK,
401422
},
423+
{
424+
name: "version not found for server",
425+
serverID: testServer1.Meta.Official.ServerID,
426+
version: "3.0.0",
427+
expectedStatus: http.StatusNotFound,
428+
expectedError: "Server not found",
429+
},
402430
{
403431
name: "invalid server ID format",
404432
serverID: "invalid-uuid",
@@ -424,6 +452,9 @@ func TestServersDetailEndpoint(t *testing.T) {
424452

425453
// Create request
426454
url := "/v0/servers/" + tc.serverID
455+
if tc.version != "" {
456+
url += "?version=" + tc.version
457+
}
427458
req := httptest.NewRequest(http.MethodGet, url, nil)
428459
w := httptest.NewRecorder()
429460

@@ -455,6 +486,115 @@ func TestServersDetailEndpoint(t *testing.T) {
455486
}
456487
}
457488

489+
func TestServersVersionsEndpoint(t *testing.T) {
490+
// Create mock registry service
491+
registryService := service.NewRegistryService(database.NewMemoryDB(), config.NewConfig())
492+
493+
// Publish multiple versions of the same server
494+
testServer1, err := registryService.Publish(apiv0.ServerJSON{
495+
Name: "com.example/versioned-server",
496+
Description: "A versioned test server",
497+
Version: "1.0.0",
498+
})
499+
assert.NoError(t, err)
500+
501+
_, err = registryService.Publish(apiv0.ServerJSON{
502+
Name: "com.example/versioned-server",
503+
Description: "A versioned test server updated",
504+
Version: "2.0.0",
505+
})
506+
assert.NoError(t, err)
507+
508+
_, err = registryService.Publish(apiv0.ServerJSON{
509+
Name: "com.example/versioned-server",
510+
Description: "A versioned test server latest",
511+
Version: "2.1.0",
512+
})
513+
assert.NoError(t, err)
514+
515+
testCases := []struct {
516+
name string
517+
serverID string
518+
expectedStatus int
519+
expectedCount int
520+
expectedError string
521+
}{
522+
{
523+
name: "successful get all versions",
524+
serverID: testServer1.Meta.Official.ServerID,
525+
expectedStatus: http.StatusOK,
526+
expectedCount: 3,
527+
},
528+
{
529+
name: "invalid server ID format",
530+
serverID: "invalid-uuid",
531+
expectedStatus: http.StatusUnprocessableEntity,
532+
expectedError: "validation failed",
533+
},
534+
{
535+
name: "server not found",
536+
serverID: uuid.New().String(),
537+
expectedStatus: http.StatusNotFound,
538+
expectedError: "Server not found",
539+
},
540+
}
541+
542+
for _, tc := range testCases {
543+
t.Run(tc.name, func(t *testing.T) {
544+
// Create a new test API
545+
mux := http.NewServeMux()
546+
api := humago.New(mux, huma.DefaultConfig("Test API", "1.0.0"))
547+
548+
// Register the servers endpoints
549+
v0.RegisterServersEndpoints(api, registryService)
550+
551+
// Create request
552+
url := "/v0/servers/" + tc.serverID + "/versions"
553+
req := httptest.NewRequest(http.MethodGet, url, nil)
554+
w := httptest.NewRecorder()
555+
556+
// Serve the request
557+
mux.ServeHTTP(w, req)
558+
559+
// Check status code
560+
assert.Equal(t, tc.expectedStatus, w.Code)
561+
562+
if tc.expectedStatus == http.StatusOK {
563+
// Check content type
564+
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
565+
566+
// Parse response body
567+
var versionsResp apiv0.ServerListResponse
568+
err := json.NewDecoder(w.Body).Decode(&versionsResp)
569+
assert.NoError(t, err)
570+
571+
// Check the response data
572+
assert.Len(t, versionsResp.Servers, tc.expectedCount)
573+
assert.Equal(t, tc.expectedCount, versionsResp.Metadata.Count)
574+
575+
// Verify all returned servers have the same server ID but different versions
576+
for _, server := range versionsResp.Servers {
577+
assert.Equal(t, tc.serverID, server.Meta.Official.ServerID)
578+
assert.NotEmpty(t, server.Version)
579+
assert.Equal(t, "com.example/versioned-server", server.Name)
580+
}
581+
582+
// Verify versions are included (should have 1.0.0, 2.0.0, 2.1.0)
583+
versions := make([]string, 0, len(versionsResp.Servers))
584+
for _, server := range versionsResp.Servers {
585+
versions = append(versions, server.Version)
586+
}
587+
assert.Contains(t, versions, "1.0.0")
588+
assert.Contains(t, versions, "2.0.0")
589+
assert.Contains(t, versions, "2.1.0")
590+
} else if tc.expectedError != "" {
591+
// Check error message for non-200 responses
592+
assert.Contains(t, w.Body.String(), tc.expectedError)
593+
}
594+
})
595+
}
596+
}
597+
458598
// TestServersEndpointsIntegration tests the servers endpoints with actual HTTP requests
459599
func TestServersEndpointsIntegration(t *testing.T) {
460600
// Create mock registry service

0 commit comments

Comments
 (0)