|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/docker/cagent/pkg/chat" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCreateUserMessageWithAttachment(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + // Create a temporary test image file |
| 18 | + tmpDir := t.TempDir() |
| 19 | + jpegPath := filepath.Join(tmpDir, "test.jpg") |
| 20 | + pngPath := filepath.Join(tmpDir, "test.png") |
| 21 | + gifPath := filepath.Join(tmpDir, "test.gif") |
| 22 | + webpPath := filepath.Join(tmpDir, "test.webp") |
| 23 | + pdfPath := filepath.Join(tmpDir, "test.pdf") |
| 24 | + unsupportedPath := filepath.Join(tmpDir, "test.xyz") |
| 25 | + |
| 26 | + // Create test files |
| 27 | + for _, path := range []string{jpegPath, pngPath, gifPath, webpPath, pdfPath, unsupportedPath} { |
| 28 | + err := os.WriteFile(path, []byte("test data"), 0o644) |
| 29 | + require.NoError(t, err) |
| 30 | + } |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + userContent string |
| 35 | + attachmentPath string |
| 36 | + wantMultiContent bool |
| 37 | + wantFileRef bool |
| 38 | + wantMimeType string |
| 39 | + wantDefaultPrompt bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "no attachment", |
| 43 | + userContent: "Hello world", |
| 44 | + attachmentPath: "", |
| 45 | + wantMultiContent: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "jpeg attachment", |
| 49 | + userContent: "Check this image", |
| 50 | + attachmentPath: jpegPath, |
| 51 | + wantMultiContent: true, |
| 52 | + wantFileRef: true, |
| 53 | + wantMimeType: "image/jpeg", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "png attachment", |
| 57 | + userContent: "Analyze this", |
| 58 | + attachmentPath: pngPath, |
| 59 | + wantMultiContent: true, |
| 60 | + wantFileRef: true, |
| 61 | + wantMimeType: "image/png", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "gif attachment", |
| 65 | + userContent: "What's in this gif?", |
| 66 | + attachmentPath: gifPath, |
| 67 | + wantMultiContent: true, |
| 68 | + wantFileRef: true, |
| 69 | + wantMimeType: "image/gif", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "webp attachment", |
| 73 | + userContent: "Describe this", |
| 74 | + attachmentPath: webpPath, |
| 75 | + wantMultiContent: true, |
| 76 | + wantFileRef: true, |
| 77 | + wantMimeType: "image/webp", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "pdf attachment", |
| 81 | + userContent: "Summarize this PDF", |
| 82 | + attachmentPath: pdfPath, |
| 83 | + wantMultiContent: true, |
| 84 | + wantFileRef: true, |
| 85 | + wantMimeType: "application/pdf", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "attachment with empty content gets default prompt", |
| 89 | + userContent: "", |
| 90 | + attachmentPath: jpegPath, |
| 91 | + wantMultiContent: true, |
| 92 | + wantFileRef: true, |
| 93 | + wantMimeType: "image/jpeg", |
| 94 | + wantDefaultPrompt: true, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "attachment with whitespace content gets default prompt", |
| 98 | + userContent: " ", |
| 99 | + attachmentPath: jpegPath, |
| 100 | + wantMultiContent: true, |
| 101 | + wantFileRef: true, |
| 102 | + wantMimeType: "image/jpeg", |
| 103 | + wantDefaultPrompt: true, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "non-existent file falls back to text only", |
| 107 | + userContent: "Hello", |
| 108 | + attachmentPath: "/non/existent/file.jpg", |
| 109 | + wantMultiContent: false, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "unsupported format falls back to text only", |
| 113 | + userContent: "Hello", |
| 114 | + attachmentPath: unsupportedPath, |
| 115 | + wantMultiContent: false, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tt := range tests { |
| 120 | + t.Run(tt.name, func(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + msg := CreateUserMessageWithAttachment(tt.userContent, tt.attachmentPath) |
| 123 | + |
| 124 | + require.NotNil(t, msg) |
| 125 | + assert.Equal(t, chat.MessageRoleUser, msg.Message.Role) |
| 126 | + |
| 127 | + if tt.wantMultiContent { |
| 128 | + assert.NotEmpty(t, msg.Message.MultiContent) |
| 129 | + assert.Len(t, msg.Message.MultiContent, 2) // text + image |
| 130 | + |
| 131 | + // Check text part |
| 132 | + textPart := msg.Message.MultiContent[0] |
| 133 | + assert.Equal(t, chat.MessagePartTypeText, textPart.Type) |
| 134 | + if tt.wantDefaultPrompt { |
| 135 | + assert.Equal(t, "Please analyze this attached file.", textPart.Text) |
| 136 | + } else { |
| 137 | + assert.Equal(t, tt.userContent, textPart.Text) |
| 138 | + } |
| 139 | + |
| 140 | + // Check image part |
| 141 | + imagePart := msg.Message.MultiContent[1] |
| 142 | + assert.Equal(t, chat.MessagePartTypeImageURL, imagePart.Type) |
| 143 | + assert.NotNil(t, imagePart.ImageURL) |
| 144 | + |
| 145 | + if tt.wantFileRef { |
| 146 | + assert.NotNil(t, imagePart.ImageURL.FileRef) |
| 147 | + assert.Equal(t, chat.FileSourceTypeLocalPath, imagePart.ImageURL.FileRef.SourceType) |
| 148 | + assert.NotEmpty(t, imagePart.ImageURL.FileRef.LocalPath) |
| 149 | + assert.Equal(t, tt.wantMimeType, imagePart.ImageURL.FileRef.MimeType) |
| 150 | + } |
| 151 | + } else { |
| 152 | + assert.Empty(t, msg.Message.MultiContent) |
| 153 | + assert.Equal(t, tt.userContent, msg.Message.Content) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestParseAttachCommand(t *testing.T) { |
| 160 | + t.Parallel() |
| 161 | + |
| 162 | + tests := []struct { |
| 163 | + name string |
| 164 | + input string |
| 165 | + wantText string |
| 166 | + wantAttachPath string |
| 167 | + }{ |
| 168 | + { |
| 169 | + name: "no attach command", |
| 170 | + input: "Hello world", |
| 171 | + wantText: "Hello world", |
| 172 | + wantAttachPath: "", |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "attach at start", |
| 176 | + input: "/attach image.png describe this", |
| 177 | + wantText: "describe this", |
| 178 | + wantAttachPath: "image.png", |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "attach in middle", |
| 182 | + input: "please /attach photo.jpg analyze it", |
| 183 | + wantText: "please analyze it", |
| 184 | + wantAttachPath: "photo.jpg", |
| 185 | + }, |
| 186 | + { |
| 187 | + name: "attach only", |
| 188 | + input: "/attach test.gif", |
| 189 | + wantText: "", |
| 190 | + wantAttachPath: "test.gif", |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "attach with path containing spaces handled", |
| 194 | + input: "/attach my_image.png what is this?", |
| 195 | + wantText: "what is this?", |
| 196 | + wantAttachPath: "my_image.png", |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "multiline with attach", |
| 200 | + input: "First line\n/attach image.jpg second part\nThird line", |
| 201 | + wantText: "First line\nsecond part\nThird line", |
| 202 | + wantAttachPath: "image.jpg", |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + for _, tt := range tests { |
| 207 | + t.Run(tt.name, func(t *testing.T) { |
| 208 | + t.Parallel() |
| 209 | + text, path := ParseAttachCommand(tt.input) |
| 210 | + assert.Equal(t, tt.wantText, text) |
| 211 | + assert.Equal(t, tt.wantAttachPath, path) |
| 212 | + }) |
| 213 | + } |
| 214 | +} |
0 commit comments