490 lines
14 KiB
Go
490 lines
14 KiB
Go
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
|
|
}
|
|
|
|
type fakeImageDownloader struct {
|
|
called bool
|
|
code string
|
|
image core.ImageInput
|
|
err error
|
|
}
|
|
|
|
func (f *fakeImageDownloader) Download(_ context.Context, code string) (core.ImageInput, error) {
|
|
f.called = true
|
|
f.code = code
|
|
return f.image, f.err
|
|
}
|
|
|
|
func newTestHandler(t *testing.T, qa *fakeQA, reply *fakeReply) *Handler {
|
|
return newTestHandlerWithDownloader(t, qa, reply, nil)
|
|
}
|
|
|
|
func newTestHandlerWithDownloader(t *testing.T, qa *fakeQA, reply *fakeReply, downloader ImageDownloader) *Handler {
|
|
t.Helper()
|
|
cfg := TenantConfig{
|
|
TenantKey: "tenant-a",
|
|
Channel: "dingtalk",
|
|
}
|
|
h, err := NewHandler(cfg, qa, reply, downloader)
|
|
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 ",
|
|
}
|
|
}
|
|
|
|
func directPictureCallback() BotCallback {
|
|
data := directTextCallback()
|
|
data.Msgtype = "picture"
|
|
data.Text = ""
|
|
data.DownloadCode = "temporary-download-code"
|
|
return data
|
|
}
|
|
|
|
// --- 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 = "audio"
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestHandlePictureDownloadsAndCallsQA(t *testing.T) {
|
|
qa := &fakeQA{result: core.QAResult{Answer: "图片回答"}}
|
|
reply := &fakeReply{}
|
|
image := core.ImageInput{DataURI: "data:image/png;base64,AAAA", MIMEType: "image/png", SizeBytes: 4, Width: 1, Height: 1}
|
|
downloader := &fakeImageDownloader{image: image}
|
|
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
|
|
|
|
if err := h.Handle(context.Background(), directPictureCallback()); err != nil {
|
|
t.Fatalf("Handle picture: %v", err)
|
|
}
|
|
if !downloader.called || downloader.code != "temporary-download-code" {
|
|
t.Fatalf("download called=%v code=%q", downloader.called, downloader.code)
|
|
}
|
|
if !qa.called || len(qa.lastReq.Images) != 1 || qa.lastReq.Images[0] != image {
|
|
t.Fatalf("QA request did not receive image: %+v", qa.lastReq)
|
|
}
|
|
if qa.lastReq.Query != "请描述并分析这张图片。" {
|
|
t.Fatalf("picture query=%q", qa.lastReq.Query)
|
|
}
|
|
if !reply.called || reply.lastText != "图片回答" {
|
|
t.Fatalf("reply=%+v", reply)
|
|
}
|
|
}
|
|
|
|
func TestHandlePicturePreparationFailuresAreFriendlyAndDoNotCallQA(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
code string
|
|
downloader ImageDownloader
|
|
}{
|
|
{name: "missing download code", code: "", downloader: &fakeImageDownloader{}},
|
|
{name: "missing downloader", code: "code", downloader: nil},
|
|
{name: "download error", code: "secret-code", downloader: &fakeImageDownloader{err: errors.New("signed URL and token must not leak")}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
qa := &fakeQA{}
|
|
reply := &fakeReply{}
|
|
h := newTestHandlerWithDownloader(t, qa, reply, tt.downloader)
|
|
data := directPictureCallback()
|
|
data.DownloadCode = tt.code
|
|
if err := h.Handle(context.Background(), data); err != nil {
|
|
t.Fatalf("Handle: %v", err)
|
|
}
|
|
if qa.called {
|
|
t.Fatal("QA must not be called")
|
|
}
|
|
if !reply.called || reply.lastText != "图片识别暂时不可用,请稍后重试或发送文字。" {
|
|
t.Fatalf("friendly reply=%q", reply.lastText)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandleGroupPictureWithoutMentionDoesNothing(t *testing.T) {
|
|
qa := &fakeQA{}
|
|
reply := &fakeReply{}
|
|
downloader := &fakeImageDownloader{}
|
|
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
|
|
data := directPictureCallback()
|
|
data.ConversationType = conversationTypeGroup
|
|
data.IsInAtList = false
|
|
data.SessionWebhook = "not even validated"
|
|
if err := h.Handle(context.Background(), data); err != nil {
|
|
t.Fatalf("Handle: %v", err)
|
|
}
|
|
if downloader.called || qa.called || reply.called {
|
|
t.Fatalf("unexpected side effects: downloader=%v qa=%v reply=%v", downloader.called, qa.called, reply.called)
|
|
}
|
|
}
|
|
|
|
func TestHandlePictureRejectsWebhookBeforeDownload(t *testing.T) {
|
|
qa := &fakeQA{}
|
|
reply := &fakeReply{}
|
|
downloader := &fakeImageDownloader{}
|
|
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
|
|
data := directPictureCallback()
|
|
data.SessionWebhook = "http://127.0.0.1/steal"
|
|
if err := h.Handle(context.Background(), data); err == nil {
|
|
t.Fatal("expected invalid webhook error")
|
|
}
|
|
if downloader.called || qa.called || reply.called {
|
|
t.Fatalf("invalid webhook caused side effects: downloader=%v qa=%v reply=%v", downloader.called, qa.called, reply.called)
|
|
}
|
|
}
|
|
|
|
// --- 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")
|
|
}
|
|
}
|