alby-hub/utils/utils_test.go
Roman D d799bdff76
feat: proposal: custom node commands (#1007)
* feat: custom node command execution models and methods

* feat: implement custom node command handlers for HTTP server and Wails

* chore: expose GetNodeCommands API methods in HTTP and Wails

* chore: add sample custom node command implementation for Cashu restore

* feat: add frontend for custom node commands

* chore: consistent naming of custom node command entities

* chore: consistent naming of custom node command entities

* chore: return interface{} as custom node command execution result

* test: add tests for ParseCommandLine

* chore: add extra ParseCommandLine tests for json

* test: add API tests and mocks

* fix: stabilize order of arguments when invoking custom node commands

* test: add test for unsupported custom node command argument

* chore: add the mockery configuration file and move mocks to tests/mocks

* docs: document mockery usage

---------

Co-authored-by: Roland Bewick <roland.bewick@gmail.com>
2025-01-30 18:20:17 +07:00

102 lines
2.5 KiB
Go

package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseCommandLine(t *testing.T) {
t.Parallel()
type testCase struct {
name string
input string
expectedSuccess []string
expectedError string
}
// When called by the API, the first argument of the command input is actually the command name
testCases := []testCase{
{
name: "empty input",
input: "",
expectedSuccess: []string{},
expectedError: "",
},
{
name: "single argument",
input: "arg1",
expectedSuccess: []string{"arg1"},
expectedError: "",
},
{
name: "multiple arguments",
input: "arg1 arg2 arg3",
expectedSuccess: []string{"arg1", "arg2", "arg3"},
expectedError: "",
},
{
name: "multiple arguments with extra whitespace",
input: " arg1\targ2 arg3 ",
expectedSuccess: []string{"arg1", "arg2", "arg3"},
expectedError: "",
},
{
name: "multiple arguments with quotes and escaping",
input: `"arg 1" arg2 "arg\"3"`,
expectedSuccess: []string{"arg 1", "arg2", `arg"3`},
expectedError: "",
},
{
name: "unquoted escaped whitespace",
input: `arg\ 1 arg2`,
expectedSuccess: []string{"arg 1", "arg2"},
expectedError: "",
},
{
name: "escaped JSON",
input: `{\"hello\":\"world\"}`,
expectedSuccess: []string{`{"hello":"world"}`},
expectedError: "",
},
{
name: "escaped JSON with space",
input: `"{\"hello\": \"world\"}"`,
expectedSuccess: []string{`{"hello": "world"}`},
expectedError: "",
},
{
name: "unclosed quote",
input: `"arg 1", "arg2", "arg\"3`,
expectedSuccess: nil,
expectedError: "unexpected end of string",
},
{
name: "three quotes",
input: `"""`,
expectedSuccess: nil,
expectedError: "unexpected end of string",
},
{
name: "unfinished escape",
input: `arg\`,
expectedSuccess: nil,
expectedError: "unexpected end of string",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
parsedArgs, err := ParseCommandLine(tc.input)
if tc.expectedError == "" {
assert.NoError(t, err)
assert.Equal(t, tc.expectedSuccess, parsedArgs)
} else {
assert.EqualError(t, err, tc.expectedError)
assert.Empty(t, parsedArgs)
}
})
}
}