Files
picoclaw/pkg/commands/cmd_new_test.go
T
loveuer a7dba6859b feat: support fresh IM sessions and OpenAI reasoning controls
支持 /new 清空会话或携带新问题执行,忙碌时拒绝并保留原任务状态。
修复路由会话隔离及旧摘要回写,完善 OpenAI reasoning.effort 和 temperature 处理。
补充中英文文档与测试;相关包回归、race、lint 和 Gateway 构建通过。
2026-11-11 11:26:23 +08:00

66 lines
2.1 KiB
Go

package commands
import (
"context"
"errors"
"strings"
"testing"
)
func TestNewCommand(t *testing.T) {
for _, tt := range []struct{ input, prompt string }{
{"/new", ""},
{" /new \t\n", ""},
{"/new 帮我查查今天成都的天气", "帮我查查今天成都的天气"},
{"/new 看看今天有啥新咖啡", "看看今天有啥新咖啡"},
{"/NEW@testbot\t first line\nsecond\tline ", "first line\nsecond\tline"},
{"!new\n问题", "问题"},
{"/new /clear", "/clear"},
} {
t.Run(tt.input, func(t *testing.T) {
calls := 0
rt := &Runtime{StartNewConversation: func(ctx context.Context, prompt string) (string, error) {
calls++
if prompt != tt.prompt {
t.Fatalf("prompt = %q, want %q", prompt, tt.prompt)
}
return "new reply", nil
}}
var reply string
res := NewExecutor(NewRegistry(BuiltinDefinitions()), rt).Execute(t.Context(), Request{
Text: tt.input,
Reply: func(text string) error { reply = text; return nil },
})
if res.Outcome != OutcomeHandled || res.Err != nil || calls != 1 || reply != "new reply" {
t.Fatalf("result = %+v, calls = %d, reply = %q", res, calls, reply)
}
})
}
}
func TestNewCommand_UnavailableAndFailure(t *testing.T) {
for _, rt := range []*Runtime{nil, {}} {
var reply string
res := NewExecutor(NewRegistry(BuiltinDefinitions()), rt).Execute(t.Context(), Request{
Text: "/new question",
Reply: func(text string) error { reply = text; return nil },
})
if res.Outcome != OutcomeHandled || reply != unavailableMsg {
t.Fatalf("result = %+v, reply = %q", res, reply)
}
}
wantErr := errors.New("clear failed")
res := NewExecutor(NewRegistry(BuiltinDefinitions()), &Runtime{
StartNewConversation: func(context.Context, string) (string, error) { return "", wantErr },
}).Execute(t.Context(), Request{Text: "/new question"})
if res.Outcome != OutcomeHandled || !errors.Is(res.Err, wantErr) {
t.Fatalf("result = %+v, want clear error", res)
}
}
func TestNewCommand_Help(t *testing.T) {
if help := formatHelpMessage(BuiltinDefinitions()); !strings.Contains(help, "/new [prompt]") {
t.Fatalf("help missing /new: %s", help)
}
}