From 6ad56557560b515408a54fd1cd0e55d908889c46 Mon Sep 17 00:00:00 2001 From: Micael Marques Date: Tue, 26 May 2026 11:15:00 -0300 Subject: [PATCH 1/2] fix(repl): aceita send --to + --template, rejeita flags desconhecidas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: no REPL, comando como send --to +5511 --template welcome era parseado errado: - phone = "--to" (1o token vira phone) - templateName = "" (nunca lia --to ou --template) - freeformText = "+5511 --template welcome" (tudo cai no default) E o REPL mandava como mensagem de sessao (body livre) ao backend, que rejeitava com 422 "Nenhuma conversa iniciada com este número" porque processSessionMessage chama validateConversationWindow. Fix: - Detectar se primeiro token comeca com "-" (e flag, nao posicional) - Aceitar --to alem do posicional - Rejeitar flags desconhecidas em vez de tratar como freeform — evita mandar lixo pro backend em silencio - Aceitar --template e --vars (alem de -t e -v) — ja funcionava - Adicionar tests cobrindo as 3 sintaxes + edge cases --- internal/tui/repl.go | 43 ++++++++++++++--- internal/tui/repl_send_args_test.go | 74 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 internal/tui/repl_send_args_test.go diff --git a/internal/tui/repl.go b/internal/tui/repl.go index b0f2d63..2514271 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 (posicional + flags curtas) +// 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,23 @@ 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 como freeformText pra REPL + // mostrar erro de uso; nunca tratar silenciosamente como texto. + freeformText = "" + freeformParts = nil + phone = "" + templateName = "" + variables = nil + return + } + 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..155b674 --- /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) + } + }) + } +} From 5801bcaed293e2de59cb872e79f097b33d6e6389 Mon Sep 17 00:00:00 2001 From: Micael Marques Date: Tue, 26 May 2026 11:16:00 -0300 Subject: [PATCH 2/2] fix(repl): apply lint feedback (ineffassign, misspell, staticcheck) --- internal/tui/repl.go | 13 ++++--------- internal/tui/repl_send_args_test.go | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/internal/tui/repl.go b/internal/tui/repl.go index 2514271..598f8f8 100644 --- a/internal/tui/repl.go +++ b/internal/tui/repl.go @@ -1009,7 +1009,7 @@ func (repl REPLModel) executeSendCommand(arguments string) tea.Cmd { // parseSendArgs aceita duas sintaxes (compat com `arara send` shell + REPL): // -// send +5511999 -t welcome (posicional + flags curtas) +// send +5511999 -t welcome (positional + short flags) // send --to +5511999 --template w (estilo CLI shell) // send +5511999 Hello there (freeform — janela 24h obrigatória) // @@ -1049,14 +1049,9 @@ func parseSendArgs(arguments string) (phone string, templateName string, variabl } default: if strings.HasPrefix(token, "-") { - // flag desconhecida — devolve como freeformText pra REPL - // mostrar erro de uso; nunca tratar silenciosamente como texto. - freeformText = "" - freeformParts = nil - phone = "" - templateName = "" - variables = nil - return + // flag desconhecida — devolve zero values pra REPL mostrar + // erro de uso; nunca tratar silenciosamente como texto. + return "", "", nil, "" } freeformParts = append(freeformParts, token) } diff --git a/internal/tui/repl_send_args_test.go b/internal/tui/repl_send_args_test.go index 155b674..1e6acf0 100644 --- a/internal/tui/repl_send_args_test.go +++ b/internal/tui/repl_send_args_test.go @@ -63,7 +63,7 @@ func TestParseSendArgs(t *testing.T) { 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) { + 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 {