Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/openai/internal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,9 @@ def close
private def read_enum(max_len)
case max_len
in nil
@stream.to_a.join
@stream.to_a.map(&:b).join
in Integer
@buf << @stream.next while @buf.length < max_len
@buf << @stream.next.b while @buf.length < max_len
@buf.slice!(..max_len)
end
rescue StopIteration
Expand Down Expand Up @@ -476,7 +476,7 @@ def initialize(src, &blk)
else
src
end
@buf = String.new
@buf = String.new.b
@blk = blk
end
end
Expand Down
31 changes: 31 additions & 0 deletions test/openai/internal/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,37 @@ def test_copy_read
end
end

def test_read_mixed_encoding_chunks
chunks = ["caf\u00E9", "\xFF\xFE".b]
expected = chunks.map(&:b).join

adapter = OpenAI::Internal::Util::ReadIOAdapter.new(chunks.to_enum) do |chunk|
chunk
end
actual = String.new.b
loop do
chunk = adapter.read(2)
break unless chunk

actual << chunk
end

assert_equal(expected, actual)
assert_equal(Encoding::ASCII_8BIT, actual.encoding)
end

def test_read_all_mixed_encoding_chunks
chunks = ["caf\u00E9", "\xFF\xFE".b]
adapter = OpenAI::Internal::Util::ReadIOAdapter.new(chunks.to_enum) do |chunk|
chunk
end

result = adapter.read

assert_equal(chunks.map(&:b).join, result)
assert_equal(Encoding::ASCII_8BIT, result.encoding)
end

def test_copy_write
cases = {
StringIO.new => "",
Expand Down