0x3E8
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þZ+m,ã�f1�­€ùÂ„ Ø¿Á_�uÐb&=LyZËcõÁÀ  /   ˆ{‹„„-i[D<†ªoZƒ›Ù«Ä454_‘I|¥‰ÓMdœv ©ƒ†ü+=ÄÃ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Channel::EmailBuild, type: :model do
  describe '#build' do
    let(:html_body) do
      <<~MSG_HTML.chomp
        <!DOCTYPE html>
        <html>
          <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
          </head>
          <body style="font-family:Geneva,Helvetica,Arial,sans-serif; font-size: 12px;">
            <div>&gt; Welcome!</div><div>&gt;</div><div>&gt; Thank you for installing Zammad. Ã¤Ã¶Ã¼ÃŸ</div><div>&gt;</div>
          </body>
        </html>
      MSG_HTML
    end

    let(:plain_text_body) do
      <<~MSG_TEXT.chomp
        > Welcome!
        >
        > Thank you for installing Zammad. Ã¤Ã¶Ã¼ÃŸ
        >
      MSG_TEXT
    end
    let(:parser)      { Channel::EmailParser.new }
    let(:parsed_data) { parser.parse(mail.to_s) }

    let(:html_part_attachment) do
      parsed_data[:attachments]
        .find { |attachment| attachment[:filename] == 'message.html' }
    end

    let(:file_attachment) do
      parsed_data[:attachments]
        .find { |attachment| attachment[:filename] == filename }
    end

    shared_examples 'adding the email html part as an attachment' do
      it 'adds the html part as an attachment' do
        expect(html_part_attachment).to be_a Hash
      end

      it 'adds the html part as an attachment' do
        expect(html_part_attachment).to include(
          'filename'    => 'message.html',
          'preferences' => include('content-alternative' => true, 'Charset' => 'UTF-8',
                                   'Mime-Type' => 'text/html', 'original-format' => true)
        )
      end

      it 'does not include content-id property in attachment preferences' do
        expect(html_part_attachment).not_to include(
          'preferences' => include('Content-ID')
        )
      end
    end

    shared_examples 'adding a text file as an attachment' do
      it 'adds the text file as an attachment' do
        expect(file_attachment).to include(
          'filename'    => filename,
          'preferences' => include('Charset' => 'UTF-8', 'Mime-Type' => mime_type,
                                   'Content-Type' => "text/plain; charset=UTF-8; filename=#{filename}")
        )
      end

      it 'does not include content* properties in attachment preferences' do
        expect(file_attachment).not_to include(
          'preferences' => include('Content-ID', 'content-alternative')
        )
      end
    end

    shared_examples 'adding a file as an attachment' do |file_type|
      it "adds a #{file_type} as an attachment'" do
        expect(file_attachment).to include(
          'data'        => content, 'filename' => filename,
          'preferences' => include('Charset' => 'UTF-8', 'Mime-Type' => mime_type,
                                   'Content-Type' => preferences_content_type)
        )
      end

      it 'does not include content* properties in attachment preferences' do
        expect(file_attachment).not_to include(
          'preferences' => include('content-alternative', 'Content-ID')
        )
      end
    end

    shared_examples 'not adding email content as attachment' do
      it 'does not add email content as an attachment' do
        expect(html_part_attachment).to be_nil
      end
    end

    context 'with email only' do
      let(:mail) do
        described_class.build(
          from:         'sender@example.com',
          to:           'recipient@example.com',
          body:         mail_body,
          content_type: content_type
        )
      end

      let(:expected_text) do
        <<~MSG_TEXT.chomp
          > Welcome!\r
          >\r
          > Thank you for installing Zammad. Ã¤Ã¶Ã¼ÃŸ\r
          >\r
        MSG_TEXT
      end

      context 'when email contains only html' do
        let(:mail_body) { html_body }
        let(:content_ty