A small service prepares log line previews for the UI. It receives an array of lines where some data comes in UTF-8 and some in Windows-1251. After a recent PR, the preview started behaving unstably: sometimes the method crashes on a mixed set of lines, and sometimes the result unexpectedly depends on reusing the same input data.
LogPreview.build(lines, source_encoding:) method accepts:
lines — an array of log lines;source_encoding — the encoding of non-UTF-8 input lines, for example Encoding::Windows_1251.text — a string in UTF-8 encoding;bytes — the size of the final text in bytes;empty — false.ruby test/log_preview_test.rb
build call.text is always suitable for safe use as a UTF-8 string.bytes field is consistent with the returned text.ruby, without a Fiber schedulerruby test/log_preview_test.rblines may have mixed Encoding, and source_encoding applies only to non-UTF-8 stringsUse line comments to explain the problem, its impact, the fix and how to test it.