@@ -42,32 +42,6 @@ func newTestLogger(t *testing.T) hclog.Logger {
42
42
})
43
43
}
44
44
45
- // extractTestEnvVarNames extracts environment variable names from Arguments for testing.
46
- func extractTestEnvVarNames (t * testing.T , args packages.Arguments ) []string {
47
- t .Helper ()
48
-
49
- var envVars []string
50
- for key , arg := range args {
51
- if arg .VariableType == packages .VariableTypeEnv {
52
- envVars = append (envVars , key )
53
- }
54
- }
55
- return envVars
56
- }
57
-
58
- // extractTestCLIArgNames extracts CLI argument names from Arguments for testing.
59
- func extractTestCLIArgNames (t * testing.T , args packages.Arguments ) []string {
60
- t .Helper ()
61
-
62
- var cliArgs []string
63
- for key , arg := range args {
64
- if arg .VariableType == packages .VariableTypeArg || arg .VariableType == packages .VariableTypeArgBool {
65
- cliArgs = append (cliArgs , key )
66
- }
67
- }
68
- return cliArgs
69
- }
70
-
71
45
func TestRegistry_ID (t * testing.T ) {
72
46
t .Parallel ()
73
47
@@ -456,69 +430,64 @@ func TestRegistry_Arguments_ToDomainType_EdgeCases(t *testing.T) {
456
430
}
457
431
458
432
func TestRegistry_Arguments_ToDomainType_WithTestdata (t * testing.T ) {
433
+ t .Parallel ()
434
+
459
435
testCases := []struct {
460
436
name string
461
437
testdataFile string
462
438
serverName string
463
- description string
464
439
expectedEnv []string
465
440
expectedArgs []string
466
441
}{
467
442
{
468
443
name : "environment variables only" ,
469
444
testdataFile : "arg_test_env_only.json" ,
470
445
serverName : "env-only-server" ,
471
- description : "Server using only environment variables should extract env vars correctly" ,
472
446
expectedEnv : []string {"API_KEY" , "DEBUG_MODE" },
473
- expectedArgs : []string {},
474
447
},
475
448
{
476
449
name : "command-line arguments only" ,
477
450
testdataFile : "arg_test_cli_only.json" ,
478
451
serverName : "cli-only-server" ,
479
- description : "Server using only CLI arguments should extract args correctly" ,
480
- expectedEnv : []string {},
481
- expectedArgs : []string {"OUTPUT_FORMAT" , "PORT" , "ENABLE_CORS" },
452
+ expectedArgs : []string {"--output-format" , "--port" , "--enable-cors" },
482
453
},
483
454
{
484
455
name : "mixed environment and arguments" ,
485
456
testdataFile : "arg_test_mixed_args.json" ,
486
457
serverName : "mixed-args-server" ,
487
- description : "Server with mixed argument types should classify each correctly" ,
488
458
expectedEnv : []string {"DATABASE_URL" },
489
- expectedArgs : []string {"CONFIG_PATH " , "VERBOSE " },
459
+ expectedArgs : []string {"--config-path " , "--verbose " },
490
460
},
491
461
}
492
462
493
463
for _ , tc := range testCases {
494
464
t .Run (tc .name , func (t * testing.T ) {
495
- t .Logf ( "Test description: %s" , tc . description )
465
+ t .Parallel ( )
496
466
497
- // Load testdata registry
498
467
registry := loadTestDataRegistry (t , tc .testdataFile )
499
-
500
- // Get the specific server we're testing
501
468
server , exists := registry [tc .serverName ]
502
- require .True (t , exists , "Server %q not found in testdata file %q" , tc . serverName , tc . testdataFile )
469
+ require .True (t , exists )
503
470
504
- // Convert arguments using Mozilla AI's ToDomainType method
505
471
result , err := server .Arguments .ToDomainType ()
506
- require .NoError (t , err , "Should convert arguments successfully" )
472
+ require .NoError (t , err )
507
473
508
474
// Verify expected environment variables
509
- envVars := extractTestEnvVarNames (t , result )
510
- require .ElementsMatch (t , tc .expectedEnv , envVars ,
511
- "Unexpected environment variables for %s" , tc .serverName )
475
+ envVars := result .FilterBy (packages .EnvVar ).Names ()
476
+ require .ElementsMatch (t , tc .expectedEnv , envVars )
477
+ for _ , key := range tc .expectedEnv {
478
+ require .Equal (t , key , result [key ].Name )
479
+ }
512
480
513
481
// Verify expected CLI arguments
514
- cliArgs := extractTestCLIArgNames (t , result )
515
- require .ElementsMatch (t , tc .expectedArgs , cliArgs ,
516
- "Unexpected CLI arguments for %s" , tc .serverName )
482
+ cliArgs := result .FilterBy (packages .Argument ).Names ()
483
+ require .ElementsMatch (t , tc .expectedArgs , cliArgs )
484
+ for _ , key := range tc .expectedArgs {
485
+ require .Equal (t , key , result [key ].Name )
486
+ }
517
487
518
488
// Verify argument count matches expectations
519
489
expectedTotal := len (tc .expectedEnv ) + len (tc .expectedArgs )
520
- require .Len (t , result , expectedTotal ,
521
- "Total argument count mismatch for %s" , tc .serverName )
490
+ require .Len (t , result , expectedTotal )
522
491
})
523
492
}
524
493
}
@@ -559,8 +528,7 @@ func TestRegistry_BuildPackageResult_ArgumentExtraction(t *testing.T) {
559
528
Installations : map [string ]Installation {
560
529
"npx" : {
561
530
Runtime : NPX ,
562
- Args : []string {"test-server" , "--config" , "${CONFIG_FILE}" , "--debug" },
563
- Env : map [string ]string {"API_TOKEN" : "${API_TOKEN}" },
531
+ Package : "test-server" ,
564
532
Version : "1.0.0" ,
565
533
Description : "Run with npx" ,
566
534
Recommended : true ,
@@ -595,11 +563,11 @@ func TestRegistry_BuildPackageResult_ArgumentExtraction(t *testing.T) {
595
563
require .Len (t , pkg .Arguments , 3 , "Should have 3 arguments" )
596
564
597
565
// Check environment variable
598
- envVars := extractTestEnvVarNames ( t , pkg .Arguments )
566
+ envVars := pkg .Arguments . FilterBy ( packages . EnvVar ). Names ( )
599
567
require .ElementsMatch (t , []string {"API_TOKEN" }, envVars , "Should extract environment variable" )
600
568
601
569
// Check CLI arguments
602
- cliArgs := extractTestCLIArgNames ( t , pkg .Arguments )
570
+ cliArgs := pkg .Arguments . FilterBy ( packages . Argument ). Names ( )
603
571
require .ElementsMatch (t , []string {"CONFIG_FILE" , "DEBUG" }, cliArgs , "Should extract CLI arguments" )
604
572
605
573
// Verify argument details
@@ -626,3 +594,74 @@ func TestRegistry_BuildPackageResult_ArgumentExtraction(t *testing.T) {
626
594
require .False (t , debug .Required )
627
595
require .Equal (t , "Enable debug mode" , debug .Description )
628
596
}
597
+
598
+ func TestRegistry_BuildPackageResult_WithOptionalArguments (t * testing.T ) {
599
+ t .Parallel ()
600
+
601
+ testRegistry := loadTestDataRegistry (t , "registry_optional_args.json" )
602
+
603
+ tests := []struct {
604
+ name string
605
+ serverID string
606
+ checkFunc func (t * testing.T , pkg packages.Package )
607
+ }{
608
+ {
609
+ name : "time server with optional timezone argument" ,
610
+ serverID : "time" ,
611
+ checkFunc : func (t * testing.T , pkg packages.Package ) {
612
+ require .Equal (t , "time" , pkg .Name )
613
+ require .Equal (t , "Time Server" , pkg .DisplayName )
614
+
615
+ // Check optional argument
616
+ tzArg , exists := pkg .Arguments ["--local-timezone" ]
617
+ require .True (t , exists )
618
+ require .False (t , tzArg .Required )
619
+ require .Equal (t , packages .VariableType ("argument" ), tzArg .VariableType )
620
+ require .Equal (t , "America/New_York" , tzArg .Example )
621
+
622
+ // Check installation
623
+ uvxInstall , exists := pkg .Installations [runtime .UVX ]
624
+ require .True (t , exists , "Should have UVX installation" )
625
+ require .Equal (t , "mcp-server-time" , uvxInstall .Package )
626
+ },
627
+ },
628
+ {
629
+ name : "sqlite with required argument and package field" ,
630
+ serverID : "sqlite-with-package" ,
631
+ checkFunc : func (t * testing.T , pkg packages.Package ) {
632
+ require .Equal (t , "sqlite-with-package" , pkg .Name )
633
+
634
+ // Check required argument
635
+ dbArg , exists := pkg .Arguments ["--db-path" ]
636
+ require .True (t , exists )
637
+ require .True (t , dbArg .Required )
638
+
639
+ // Check installation with package field
640
+ uvxInstall , exists := pkg .Installations [runtime .UVX ]
641
+ require .True (t , exists , "Should have UVX installation" )
642
+ require .Equal (t , "mcp-server-sqlite" , uvxInstall .Package )
643
+ },
644
+ },
645
+ }
646
+
647
+ for _ , tc := range tests {
648
+ t .Run (tc .name , func (t * testing.T ) {
649
+ t .Parallel ()
650
+
651
+ logger := newTestLogger (t )
652
+ registry := & Registry {
653
+ mcpServers : testRegistry ,
654
+ logger : logger ,
655
+ supportedRuntimes : map [runtime.Runtime ]struct {}{
656
+ runtime .UVX : {},
657
+ runtime .NPX : {},
658
+ },
659
+ filterOptions : []options.Option {},
660
+ }
661
+
662
+ pkg , ok := registry .buildPackageResult (tc .serverID )
663
+ require .True (t , ok , "Should successfully build package result" )
664
+ tc .checkFunc (t , pkg )
665
+ })
666
+ }
667
+ }
0 commit comments