Files
Enterprise-Knowledge-Base/internal/im/image_qa_test.go
T
2026-07-16 00:17:16 +08:00

133 lines
4.6 KiB
Go

package im
import (
"bytes"
"context"
"errors"
"image"
"image/color"
"image/png"
"io"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
type staticImageDownloader struct {
body string
fileName string
}
func (d staticImageDownloader) DownloadFile(context.Context, *IncomingMessage) (io.ReadCloser, string, error) {
return io.NopCloser(strings.NewReader(d.body)), d.fileName, nil
}
func testPNG(t *testing.T, width, height int) string {
t.Helper()
img := image.NewNRGBA(image.Rect(0, 0, width, height))
img.SetNRGBA(0, 0, color.NRGBA{R: 20, G: 120, B: 240, A: 255})
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("encode png: %v", err)
}
return buf.String()
}
func TestPrepareIMImageForQAAndBuildRequest(t *testing.T) {
png := testPNG(t, 32, 32)
msg := &IncomingMessage{
Platform: PlatformDingtalk,
MessageType: MessageTypeImage,
MessageID: "picture-message-1",
FileKey: "temporary-download-code",
}
if err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: png, fileName: "question.png"}); err != nil {
t.Fatalf("prepareIMImageForQA: %v", err)
}
if len(msg.ImageURLs) != 1 || !strings.HasPrefix(msg.ImageURLs[0], "data:image/png;base64,") {
t.Fatalf("ImageURLs = %#v", msg.ImageURLs)
}
if msg.Content == "" {
t.Fatal("image-only message should receive a default visual question")
}
req := buildIMQARequest(&types.Session{ID: "session-1"}, msg.Content, "assistant-1", "user-1", nil, nil, nil, msg.ImageURLs)
if len(req.ImageURLs) != 1 || req.ImageURLs[0] != msg.ImageURLs[0] {
t.Fatalf("request ImageURLs = %#v", req.ImageURLs)
}
}
func TestPrepareIMImageForQARejectsInvalidOrOversizedPayload(t *testing.T) {
tests := []struct {
name string
body string
want string
}{
{name: "not an image", body: strings.Repeat("plain text", 80), want: "unsupported image content type"},
{name: "oversized", body: strings.Repeat("x", maxIMImageBytes+1), want: "exceeds 10 MiB limit"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := &IncomingMessage{Platform: PlatformDingtalk, MessageType: MessageTypeImage, MessageID: "image-1"}
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: tt.body, fileName: "payload.bin"})
if err == nil || !strings.Contains(err.Error(), tt.want) {
t.Fatalf("error = %v, want containing %q", err, tt.want)
}
if len(msg.ImageURLs) != 0 {
t.Fatal("rejected image must not reach the model")
}
})
}
t.Run("dimensions", func(t *testing.T) {
msg := &IncomingMessage{MessageType: MessageTypeImage}
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: testPNG(t, maxIMImageDimension+1, 1), fileName: "wide.png"})
if err == nil || !strings.Contains(err.Error(), "dimensions") {
t.Fatalf("error = %v, want dimensions rejection", err)
}
})
t.Run("animated formats rejected", func(t *testing.T) {
msg := &IncomingMessage{MessageType: MessageTypeImage}
err := prepareIMImageForQA(context.Background(), msg, staticImageDownloader{body: "GIF89a" + strings.Repeat("x", 600), fileName: "animated.gif"})
if err == nil || !strings.Contains(err.Error(), "only JPEG and PNG") {
t.Fatalf("error = %v, want JPEG/PNG rejection", err)
}
})
}
func TestIMQAErrorReplyDistinguishesImageFailures(t *testing.T) {
imageReply := imQAErrorReply([]string{"data:image/png;base64,abc"})
if !strings.Contains(imageReply, "图片识别失败") || !strings.Contains(imageReply, "视觉模型") {
t.Fatalf("image error reply = %q", imageReply)
}
if got := imQAErrorReply(nil); got != "抱歉,处理您的问题时出现了异常,请稍后再试。" {
t.Fatalf("text error reply = %q", got)
}
}
func TestImageStreamDoesNotPublishPartialContent(t *testing.T) {
if allowIncrementalIMFlush(&IncomingMessage{ImageURLs: []string{"data:image/png;base64,YWJj"}}) {
t.Fatal("image streams must buffer until successful completion")
}
if !allowIncrementalIMFlush(&IncomingMessage{Content: "hello"}) {
t.Fatal("text streams must preserve incremental updates")
}
}
func TestFailClosedIMImageAnswerDiscardsPartialOutput(t *testing.T) {
partial := "I think the image might show"
err := errors.New("multimodal request rejected")
got, overridden := failClosedIMImageAnswer(partial, err, []string{"data:image/png;base64,abc"})
if !overridden || strings.Contains(got, partial) || !strings.Contains(got, "图片识别失败") {
t.Fatalf("image override = %q overridden=%v", got, overridden)
}
got, overridden = failClosedIMImageAnswer(partial, err, nil)
if overridden || got != partial {
t.Fatalf("text answer changed = %q overridden=%v", got, overridden)
}
}