Files
Enterprise-Knowledge-Base/internal/im/service_quote_test.go
T

92 lines
2.5 KiB
Go

package im
import (
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
func TestFormatQuotedContext(t *testing.T) {
tests := []struct {
name string
quote *QuotedMessage
want string
}{
{
name: "nil quote",
quote: nil,
want: "",
},
{
name: "empty content",
quote: &QuotedMessage{Content: ""},
want: "",
},
{
name: "bot message",
quote: &QuotedMessage{Content: "bot reply text", IsBotMessage: true},
want: "[引用的机器人回复]\nbot reply text",
},
{
name: "user message",
quote: &QuotedMessage{Content: "user message text", IsBotMessage: false},
want: "[被引用的消息]\nuser message text",
},
{
name: "truncation at 500 runes",
quote: &QuotedMessage{
Content: string(make([]rune, 600)),
IsBotMessage: false,
},
want: "[被引用的消息]\n" + string(make([]rune, 500)) + "...",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := formatQuotedContext(tt.quote)
if got != tt.want {
t.Errorf("formatQuotedContext() length = %d, want length %d", len(got), len(tt.want))
if len(got) < 200 && len(tt.want) < 200 {
t.Errorf("got = %q, want %q", got, tt.want)
}
}
})
}
}
func TestBuildIMQARequest_QuotedContext(t *testing.T) {
session := &types.Session{ID: "s1"}
t.Run("nil quote produces empty QuotedContext", func(t *testing.T) {
req := buildIMQARequest(session, "hello", "a1", "u1", nil, nil, nil)
if req.QuotedContext != "" {
t.Errorf("QuotedContext = %q, want empty", req.QuotedContext)
}
if req.Query != "hello" {
t.Errorf("Query = %q, want %q", req.Query, "hello")
}
})
t.Run("bot quote sets QuotedContext with bot label", func(t *testing.T) {
quote := &QuotedMessage{Content: "bot reply", IsBotMessage: true}
req := buildIMQARequest(session, "follow up", "a1", "u1", nil, nil, quote)
if req.Query != "follow up" {
t.Errorf("Query = %q, want %q", req.Query, "follow up")
}
want := "[引用的机器人回复]\nbot reply"
if req.QuotedContext != want {
t.Errorf("QuotedContext = %q, want %q", req.QuotedContext, want)
}
})
t.Run("user quote sets QuotedContext with user label", func(t *testing.T) {
quote := &QuotedMessage{Content: "user msg", IsBotMessage: false}
req := buildIMQARequest(session, "question", "a1", "u1", nil, nil, quote)
want := "[被引用的消息]\nuser msg"
if req.QuotedContext != want {
t.Errorf("QuotedContext = %q, want %q", req.QuotedContext, want)
}
})
}