-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_caption_test.go
More file actions
226 lines (177 loc) · 6.6 KB
/
image_caption_test.go
File metadata and controls
226 lines (177 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package goreason
import (
"context"
"fmt"
"strings"
"testing"
"github.com/bbiangul/go-reason/llm"
"github.com/bbiangul/go-reason/parser"
)
// mockVisionProvider implements both llm.Provider and llm.VisionProvider for testing.
type mockVisionProvider struct {
captionResponse string
captionErr error
callCount int
}
func (m *mockVisionProvider) Chat(_ context.Context, _ llm.ChatRequest) (*llm.ChatResponse, error) {
return &llm.ChatResponse{Content: "mock"}, nil
}
func (m *mockVisionProvider) Embed(_ context.Context, _ []string) ([][]float32, error) {
return nil, nil
}
func (m *mockVisionProvider) ChatWithImages(_ context.Context, _ llm.VisionChatRequest) (*llm.ChatResponse, error) {
m.callCount++
if m.captionErr != nil {
return nil, m.captionErr
}
return &llm.ChatResponse{Content: m.captionResponse}, nil
}
func TestCaptionImages_CaptioningEnabled(t *testing.T) {
mock := &mockVisionProvider{captionResponse: "A wiring diagram showing power connections"}
e := &engine{
cfg: Config{CaptionImages: true},
visionLLM: mock,
}
sections := []parser.Section{
{Heading: "Section 1", Content: "Some text about wiring."},
{Heading: "Section 2", Content: "More text."},
}
images := []parser.ExtractedImage{
{Data: []byte("fake-img"), MIMEType: "image/png", PageNumber: 1, SectionIndex: 0, Width: 800, Height: 600},
}
result, _ := e.captionImages(context.Background(), sections, images)
if mock.callCount != 1 {
t.Errorf("expected 1 vision call, got %d", mock.callCount)
}
if !strings.Contains(result[0].Content, "[Image: A wiring diagram showing power connections]") {
t.Errorf("expected caption in section content, got: %s", result[0].Content)
}
}
func TestCaptionImages_CaptioningDisabled(t *testing.T) {
mock := &mockVisionProvider{captionResponse: "should not be called"}
e := &engine{
cfg: Config{CaptionImages: false}, // disabled
visionLLM: mock,
}
sections := []parser.Section{
{Heading: "Section 1", Content: "Text."},
}
images := []parser.ExtractedImage{
{Data: []byte("fake"), MIMEType: "image/png", PageNumber: 1, SectionIndex: 0, Width: 100, Height: 100},
}
result, _ := e.captionImages(context.Background(), sections, images)
if mock.callCount != 0 {
t.Errorf("expected 0 vision calls when captioning disabled, got %d", mock.callCount)
}
if !strings.Contains(result[0].Content, "[image]") {
t.Errorf("expected [image] marker when captioning disabled, got: %s", result[0].Content)
}
}
func TestCaptionImages_LargestImagePerPage(t *testing.T) {
mock := &mockVisionProvider{captionResponse: "The large chart"}
e := &engine{
cfg: Config{CaptionImages: true},
visionLLM: mock,
}
sections := []parser.Section{
{Heading: "Page 1", Content: "Content."},
}
// Two images on the same page — only the larger should be captioned
images := []parser.ExtractedImage{
{Data: []byte("small"), MIMEType: "image/png", PageNumber: 1, SectionIndex: 0, Width: 100, Height: 100},
{Data: []byte("large"), MIMEType: "image/jpeg", PageNumber: 1, SectionIndex: 0, Width: 800, Height: 600},
}
result, _ := e.captionImages(context.Background(), sections, images)
// Should only make 1 call (one per page, not per image)
if mock.callCount != 1 {
t.Errorf("expected 1 vision call (one per page), got %d", mock.callCount)
}
content := result[0].Content
if !strings.Contains(content, "[Image: The large chart]") {
t.Errorf("expected captioned largest image, got: %s", content)
}
// The other image should get [image]
if strings.Count(content, "[image]") != 1 {
t.Errorf("expected 1 [image] marker for the non-captioned image, got content: %s", content)
}
}
func TestCaptionImages_FailureFallback(t *testing.T) {
mock := &mockVisionProvider{captionErr: fmt.Errorf("API error")}
e := &engine{
cfg: Config{CaptionImages: true},
visionLLM: mock,
}
sections := []parser.Section{
{Heading: "Section 1", Content: "Text."},
}
images := []parser.ExtractedImage{
{Data: []byte("fake"), MIMEType: "image/png", PageNumber: 1, SectionIndex: 0, Width: 200, Height: 200},
}
result, _ := e.captionImages(context.Background(), sections, images)
// Should fall back to [image] on error
if !strings.Contains(result[0].Content, "[image]") {
t.Errorf("expected [image] fallback on error, got: %s", result[0].Content)
}
if strings.Contains(result[0].Content, "[Image:") {
t.Errorf("should not contain captioned image on error, got: %s", result[0].Content)
}
}
func TestCaptionImages_NilVisionLLM(t *testing.T) {
e := &engine{
cfg: Config{CaptionImages: true},
visionLLM: nil, // no vision LLM configured
}
sections := []parser.Section{
{Heading: "Section 1", Content: "Text."},
}
images := []parser.ExtractedImage{
{Data: []byte("fake"), MIMEType: "image/png", PageNumber: 1, SectionIndex: 0, Width: 200, Height: 200},
}
result, _ := e.captionImages(context.Background(), sections, images)
if !strings.Contains(result[0].Content, "[image]") {
t.Errorf("expected [image] when no vision LLM, got: %s", result[0].Content)
}
}
func TestCaptionImages_MultiplePages(t *testing.T) {
mock := &mockVisionProvider{captionResponse: "Chart description"}
e := &engine{
cfg: Config{CaptionImages: true},
visionLLM: mock,
}
sections := []parser.Section{
{Heading: "Page 1", Content: "Text1.", PageNumber: 1},
{Heading: "Page 2", Content: "Text2.", PageNumber: 2},
}
// One image per page
images := []parser.ExtractedImage{
{Data: []byte("img1"), MIMEType: "image/png", PageNumber: 1, SectionIndex: 0, Width: 400, Height: 300},
{Data: []byte("img2"), MIMEType: "image/jpeg", PageNumber: 2, SectionIndex: 1, Width: 500, Height: 400},
}
result, _ := e.captionImages(context.Background(), sections, images)
// Should make 2 calls (one per page)
if mock.callCount != 2 {
t.Errorf("expected 2 vision calls (one per page), got %d", mock.callCount)
}
if !strings.Contains(result[0].Content, "[Image: Chart description]") {
t.Errorf("page 1 missing caption, got: %s", result[0].Content)
}
if !strings.Contains(result[1].Content, "[Image: Chart description]") {
t.Errorf("page 2 missing caption, got: %s", result[1].Content)
}
}
func TestCaptionImages_EmptyImages(t *testing.T) {
e := &engine{
cfg: Config{CaptionImages: true},
}
sections := []parser.Section{
{Heading: "Section 1", Content: "Text."},
}
result, collected := e.captionImages(context.Background(), sections, nil)
// No images — sections should be unchanged
if collected != nil {
t.Errorf("expected nil collected images, got %d", len(collected))
}
if result[0].Content != "Text." {
t.Errorf("sections should be unchanged with no images, got: %s", result[0].Content)
}
}