Files
Enterprise-Knowledge-Base/internal/im/proactive_reply_test.go

94 lines
3.2 KiB
Go

package im
import (
"context"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
)
type recordingReplyAdapter struct {
incoming *IncomingMessage
reply *ReplyMessage
}
func (a *recordingReplyAdapter) Platform() Platform { return PlatformDingtalk }
func (a *recordingReplyAdapter) VerifyCallback(*gin.Context) error { return nil }
func (a *recordingReplyAdapter) ParseCallback(*gin.Context) (*IncomingMessage, error) {
return nil, nil
}
func (a *recordingReplyAdapter) SendReply(_ context.Context, incoming *IncomingMessage, reply *ReplyMessage) error {
a.incoming = incoming
a.reply = reply
return nil
}
func (a *recordingReplyAdapter) HandleURLVerification(*gin.Context) bool { return false }
func TestSendReplyToSource(t *testing.T) {
adapter := &recordingReplyAdapter{}
service := &Service{channels: map[string]*channelState{
"channel-1": {
Adapter: adapter,
Channel: &IMChannel{ID: "channel-1", Platform: "dingtalk", Enabled: true},
},
}}
err := service.SendReplyToSource(context.Background(), ReplyTarget{
ChannelID: "channel-1",
UserID: "staff-1",
ChatID: "conversation-1",
ChatType: ChatTypeGroup,
}, "技术支持回复内容")
if err != nil {
t.Fatalf("SendReplyToSource: %v", err)
}
if adapter.incoming == nil || adapter.incoming.UserID != "staff-1" || adapter.incoming.ChatID != "conversation-1" || adapter.incoming.ChatType != ChatTypeGroup {
t.Fatalf("incoming = %#v", adapter.incoming)
}
if adapter.reply == nil || adapter.reply.Content != "技术支持回复内容" || !adapter.reply.IsFinal {
t.Fatalf("reply = %#v", adapter.reply)
}
}
func TestSendReplyToSourceRejectsUnavailableChannel(t *testing.T) {
service := &Service{channels: map[string]*channelState{}}
if err := service.SendReplyToSource(context.Background(), ReplyTarget{ChannelID: "missing"}, "reply"); err == nil {
t.Fatal("expected missing channel error")
}
}
func TestSendSupportNotificationUsesConfiguredGroup(t *testing.T) {
adapter := &recordingReplyAdapter{}
service := &Service{channels: map[string]*channelState{
"channel-1": {
Adapter: adapter,
Channel: &IMChannel{
ID: "channel-1",
Platform: "dingtalk",
Enabled: true,
Credentials: types.JSON(`{"support_group_open_conversation_id":"support-group-1"}`),
},
},
}}
if err := service.SendSupportNotification(context.Background(), "channel-1", "新工单:客户未解决"); err != nil {
t.Fatalf("SendSupportNotification: %v", err)
}
if adapter.incoming == nil || adapter.incoming.ChatType != ChatTypeGroup || adapter.incoming.ChatID != "support-group-1" {
t.Fatalf("support target = %#v", adapter.incoming)
}
if adapter.reply == nil || adapter.reply.Content != "新工单:客户未解决" {
t.Fatalf("support reply = %#v", adapter.reply)
}
}
func TestSendSupportNotificationRequiresConfiguredGroup(t *testing.T) {
service := &Service{channels: map[string]*channelState{
"channel-1": {Adapter: &recordingReplyAdapter{}, Channel: &IMChannel{ID: "channel-1", Platform: "dingtalk", Enabled: true}},
}}
if err := service.SendSupportNotification(context.Background(), "channel-1", "ticket"); err == nil {
t.Fatal("expected missing support group configuration error")
}
}