-
Notifications
You must be signed in to change notification settings - Fork 800
Expand file tree
/
Copy pathregistry_service_test.go
More file actions
1124 lines (1010 loc) · 33.5 KB
/
registry_service_test.go
File metadata and controls
1124 lines (1010 loc) · 33.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//nolint:testpackage
package service
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/modelcontextprotocol/registry/internal/config"
"github.com/modelcontextprotocol/registry/internal/database"
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
"github.com/modelcontextprotocol/registry/pkg/model"
)
func TestValidateNoDuplicateRemoteURLs(t *testing.T) {
ctx := context.Background()
// Create test data
existingServers := map[string]*apiv0.ServerJSON{
"existing1": {
Schema: model.CurrentSchemaURL,
Name: "com.example/existing-server",
Description: "An existing server",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://api.example.com/mcp"},
{Type: "sse", URL: "https://webhook.example.com/sse"},
},
},
"existing2": {
Schema: model.CurrentSchemaURL,
Name: "com.microsoft/another-server",
Description: "Another existing server",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://api.microsoft.com/mcp"},
},
},
}
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
// Create existing servers using the new CreateServer method
for _, server := range existingServers {
_, err := service.CreateServer(ctx, server)
require.NoError(t, err, "failed to create server: %v", err)
}
deprecatedServer := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/deprecated-server",
Description: "A deprecated server",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://deprecated.example.com/mcp"},
},
}
_, err := service.CreateServer(ctx, deprecatedServer)
require.NoError(t, err)
_, err = service.UpdateServerStatus(ctx, deprecatedServer.Name, deprecatedServer.Version, &StatusChangeRequest{
NewStatus: model.StatusDeprecated,
})
require.NoError(t, err)
tests := []struct {
name string
serverDetail apiv0.ServerJSON
expectError bool
errorMsg string
}{
{
name: "no remote URLs - should pass",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/new-server",
Description: "A new server with no remotes",
Version: "1.0.0",
Remotes: []model.Transport{},
},
expectError: false,
},
{
name: "new unique remote URLs - should pass",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/new-server-unique",
Description: "A new server",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://new.example.com/mcp"},
{Type: "sse", URL: "https://unique.example.com/sse"},
},
},
expectError: false,
},
{
name: "duplicate remote URL - should fail",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/new-server-duplicate",
Description: "A new server with duplicate URL",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://api.example.com/mcp"}, // This URL already exists
},
},
expectError: true,
errorMsg: "remote URL https://api.example.com/mcp is already used by server com.example/existing-server",
},
{
name: "duplicate remote URL used only by deprecated server - should pass",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/new-server-with-deprecated-conflict",
Description: "A new server reusing a deprecated remote URL",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://deprecated.example.com/mcp"},
},
},
expectError: false,
},
{
name: "updating same server with same URLs - should pass",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/existing-server", // Same name as existing
Description: "Updated existing server",
Version: "1.1.0", // Different version
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://api.example.com/mcp"}, // Same URL as before
},
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := service.(*registryServiceImpl)
err := impl.validateNoDuplicateRemoteURLs(ctx, nil, tt.serverDetail)
if tt.expectError {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMsg)
} else {
assert.NoError(t, err)
}
})
}
}
func TestGetServerByName(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
// Create multiple versions of the same server
_, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "Test server v1",
Version: "1.0.0",
})
require.NoError(t, err)
_, err = service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "Test server v2",
Version: "2.0.0",
})
require.NoError(t, err)
tests := []struct {
name string
serverName string
expectError bool
errorMsg string
checkResult func(*testing.T, *apiv0.ServerResponse)
}{
{
name: "get latest version by server name",
serverName: "com.example/test-server",
expectError: false,
checkResult: func(t *testing.T, result *apiv0.ServerResponse) {
t.Helper()
assert.Equal(t, "2.0.0", result.Server.Version) // Should get latest version
assert.Equal(t, "Test server v2", result.Server.Description)
assert.True(t, result.Meta.Official.IsLatest)
},
},
{
name: "server not found",
serverName: "com.example/non-existent",
expectError: true,
errorMsg: "record not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := service.GetServerByName(ctx, tt.serverName, false)
if tt.expectError {
assert.Error(t, err)
if tt.errorMsg != "" {
assert.Contains(t, err.Error(), tt.errorMsg)
}
assert.Nil(t, result)
} else {
assert.NoError(t, err)
assert.NotNil(t, result)
if tt.checkResult != nil {
tt.checkResult(t, result)
}
}
})
}
}
func TestGetServerByNameAndVersion(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
serverName := "com.example/versioned-server"
// Create multiple versions of the same server
_, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Versioned server v1",
Version: "1.0.0",
})
require.NoError(t, err)
_, err = service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Versioned server v2",
Version: "2.0.0",
})
require.NoError(t, err)
tests := []struct {
name string
serverName string
version string
expectError bool
errorMsg string
checkResult func(*testing.T, *apiv0.ServerResponse)
}{
{
name: "get specific version 1.0.0",
serverName: serverName,
version: "1.0.0",
expectError: false,
checkResult: func(t *testing.T, result *apiv0.ServerResponse) {
t.Helper()
assert.Equal(t, "1.0.0", result.Server.Version)
assert.Equal(t, "Versioned server v1", result.Server.Description)
assert.False(t, result.Meta.Official.IsLatest)
},
},
{
name: "get specific version 2.0.0",
serverName: serverName,
version: "2.0.0",
expectError: false,
checkResult: func(t *testing.T, result *apiv0.ServerResponse) {
t.Helper()
assert.Equal(t, "2.0.0", result.Server.Version)
assert.Equal(t, "Versioned server v2", result.Server.Description)
assert.True(t, result.Meta.Official.IsLatest)
},
},
{
name: "version not found",
serverName: serverName,
version: "3.0.0",
expectError: true,
errorMsg: "record not found",
},
{
name: "server not found",
serverName: "com.example/non-existent",
version: "1.0.0",
expectError: true,
errorMsg: "record not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := service.GetServerByNameAndVersion(ctx, tt.serverName, tt.version, false)
if tt.expectError {
assert.Error(t, err)
if tt.errorMsg != "" {
assert.Contains(t, err.Error(), tt.errorMsg)
}
assert.Nil(t, result)
} else {
assert.NoError(t, err)
assert.NotNil(t, result)
if tt.checkResult != nil {
tt.checkResult(t, result)
}
}
})
}
}
func TestGetAllVersionsByServerName(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
serverName := "com.example/multi-version-server"
// Create multiple versions of the same server
_, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Multi-version server v1",
Version: "1.0.0",
})
require.NoError(t, err)
_, err = service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Multi-version server v2",
Version: "2.0.0",
})
require.NoError(t, err)
_, err = service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Multi-version server v2.1",
Version: "2.1.0",
})
require.NoError(t, err)
tests := []struct {
name string
serverName string
expectError bool
errorMsg string
checkResult func(*testing.T, []*apiv0.ServerResponse)
}{
{
name: "get all versions of server",
serverName: serverName,
expectError: false,
checkResult: func(t *testing.T, result []*apiv0.ServerResponse) {
t.Helper()
assert.Len(t, result, 3)
// Collect versions
versions := make([]string, 0, len(result))
latestCount := 0
for _, server := range result {
versions = append(versions, server.Server.Version)
assert.Equal(t, serverName, server.Server.Name)
if server.Meta.Official.IsLatest {
latestCount++
}
}
// Verify all versions are present
assert.Contains(t, versions, "1.0.0")
assert.Contains(t, versions, "2.0.0")
assert.Contains(t, versions, "2.1.0")
// Only one should be marked as latest
assert.Equal(t, 1, latestCount)
},
},
{
name: "server not found",
serverName: "com.example/non-existent",
expectError: true,
errorMsg: "record not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := service.GetAllVersionsByServerName(ctx, tt.serverName, false)
if tt.expectError {
assert.Error(t, err)
if tt.errorMsg != "" {
assert.Contains(t, err.Error(), tt.errorMsg)
}
assert.Empty(t, result)
} else {
assert.NoError(t, err)
assert.NotEmpty(t, result)
if tt.checkResult != nil {
tt.checkResult(t, result)
}
}
})
}
}
func TestCreateServerConcurrentVersionsNoRace(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
const concurrency = 100
serverName := "com.example/test-concurrent"
results := make([]*apiv0.ServerResponse, concurrency)
errors := make([]error, concurrency)
var wg sync.WaitGroup
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
result, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: fmt.Sprintf("Version %d", idx),
Version: fmt.Sprintf("1.0.%d", idx),
})
results[idx] = result
errors[idx] = err
}(i)
}
wg.Wait()
// All publishes should succeed
for i, err := range errors {
assert.NoError(t, err, "create server %d failed", i)
}
// All results should have the same server name
for i, result := range results {
if result != nil {
assert.Equal(t, serverName, result.Server.Name, "version %d has different server name", i)
}
}
// Query database to check the final state after all creates complete
allVersions, err := service.GetAllVersionsByServerName(ctx, serverName, false)
require.NoError(t, err, "failed to get all versions")
latestCount := 0
var latestVersion string
for _, r := range allVersions {
if r.Meta.Official.IsLatest {
latestCount++
latestVersion = r.Server.Version
}
}
assert.Equal(t, 1, latestCount, "should have exactly one latest version in database, found version: %s", latestVersion)
assert.Len(t, allVersions, concurrency, "should have all %d versions", concurrency)
}
func TestUpdateServer(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
serverName := "com.example/update-test-server"
version := "1.0.0"
// Create initial server
_, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Original description",
Version: version,
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://original.example.com/mcp"},
},
})
require.NoError(t, err)
tests := []struct {
name string
serverName string
version string
updatedServer *apiv0.ServerJSON
statusChange *StatusChangeRequest
expectError bool
errorMsg string
checkResult func(*testing.T, *apiv0.ServerResponse)
}{
{
name: "successful server update",
serverName: serverName,
version: version,
updatedServer: &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Updated description",
Version: version,
Remotes: []model.Transport{
{Type: "streamable-http", URL: "https://updated.example.com/mcp"},
},
},
expectError: false,
checkResult: func(t *testing.T, result *apiv0.ServerResponse) {
t.Helper()
assert.Equal(t, "Updated description", result.Server.Description)
assert.Len(t, result.Server.Remotes, 1)
assert.Equal(t, "https://updated.example.com/mcp", result.Server.Remotes[0].URL)
assert.NotZero(t, result.Meta.Official.UpdatedAt)
},
},
{
name: "update with status change",
serverName: serverName,
version: version,
updatedServer: &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Updated with status change",
Version: version,
},
statusChange: &StatusChangeRequest{
NewStatus: model.StatusDeprecated,
},
expectError: false,
checkResult: func(t *testing.T, result *apiv0.ServerResponse) {
t.Helper()
assert.Equal(t, "Updated with status change", result.Server.Description)
assert.Equal(t, model.StatusDeprecated, result.Meta.Official.Status)
},
},
{
name: "update non-existent server",
serverName: "com.example/non-existent",
version: "1.0.0",
updatedServer: &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/non-existent",
Description: "Should fail",
Version: "1.0.0",
},
expectError: true,
errorMsg: "record not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := service.UpdateServer(ctx, tt.serverName, tt.version, tt.updatedServer, tt.statusChange)
if tt.expectError {
assert.Error(t, err)
if tt.errorMsg != "" {
assert.Contains(t, err.Error(), tt.errorMsg)
}
assert.Nil(t, result)
} else {
assert.NoError(t, err)
assert.NotNil(t, result)
if tt.checkResult != nil {
tt.checkResult(t, result)
}
}
})
}
}
func TestUpdateServer_SkipValidationForDeletedServers(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
// Enable registry validation to test that it gets skipped for deleted servers
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: true})
serverName := "com.example/validation-skip-test"
version := "1.0.0"
// Create server with invalid package configuration (this would fail registry validation)
invalidServer := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Server with invalid package for testing validation skip",
Version: version,
Packages: []model.Package{
{
RegistryType: "npm",
Identifier: "non-existent-package-for-validation-test",
Version: "1.0.0",
Transport: model.Transport{Type: "stdio"},
},
},
}
// Create initial server (validation disabled for creation in this test)
originalConfig := service.(*registryServiceImpl).cfg.EnableRegistryValidation
service.(*registryServiceImpl).cfg.EnableRegistryValidation = false
_, err := service.CreateServer(ctx, invalidServer)
require.NoError(t, err, "failed to create server with validation disabled")
service.(*registryServiceImpl).cfg.EnableRegistryValidation = originalConfig
// First, set server to deleted status
deletedStatusChange := &StatusChangeRequest{
NewStatus: model.StatusDeleted,
}
_, err = service.UpdateServer(ctx, serverName, version, invalidServer, deletedStatusChange)
require.NoError(t, err, "should be able to set server to deleted (validation should be skipped)")
// Verify server is now deleted (need includeDeleted=true to find it)
updatedServer, err := service.GetServerByNameAndVersion(ctx, serverName, version, true)
require.NoError(t, err)
assert.Equal(t, model.StatusDeleted, updatedServer.Meta.Official.Status)
// Now try to update a deleted server - validation should be skipped
updatedInvalidServer := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: "Updated description for deleted server",
Version: version,
Packages: []model.Package{
{
RegistryType: "npm",
Identifier: "another-non-existent-package-for-validation-test",
Version: "2.0.0",
Transport: model.Transport{Type: "stdio"},
},
},
}
// This should succeed despite invalid packages because server is deleted
result, err := service.UpdateServer(ctx, serverName, version, updatedInvalidServer, nil)
assert.NoError(t, err, "updating deleted server should skip registry validation")
assert.NotNil(t, result)
assert.Equal(t, "Updated description for deleted server", result.Server.Description)
assert.Equal(t, model.StatusDeleted, result.Meta.Official.Status)
// Test updating a server being set to deleted status
activeServer := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/being-deleted-test",
Description: "Server being deleted",
Version: "1.0.0",
Packages: []model.Package{
{
RegistryType: "npm",
Identifier: "yet-another-non-existent-package",
Version: "1.0.0",
Transport: model.Transport{Type: "stdio"},
},
},
}
// Create active server (with validation disabled)
service.(*registryServiceImpl).cfg.EnableRegistryValidation = false
_, err = service.CreateServer(ctx, activeServer)
require.NoError(t, err)
service.(*registryServiceImpl).cfg.EnableRegistryValidation = originalConfig
// Update server and set to deleted in same operation - should skip validation
newDeletedStatusChange := &StatusChangeRequest{
NewStatus: model.StatusDeleted,
}
result2, err := service.UpdateServer(ctx, "com.example/being-deleted-test", "1.0.0", activeServer, newDeletedStatusChange)
assert.NoError(t, err, "updating server being set to deleted should skip registry validation")
assert.NotNil(t, result2)
assert.Equal(t, model.StatusDeleted, result2.Meta.Official.Status)
}
func TestListServers(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
// Create test servers
testServers := []struct {
name string
version string
description string
}{
{"com.example/server-alpha", "1.0.0", "Alpha server"},
{"com.example/server-beta", "1.0.0", "Beta server"},
{"com.example/server-gamma", "2.0.0", "Gamma server"},
}
for _, server := range testServers {
_, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: server.name,
Description: server.description,
Version: server.version,
})
require.NoError(t, err)
}
tests := []struct {
name string
filter *database.ServerFilter
cursor string
limit int
expectedCount int
expectError bool
}{
{
name: "list all servers",
filter: nil,
limit: 10,
expectedCount: 3,
},
{
name: "filter by name",
filter: &database.ServerFilter{
Name: stringPtr("com.example/server-alpha"),
},
limit: 10,
expectedCount: 1,
},
{
name: "filter by version",
filter: &database.ServerFilter{
Version: stringPtr("1.0.0"),
},
limit: 10,
expectedCount: 2,
},
{
name: "pagination with limit",
filter: nil,
limit: 2,
expectedCount: 2,
},
{
name: "cursor pagination",
filter: nil,
cursor: "com.example/server-alpha",
limit: 10,
// Should return servers after 'server-alpha' alphabetically
expectedCount: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
results, nextCursor, err := service.ListServers(ctx, tt.filter, tt.cursor, tt.limit)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Len(t, results, tt.expectedCount)
// Test cursor behavior
if tt.limit < len(testServers) && len(results) == tt.limit {
assert.NotEmpty(t, nextCursor, "Should return next cursor when results are limited")
}
})
}
}
func TestVersionComparison(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
serverName := "com.example/version-comparison-server"
// Create versions in non-chronological order to test version comparison logic
versions := []struct {
version string
description string
delay time.Duration // Delay to simulate different publish times
}{
{"2.0.0", "Version 2.0.0", 0},
{"1.0.0", "Version 1.0.0", 10 * time.Millisecond},
{"2.1.0", "Version 2.1.0", 20 * time.Millisecond},
{"1.5.0", "Version 1.5.0", 30 * time.Millisecond},
}
for _, v := range versions {
if v.delay > 0 {
time.Sleep(v.delay)
}
_, err := service.CreateServer(ctx, &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: serverName,
Description: v.description,
Version: v.version,
})
require.NoError(t, err, "Failed to create version %s", v.version)
}
// Get the latest version - should be 2.1.0 based on semantic versioning
latest, err := service.GetServerByName(ctx, serverName, false)
require.NoError(t, err)
assert.Equal(t, "2.1.0", latest.Server.Version, "Latest version should be 2.1.0")
assert.True(t, latest.Meta.Official.IsLatest)
// Verify only one version is marked as latest
allVersions, err := service.GetAllVersionsByServerName(ctx, serverName, false)
require.NoError(t, err)
latestCount := 0
for _, version := range allVersions {
if version.Meta.Official.IsLatest {
latestCount++
}
}
assert.Equal(t, 1, latestCount, "Exactly one version should be marked as latest")
}
func TestUpdateServerStatus_ValidateRemoteURLsOnRestoreToActive(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
remoteURL := "https://api.example.com/mcp"
// Create Server A with a remote URL
serverA := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/server-a",
Description: "Server A with remote URL",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err := service.CreateServer(ctx, serverA)
require.NoError(t, err)
// Delete Server A
_, err = service.UpdateServerStatus(ctx, "com.example/server-a", "1.0.0", &StatusChangeRequest{
NewStatus: model.StatusDeleted,
})
require.NoError(t, err)
// Create Server B with the same remote URL (should succeed since A is deleted)
serverB := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/server-b",
Description: "Server B with same remote URL",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err = service.CreateServer(ctx, serverB)
require.NoError(t, err)
// Try to restore Server A to active - should fail due to URL conflict
_, err = service.UpdateServerStatus(ctx, "com.example/server-a", "1.0.0", &StatusChangeRequest{
NewStatus: model.StatusActive,
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "remote URL")
assert.Contains(t, err.Error(), "already used by server")
}
func TestUpdateServerStatus_ValidateRemoteURLsOnRestoreFromDeleted(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
remoteURL := "https://api.deleted.com/mcp"
// Create Server A with a remote URL and delete it
serverA := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/deleted-server",
Description: "Server to be deleted",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err := service.CreateServer(ctx, serverA)
require.NoError(t, err)
// Delete Server A
_, err = service.UpdateServerStatus(ctx, "com.example/deleted-server", "1.0.0", &StatusChangeRequest{
NewStatus: model.StatusDeleted,
})
require.NoError(t, err)
// Create Server B with the same remote URL (should succeed since A is deleted)
serverB := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/new-server",
Description: "New server with same remote URL",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err = service.CreateServer(ctx, serverB)
require.NoError(t, err)
// Try to restore deleted server to active - should fail due to URL conflict
_, err = service.UpdateServerStatus(ctx, "com.example/deleted-server", "1.0.0", &StatusChangeRequest{
NewStatus: model.StatusActive,
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "remote URL")
assert.Contains(t, err.Error(), "already used by server")
}
func TestUpdateServerStatus_ValidateRemoteURLsOnRestoreFromDeprecated(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
remoteURL := "https://api.deprecated.com/mcp"
// Create Server A with a remote URL and deprecate it.
serverA := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/deprecated-server",
Description: "Server to be deprecated",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err := service.CreateServer(ctx, serverA)
require.NoError(t, err)
_, err = service.UpdateServerStatus(ctx, "com.example/deprecated-server", "1.0.0", &StatusChangeRequest{
NewStatus: model.StatusDeprecated,
})
require.NoError(t, err)
// Create Server B with the same remote URL (should succeed since A is deprecated).
serverB := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/replacement-server",
Description: "Replacement server with same remote URL",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err = service.CreateServer(ctx, serverB)
require.NoError(t, err)
// Try to restore deprecated server to active - should fail due to URL conflict.
_, err = service.UpdateServerStatus(ctx, "com.example/deprecated-server", "1.0.0", &StatusChangeRequest{
NewStatus: model.StatusActive,
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "remote URL")
assert.Contains(t, err.Error(), "already used by server")
}
func TestUpdateAllVersionsStatus_ValidateRemoteURLsOnRestoreToActive(t *testing.T) {
ctx := context.Background()
testDB := database.NewTestDB(t)
service := NewRegistryService(testDB, &config.Config{EnableRegistryValidation: false})
remoteURL := "https://api.allversions.com/mcp"
// Create Server A with multiple versions
serverAv1 := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/multi-version-server",
Description: "Server A version 1",
Version: "1.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err := service.CreateServer(ctx, serverAv1)
require.NoError(t, err)
serverAv2 := &apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/multi-version-server",
Description: "Server A version 2",
Version: "2.0.0",
Remotes: []model.Transport{
{Type: "streamable-http", URL: remoteURL},
},
}
_, err = service.CreateServer(ctx, serverAv2)
require.NoError(t, err)
// Delete all versions of Server A