Skip to content

Commit daf61c0

Browse files
Copilotpelikhan
andcommitted
Add tests for mcp-gateway command
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 9f318c6 commit daf61c0

2 files changed

Lines changed: 173 additions & 1 deletion

File tree

pkg/cli/mcp_gateway_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (g *MCPGatewayServer) startHTTPServer() error {
288288
WriteTimeout: 30 * time.Second,
289289
}
290290

291-
fmt.Fprintf(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("MCP gateway listening on http://localhost:%d", port)))
291+
fmt.Fprintf(os.Stderr, "%s\n", console.FormatSuccessMessage(fmt.Sprintf("MCP gateway listening on http://localhost:%d", port)))
292292
gatewayLog.Printf("HTTP server ready on port %d", port)
293293

294294
return server.ListenAndServe()
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package cli
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
)
9+
10+
func TestReadGatewayConfig_FromFile(t *testing.T) {
11+
// Create a temporary config file
12+
tmpDir := t.TempDir()
13+
configFile := filepath.Join(tmpDir, "gateway-config.json")
14+
15+
config := MCPGatewayConfig{
16+
MCPServers: map[string]MCPServerConfig{
17+
"test-server": {
18+
Command: "test-command",
19+
Args: []string{"arg1", "arg2"},
20+
Env: map[string]string{
21+
"KEY": "value",
22+
},
23+
},
24+
},
25+
Gateway: GatewaySettings{
26+
Port: 8080,
27+
},
28+
}
29+
30+
configJSON, err := json.Marshal(config)
31+
if err != nil {
32+
t.Fatalf("Failed to marshal config: %v", err)
33+
}
34+
35+
if err := os.WriteFile(configFile, configJSON, 0644); err != nil {
36+
t.Fatalf("Failed to write config file: %v", err)
37+
}
38+
39+
// Read config
40+
result, err := readGatewayConfig(configFile)
41+
if err != nil {
42+
t.Fatalf("Failed to read config: %v", err)
43+
}
44+
45+
// Verify config
46+
if len(result.MCPServers) != 1 {
47+
t.Errorf("Expected 1 server, got %d", len(result.MCPServers))
48+
}
49+
50+
testServer, exists := result.MCPServers["test-server"]
51+
if !exists {
52+
t.Fatal("test-server not found in config")
53+
}
54+
55+
if testServer.Command != "test-command" {
56+
t.Errorf("Expected command 'test-command', got '%s'", testServer.Command)
57+
}
58+
59+
if len(testServer.Args) != 2 {
60+
t.Errorf("Expected 2 args, got %d", len(testServer.Args))
61+
}
62+
63+
if result.Gateway.Port != 8080 {
64+
t.Errorf("Expected port 8080, got %d", result.Gateway.Port)
65+
}
66+
}
67+
68+
func TestReadGatewayConfig_InvalidJSON(t *testing.T) {
69+
// Create a temporary config file with invalid JSON
70+
tmpDir := t.TempDir()
71+
configFile := filepath.Join(tmpDir, "invalid-config.json")
72+
73+
if err := os.WriteFile(configFile, []byte("not valid json"), 0644); err != nil {
74+
t.Fatalf("Failed to write config file: %v", err)
75+
}
76+
77+
// Read config - should fail
78+
_, err := readGatewayConfig(configFile)
79+
if err == nil {
80+
t.Error("Expected error for invalid JSON, got nil")
81+
}
82+
}
83+
84+
func TestMCPGatewayConfig_EmptyServers(t *testing.T) {
85+
config := &MCPGatewayConfig{
86+
MCPServers: make(map[string]MCPServerConfig),
87+
Gateway: GatewaySettings{
88+
Port: 8080,
89+
},
90+
}
91+
92+
if len(config.MCPServers) != 0 {
93+
t.Errorf("Expected 0 servers, got %d", len(config.MCPServers))
94+
}
95+
}
96+
97+
func TestMCPServerConfig_CommandType(t *testing.T) {
98+
config := MCPServerConfig{
99+
Command: "gh",
100+
Args: []string{"aw", "mcp-server"},
101+
Env: map[string]string{
102+
"DEBUG": "cli:*",
103+
},
104+
}
105+
106+
if config.Command != "gh" {
107+
t.Errorf("Expected command 'gh', got '%s'", config.Command)
108+
}
109+
110+
if config.URL != "" {
111+
t.Error("Expected empty URL for command-based server")
112+
}
113+
114+
if config.Container != "" {
115+
t.Error("Expected empty container for command-based server")
116+
}
117+
}
118+
119+
func TestMCPServerConfig_URLType(t *testing.T) {
120+
config := MCPServerConfig{
121+
URL: "http://localhost:3000",
122+
}
123+
124+
if config.URL != "http://localhost:3000" {
125+
t.Errorf("Expected URL 'http://localhost:3000', got '%s'", config.URL)
126+
}
127+
128+
if config.Command != "" {
129+
t.Error("Expected empty command for URL-based server")
130+
}
131+
}
132+
133+
func TestMCPServerConfig_ContainerType(t *testing.T) {
134+
config := MCPServerConfig{
135+
Container: "mcp-server:latest",
136+
Args: []string{"--verbose"},
137+
Env: map[string]string{
138+
"LOG_LEVEL": "debug",
139+
},
140+
}
141+
142+
if config.Container != "mcp-server:latest" {
143+
t.Errorf("Expected container 'mcp-server:latest', got '%s'", config.Container)
144+
}
145+
146+
if config.Command != "" {
147+
t.Error("Expected empty command for container-based server")
148+
}
149+
150+
if config.URL != "" {
151+
t.Error("Expected empty URL for container-based server")
152+
}
153+
}
154+
155+
func TestGatewaySettings_DefaultPort(t *testing.T) {
156+
settings := GatewaySettings{}
157+
158+
if settings.Port != 0 {
159+
t.Errorf("Expected default port 0, got %d", settings.Port)
160+
}
161+
}
162+
163+
func TestGatewaySettings_WithAPIKey(t *testing.T) {
164+
settings := GatewaySettings{
165+
Port: 8080,
166+
APIKey: "test-api-key",
167+
}
168+
169+
if settings.APIKey != "test-api-key" {
170+
t.Errorf("Expected API key 'test-api-key', got '%s'", settings.APIKey)
171+
}
172+
}

0 commit comments

Comments
 (0)