Skip to content
Open
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
4 changes: 2 additions & 2 deletions lib/context_dev/internal/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -456,7 +456,7 @@ def read(max_len = nil, out_string = nil)
read
end
end
.tap(&@blk)
.tap { @blk&.call(_1) }
end

# @api private
Expand Down
26 changes: 26 additions & 0 deletions test/context_dev/internal/util_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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