diff --git a/lib/context_dev/internal/util.rb b/lib/context_dev/internal/util.rb index 1c922103..ae1af068 100644 --- a/lib/context_dev/internal/util.rb +++ b/lib/context_dev/internal/util.rb @@ -428,7 +428,7 @@ def close @stream.to_a.join in Integer @buf << @stream.next while @buf.length < max_len - @buf.slice!(..max_len) + @buf.slice!(0, max_len) end rescue StopIteration @stream = nil @@ -456,7 +456,7 @@ def read(max_len = nil, out_string = nil) read end end - .tap(&@blk) + .tap { @blk&.call(_1) } end # @api private diff --git a/test/context_dev/internal/util_test.rb b/test/context_dev/internal/util_test.rb index 1dfca4d2..7f441f3c 100644 --- a/test/context_dev/internal/util_test.rb +++ b/test/context_dev/internal/util_test.rb @@ -673,3 +673,29 @@ def test_decode_sse end end end + +class ContextDev::Test::UtilReadIOAdapterTest < Minitest::Test + def test_read_with_max_len + enum = ["hello ", "world"].to_enum + adapter = ContextDev::Internal::Util::ReadIOAdapter.new(enum) + + chunk1 = adapter.read(3) + assert_equal("hel", chunk1) + assert_equal(3, chunk1.bytesize) + + chunk2 = adapter.read(4) + assert_equal("lo w", chunk2) + assert_equal(4, chunk2.bytesize) + + chunk3 = adapter.read(10) + assert_equal("orld", chunk3) + + assert_nil(adapter.read(5)) + end + + def test_read_without_block + adapter = ContextDev::Internal::Util::ReadIOAdapter.new("sample content") + assert_equal("sample", adapter.read(6)) + assert_equal(" content", adapter.read) + end +end