feat: add DingTalk Stream text ingress
This commit is contained in:
@@ -0,0 +1,378 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
type fakeQA struct {
|
||||
called bool
|
||||
lastReq core.QARequest
|
||||
result core.QAResult
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeQA) Ask(_ context.Context, req core.QARequest) (core.QAResult, error) {
|
||||
f.called = true
|
||||
f.lastReq = req
|
||||
return f.result, f.err
|
||||
}
|
||||
|
||||
type fakeReply struct {
|
||||
called bool
|
||||
lastURL string
|
||||
lastTitle string
|
||||
lastText string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeReply) SendMarkdown(_ context.Context, url, title, text string) error {
|
||||
f.called = true
|
||||
f.lastURL = url
|
||||
f.lastTitle = title
|
||||
f.lastText = text
|
||||
return f.err
|
||||
}
|
||||
|
||||
func newTestHandler(t *testing.T, qa *fakeQA, reply *fakeReply) *Handler {
|
||||
t.Helper()
|
||||
cfg := TenantConfig{
|
||||
TenantKey: "tenant-a",
|
||||
Channel: "dingtalk",
|
||||
}
|
||||
h, err := NewHandler(cfg, qa, reply)
|
||||
if err != nil {
|
||||
t.Fatalf("NewHandler: %v", err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func directTextCallback() BotCallback {
|
||||
return BotCallback{
|
||||
ConversationType: "1",
|
||||
Msgtype: "text",
|
||||
MsgID: "msg-1",
|
||||
SenderNick: "Alice",
|
||||
SenderStaffID: "staff-123",
|
||||
SenderID: "user-abc",
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/send?access_token=x",
|
||||
Text: " hello world ",
|
||||
}
|
||||
}
|
||||
|
||||
func groupTextCallback() BotCallback {
|
||||
return BotCallback{
|
||||
ConversationType: "2",
|
||||
ConversationID: "cid-group-1",
|
||||
Msgtype: "text",
|
||||
MsgID: "msg-2",
|
||||
SenderNick: "Bob",
|
||||
SenderStaffID: "staff-456",
|
||||
SenderID: "user-def",
|
||||
IsInAtList: true,
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/send?access_token=x",
|
||||
Text: " group question ",
|
||||
}
|
||||
}
|
||||
|
||||
// --- A: direct text ---
|
||||
|
||||
func TestHandleDirectTextCallsQAAndRepliesAnswer(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "**answer**"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), directTextCallback())
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if !qa.called {
|
||||
t.Fatal("QA.Ask was not called")
|
||||
}
|
||||
req := qa.lastReq
|
||||
if req.TenantKey != "tenant-a" || req.Channel != "dingtalk" {
|
||||
t.Fatalf("unexpected TenantKey/Channel: %+v", req)
|
||||
}
|
||||
if req.ConversationType != core.ConversationDirect {
|
||||
t.Fatalf("ConversationType = %q, want direct", req.ConversationType)
|
||||
}
|
||||
if req.ExternalUserID != "staff-123" {
|
||||
t.Fatalf("ExternalUserID = %q, want staff-123", req.ExternalUserID)
|
||||
}
|
||||
if req.ExternalMessageID != "msg-1" {
|
||||
t.Fatalf("ExternalMessageID = %q, want msg-1", req.ExternalMessageID)
|
||||
}
|
||||
if req.SenderName != "Alice" {
|
||||
t.Fatalf("SenderName = %q, want Alice", req.SenderName)
|
||||
}
|
||||
if req.Query != "hello world" {
|
||||
t.Fatalf("Query = %q, want trimmed 'hello world'", req.Query)
|
||||
}
|
||||
if req.ExternalChatID != "" {
|
||||
t.Fatalf("ExternalChatID should be empty for direct, got %q", req.ExternalChatID)
|
||||
}
|
||||
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown was not called")
|
||||
}
|
||||
if reply.lastText != "**answer**" {
|
||||
t.Fatalf("reply text = %q, want answer", reply.lastText)
|
||||
}
|
||||
if reply.lastURL == "" {
|
||||
t.Fatal("reply URL should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectTextFallsBackToSenderID(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.SenderStaffID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa.lastReq.ExternalUserID != "user-abc" {
|
||||
t.Fatalf("ExternalUserID = %q, want fallback to senderId user-abc", qa.lastReq.ExternalUserID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleDirectTextTrimsQuery(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Text = " padded "
|
||||
_ = h.Handle(context.Background(), data)
|
||||
if qa.lastReq.Query != "padded" {
|
||||
t.Fatalf("Query = %q, want trimmed 'padded'", qa.lastReq.Query)
|
||||
}
|
||||
}
|
||||
|
||||
// --- B: group text, @bot ---
|
||||
|
||||
func TestHandleGroupTextAtBotCallsQAAndReplies(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "group answer"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), groupTextCallback())
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if !qa.called {
|
||||
t.Fatal("QA.Ask was not called")
|
||||
}
|
||||
req := qa.lastReq
|
||||
if req.ConversationType != core.ConversationGroup {
|
||||
t.Fatalf("ConversationType = %q, want group", req.ConversationType)
|
||||
}
|
||||
if req.ExternalChatID != "cid-group-1" {
|
||||
t.Fatalf("ExternalChatID = %q, want cid-group-1", req.ExternalChatID)
|
||||
}
|
||||
if req.Query != "group question" {
|
||||
t.Fatalf("Query = %q, want trimmed 'group question'", req.Query)
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown was not called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGroupTextNotAtBotSilentlyIgnored(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := groupTextCallback()
|
||||
data.IsInAtList = false
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: expected nil error for non-@bot group message, got %v", err)
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for non-@bot group message")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for non-@bot group message")
|
||||
}
|
||||
}
|
||||
|
||||
// --- C: non-text message ---
|
||||
|
||||
func TestHandleNonTextRepliesUnsupported(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for non-text message")
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should be called for non-text message")
|
||||
}
|
||||
if !strings.Contains(reply.lastText, "暂仅支持文字消息") {
|
||||
t.Fatalf("reply text = %q, want unsupported-message notice", reply.lastText)
|
||||
}
|
||||
}
|
||||
|
||||
// --- D: QA error ---
|
||||
|
||||
func TestHandleQAErrorRepliesFriendlyMessage(t *testing.T) {
|
||||
qa := &fakeQA{err: errors.New("weknora connection timeout: internal stack trace")}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), directTextCallback())
|
||||
if err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if !reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should be called on QA error")
|
||||
}
|
||||
if !strings.Contains(reply.lastText, "暂时无法完成回答") {
|
||||
t.Fatalf("reply text = %q, want friendly error message", reply.lastText)
|
||||
}
|
||||
if strings.Contains(reply.lastText, "weknora") || strings.Contains(reply.lastText, "timeout") || strings.Contains(reply.lastText, "stack") {
|
||||
t.Fatalf("reply text leaks internal error: %q", reply.lastText)
|
||||
}
|
||||
}
|
||||
|
||||
// --- E: invalid input ---
|
||||
|
||||
func TestHandleEmptyTextReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Text = " "
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty text")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for empty text")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for empty text")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMissingMsgIDReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.MsgID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing MsgId")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for missing MsgId")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for missing MsgId")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleMissingUserReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.SenderStaffID = ""
|
||||
data.SenderID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing user")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for missing user")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for missing user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGroupMissingConversationIDReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := groupTextCallback()
|
||||
data.ConversationID = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for group missing ConversationId")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called for group missing ConversationId")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called for group missing ConversationId")
|
||||
}
|
||||
}
|
||||
|
||||
// --- F: missing sessionWebhook ---
|
||||
|
||||
func TestHandleMissingSessionWebhookReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.SessionWebhook = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing sessionWebhook")
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA.Ask should not be called when sessionWebhook is missing (model call would be wasted)")
|
||||
}
|
||||
if reply.called {
|
||||
t.Fatal("Reply.SendMarkdown should not be called when sessionWebhook is missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleNonTextMissingSessionWebhookReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
data.SessionWebhook = ""
|
||||
err := h.Handle(context.Background(), data)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-text with missing sessionWebhook")
|
||||
}
|
||||
}
|
||||
|
||||
// --- reply send failure ---
|
||||
|
||||
func TestHandleReplySendFailureReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{err: errors.New("webhook timeout")}
|
||||
h := newTestHandler(t, qa, reply)
|
||||
|
||||
err := h.Handle(context.Background(), directTextCallback())
|
||||
if err == nil {
|
||||
t.Fatal("expected error when reply send fails")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user