mirror of
https://github.com/luxfi/cli.git
synced 2026-10-30 17:01:20 +00:00
- Update github.com/luxfi/netrunner v1.15.3 → v1.15.4 - Update github.com/luxfi/node v1.22.77 → v1.22.80 - Remove local replace directive for netrunner - Use published package versions for reproducible builds
170 lines
4.6 KiB
Go
170 lines
4.6 KiB
Go
// Copyright (C) 2022-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package flags
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/luxfi/cli/pkg/application"
|
|
"github.com/luxfi/cli/pkg/prompts/mocks"
|
|
"github.com/spf13/cobra"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestValidateRPC(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
rpcURL string
|
|
cmdName string
|
|
args []string
|
|
promptURL string
|
|
promptError error
|
|
wantError bool
|
|
shouldPrompt bool
|
|
expectedRPCURL string
|
|
}{
|
|
// URL validation test cases
|
|
{
|
|
name: "Valid HTTP URL",
|
|
rpcURL: "http://localhost:9630",
|
|
cmdName: "testcmd",
|
|
args: []string{},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "http://localhost:9630",
|
|
},
|
|
{
|
|
name: "Valid HTTPS URL",
|
|
rpcURL: "https://api.lux.network:9630",
|
|
cmdName: "testcmd",
|
|
args: []string{},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "https://api.lux.network:9630",
|
|
},
|
|
{
|
|
name: "Invalid URL - no protocol",
|
|
rpcURL: "localhost:9630",
|
|
cmdName: "testcmd",
|
|
args: []string{},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "localhost:9630",
|
|
},
|
|
{
|
|
name: "Invalid URL - malformed",
|
|
rpcURL: "http://invalid url",
|
|
cmdName: "testcmd",
|
|
args: []string{},
|
|
wantError: true,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "http://invalid url",
|
|
},
|
|
// Command-specific test cases
|
|
{
|
|
name: "addValidator command with empty URL - valid prompt response",
|
|
rpcURL: "",
|
|
cmdName: "addValidator",
|
|
args: []string{},
|
|
promptURL: "https://api.lux.network:9630",
|
|
promptError: nil,
|
|
wantError: false,
|
|
shouldPrompt: true,
|
|
expectedRPCURL: "https://api.lux.network:9630",
|
|
},
|
|
{
|
|
name: "addValidator command with empty URL - invalid prompt response",
|
|
rpcURL: "",
|
|
cmdName: "addValidator",
|
|
args: []string{},
|
|
promptURL: "invalid-url",
|
|
promptError: fmt.Errorf("invalid URL"),
|
|
wantError: true,
|
|
shouldPrompt: true,
|
|
expectedRPCURL: "invalid-url",
|
|
},
|
|
{
|
|
name: "addValidator command with args",
|
|
rpcURL: "",
|
|
cmdName: "addValidator",
|
|
args: []string{"arg1"},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "",
|
|
},
|
|
{
|
|
name: "addValidator command with valid URL",
|
|
rpcURL: "https://api.lux.network:9630",
|
|
cmdName: "addValidator",
|
|
args: []string{},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "https://api.lux.network:9630",
|
|
},
|
|
{
|
|
name: "non-addValidator command with empty URL",
|
|
rpcURL: "",
|
|
cmdName: "otherCommand",
|
|
args: []string{},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "",
|
|
},
|
|
{
|
|
name: "non-addValidator command with valid URL",
|
|
rpcURL: "https://api.lux.network:9630",
|
|
cmdName: "otherCommand",
|
|
args: []string{},
|
|
wantError: false,
|
|
shouldPrompt: false,
|
|
expectedRPCURL: "https://api.lux.network:9630",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
// Create and configure mock prompter if needed
|
|
var mockPrompter *mocks.Prompter
|
|
if tt.cmdName == "addValidator" && len(tt.args) == 0 && tt.rpcURL == "" {
|
|
mockPrompter = &mocks.Prompter{}
|
|
mockPrompter.On("CaptureURL", "What is the RPC endpoint?", false).
|
|
Return(tt.promptURL, tt.promptError)
|
|
}
|
|
|
|
app := application.New()
|
|
// Use Setup method to properly initialize the app with the mock prompter
|
|
app.Setup("", nil, nil, mockPrompter, nil)
|
|
cmd := &cobra.Command{Use: tt.cmdName}
|
|
|
|
rpcValue := tt.rpcURL
|
|
err := ValidateRPC(app, &rpcValue, cmd, tt.args)
|
|
|
|
// Check error expectation
|
|
if tt.wantError {
|
|
require.Error(err)
|
|
} else {
|
|
require.NoError(err)
|
|
}
|
|
|
|
// Verify prompt expectations
|
|
if mockPrompter != nil {
|
|
mockPrompter.AssertExpectations(t)
|
|
|
|
// Verify prompt calls
|
|
if tt.shouldPrompt {
|
|
mockPrompter.AssertCalled(t, "CaptureURL", "What is the RPC endpoint?", false)
|
|
} else {
|
|
mockPrompter.AssertNotCalled(t, "CaptureURL", "What is the RPC endpoint?", false)
|
|
}
|
|
}
|
|
|
|
// Verify final RPC URL value
|
|
require.Equal(tt.expectedRPCURL, rpcValue, "RPC URL value mismatch")
|
|
})
|
|
}
|
|
}
|