Files
Enterprise-Knowledge-Base/internal/im/dingtalk/longconn_test.go
T
2026-07-16 00:49:26 +08:00

93 lines
3.2 KiB
Go

package dingtalk
import (
"testing"
"github.com/Tencent/WeKnora/internal/im"
)
func TestParseDingTalkStreamPayloadPicture(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("picture", map[string]interface{}{
"downloadCode": "stream-download-code",
}, "ignored text")
if messageType != im.MessageTypeImage {
t.Fatalf("MessageType = %q, want image", messageType)
}
if content != "" {
t.Fatalf("content = %q, want empty", content)
}
if fileKey != "stream-download-code" {
t.Fatalf("fileKey = %q", fileKey)
}
}
func TestParseDingTalkStreamPayloadText(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("text", nil, " hello ")
if messageType != im.MessageTypeText || content != "hello" || fileKey != "" {
t.Fatalf("got type=%q content=%q fileKey=%q", messageType, content, fileKey)
}
}
func TestParseDingTalkStreamPayloadPictureJSONString(t *testing.T) {
messageType, _, fileKey := parseDingTalkStreamPayload("picture", `{"downloadCode":"string-code"}`, "")
if messageType != im.MessageTypeImage || fileKey != "string-code" {
t.Fatalf("got type=%q fileKey=%q", messageType, fileKey)
}
}
func TestParseDingTalkStreamPayloadRichTextWithPictureAndText(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("richText", map[string]interface{}{
"richText": []interface{}{
map[string]interface{}{"text": " 请回答图片中的问题 "},
map[string]interface{}{"type": "picture", "downloadCode": "rich-stream-code"},
},
}, "")
if messageType != im.MessageTypeImage {
t.Fatalf("MessageType = %q, want image", messageType)
}
if content != "请回答图片中的问题" {
t.Fatalf("content = %q", content)
}
if fileKey != "rich-stream-code" {
t.Fatalf("fileKey = %q", fileKey)
}
}
func TestParseDingTalkStreamPayloadRichTextJoinsTextParts(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("richText", map[string]interface{}{
"richText": []interface{}{
map[string]interface{}{"text": "第一段"},
map[string]interface{}{"type": "picture", "downloadCode": "rich-code"},
map[string]interface{}{"text": "第二段"},
},
}, "")
if messageType != im.MessageTypeImage || content != "第一段\n第二段" || fileKey != "rich-code" {
t.Fatalf("got type=%q content=%q fileKey=%q", messageType, content, fileKey)
}
}
func TestParseDingTalkStreamPayloadRichTextWithoutPictureStaysText(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("richText", map[string]interface{}{
"richText": []interface{}{
map[string]interface{}{"text": " 只有文字 "},
},
}, "")
if messageType != im.MessageTypeText || content != "只有文字" || fileKey != "" {
t.Fatalf("got type=%q content=%q fileKey=%q", messageType, content, fileKey)
}
}
func TestParseDingTalkStreamPayloadRichTextPictureWithoutDownloadCodeStaysImage(t *testing.T) {
messageType, content, fileKey := parseDingTalkStreamPayload("richText", map[string]interface{}{
"richText": []interface{}{
map[string]interface{}{"text": "识别图片"},
map[string]interface{}{"type": "picture"},
},
}, "")
if messageType != im.MessageTypeImage || content != "识别图片" || fileKey != "" {
t.Fatalf("got type=%q content=%q fileKey=%q", messageType, content, fileKey)
}
}