|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMCPGateway_BasicStartup(t *testing.T) { |
| 16 | + // Skip if the binary doesn't exist |
| 17 | + binaryPath := "../../gh-aw" |
| 18 | + if _, err := os.Stat(binaryPath); os.IsNotExist(err) { |
| 19 | + t.Skip("Skipping test: gh-aw binary not found. Run 'make build' first.") |
| 20 | + } |
| 21 | + |
| 22 | + // Create temporary config |
| 23 | + tmpDir := t.TempDir() |
| 24 | + configFile := filepath.Join(tmpDir, "gateway-config.json") |
| 25 | + |
| 26 | + config := MCPGatewayConfig{ |
| 27 | + MCPServers: map[string]MCPServerConfig{ |
| 28 | + "gh-aw": { |
| 29 | + Command: binaryPath, |
| 30 | + Args: []string{"mcp-server"}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + Gateway: GatewaySettings{ |
| 34 | + Port: 8088, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + configJSON, err := json.Marshal(config) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("Failed to marshal config: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + if err := os.WriteFile(configFile, configJSON, 0644); err != nil { |
| 44 | + t.Fatalf("Failed to write config file: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Start gateway in background |
| 48 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 49 | + defer cancel() |
| 50 | + |
| 51 | + // Use the runMCPGateway function directly in a goroutine |
| 52 | + errChan := make(chan error, 1) |
| 53 | + go func() { |
| 54 | + errChan <- runMCPGateway(configFile, 8088, tmpDir) |
| 55 | + }() |
| 56 | + |
| 57 | + // Wait for server to start |
| 58 | + select { |
| 59 | + case <-ctx.Done(): |
| 60 | + t.Fatal("Context canceled before server could start") |
| 61 | + case <-time.After(2 * time.Second): |
| 62 | + // Server should be ready |
| 63 | + } |
| 64 | + |
| 65 | + // Test health endpoint |
| 66 | + resp, err := http.Get("http://localhost:8088/health") |
| 67 | + if err != nil { |
| 68 | + cancel() |
| 69 | + t.Fatalf("Failed to connect to gateway: %v", err) |
| 70 | + } |
| 71 | + defer resp.Body.Close() |
| 72 | + |
| 73 | + if resp.StatusCode != http.StatusOK { |
| 74 | + t.Errorf("Expected status 200, got %d", resp.StatusCode) |
| 75 | + } |
| 76 | + |
| 77 | + // Test servers list endpoint |
| 78 | + resp, err = http.Get("http://localhost:8088/servers") |
| 79 | + if err != nil { |
| 80 | + cancel() |
| 81 | + t.Fatalf("Failed to get servers list: %v", err) |
| 82 | + } |
| 83 | + defer resp.Body.Close() |
| 84 | + |
| 85 | + var serversResp map[string]any |
| 86 | + if err := json.NewDecoder(resp.Body).Decode(&serversResp); err != nil { |
| 87 | + t.Fatalf("Failed to decode servers response: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + servers, ok := serversResp["servers"].([]any) |
| 91 | + if !ok { |
| 92 | + t.Fatal("Expected servers array in response") |
| 93 | + } |
| 94 | + |
| 95 | + if len(servers) != 1 { |
| 96 | + t.Errorf("Expected 1 server, got %d", len(servers)) |
| 97 | + } |
| 98 | + |
| 99 | + // Check if gh-aw server is present |
| 100 | + foundGhAw := false |
| 101 | + for _, server := range servers { |
| 102 | + if serverName, ok := server.(string); ok && serverName == "gh-aw" { |
| 103 | + foundGhAw = true |
| 104 | + break |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if !foundGhAw { |
| 109 | + t.Error("Expected gh-aw server in servers list") |
| 110 | + } |
| 111 | + |
| 112 | + // Cancel context to stop the server |
| 113 | + cancel() |
| 114 | + |
| 115 | + // Wait for server to stop or timeout |
| 116 | + select { |
| 117 | + case err := <-errChan: |
| 118 | + // Server stopped, check if it was a clean shutdown |
| 119 | + if err != nil && err != http.ErrServerClosed && err.Error() != "context canceled" { |
| 120 | + t.Logf("Server stopped with error: %v", err) |
| 121 | + } |
| 122 | + case <-time.After(2 * time.Second): |
| 123 | + t.Log("Server shutdown timed out") |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestMCPGateway_ConfigFromStdin(t *testing.T) { |
| 128 | + // This test would require piping config to stdin |
| 129 | + // which is more complex in Go tests, so we'll skip for now |
| 130 | + t.Skip("Stdin config test requires more complex setup") |
| 131 | +} |
0 commit comments