Sam af4f871096
FIX: never provide tools with invalid UTF-8 strings (#692)
Previous to this change, on truncation we could return invalid
UTF-8 strings to caller

This also allows tools to read up to 30 megs vs the old 4 megs.
2024-06-27 14:06:52 +10:00

45 lines
1.1 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DiscourseAi::AiBot::Tools::Tool do
let :tool_class do
described_class
end
let :corrupt_string do
"\xC3\x28\xA0\xA1\xE2\x28\xA1\xE2\x82\x28\xF0\x28\x8C\xBC"
end
describe "#read_response_body" do
class FakeResponse
def initialize(chunk)
@chunk = chunk
end
def read_body
yield @chunk while true
end
end
it "never returns a corrupt string" do
response = FakeResponse.new(corrupt_string)
result = tool_class.read_response_body(response, max_length: 100.bytes)
expect(result.encoding).to eq(Encoding::UTF_8)
expect(result.valid_encoding?).to eq(true)
# scrubbing removes 7 chars
expect(result.length).to eq(93)
end
it "returns correctly truncated strings" do
response = FakeResponse.new("abc")
result = tool_class.read_response_body(response, max_length: 10.bytes)
expect(result.encoding).to eq(Encoding::UTF_8)
expect(result.valid_encoding?).to eq(true)
expect(result).to eq("abcabcabca")
end
end
end