Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions internal/tui/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <phone>` (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++
Expand All @@ -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
}

Expand Down
74 changes: 74 additions & 0 deletions internal/tui/repl_send_args_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading