feat: add DingTalk picture agent QA

This commit is contained in:
姚章浩
2026-07-20 17:49:03 +08:00
parent ea262551c0
commit ea54b229ee
17 changed files with 1345 additions and 71 deletions
+113 -2
View File
@@ -38,13 +38,30 @@ func (f *fakeReply) SendMarkdown(_ context.Context, url, title, text string) err
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)
h, err := NewHandler(cfg, qa, reply, downloader)
if err != nil {
t.Fatalf("NewHandler: %v", err)
}
@@ -79,6 +96,14 @@ func groupTextCallback() BotCallback {
}
}
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) {
@@ -212,7 +237,7 @@ func TestHandleNonTextRepliesUnsupported(t *testing.T) {
h := newTestHandler(t, qa, reply)
data := directTextCallback()
data.Msgtype = "picture"
data.Msgtype = "audio"
err := h.Handle(context.Background(), data)
if err != nil {
t.Fatalf("Handle: %v", err)
@@ -228,6 +253,92 @@ func TestHandleNonTextRepliesUnsupported(t *testing.T) {
}
}
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) {