|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe Raix::MultimodalContentAdapter do |
| 6 | + # 2x2 solid-red PNG |
| 7 | + let(:red_png_base64) do |
| 8 | + "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAEUlEQVR4nGP8z8Dwn4EIwDiqEAAQOAQBjEZ1pgAAAABJRU5ErkJggg==" |
| 9 | + end |
| 10 | + let(:data_uri) { "data:image/png;base64,#{red_png_base64}" } |
| 11 | + |
| 12 | + describe ".translate" do |
| 13 | + it "turns a data-URI image_url part into a RubyLLM::Content with a decoded image attachment" do |
| 14 | + content = [{ type: "image_url", image_url: { url: data_uri } }] |
| 15 | + |
| 16 | + result = described_class.translate(content) |
| 17 | + |
| 18 | + expect(result).to be_a(RubyLLM::Content) |
| 19 | + expect(result.attachments.size).to eq(1) |
| 20 | + attachment = result.attachments.first |
| 21 | + expect(attachment).to be_image |
| 22 | + expect(attachment.mime_type).to eq("image/png") |
| 23 | + end |
| 24 | + |
| 25 | + it "turns an http image_url part into a RubyLLM::Content with a URL attachment" do |
| 26 | + content = [{ type: "image_url", image_url: { url: "https://example.com/red.png" } }] |
| 27 | + |
| 28 | + result = described_class.translate(content) |
| 29 | + |
| 30 | + expect(result).to be_a(RubyLLM::Content) |
| 31 | + attachment = result.attachments.first |
| 32 | + expect(attachment).to be_url |
| 33 | + expect(attachment.source.to_s).to eq("https://example.com/red.png") |
| 34 | + end |
| 35 | + |
| 36 | + it "keeps the text part alongside the image attachment" do |
| 37 | + content = [ |
| 38 | + { type: "text", text: "What color is this?" }, |
| 39 | + { type: "image_url", image_url: { url: data_uri } } |
| 40 | + ] |
| 41 | + |
| 42 | + result = described_class.translate(content) |
| 43 | + |
| 44 | + expect(result.text).to eq("What color is this?") |
| 45 | + expect(result.attachments.size).to eq(1) |
| 46 | + end |
| 47 | + |
| 48 | + it "accepts string-keyed parts (OpenAI JSON shape)" do |
| 49 | + content = [{ "type" => "image_url", "image_url" => { "url" => data_uri } }] |
| 50 | + |
| 51 | + result = described_class.translate(content) |
| 52 | + |
| 53 | + expect(result.attachments.size).to eq(1) |
| 54 | + end |
| 55 | + |
| 56 | + it "returns plain string content unchanged" do |
| 57 | + expect(described_class.translate("just text")).to eq("just text") |
| 58 | + end |
| 59 | + |
| 60 | + it "leaves a text-only content array untouched" do |
| 61 | + content = [{ type: "text", text: "hello" }] |
| 62 | + |
| 63 | + expect(described_class.translate(content)).to equal(content) |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments