test: tests para comandos sin prefijo

Tests del parser (pkg/message/parse_test.go):
- Con prefijo !: comando estandar, con args, sin prefijo no detecta, lowercase
- Sin prefijo: comando bare, con args, ! retrocompatible, primer token, lowercase
- Casos borde: mensaje vacio, solo espacios, solo "!"
- Retrocompatibilidad: "!help" con prefix="" produce mismo resultado que con "!"
- Deteccion de menciones independiente del modo de prefijo

Tests del robot (agents/robot_test.go):
- !help muestra comandos sin ! cuando command_prefix es ""
- Comandos custom en modo sin prefijo
- Mismo conjunto de built-ins en ambos modos
- newTestRobot ahora configura command_prefix: "!" explicitamente
This commit is contained in:
2026-04-09 20:10:14 +00:00
parent 421ab5c773
commit 9a3c09ecf6
2 changed files with 291 additions and 1 deletions
+81 -1
View File
@@ -14,8 +14,20 @@ import (
)
// newTestRobot creates a minimal Robot for testing without requiring
// Matrix or network. Fields are initialized directly.
// Matrix or network. Fields are initialized directly. Uses standard "!" prefix.
func newTestRobot(t *testing.T) *Robot {
t.Helper()
return newTestRobotWithPrefix(t, "!")
}
// newTestRobotNoPrefix creates a minimal Robot with command_prefix: "" (no prefix).
func newTestRobotNoPrefix(t *testing.T) *Robot {
t.Helper()
return newTestRobotWithPrefix(t, "")
}
// newTestRobotWithPrefix creates a Robot with the given command prefix.
func newTestRobotWithPrefix(t *testing.T, prefix string) *Robot {
t.Helper()
cfg := &config.AgentConfig{
Agent: config.AgentMeta{
@@ -25,6 +37,11 @@ func newTestRobot(t *testing.T) *Robot {
Description: "robot for tests",
Version: "1.0.0",
},
Matrix: config.MatrixCfg{
Filters: config.FiltersCfg{
CommandPrefix: prefix,
},
},
}
r := &Robot{
cfg: cfg,
@@ -288,3 +305,66 @@ func TestRobotBuiltinCommandCount(t *testing.T) {
}
}
}
// ── No-prefix command tests ───────────────────────────────────────────────
// TestRobotNoPrefixCmdHelp verifies that help shows commands without ! prefix
// when command_prefix is "".
func TestRobotNoPrefixCmdHelp(t *testing.T) {
r := newTestRobotNoPrefix(t)
reply := r.cmdHelp(context.Background(), decision.MessageContext{})
if !strings.Contains(reply, "Comandos disponibles") {
t.Error("help reply missing header")
}
// Commands should appear WITHOUT ! prefix
for _, cmd := range []string{"help", "ping", "status", "info", "version"} {
// Should contain the command name
if !strings.Contains(reply, cmd) {
t.Errorf("help reply missing command %s", cmd)
}
// Should NOT contain "!cmd" as usage (but might contain it elsewhere)
if strings.Contains(reply, "!"+cmd) {
t.Errorf("help reply should not show !%s in no-prefix mode", cmd)
}
}
}
// TestRobotNoPrefixCmdHelpWithCustom verifies custom commands in no-prefix mode.
func TestRobotNoPrefixCmdHelpWithCustom(t *testing.T) {
r := newTestRobotNoPrefix(t)
r.RegisterCommand(
command.Spec{Name: "deploy", Description: "Deploy to env", Usage: "deploy <env>"},
func(_ context.Context, _ decision.MessageContext) string { return "deployed" },
)
reply := r.cmdHelp(context.Background(), decision.MessageContext{})
if !strings.Contains(reply, "Comandos del robot") {
t.Error("help reply missing 'Comandos del robot' section")
}
if !strings.Contains(reply, "deploy") {
t.Error("help reply missing custom command deploy")
}
}
// TestRobotNoPrefixSameBuiltins verifies that no-prefix robots have the
// same set of built-in commands as standard robots.
func TestRobotNoPrefixSameBuiltins(t *testing.T) {
standard := newTestRobot(t)
noPrefix := newTestRobotNoPrefix(t)
if len(standard.commands) != len(noPrefix.commands) {
t.Errorf("command count mismatch: standard=%d, noPrefix=%d",
len(standard.commands), len(noPrefix.commands))
}
for name := range standard.commands {
if _, ok := noPrefix.commands[name]; !ok {
t.Errorf("no-prefix robot missing command %q", name)
}
}
}