返回 DeepSeek-Reasonix
toolresult_image_test.go
根目录 / internal / plugin / toolresult_image_test.go
1 package plugin
2
3 import (
4 "encoding/base64"
5 "encoding/json"
6 "strings"
7 "testing"
8 )
9
10 func imageItem(mime, data string) string {
11 b, _ := json.Marshal(map[string]string{"type": "image", "data": data, "mimeType": mime})
12 return string(b)
13 }
14
15 func TestParseToolResultImageItem(t *testing.T) {
16 payload := base64.StdEncoding.EncodeToString([]byte("png-bytes"))
17 res := `{"content":[{"type":"text","text":"before "},` + imageItem("image/png", payload) + `,{"type":"text","text":" after"}]}`
18 text, images, err := parseToolResult(json.RawMessage(res))
19 if err != nil {
20 t.Fatalf("parseToolResult: %v", err)
21 }
22 if want := "before [image: image/png] after"; text != want {
23 t.Fatalf("text = %q, want %q", text, want)
24 }
25 if len(images) != 1 || images[0] != "data:image/png;base64,"+payload {
26 t.Fatalf("images = %v, want one png data URL", images)
27 }
28 }
29
30 func TestParseToolResultImageDefaultsToPNG(t *testing.T) {
31 payload := base64.StdEncoding.EncodeToString([]byte("x"))
32 res := `{"content":[{"type":"image","data":"` + payload + `"}]}`
33 text, images, err := parseToolResult(json.RawMessage(res))
34 if err != nil {
35 t.Fatalf("parseToolResult: %v", err)
36 }
37 if text != "[image: image/png]" {
38 t.Fatalf("text = %q, want png placeholder", text)
39 }
40 if len(images) != 1 || !strings.HasPrefix(images[0], "data:image/png;base64,") {
41 t.Fatalf("images = %v, want png data URL", images)
42 }
43 }
44
45 func TestParseToolResultImageNormalizesWrappedBase64(t *testing.T) {
46 payload := base64.StdEncoding.EncodeToString([]byte("png-bytes"))
47 wrapped := payload[:4] + "\n" + payload[4:8] + " " + payload[8:]
48 res := `{"content":[` + imageItem("image/png", wrapped) + `]}`
49 _, images, err := parseToolResult(json.RawMessage(res))
50 if err != nil {
51 t.Fatalf("parseToolResult: %v", err)
52 }
53 if len(images) != 1 || images[0] != "data:image/png;base64,"+payload {
54 t.Fatalf("images = %v, want whitespace stripped to %q", images, payload)
55 }
56 }
57
58 func TestParseToolResultImageInvalidBase64Omitted(t *testing.T) {
59 res := `{"content":[` + imageItem("image/png", "not-base64!!") + `]}`
60 text, images, err := parseToolResult(json.RawMessage(res))
61 if err != nil {
62 t.Fatalf("parseToolResult: %v", err)
63 }
64 if len(images) != 0 {
65 t.Fatalf("images = %v, want none for invalid base64", images)
66 }
67 if !strings.Contains(text, "invalid base64") {
68 t.Fatalf("text = %q, want invalid-base64 placeholder", text)
69 }
70 }
71
72 func TestParseToolResultImageUnsupportedMimeOmitted(t *testing.T) {
73 payload := base64.StdEncoding.EncodeToString([]byte("bmp"))
74 res := `{"content":[` + imageItem("image/bmp", payload) + `]}`
75 text, images, err := parseToolResult(json.RawMessage(res))
76 if err != nil {
77 t.Fatalf("parseToolResult: %v", err)
78 }
79 if len(images) != 0 {
80 t.Fatalf("images = %v, want none for unsupported mime", images)
81 }
82 if !strings.Contains(text, "unsupported type image/bmp") {
83 t.Fatalf("text = %q, want unsupported-type placeholder", text)
84 }
85 }
86
87 func TestParseToolResultImageOversizedOmitted(t *testing.T) {
88 // Valid base64 alphabet, over the byte budget.
89 big := strings.Repeat("QUFB", maxToolResultImageBytes/4+1)
90 res := `{"content":[` + imageItem("image/png", big) + `]}`
91 text, images, err := parseToolResult(json.RawMessage(res))
92 if err != nil {
93 t.Fatalf("parseToolResult: %v", err)
94 }
95 if len(images) != 0 {
96 t.Fatalf("got %d images, want none for oversized payload", len(images))
97 }
98 if !strings.Contains(text, "exceeds") {
99 t.Fatalf("text = %q, want oversize placeholder", text)
100 }
101 }
102
103 func TestParseToolResultImageCountCapped(t *testing.T) {
104 payload := base64.StdEncoding.EncodeToString([]byte("x"))
105 items := make([]string, 0, maxToolResultImages+1)
106 for i := 0; i < maxToolResultImages+1; i++ {
107 items = append(items, imageItem("image/png", payload))
108 }
109 res := `{"content":[` + strings.Join(items, ",") + `]}`
110 text, images, err := parseToolResult(json.RawMessage(res))
111 if err != nil {
112 t.Fatalf("parseToolResult: %v", err)
113 }
114 if len(images) != maxToolResultImages {
115 t.Fatalf("got %d images, want cap of %d", len(images), maxToolResultImages)
116 }
117 if !strings.Contains(text, "image limit reached") {
118 t.Fatalf("text = %q, want limit placeholder for the overflow item", text)
119 }
120 }
121
122 func TestParseToolResultErrorStillReturnsImages(t *testing.T) {
123 payload := base64.StdEncoding.EncodeToString([]byte("x"))
124 res := `{"content":[{"type":"text","text":"boom "},` + imageItem("image/png", payload) + `],"isError":true}`
125 text, images, err := parseToolResult(json.RawMessage(res))
126 if err == nil {
127 t.Fatal("want error for isError result")
128 }
129 if !strings.Contains(text, "boom") || !strings.Contains(text, "[image: image/png]") {
130 t.Fatalf("text = %q, want text and placeholder preserved", text)
131 }
132 if len(images) != 1 {
133 t.Fatalf("images = %v, want the image parsed even on error", images)
134 }
135 }
136
136 lines GO