diff --git a/ui/input.go b/ui/input.go index 0a770bb..28aa4c8 100644 --- a/ui/input.go +++ b/ui/input.go @@ -18,18 +18,6 @@ const cmdHelp = `/acn on | Turn on the Tor /start | Show getting started guide /quit | Quit the program` -func isAcnOnCmd(cmds []string) bool { - return cmds[0] == "acn" && cmds[1] == "on" -} - -func isAcnOffCmd(cmds []string) bool { - return cmds[0] == "acn" && cmds[1] == "off" -} - -func isClearCmd(cmds []string) bool { - return cmds[0] == "clear" -} - func handleCommand(cmd, hiddenInput string) tea.Msg { cmds := strings.Split(cmd, " ") @@ -71,23 +59,36 @@ func handleCommand(cmd, hiddenInput string) tea.Msg { } passwords := strings.Split(strings.TrimSpace(hiddenInput), " ") + if len(passwords) != 2 { + return cmdMsg{output: []string{"Profile create: unable to parse hidden input"}} + } + if passwords[0] != passwords[1] { return cmdMsg{output: []string{"Profile create: passwords do not match?"}} } - return createProfileMsg{ + return profileCreateMsg{ name: cmds[2], password: passwords[1], } } - case isAcnOnCmd(cmds): - return turnAcnOnMsg{} + case cmds[0] == "acn": + if cmds[1] != "on" && cmds[1] != "off" { + acnHelp := `Unknown "acn" command? +/acn on | Turn on the Tor ACN +/acn off | Turn off the Tor ACN` + return cmdMsg{output: strings.Split(acnHelp, "\n")} + } - case isAcnOffCmd(cmds): - return turnAcnOffMsg{} + switch { + case cmds[1] == "on": + return turnAcnOnMsg{} + case cmds[1] == "off": + return turnAcnOffMsg{} + } - case isClearCmd(cmds): + case cmds[0] == "clear": return clearScreenMsg{} } diff --git a/ui/input_test.go b/ui/input_test.go index bc28167..e631f70 100644 --- a/ui/input_test.go +++ b/ui/input_test.go @@ -6,8 +6,34 @@ import ( "github.com/stretchr/testify/assert" ) -func TestHandleCommand(t *testing.T) { - msg := handleCommand("/foo", "") +func TestHandleCommandTypes(t *testing.T) { + tests := []struct { + cmdInput string + hiddenInput string + cmdOutput interface{} + }{ + {"help", "", cmdMsg{}}, + {"quit", "", turnAcnOffMsg{}}, + {"start", "", showGettingStartedGuideMsg{}}, + {"profile unlock foo", "", profileUnlockMsg{}}, + {"profile info", "", profileInfoMsg{}}, + {"profile create foo *** ***", "bar bar", profileCreateMsg{}}, + {"acn on", "", turnAcnOnMsg{}}, + {"acn off", "", turnAcnOffMsg{}}, + {"clear", "", clearScreenMsg{}}, + } + + for _, test := range tests { + msg := handleCommand(test.cmdInput, test.hiddenInput) + switch msg := msg.(type) { + default: + assert.IsType(t, msg, test.cmdOutput) + } + } +} + +func TestHandleCommandUnknownCommand(t *testing.T) { + msg := handleCommand("foo", "") switch msg := msg.(type) { case cmdMsg: assert.Equal(t, msg.output, []string{unknownCmdHelp}) @@ -15,3 +41,13 @@ func TestHandleCommand(t *testing.T) { assert.IsType(t, msg, cmdMsg{}) } } + +func TestHandleCommandIncorrectCommand(t *testing.T) { + msg := handleCommand("profile foo", "") + switch msg := msg.(type) { + case cmdMsg: + assert.Contains(t, msg.output, `Unknown "profile" command?`) + default: + assert.IsType(t, msg, cmdMsg{}) + } +} diff --git a/ui/message.go b/ui/message.go index 803dbc7..3fa5936 100644 --- a/ui/message.go +++ b/ui/message.go @@ -49,7 +49,7 @@ type profileInfoMsg struct{} type startProfileQueuePollMsg struct{ onion string } -type createProfileMsg struct { +type profileCreateMsg struct { name string password string } diff --git a/ui/model.go b/ui/model.go index 6d9353d..c0f46c6 100644 --- a/ui/model.go +++ b/ui/model.go @@ -332,12 +332,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, m.sendStatusCmd(msg.Error())) case showGettingStartedGuideMsg: + m.input.Reset() cmds = append(cmds, m.sendStatusCmd( strings.Split(gettingStartedMessage, "\n")..., )) - m.input.Reset() - case createProfileMsg: + case profileCreateMsg: m.hiddenInput = "" m.input.Reset()