diff --git a/internal/tui/repl.go b/internal/tui/repl.go index b0f2d63..598f8f8 100644 --- a/internal/tui/repl.go +++ b/internal/tui/repl.go @@ -1007,17 +1007,36 @@ func (repl REPLModel) executeSendCommand(arguments string) tea.Cmd { } } +// parseSendArgs aceita duas sintaxes (compat com `arara send` shell + REPL): +// +// send +5511999 -t welcome (positional + short flags) +// send --to +5511999 --template w (estilo CLI shell) +// send +5511999 Hello there (freeform — janela 24h obrigatória) +// +// Flag desconhecida vira erro de uso, não silenciosamente "freeform" — evita +// que `--to ` (estilo CLI) seja interpretado como texto livre e mandado +// pra processSessionMessage no backend. func parseSendArgs(arguments string) (phone string, templateName string, variables []string, freeformText string) { parts := strings.Fields(arguments) if len(parts) == 0 { return } - phone = parts[0] - remaining := parts[1:] + remaining := parts + if !strings.HasPrefix(parts[0], "-") { + phone = parts[0] + remaining = parts[1:] + } + freeformParts := make([]string, 0, len(remaining)) for index := 0; index < len(remaining); index++ { - switch remaining[index] { + token := remaining[index] + switch token { + case "--to": + if index+1 < len(remaining) { + index++ + phone = remaining[index] + } case "-t", "--template": if index+1 < len(remaining) { index++ @@ -1029,11 +1048,18 @@ func parseSendArgs(arguments string) (phone string, templateName string, variabl variables = strings.Split(remaining[index], ",") } default: - // everything else is freeform text - freeformText = strings.Join(remaining[index:], " ") - return + if strings.HasPrefix(token, "-") { + // flag desconhecida — devolve zero values pra REPL mostrar + // erro de uso; nunca tratar silenciosamente como texto. + return "", "", nil, "" + } + freeformParts = append(freeformParts, token) } } + + if templateName == "" && len(freeformParts) > 0 { + freeformText = strings.Join(freeformParts, " ") + } return } diff --git a/internal/tui/repl_send_args_test.go b/internal/tui/repl_send_args_test.go new file mode 100644 index 0000000..1e6acf0 --- /dev/null +++ b/internal/tui/repl_send_args_test.go @@ -0,0 +1,74 @@ +package tui + +import ( + "reflect" + "testing" +) + +func TestParseSendArgs(t *testing.T) { + tests := []struct { + name string + input string + wantPhone string + wantTemplateName string + wantVariables []string + wantFreeformText string + }{ + { + name: "positional + short flag", + input: "+5511999999999 -t welcome", + wantPhone: "+5511999999999", + wantTemplateName: "welcome", + }, + { + name: "positional + long flag", + input: "+5511999999999 --template welcome", + wantPhone: "+5511999999999", + wantTemplateName: "welcome", + }, + { + name: "CLI-shell style: --to + --template", + input: "--to +5511999999999 --template welcome", + wantPhone: "+5511999999999", + wantTemplateName: "welcome", + }, + { + name: "with vars", + input: "+5511 -t hi -v João,Doe", + wantPhone: "+5511", + wantTemplateName: "hi", + wantVariables: []string{"João", "Doe"}, + }, + { + name: "freeform when no template flag", + input: "+5511999999999 Hello there", + wantPhone: "+5511999999999", + wantFreeformText: "Hello there", + }, + { + name: "unknown flag returns empty (REPL shows usage)", + input: "+5511 --foo bar", + }, + { + name: "empty input returns zero values", + }, + } + + for _, testCase := range tests { + t.Run(testCase.name, func(t *testing.T) { + phone, templateName, variables, freeformText := parseSendArgs(testCase.input) + if phone != testCase.wantPhone { + t.Errorf("phone = %q, want %q", phone, testCase.wantPhone) + } + if templateName != testCase.wantTemplateName { + t.Errorf("templateName = %q, want %q", templateName, testCase.wantTemplateName) + } + if !reflect.DeepEqual(variables, testCase.wantVariables) && (len(variables) != 0 || len(testCase.wantVariables) != 0) { + t.Errorf("variables = %v, want %v", variables, testCase.wantVariables) + } + if freeformText != testCase.wantFreeformText { + t.Errorf("freeformText = %q, want %q", freeformText, testCase.wantFreeformText) + } + }) + } +}