|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe RubyLLM::Providers::OpenRouter::Images do |
| 6 | + let(:images_module) { described_class } |
| 7 | + |
| 8 | + describe '.images_url' do |
| 9 | + it 'returns the chat completions endpoint' do |
| 10 | + expect(images_module.images_url).to eq('chat/completions') |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + describe '.render_image_payload' do |
| 15 | + it 'renders a chat completion payload with image modality' do |
| 16 | + payload = images_module.render_image_payload('a cute cat', model: 'test-model', size: '1024x1024') |
| 17 | + |
| 18 | + expect(payload[:model]).to eq('test-model') |
| 19 | + expect(payload[:messages]).to eq([{ role: 'user', content: 'a cute cat' }]) |
| 20 | + expect(payload[:modalities]).to eq(%w[image text]) |
| 21 | + end |
| 22 | + |
| 23 | + it 'ignores size parameter and logs debug message' do |
| 24 | + allow(RubyLLM.logger).to receive(:debug) |
| 25 | + images_module.render_image_payload('a cute cat', model: 'test-model', size: '512x512') |
| 26 | + expect(RubyLLM.logger).to have_received(:debug).with(/Ignoring size/) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe '.parse_image_response' do |
| 31 | + context 'with base64 data URL response' do |
| 32 | + let(:response_body) do |
| 33 | + { |
| 34 | + 'choices' => [{ |
| 35 | + 'message' => { |
| 36 | + 'role' => 'assistant', |
| 37 | + 'content' => 'Here is an image of a cat', |
| 38 | + 'images' => [{ |
| 39 | + 'type' => 'image_url', |
| 40 | + 'image_url' => { |
| 41 | + 'url' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAE=' |
| 42 | + } |
| 43 | + }] |
| 44 | + } |
| 45 | + }] |
| 46 | + } |
| 47 | + end |
| 48 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 49 | + |
| 50 | + it 'parses base64 image data from response' do |
| 51 | + image = images_module.parse_image_response(response, model: 'test-model') |
| 52 | + |
| 53 | + expect(image).to be_a(RubyLLM::Image) |
| 54 | + expect(image.base64?).to be(true) |
| 55 | + expect(image.data).to eq('iVBORw0KGgoAAAANSUhEUgAAAAE=') |
| 56 | + expect(image.mime_type).to eq('image/png') |
| 57 | + expect(image.model_id).to eq('test-model') |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'with jpeg image data URL' do |
| 62 | + let(:response_body) do |
| 63 | + { |
| 64 | + 'choices' => [{ |
| 65 | + 'message' => { |
| 66 | + 'images' => [{ |
| 67 | + 'image_url' => { 'url' => 'data:image/jpeg;base64,/9j/4AAQSkZJRg==' } |
| 68 | + }] |
| 69 | + } |
| 70 | + }] |
| 71 | + } |
| 72 | + end |
| 73 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 74 | + |
| 75 | + it 'parses jpeg image data' do |
| 76 | + image = images_module.parse_image_response(response, model: 'test-model') |
| 77 | + |
| 78 | + expect(image.mime_type).to eq('image/jpeg') |
| 79 | + expect(image.data).to eq('/9j/4AAQSkZJRg==') |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + context 'with direct url field (alternative format)' do |
| 84 | + let(:response_body) do |
| 85 | + { |
| 86 | + 'choices' => [{ |
| 87 | + 'message' => { |
| 88 | + 'images' => [{ 'url' => 'data:image/png;base64,abc123' }] |
| 89 | + } |
| 90 | + }] |
| 91 | + } |
| 92 | + end |
| 93 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 94 | + |
| 95 | + it 'parses image from direct url field' do |
| 96 | + image = images_module.parse_image_response(response, model: 'test-model') |
| 97 | + |
| 98 | + expect(image.data).to eq('abc123') |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context 'with regular URL response' do |
| 103 | + let(:response_body) do |
| 104 | + { |
| 105 | + 'choices' => [{ |
| 106 | + 'message' => { |
| 107 | + 'images' => [{ |
| 108 | + 'image_url' => { 'url' => 'https://example.com/image.png' } |
| 109 | + }] |
| 110 | + } |
| 111 | + }] |
| 112 | + } |
| 113 | + end |
| 114 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 115 | + |
| 116 | + it 'creates image with URL instead of base64 data' do |
| 117 | + image = images_module.parse_image_response(response, model: 'test-model') |
| 118 | + |
| 119 | + expect(image.base64?).to be(false) |
| 120 | + expect(image.url).to eq('https://example.com/image.png') |
| 121 | + expect(image.mime_type).to eq('image/png') |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + context 'with missing images in response' do |
| 126 | + let(:response_body) do |
| 127 | + { |
| 128 | + 'choices' => [{ |
| 129 | + 'message' => { 'content' => 'Sorry, I cannot generate images' } |
| 130 | + }] |
| 131 | + } |
| 132 | + end |
| 133 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 134 | + |
| 135 | + it 'raises an error' do |
| 136 | + expect do |
| 137 | + images_module.parse_image_response(response, model: 'test-model') |
| 138 | + end.to raise_error(RubyLLM::Error, /Unexpected response format/) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'with empty images array' do |
| 143 | + let(:response_body) do |
| 144 | + { |
| 145 | + 'choices' => [{ |
| 146 | + 'message' => { 'images' => [] } |
| 147 | + }] |
| 148 | + } |
| 149 | + end |
| 150 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 151 | + |
| 152 | + it 'raises an error' do |
| 153 | + expect do |
| 154 | + images_module.parse_image_response(response, model: 'test-model') |
| 155 | + end.to raise_error(RubyLLM::Error, /Unexpected response format/) |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + context 'with invalid data URL format' do |
| 160 | + let(:response_body) do |
| 161 | + { |
| 162 | + 'choices' => [{ |
| 163 | + 'message' => { |
| 164 | + 'images' => [{ |
| 165 | + 'image_url' => { 'url' => 'data:invalid-format' } |
| 166 | + }] |
| 167 | + } |
| 168 | + }] |
| 169 | + } |
| 170 | + end |
| 171 | + let(:response) { instance_double(Faraday::Response, body: response_body) } |
| 172 | + |
| 173 | + it 'raises an error for invalid data URL' do |
| 174 | + expect do |
| 175 | + images_module.parse_image_response(response, model: 'test-model') |
| 176 | + end.to raise_error(RubyLLM::Error, /Invalid data URL format/) |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | +end |
0 commit comments