10:126
  7   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þE,`YÇÌc!Z”"ùÏ„„¿ÎÄÀÌ /°    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe TextEncoding, :aggregate_failures do
  describe '.utf8_encode' do
    context 'with valid, UTF-8-encoded strings' do
      subject(:string) { 'hello' }

      it 'returns an identical copy' do
        expect(described_class.utf8_encode(string)).to eq(string)
        expect(described_class.utf8_encode(string).encoding).to be(string.encoding)
        expect(described_class.utf8_encode(string)).not_to be(string)
      end

      context 'when incorrectly set to other, technically valid encodings' do
        subject(:string) { String.new('Ã¶', encoding: 'tis-620') }

        it 'sets input encoding to UTF-8 instead of attempting conversion' do
          expect(described_class.utf8_encode(string)).to eq(string.dup.force_encoding('utf-8'))
        end
      end
    end

    context 'with values that are not strings' do
      it 'coerces them with to_s' do
        expect(described_class.utf8_encode(:hello)).to eq('hello')
        expect(described_class.utf8_encode(42)).to eq('42')
      end

      it 'returns an empty string for nil' do
        expect(described_class.utf8_encode(nil)).to eq('')
      end

      it 'applies the options to the coerced string' do
        object = Struct.new(:value).new('TschÃ¼ss!'.encode(Encoding::ISO_8859_2))
        allow(object).to receive(:to_s).and_return(object.value)

        expect(described_class.utf8_encode(object, from: 'iso-8859-2')).to eq('TschÃ¼ss!')
      end
    end

    context 'with strings in other encodings' do
      subject(:string) { original_string.encode(input_encoding) }

      context 'with no from: option' do
        let(:original_string) { 'TschÃ¼ss!' }
        let(:input_encoding)  { Encoding::ISO_8859_2 }

        it 'detects the input encoding' do
          expect(described_class.utf8_encode(string)).to eq(original_string)
        end
      end

      # Code page 936 writes the euro sign as the single byte 0x80. The detection
      # reports it as GB18030, which rejects that byte in Ruby.
      context 'with code page 936 content that only the detection can place' do
        subject(:string) { original_string.encode(input_encoding).b }

        let(:original_string) { 'æ¬§å…ƒæŠ¥ä»·ï¼šâ‚¬1,200 â€“ è¯·ç¡®è®¤ã€‚è°¢è°¢' }
        let(:input_encoding)  { Encoding::GBK }

        it 'decodes it without a from: option' do
          expect(CharDet.detect(string)['encoding']).to eq('GB18030')
          expect(described_class.utf8_encode(string)).to eq(original_string)
        end

        it 'decodes it with a from: option nobody resolves' do
          expect(described_class.utf8_encode(string, from: 'x-gbk')).to eq(original_string)
        end
      end

      context 'with a valid from: option' do
        let(:original_string) { 'TschÃ¼ss!' }
        let(:input_encoding) { Encoding::ISO_8859_2 }

        it 'uses the specified input encoding' do
          expect(described_class.utf8_encode(string, from: 'iso-8859-2')).to eq(original_string)
        end

        it 'uses any valid input encoding, even if not correct' do
          expect(described_class.utf8_encode(string, from: 'gb18030')).to eq('Tsché»¶s!')
        end
      end

      context 'with an invalid from: option' do
        # Binary, like the mail parser hands it over - otherwise the encoding of
        # the string itself would be a viable candidate and mask the detection.
        subject(:string) { original_string.encode(input_encoding).b }

        let(:original_string) { 'TschÃ¼ss!' }
        let(:input_encoding)  { Encoding::ISO_8859_2 }

        it 'does not try it' do
          expect { string.encode('utf-8', 'us-ascii') }
            .to raise_error(Encoding::InvalidByteSequenceError)

          expect { described_class.utf8_encode(string, from: 'us-ascii') }
            .not_to raise_error
        end

        it 'uses the detected input encoding instead' do
          expect(described_class.utf8_encode(string, f