|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'spec_helper' |
4 | | - |
| 4 | +require 'action_dispatch/http/upload' |
5 | 5 | RSpec.describe RubyLLM::Chat do # rubocop:disable RSpec/MultipleMemoizedHelpers |
6 | 6 | include_context 'with configured RubyLLM' |
7 | 7 |
|
|
226 | 226 | expect(attachment.send(:url?)).to be true |
227 | 227 | end |
228 | 228 | end |
| 229 | + |
| 230 | + describe 'IO attachment handling' do # rubocop:disable RSpec/MultipleMemoizedHelpers |
| 231 | + it 'handles StringIO objects' do |
| 232 | + require 'stringio' |
| 233 | + text_content = 'Hello, this is a test file' |
| 234 | + string_io = StringIO.new(text_content) |
| 235 | + |
| 236 | + attachment = RubyLLM::Attachment.new(string_io) |
| 237 | + |
| 238 | + expect(attachment.io_like?).to be true |
| 239 | + expect(attachment.content).to eq(text_content) |
| 240 | + expect(attachment.filename).to eq('attachment') |
| 241 | + expect(attachment.mime_type).to eq('application/octet-stream') |
| 242 | + end |
| 243 | + |
| 244 | + it 'handles StringIO objects with filename' do |
| 245 | + require 'stringio' |
| 246 | + text_content = 'Hello, this is a test file' |
| 247 | + string_io = StringIO.new(text_content) |
| 248 | + |
| 249 | + attachment = RubyLLM::Attachment.new(string_io, filename: 'test.txt') |
| 250 | + |
| 251 | + expect(attachment.io_like?).to be true |
| 252 | + expect(attachment.content).to eq(text_content) |
| 253 | + expect(attachment.filename).to eq('test.txt') |
| 254 | + expect(attachment.mime_type).to eq('text/plain') |
| 255 | + end |
| 256 | + |
| 257 | + it 'handles Tempfile objects' do |
| 258 | + tempfile = Tempfile.new(['test', '.txt']) |
| 259 | + tempfile.write('Tempfile content') |
| 260 | + tempfile.rewind |
| 261 | + |
| 262 | + attachment = RubyLLM::Attachment.new(tempfile) |
| 263 | + |
| 264 | + expect(attachment.io_like?).to be true |
| 265 | + expect(attachment.content).to eq('Tempfile content') |
| 266 | + expect(attachment.filename).to be_present |
| 267 | + expect(attachment.mime_type).to eq('text/plain') |
| 268 | + end |
| 269 | + |
| 270 | + it 'handles File objects' do |
| 271 | + file = File.open(text_path, 'r') |
| 272 | + |
| 273 | + attachment = RubyLLM::Attachment.new(file) |
| 274 | + |
| 275 | + expect(attachment.io_like?).to be true |
| 276 | + expect(attachment.content).to be_present |
| 277 | + expect(attachment.filename).to eq('ruby.txt') |
| 278 | + expect(attachment.mime_type).to eq('text/plain') |
| 279 | + |
| 280 | + file.close |
| 281 | + end |
| 282 | + |
| 283 | + it 'handles ActionDispatch::Http::UploadedFile' do |
| 284 | + tempfile = Tempfile.new(['ruby', '.png']) |
| 285 | + tempfile.binmode |
| 286 | + File.open(image_path, 'rb') { |f| tempfile.write(f.read) } |
| 287 | + tempfile.rewind |
| 288 | + |
| 289 | + uploaded_file = ActionDispatch::Http::UploadedFile.new( |
| 290 | + tempfile: tempfile, |
| 291 | + filename: 'ruby.png', |
| 292 | + type: 'image/png' |
| 293 | + ) |
| 294 | + |
| 295 | + attachment = RubyLLM::Attachment.new(uploaded_file) |
| 296 | + |
| 297 | + expect(attachment.io_like?).to be true |
| 298 | + expect(attachment.content).to be_present |
| 299 | + expect(attachment.filename).to eq('ruby.png') |
| 300 | + expect(attachment.mime_type).to eq('image/png') |
| 301 | + expect(attachment.type).to eq(:image) |
| 302 | + end |
| 303 | + |
| 304 | + it 'rewinds IO objects before reading' do |
| 305 | + require 'stringio' |
| 306 | + string_io = StringIO.new('Initial content') |
| 307 | + string_io.read # Move position to end |
| 308 | + |
| 309 | + attachment = RubyLLM::Attachment.new(string_io, filename: 'test.txt') |
| 310 | + |
| 311 | + expect(attachment.content).to eq('Initial content') |
| 312 | + end |
| 313 | + |
| 314 | + it 'creates content with IO attachments' do |
| 315 | + require 'stringio' |
| 316 | + string_io = StringIO.new('Test content') |
| 317 | + content = RubyLLM::Content.new('Check this') |
| 318 | + content.add_attachment(string_io, filename: 'test.txt') |
| 319 | + |
| 320 | + expect(content.attachments).not_to be_empty |
| 321 | + expect(content.attachments.first).to be_a(RubyLLM::Attachment) |
| 322 | + expect(content.attachments.first.io_like?).to be true |
| 323 | + expect(content.attachments.first.filename).to eq('test.txt') |
| 324 | + expect(content.attachments.first.mime_type).to eq('text/plain') |
| 325 | + end |
| 326 | + end |
229 | 327 | end |
0 commit comments