diff --git a/lib/ruby_llm/content.rb b/lib/ruby_llm/content.rb index d4010c4ba..1a9a27742 100644 --- a/lib/ruby_llm/content.rb +++ b/lib/ruby_llm/content.rb @@ -26,6 +26,10 @@ def format end end + def empty? + @text.to_s.strip.empty? && @attachments.empty? + end + # For Rails serialization def to_h { text: @text, attachments: @attachments.map(&:to_h) } diff --git a/spec/ruby_llm/content_spec.rb b/spec/ruby_llm/content_spec.rb new file mode 100644 index 000000000..70dc44c1a --- /dev/null +++ b/spec/ruby_llm/content_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe RubyLLM::Content do + describe '#empty?' do + it 'is true when text is empty and there are no attachments' do + expect(described_class.new('')).to be_empty + end + + it 'is true when text is whitespace-only and there are no attachments' do + expect(described_class.new(" \n\t")).to be_empty + end + + it 'is false when text has content' do + expect(described_class.new('hello')).not_to be_empty + end + + it 'is false when attachments are present without text' do + content = described_class.new(nil, 'spec/fixtures/ruby.txt') + expect(content).not_to be_empty + end + + it 'is false when both text and attachments are present' do + content = described_class.new('hello', 'spec/fixtures/ruby.txt') + expect(content).not_to be_empty + end + end +end