feat: support DingTalk rich text images
This commit is contained in:
@@ -477,6 +477,122 @@ func TestHandleNonTextMissingSessionWebhookReturnsError(t *testing.T) {
|
||||
|
||||
// --- reply send failure ---
|
||||
|
||||
// --- F: richText mixed (text + picture) ---
|
||||
|
||||
func TestHandleRichTextMixedDownloadsAndCallsQAWithRealQuery(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)
|
||||
|
||||
// Simulate richText mixed: text + picture
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
data.DownloadCode = "temporary-download-code"
|
||||
data.Text = " 请分析这张截图中的错误 "
|
||||
|
||||
if err := h.Handle(context.Background(), data); err != nil {
|
||||
t.Fatalf("Handle richText mixed: %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)
|
||||
}
|
||||
// Key assert: mixed uses real query, not default imageQuery
|
||||
if qa.lastReq.Query != "请分析这张截图中的错误" {
|
||||
t.Fatalf("richText mixed query=%q, want real query", qa.lastReq.Query)
|
||||
}
|
||||
if !reply.called || reply.lastText != "图文回答" {
|
||||
t.Fatalf("reply=%+v", reply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleRichTextMixedEmptyQueryFallsBackToDefault(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)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
data.DownloadCode = "code"
|
||||
data.Text = " "
|
||||
|
||||
if err := h.Handle(context.Background(), data); err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
// When Text is empty after trim, falls back to default imageQuery
|
||||
if qa.lastReq.Query != imageQuery {
|
||||
t.Fatalf("query=%q, want default imageQuery=%q", qa.lastReq.Query, imageQuery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleIndependentPictureStillUsesDefaultQuery(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)
|
||||
|
||||
// Independent picture: Text is empty (as A1 produces)
|
||||
data := directPictureCallback()
|
||||
if err := h.Handle(context.Background(), data); err != nil {
|
||||
t.Fatalf("Handle picture: %v", err)
|
||||
}
|
||||
// Independent picture should still use default query
|
||||
if qa.lastReq.Query != imageQuery {
|
||||
t.Fatalf("picture query=%q, want default imageQuery=%q", qa.lastReq.Query, imageQuery)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGroupRichTextMixedWithoutMentionDoesNothing(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
downloader := &fakeImageDownloader{}
|
||||
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
data.DownloadCode = "code"
|
||||
data.Text = "忽略这条"
|
||||
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 TestHandleRichTextMixedDownloadFailureFriendlyReplyNoQA(t *testing.T) {
|
||||
qa := &fakeQA{}
|
||||
reply := &fakeReply{}
|
||||
downloader := &fakeImageDownloader{err: errors.New("download failed")}
|
||||
h := newTestHandlerWithDownloader(t, qa, reply, downloader)
|
||||
|
||||
data := directTextCallback()
|
||||
data.Msgtype = "picture"
|
||||
data.DownloadCode = "broken-code"
|
||||
data.Text = "帮我看看这张图"
|
||||
|
||||
if err := h.Handle(context.Background(), data); err != nil {
|
||||
t.Fatalf("Handle: %v", err)
|
||||
}
|
||||
if qa.called {
|
||||
t.Fatal("QA must not be called on download failure")
|
||||
}
|
||||
if !reply.called || reply.lastText != imageErrorReply {
|
||||
t.Fatalf("friendly reply=%q, want %q", reply.lastText, imageErrorReply)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleReplySendFailureReturnsError(t *testing.T) {
|
||||
qa := &fakeQA{result: core.QAResult{Answer: "ok"}}
|
||||
reply := &fakeReply{err: errors.New("webhook timeout")}
|
||||
|
||||
Reference in New Issue
Block a user