# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

require 'models/concerns/checks_human_changes_examples'

RSpec.describe Transaction::Notification, type: :model do
  describe 'pending ticket reminder repeats after midnight at selected time zone', time_zone: 'UTC' do
    let(:group)  { create(:group) }
    let(:user)   { create(:agent) }
    let(:ticket) { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) }

    before do
      travel_to DateTime.parse('2024-11-15T12:00:00Z')

      user.groups << group
      ticket

      Setting.set('timezone_default', 'America/Santiago')
      run(ticket, user, 'reminder_reached')
      OnlineNotification.destroy_all
    end

    it 'notification not sent at UTC midnight' do
      travel_to DateTime.parse('2024-11-16T00:01:00Z')

      expect { run(ticket, user, 'reminder_reached') }.not_to change(OnlineNotification, :count)
    end

    it 'notification sent at selected time zone midnight' do
      travel_to DateTime.parse('2024-11-16T03:01:00Z')

      expect { run(ticket, user, 'reminder_reached') }.to change(OnlineNotification, :count).by(1)
    end
  end

  # https://github.com/zammad/zammad/issues/4066
  describe 'notification sending reason may be fully translated' do
    let(:group) { create(:group) }
    let(:user)      { create(:agent, groups: [group]) }
    let(:ticket)    { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) }
    let(:reason_en) { 'You are receiving this because you are the owner of this ticket.' }
    let(:reason_de) do
      Translation.translate('de-de', reason_en).tap do |translated|
        expect(translated).not_to eq(reason_en) # rubocop:disable RSpec/ExpectInLet
      end
    end

    before do
      allow(NotificationFactory::Mailer).to receive(:deliver)
    end

    it 'notification includes English footer' do
      run(ticket, user, 'reminder_reached')

      expect(NotificationFactory::Mailer)
        .to have_received(:deliver)
        .with hash_including body: %r{#{reason_en}}
    end

    context 'when locale set to Deutsch' do
      before do
        user.preferences[:locale] = 'de-de'
        user.save
      end

      it 'notification includes German footer' do
        run(ticket, user, 'reminder_reached')

        expect(NotificationFactory::Mailer)
          .to have_received(:deliver)
          .with hash_including body: %r{#{reason_de}}
      end
    end
  end

  describe '#ooo_replacements' do
    subject(:notification_instance) { build(ticket, user) }

    let(:group)         { create(:group) }
    let(:user)          { create(:agent, :ooo, :groupable, ooo_agent: replacement_1, group: group) }
    let(:ticket)        { create(:ticket, owner: user, group: group, state_name: 'open', pending_time: Time.current) }

    context 'when replacement has access' do
      let(:replacement_1) { create(:agent, :groupable, group: group) }

      it 'is added to list' do
        replacements = Set.new

        ooo(notification_instance, user, replacements: replacements)

        expect(replacements).to include replacement_1
      end

      context 'when replacement has replacement' do
        let(:replacement_1) { create(:agent, :ooo, :groupable, ooo_agent: replacement_2, group: group) }
        let(:replacement_2) { create(:agent, :groupable, group: group) }

        it 'replacement\'s replacement added to list' do
          replacements = Set.new

          ooo(notification_instance, user, replacements: replacements)

          expect(replacements).to include replacement_2
        end

        it 'intermediary replacement is not in list' do
          replacements = Set.new

          ooo(notification_instance, user, replacements: replacements)

          expect(replacements).not_to include replacement_1
        end
      end
    end

    context 'when replacement does not have access' do
      let(:replacement_1) { create(:agent) }

      it 'is not added to list' do
        replacements = Set.new

        ooo(notification_instance, user, replacements: replacements)

        expect(replacements).not_to include replacement_1
      end

      context 'when replacement has replacement with access' do
        let(:replacement_1) { create(:agent, :ooo, ooo_agent: replacement_2) }
        let(:replacement_2) { create(:agent, :groupable, group: group) }

        it 'his replacement may be added' do
          replacements = Set.new

          ooo(notification_instance, user, replacements: replacements)

          expect(replacements).to include replacement_2
        end
      end
    end
  end

  describe 'SMTP errors' do
    let(:group)    { create(:group) }
    let(:user)     { create(:agent, groups: [group]) }
    let(:ticket)   { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) }
    let(:response) { Net::SMTP::Response.new(response_status_code, 'mocked SMTP response') }
    let(:error)    { Net::SMTPFatalError.new(response) }

    before do
      allow_any_instance_of(Net::SMTP).to receive(:start).and_raise(error)

      Service::System::SetEmailNotificationConfiguration
        .execute(
          adapter:           'smtp',
          new_configuration: {}
        )
    end

    context 'when there is a problem with the sending SMTP server' do
      let(:response_status_code) { 535 }

      it 'delivers the online notification anyway' do
        expect { run(ticket, user, 'reminder_reached') }
          .to not_raise_error
          .and change { OnlineNotification.exists?(user: user, object_lookup_id: ObjectLookup.by_name('Ticket'), o_id: ticket.id) }
          .to(true)
      end

      it 'marks the notification channel as faulty' do
        run(ticket, user, 'reminder_reached')

        expect(Channel.find_by(area: 'Email::Notification', active: true)).to have_attributes(status_out: 'error', last_log_out: include('Net::SMTPFatalError'))
      end

      it 'does not list the email channel in the ticket history' do
        run(ticket, user, 'reminder_reached')

        expect(ticket.history_get.find { |entry| entry['type'] == 'notification' })
          .to include('value_to' => "#{user.email}(reminder_reached:online)")
      end

      context 'with more than one recipient' do
        let(:other_user) { create(:agent, groups: [ticket.group]) }

        before do
          other_user.preferences[:notification_config][:matrix][:reminder_reached][:criteria] = {
            'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => false, 'no' => true
          }
          other_user.save!
        end

        it 'delivers the online notification to all recipients' do
          expect { run(ticket, user, 'reminder_reached') }
            .to change(OnlineNotification, :count).by(2)
        end

        it 'attempts the email delivery only once' do
          delivery_attempts = 0
          allow_any_instance_of(Net::SMTP).to receive(:start) do
            delivery_attempts += 1
            raise error
          end

          run(ticket, user, 'reminder_reached')

          expect(delivery_attempts).to eq(1)
        end
      end
    end

    context 'when there is a problem with the receiving SMTP server' do
      let(:response_status_code) { 550 }

      it 'logs the information about failed email delivery' do
        allow(Rails.logger).to receive(:info)
        run(ticket, user, 'reminder_reached')
        expect(Rails.logger).to have_received(:info)
      end

      it 'does not list the email channel in the ticket history' do
        run(ticket, user, 'reminder_reached')

        expect(ticket.history_get.find { |entry| entry['type'] == 'notification' })
          .to include('value_to' => "#{user.email}(reminder_reached:online)")
      end
    end
  end

  describe 'recipient with a failed email delivery' do
    let(:group)       { create(:group) }
    let(:preferences) { { mail_delivery_failed: true, mail_delivery_failed_data: Time.current } }
    let(:user)        { create(:agent, groups: [group], preferences: preferences) }
    let(:ticket)      { create(:ticket, owner: user, group: group, state_name: 'open', pending_time: Time.current) }

    before do
      allow(NotificationFactory::Mailer).to receive(:notification)
    end

    it 'delivers the online notification' do
      expect { run(ticket, user, 'reminder_reached') }
        .to change(OnlineNotification, :count).by(1)
    end

    it 'does not deliver the email notification' do
      run(ticket, user, 'reminder_reached')

      expect(NotificationFactory::Mailer).not_to have_received(:notification)
    end

    context 'without a failed email delivery' do
      let(:preferences) { {} }

      it 'delivers the email notification' do
        run(ticket, user, 'reminder_reached')

        expect(NotificationFactory::Mailer).to have_received(:notification)
      end
    end
  end

  describe 'a single email notification failing to be sent' do
    let(:group)      { create(:group) }
    let(:user)       { create(:agent, groups: [group]) }
    let(:other_user) { create(:agent, groups: [group]) }
    let(:ticket)     { create(:ticket, owner: user, group: group, state_name: 'open', pending_time: Time.current) }
    let(:error)      { Channel::DeliveryError.new('Smtp: broken message (ArgumentError)', ArgumentError.new('broken message')) }

    before do
      other_user.preferences[:notification_config][:matrix][:reminder_reached][:criteria] = {
        'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => false, 'no' => true
      }
      other_user.save!

      allow(NotificationFactory::Mailer).to receive(:notification).and_raise(error)
    end

    it 'still attempts the delivery for the other recipient' do
      run(ticket, user, 'reminder_reached')

      expect(NotificationFactory::Mailer).to have_received(:notification).twice
    end

    it 'delivers the online notification to all recipients' do
      expect { run(ticket, user, 'reminder_reached') }
        .to change(OnlineNotification, :count).by(2)
    end
  end

  describe 'online notifications of several recipients' do
    let(:group)          { create(:group) }
    let(:agents)         { create_list(:agent, 3, groups: [group]) }
    let(:last_recipient) { agents.max_by(&:login) }
    let(:ticket)         { create(:ticket, group:, state_name: 'open', pending_time: Time.current) }
    let(:instance)       { build(ticket, User.find(1), 'reminder_reached') }

    def online_notification_count
      OnlineNotification.where(object_lookup_id: ObjectLookup.by_name('Ticket'), o_id: ticket.id).count
    end

    def notification_history
      ticket.history_get.select { |entry| entry['type'] == 'notification' }.pluck('value_to')
    end

    before do
      agents.each do |agent|
        agent.preferences[:notification_config][:matrix][:reminder_reached][:criteria] = {
          'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => false, 'no' => true
        }
        agent.save!
      end

      allow(NotificationFactory::Mailer).to receive(:notification).and_return(true)
    end

    it 'creates the online notifications of all recipients before the first email' do
      counts_at_delivery = []
      allow(NotificationFactory::Mailer).to receive(:notification) do
        counts_at_delivery.push(online_notification_count)
        true
      end

      instance.perform

      expect(counts_at_delivery).to eq([3, 3, 3])
    end

    it 'sends the emails outside of the database transactions of the online notifications' do
      open_transactions = ActiveRecord::Base.connection.open_transactions
      open_transactions_at_delivery = []
      allow(NotificationFactory::Mailer).to receive(:notification) do
        open_transactions_at_delivery.push(ActiveRecord::Base.connection.open_transactions)
        true
      end

      instance.perform

      expect(open_transactions_at_delivery).to eq([open_transactions] * 3)
    end

    it 'lists the used channels of every recipient in the ticket history' do
      instance.perform

      expect(notification_history).to match_array(agents.map { |agent| "#{agent.email}(reminder_reached:online,email)" })
    end

    context 'when the email of the first recipient fails to be sent' do
      let(:error) { Channel::DeliveryError.new('Smtp: broken message (ArgumentError)', ArgumentError.new('broken message')) }

      before do
        call_count = 0
        allow(NotificationFactory::Mailer).to receive(:notification) do
          call_count += 1
          raise error if call_count == 1

          true
        end
      end

      it 'keeps the online notifications and sends the emails of the other recipients', aggregate_failures: true do
        expect { instance.perform }.to change { online_notification_count }.by(3)
        expect(notification_history).to contain_exactly(
          "#{agents.min_by(&:login).email}(reminder_reached:online)",
          *agents.sort_by(&:login).drop(1).map { |agent| "#{agent.email}(reminder_reached:online,email)" },
        )
      end
    end

    context 'when an unexpected error occurs while sending the first email' do
      before do
        call_count = 0
        allow(NotificationFactory::Mailer).to receive(:notification) do
          call_count += 1
          raise 'broken template' if call_count == 1

          true
        end
      end

      it 'keeps the online notifications and sends the emails of the other recipients', aggregate_failures: true do
        expect { instance.perform }.to change { online_notification_count }.by(3)
        expect(notification_history).to contain_exactly(
          "#{agents.min_by(&:login).email}(reminder_reached:online)",
          *agents.sort_by(&:login).drop(1).map { |agent| "#{agent.email}(reminder_reached:online,email)" },
        )
      end
    end

    context 'when the history entry of the first recipient fails to be written' do
      let(:error) { ActiveRecord::StatementInvalid.new('broken history') }

      before do
        call_count = 0
        allow(History).to receive(:add).and_wrap_original do |original, data|
          if data[:history_type] == 'notification'
            call_count += 1
            raise error if call_count == 1
          end

          original.call(data)
        end
        allow(Rails.logger).to receive(:error)
      end

      it 'sends the emails and writes the history entries of the other recipients', aggregate_failures: true do
        instance.perform

        expect(NotificationFactory::Mailer).to have_received(:notification).exactly(3).times
        expect(notification_history).to match_array(agents.sort_by(&:login).drop(1).map { |agent| "#{agent.email}(reminder_reached:online,email)" })
        expect(Rails.logger).to have_received(:error).with(error)
      end
    end

    context 'when the online notification of the last recipient fails' do
      before do
        allow(instance).to receive(:send_to_single_recipient_online).and_wrap_original do |original, user, *args|
          raise 'broken online notification' if user == last_recipient

          original.call(user, *args)
        end
      end

      it 'still sends the emails of the recipients notified before', aggregate_failures: true do
        expect { instance.perform }
          .to raise_error(RuntimeError, 'broken online notification')
          .and change { online_notification_count }.by(2)
        expect(NotificationFactory::Mailer).to have_received(:notification).twice
        expect(notification_history).to match_array((agents - [last_recipient]).map { |agent| "#{agent.email}(reminder_reached:online,email)" })
      end
    end
  end

  describe 'daily locks behaviour' do
    let(:group)      { create(:group) }
    let(:user)       { create(:agent, groups: [group]) }
    let(:other_user) { create(:agent, groups: [group]) }
    let(:ticket)     { create(:ticket, group:, state_name: 'open', pending_time: Time.current) }
    let(:instance)   { build(ticket, user, 'reminder_reached') }

    def user_gets_reminders(user)
      user.preferences[:notification_config][:matrix][:reminder_reached][:criteria] = {
        'owned_by_me' => true, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => true
      }
      user.save!
    end

    before do
      travel_to Time.current.noon

      [user, other_user].each { user_gets_reminders(it) }

      allow(instance).to receive(:send_to_single_recipient_online)
    end

    context 'with existing locks' do
      before do
        run(ticket, user, 'reminder_reached')
      end

      it 'notification not resent on same day' do
        instance.perform

        expect(instance).not_to have_received(:send_to_single_recipient_online)
      end

      it 'notification is resent on same day if ticket pending time changes' do
        ticket.update!(pending_time: 1.hour.from_now)

        instance.perform

        expect(instance).to have_received(:send_to_single_recipient_online).twice
      end

      context 'when next day' do
        before { travel 1.day }

        it 'notification is resent on next day' do
          instance.perform

          expect(instance).to have_received(:send_to_single_recipient_online).twice
        end

        it 'notification lock is gone next day' do
          expect { run(ticket, other_user, 'reminder_reached') }.to change(Ticket::DailyEventLock, :count).by(2)
        end
      end

      context 'with an additional user' do
        let(:new_user) { create(:agent, groups: [group]) }

        before do
          user_gets_reminders(new_user)
          Rails.cache.clear # clears cache because notification preferences are cached
        end

        it 'notification is sent to new user on same day' do
          instance.perform

          expect(instance).to have_received(:send_to_single_recipient_online).once
        end
      end
    end

    it 'one of notification locks are present when one user fails', aggregate_failures: true do
      call_count = 0
      allow(instance).to receive(:send_to_single_recipient_online) do
        raise StandardError if call_count.positive?

        call_count += 1
      end

      expect do
        expect { instance.perform }.to raise_error(StandardError)
      end.to change(Ticket::DailyEventLock, :count).by(1)
    end
  end

  it_behaves_like 'ChecksHumanChanges'

  describe 'group recipients after role based access changes' do
    let(:group)  { create(:group) }
    let(:role)   { create(:role, :agent, group_names_access_map: { group.name => 'full' }) }
    let(:agent)  { create(:user, roles: [role]) }
    let(:ticket) { create(:ticket, group: group) }

    before do
      agent
      ticket

      run(ticket, User.find(1), 'create')
    end

    it 'notifies the agent while the role grants access' do
      expect { run(ticket, User.find(1), 'create') }
        .to change { OnlineNotification.where(user: agent).count }.by(1)
    end

    context 'when the role is removed from the agent' do
      before do
        agent.update!(roles: Role.where(name: 'Customer'))
      end

      it 'does not notify the agent' do
        expect { run(ticket, User.find(1), 'create') }
          .not_to change { OnlineNotification.where(user: agent).count }
      end
    end

    context 'when the group is removed from the role' do
      before do
        role.update!(group_names_access_map: {})
      end

      it 'does not notify the agent' do
        expect { run(ticket, User.find(1), 'create') }
          .not_to change { OnlineNotification.where(user: agent).count }
      end
    end

    context 'when the role is deactivated' do
      before do
        role.update!(active: false)
      end

      it 'does not notify the agent' do
        expect { run(ticket, User.find(1), 'create') }
          .not_to change { OnlineNotification.where(user: agent).count }
      end
    end
  end

  describe 'agent notifications across the ticket create/update lifecycle', performs_jobs: true do
    let(:group) do
      Group.create_or_update(
        name:          'TicketNotificationTest',
        updated_by_id: 1,
        created_by_id: 1
      )
    end
    let(:groups) { Group.where(name: 'TicketNotificationTest') }
    let(:agent1) do
      User.create_or_update(
        login:         'ticket-notification-agent1@example.com',
        firstname:     'Notification',
        lastname:      'Agent1',
        email:         'ticket-notification-agent1@example.com',
        password:      'agentpw',
        out_of_office: false,
        active:        true,
        roles:         Role.where(name: 'Agent'),
        groups:        groups,
        preferences:   {
          locale: 'de-de',
        },
        updated_by_id: 1,
        created_by_id: 1,
      )
    end
    let(:agent2) do
      User.create_or_update(
        login:         'ticket-notification-agent2@example.com',
        firstname:     'Notification',
        lastname:      'Agent2',
        email:         'ticket-notification-agent2@example.com',
        password:      'agentpw',
        out_of_office: false,
        active:        true,
        roles:         Role.where(name: 'Agent'),
        groups:        groups,
        preferences:   {
          locale:   'en-us',
          timezone: 'America/St_Lucia',
        },
        updated_by_id: 1,
        created_by_id: 1,
      )
    end
    let(:agent3) do
      User.create_or_update(
        login:         'ticket-notification-agent3@example.com',
        firstname:     'Notification',
        lastname:      'Agent3',
        email:         'ticket-notification-agent3@example.com',
        password:      'agentpw',
        out_of_office: false,
        active:        true,
        roles:         Role.where(name: 'Agent'),
        groups:        groups,
        preferences:   {
          locale: 'de-de',
        },
        updated_by_id: 1,
        created_by_id: 1,
      )
    end
    let(:agent4) do
      User.create_or_update(
        login:         'ticket-notification-agent4@example.com',
        firstname:     'Notification',
        lastname:      'Agent4',
        email:         'ticket-notification-agent4@example.com',
        password:      'agentpw',
        out_of_office: false,
        active:        true,
        roles:         Role.where(name: 'Agent'),
        groups:        groups,
        preferences:   {
          locale: 'de-de',
        },
        updated_by_id: 1,
        created_by_id: 1,
      )
    end
    let(:customer) do
      User.create_or_update(
        login:         'ticket-notification-customer@example.com',
        firstname:     'Notification',
        lastname:      'Customer',
        email:         'ticket-notification-customer@example.com',
        password:      'agentpw',
        active:        true,
        roles:         Role.where(name: 'Customer'),
        groups:        groups,
        updated_by_id: 1,
        created_by_id: 1,
      )
    end

    before do
      Setting.set('timezone_default', 'Europe/Berlin')
      Trigger.create_or_update(
        name:                 'auto reply - new ticket',
        condition:            {
          'ticket.action'   => {
            'operator' => 'is',
            'value'    => 'create',
          },
          'ticket.state_id' => {
            'operator' => 'is not',
            'value'    => Ticket::State.lookup(name: 'closed').id,
          },
          'article.type_id' => {
            'operator' => 'is',
            'value'    => [
              Ticket::Article::Type.lookup(name: 'email').id,
              Ticket::Article::Type.lookup(name: 'phone').id,
              Ticket::Article::Type.lookup(name: 'web').id,
            ],
          },
        },
        perform:              {
          'notification.email' => {
            # rubocop:disable Lint/InterpolationCheck
            'body'      => '<p>Your request (Ticket##{ticket.number}) has been received and will be reviewed by our support staff.<p>
<br/>
<p>To provide additional information, please reply to this email or click on the following link:
<a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
</p>
<br/>
<p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
            'recipient' => 'ticket_customer',
            'subject'   => 'Thanks for your inquiry (#{ticket.title})',
            # rubocop:enable Lint/InterpolationCheck
          },
        },
        disable_notification: true,
        active:               true,
        created_by_id:        1,
        updated_by_id:        1,
      )

      group
      Group.create_if_not_exists(
        name:          'WithoutAccess',
        note:          'Test for notification check.',
        updated_by_id: 1,
        created_by_id: 1
      )

      agent1
      agent2
      agent3
      agent4
      customer
    end

    it 'notifies all group agents for postmaster-created tickets, but not the creating agent for application-server-created tickets', :aggregate_failures do
      ApplicationHandleInfo.use('scheduler.postmaster') do
        ticket1 = Ticket.create!(
          title:         'some notification test 1',
          group:         group,
          customer:      customer,
          state:         Ticket::State.lookup(name: 'new'),
          priority:      Ticket::Priority.lookup(name: '2 normal'),
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )
        Ticket::Article.create!(
          ticket_id:     ticket1.id,
          from:          'some_sender@example.com',
          to:            'some_recipient@example.com',
          subject:       'some subject',
          message_id:    'some@id',
          body:          'some message',
          internal:      false,
          sender:        Ticket::Article::Sender.where(name: 'Customer').first,
          type:          Ticket::Article::Type.where(name: 'email').first,
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )
        expect(ticket1).to be_truthy

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 + agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(1)
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(1)
      end

      ApplicationHandleInfo.use('application_server') do
        ticket2 = Ticket.create!(
          title:         'some notification test 2',
          group:         group,
          customer:      customer,
          state:         Ticket::State.lookup(name: 'new'),
          priority:      Ticket::Priority.lookup(name: '2 normal'),
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )
        Ticket::Article.create!(
          ticket_id:     ticket2.id,
          from:          'some_sender@example.com',
          to:            'some_recipient@example.com',
          subject:       'some subject',
          message_id:    'some@id',
          body:          'some message',
          internal:      false,
          sender:        Ticket::Article::Sender.where(name: 'Customer').first,
          type:          Ticket::Article::Type.where(name: 'email').first,
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )
        expect(ticket2).to be_truthy

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 + agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(0)
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(1)
      end
    end

    it 'notifies the group agents about a customer ticket, but not the agent who changes it', :aggregate_failures do
      ApplicationHandleInfo.use('application_server') do
        ticket1 = Ticket.create!(
          title:         'some notification test 3',
          group:         group,
          customer:      customer,
          state:         Ticket::State.lookup(name: 'new'),
          priority:      Ticket::Priority.lookup(name: '2 normal'),
          updated_by_id: customer.id,
          created_by_id: customer.id,
        )
        Ticket::Article.create!(
          ticket_id:     ticket1.id,
          from:          'some_sender@example.com',
          to:            'some_recipient@example.com',
          subject:       'some subject',
          message_id:    'some@id',
          body:          'some message',
          internal:      false,
          sender:        Ticket::Article::Sender.where(name: 'Customer').first,
          type:          Ticket::Article::Type.where(name: 'email').first,
          updated_by_id: customer.id,
          created_by_id: customer.id,
        )
        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 + agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(1)
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(1)

        # update ticket attributes
        ticket1.title    = "#{ticket1.title} - #2"
        ticket1.priority = Ticket::Priority.lookup(name: '3 high')
        ticket1.save!

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 + agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(2)
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(2)

        # add article to ticket
        Ticket::Article.create!(
          ticket_id:     ticket1.id,
          from:          'some person',
          subject:       'some note',
          body:          'some message',
          internal:      true,
          sender:        Ticket::Article::Sender.where(name: 'Agent').first,
          type:          Ticket::Article::Type.where(name: 'note').first,
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )

        perform_enqueued_jobs commit_transaction: true

        # verify notifications not to agent1 but to agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(2)
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(3)

        # update ticket by agent1
        ticket1.owner_id      = agent1.id
        ticket1.updated_by_id = agent1.id
        ticket1.save!
        Ticket::Article.create!(
          ticket_id:     ticket1.id,
          from:          'some person',
          subject:       'some note',
          body:          'some message',
          internal:      true,
          sender:        Ticket::Article::Sender.where(name: 'Agent').first,
          type:          Ticket::Article::Type.where(name: 'note').first,
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )

        perform_enqueued_jobs commit_transaction: true

        # verify notifications not to agent1 but to agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(2)
        expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(3)
      end
    end

    it 'notifies the owner only about changes made by another agent', :aggregate_failures do
      ApplicationHandleInfo.use('application_server') do
        ticket2 = Ticket.create!(
          title:         'some notification test 4',
          group:         group,
          customer_id:   2,
          owner_id:      agent1.id,
          state:         Ticket::State.lookup(name: 'new'),
          priority:      Ticket::Priority.lookup(name: '2 normal'),
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )
        Ticket::Article.create!(
          ticket_id:     ticket2.id,
          from:          'some_sender@example.com',
          to:            'some_recipient@example.com',
          subject:       'some subject',
          message_id:    'some@id',
          body:          'some message',
          internal:      false,
          sender:        Ticket::Article::Sender.where(name: 'Agent').first,
          type:          Ticket::Article::Type.where(name: 'phone').first,
          updated_by_id: agent1.id,
          created_by_id: agent1.id,
        )

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to no one
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(0)
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(0)

        # update ticket
        ticket2.title         = "#{ticket2.title} - #2"
        ticket2.updated_by_id = agent1.id
        ticket2.priority      = Ticket::Priority.lookup(name: '3 high')
        ticket2.save!

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to none
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(0)
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(0)

        # update ticket
        ticket2.title         = "#{ticket2.title} - #3"
        ticket2.updated_by_id = agent2.id
        ticket2.priority      = Ticket::Priority.lookup(name: '2 normal')
        ticket2.save!

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 and not to agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(1)
        expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(0)
      end
    end

    it 'notifies the owner about a ticket created by another agent, but not about an article becoming internal', :aggregate_failures do # rubocop:disable RSpec/ExampleLength
      ApplicationHandleInfo.use('application_server') do
        ticket3 = Ticket.create!(
          title:         'some notification test 5',
          group:         group,
          customer_id:   2,
          owner_id:      agent1.id,
          state:         Ticket::State.lookup(name: 'new'),
          priority:      Ticket::Priority.lookup(name: '2 normal'),
          updated_by_id: agent2.id,
          created_by_id: agent2.id,
        )
        article_inbound = Ticket::Article.create!(
          ticket_id:     ticket3.id,
          from:          'some_sender@example.com',
          to:            'some_recipient@example.com',
          subject:       'some subject',
          message_id:    'some@id',
          body:          'some message',
          internal:      false,
          sender:        Ticket::Article::Sender.where(name: 'Agent').first,
          type:          Ticket::Article::Type.where(name: 'phone').first,
          updated_by_id: agent2.id,
          created_by_id: agent2.id,
        )

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 and not to agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent1, 'email')).to eq(1)
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent2, 'email')).to eq(0)

        # update ticket
        ticket3.title         = "#{ticket3.title} - #2"
        ticket3.updated_by_id = agent1.id
        ticket3.priority      = Ticket::Priority.lookup(name: '3 high')
        ticket3.save!

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to no one
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent1, 'email')).to eq(1)
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent2, 'email')).to eq(0)

        # update ticket
        ticket3.title         = "#{ticket3.title} - #3"
        ticket3.updated_by_id = agent2.id
        ticket3.priority      = Ticket::Priority.lookup(name: '2 normal')
        ticket3.save!

        perform_enqueued_jobs commit_transaction: true

        # verify notifications to agent1 and not to agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent1, 'email')).to eq(2)
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent2, 'email')).to eq(0)

        # update article / no notification should be sent
        article_inbound.internal = true
        article_inbound.save!

        perform_enqueued_jobs commit_transaction: true

        # verify notifications not to agent1 and not to agent2
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent1, 'email')).to eq(2)
        expect(NotificationFactory::Mailer.already_sent?(ticket3, agent2, 'email')).to eq(0)
      end
    end

    it 'sends no notifications when disable_notification is passed to the dispatcher', :aggregate_failures do
      ticket1 = Ticket.create!(
        title:         'some notification test 1 - no notification',
        group:         group,
        customer:      customer,
        state:         Ticket::State.lookup(name: 'new'),
        priority:      Ticket::Priority.lookup(name: '2 normal'),
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      Ticket::Article.create!(
        ticket_id:     ticket1.id,
        from:          'some_sender@example.com',
        to:            'some_recipient@example.com',
        subject:       'some subject',
        message_id:    'some@id',
        body:          'some message',
        internal:      false,
        sender:        Ticket::Article::Sender.where(name: 'Customer').first,
        type:          Ticket::Article::Type.where(name: 'email').first,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      expect(ticket1).to be_truthy

      TransactionDispatcher.commit(disable_notification: true)
      perform_enqueued_jobs

      # verify notifications to agent1 + agent2
      expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(0)
      expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(0)
    end

    context 'with notification_config matrix preferences' do
      around do |example|
        ApplicationHandleInfo.use('scheduler.postmaster') { example.run }
      end

      def configure_matrix(agent, owned_by_me:, all_tickets:, group_ids: nil, channel: nil)
        %w[create update].each do |type|
          matrix = agent.preferences['notification_config']['matrix'][type]
          matrix['criteria'].merge!('owned_by_me' => owned_by_me, 'owned_by_nobody' => false, 'no' => all_tickets)
          matrix['channel'].merge!(channel) if channel
        end
        agent.preferences['notification_config']['group_ids'] = group_ids if group_ids
        agent.save!
      end

      def create_customer_ticket(owner: nil)
        ticket = Ticket.create!(
          title:         'some notification test - preferences',
          group:         group,
          customer:      customer,
          owner_id:      owner&.id || 1,
          state:         Ticket::State.lookup(name: 'new'),
          priority:      Ticket::Priority.lookup(name: '2 normal'),
          updated_by_id: customer.id,
          created_by_id: customer.id,
        )
        Ticket::Article.create!(
          ticket_id:     ticket.id,
          from:          'some_sender@example.com',
          to:            'some_recipient@example.com',
          subject:       'some subject',
          message_id:    'some@id',
          body:          'some message',
          internal:      false,
          sender:        Ticket::Article::Sender.where(name: 'Customer').first,
          type:          Ticket::Article::Type.where(name: 'email').first,
          updated_by_id: customer.id,
          created_by_id: customer.id,
        )

        perform_enqueued_jobs commit_transaction: true

        ticket
      end

      def update_ticket(ticket)
        ticket.title    = "#{ticket.title} - #2"
        ticket.priority = Ticket::Priority.lookup(name: '3 high')
        ticket.save!

        perform_enqueued_jobs commit_transaction: true
      end

      def sent(ticket, agent, channel = 'email')
        NotificationFactory::Mailer.already_sent?(ticket, agent, channel)
      end

      context 'when agent1 wants notifications about own tickets only and agent2 about all tickets' do
        before do
          configure_matrix(agent1, owned_by_me: true, all_tickets: false)
          configure_matrix(agent2, owned_by_me: false, all_tickets: true)
        end

        it 'notifies only agent2 about an unowned ticket', :aggregate_failures do
          ticket = create_customer_ticket

          expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([0, 1])

          update_ticket(ticket)

          expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([0, 2])
        end

        it 'notifies both agents about a ticket owned by agent1', :aggregate_failures do
          ticket = create_customer_ticket(owner: agent1)

          expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([1, 1])

          update_ticket(ticket)

          expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([2, 2])
        end

        it 'notifies only agent2 about a ticket owned by agent2', :aggregate_failures do
          ticket = create_customer_ticket(owner: agent2)

          expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([0, 1])

          update_ticket(ticket)

          expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([0, 2])
        end
      end

      it 'notifies an agent whose group filter includes the ticket group', :aggregate_failures do
        configure_matrix(agent1, owned_by_me: true, all_tickets: true, group_ids: [group.id.to_s])
        configure_matrix(agent2, owned_by_me: false, all_tickets: true)

        ticket = create_customer_ticket

        expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([1, 1])

        update_ticket(ticket)

        expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([2, 2])
      end

      it 'does not notify an agent whose group filter excludes the ticket group', :aggregate_failures do
        configure_matrix(agent1, owned_by_me: true, all_tickets: true, group_ids: [group.id.to_s])
        configure_matrix(agent2, owned_by_me: false, all_tickets: true, group_ids: [99])

        ticket = create_customer_ticket

        expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([1, 0])

        update_ticket(ticket)

        expect([sent(ticket, agent1), sent(ticket, agent2)]).to eq([2, 0])
      end

      it 'notifies the owner even if the group filter excludes the ticket group', :aggregate_failures do
        configure_matrix(agent1, owned_by_me: true, all_tickets: true, group_ids: [999])
        configure_matrix(agent2, owned_by_me: true, all_tickets: true, group_ids: [999])

        ticket = create_customer_ticket(owner: agent1)

        expect([sent(ticket, agent1), sent(ticket, agent1, 'online')]).to eq([1, 1])
        expect([sent(ticket, agent2), sent(ticket, agent2, 'online')]).to eq([0, 0])

        update_ticket(ticket)

        expect([sent(ticket, agent1), sent(ticket, agent1, 'online')]).to eq([2, 2])
        expect([sent(ticket, agent2), sent(ticket, agent2, 'online')]).to eq([0, 0])
      end

      it 'notifies only via the enabled channels', :aggregate_failures do
        configure_matrix(agent1, owned_by_me: true, all_tickets: true, group_ids: [999], channel: { 'email' => false, 'online' => true })
        configure_matrix(agent2, owned_by_me: true, all_tickets: true, group_ids: [999], channel: { 'email' => false, 'online' => true })

        ticket = create_customer_ticket(owner: agent1)

        expect([sent(ticket, agent1), sent(ticket, agent1, 'online')]).to eq([0, 1])
        expect([sent(ticket, agent2), sent(ticket, agent2, 'online')]).to eq([0, 0])

        update_ticket(ticket)

        expect([sent(ticket, agent1), sent(ticket, agent1, 'online')]).to eq([0, 2])
        expect([sent(ticket, agent2), sent(ticket, agent2, 'online')]).to eq([0, 0])
      end
    end

    it 'merges buffered ticket changes into a single uniq change set per attribute', :aggregate_failures do
      # create ticket in group
      ticket1 = Ticket.create!(
        title:         'some notification event test 1',
        group:         group,
        customer:      customer,
        state:         Ticket::State.lookup(name: 'new'),
        priority:      Ticket::Priority.lookup(name: '2 normal'),
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      Ticket::Article.create!(
        ticket_id:     ticket1.id,
        from:          'some_sender@example.com',
        to:            'some_recipient@example.com',
        subject:       'some subject',
        message_id:    'some@id',
        body:          'some message',
        internal:      false,
        sender:        Ticket::Article::Sender.where(name: 'Customer').first,
        type:          Ticket::Article::Type.where(name: 'email').first,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      expect(ticket1).to be_truthy

      # execute object transaction
      TransactionDispatcher.commit

      # update ticket attributes
      ticket1.title    = "#{ticket1.title} - #2"
      ticket1.priority = Ticket::Priority.lookup(name: '3 high')
      ticket1.save!

      list         = EventBuffer.list('transaction')
      list_objects = TransactionDispatcher.get_uniq_changes(list)

      expect(list_objects['Ticket'][ticket1.id][:changes]['title'][0]).to eq('some notification event test 1')
      expect(list_objects['Ticket'][ticket1.id][:changes]['title'][1]).to eq('some notification event test 1 - #2')
      expect(list_objects['Ticket'][ticket1.id][:changes]['priority']).to be_falsey
      expect(list_objects['Ticket'][ticket1.id][:changes]['priority_id'][0]).to eq(2)
      expect(list_objects['Ticket'][ticket1.id][:changes]['priority_id'][1]).to eq(3)

      # update ticket attributes
      ticket1.title    = "#{ticket1.title} - #3"
      ticket1.priority = Ticket::Priority.lookup(name: '1 low')
      ticket1.save!

      list         = EventBuffer.list('transaction')
      list_objects = TransactionDispatcher.get_uniq_changes(list)

      expect(list_objects['Ticket'][ticket1.id][:changes]['title'][0]).to eq('some notification event test 1')
      expect(list_objects['Ticket'][ticket1.id][:changes]['title'][1]).to eq('some notification event test 1 - #2 - #3')
      expect(list_objects['Ticket'][ticket1.id][:changes]['priority']).to be_falsey
      expect(list_objects['Ticket'][ticket1.id][:changes]['priority_id'][0]).to eq(2)
      expect(list_objects['Ticket'][ticket1.id][:changes]['priority_id'][1]).to eq(1)
    end

    it 'reroutes email notifications to an out-of-office agent\'s replacement', :aggregate_failures do # rubocop:disable RSpec/ExampleLength
      # create ticket in group
      ticket1 = Ticket.create!(
        title:         'some notification test out of office',
        group:         group,
        customer:      customer,
        owner_id:      agent2.id,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      Ticket::Article.create!(
        ticket_id:     ticket1.id,
        from:          'some_sender@example.com',
        to:            'some_recipient@example.com',
        subject:       'some subject',
        message_id:    'some@id',
        body:          'some message',
        internal:      false,
        sender:        Ticket::Article::Sender.where(name: 'Customer').first,
        type:          Ticket::Article::Type.where(name: 'email').first,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      expect(ticket1).to be_truthy

      perform_enqueued_jobs commit_transaction: true

      # verify notifications to agent1 + agent2
      expect(NotificationFactory::Mailer.already_sent?(ticket1, agent1, 'email')).to eq(0)
      expect(NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email')).to eq(1)
      expect(NotificationFactory::Mailer.already_sent?(ticket1, agent3, 'email')).to eq(0)
      expect(NotificationFactory::Mailer.already_sent?(ticket1, agent4, 'email')).to eq(0)

      agent2.out_of_office = true
      agent2.preferences[:out_of_office_text] = 'at the doctor'
      agent2.out_of_office_replacement_id = agent3.id
      agent2.out_of_office_start_at = Time.zone.today - 2.days
      agent2.out_of_office_end_at = Time.zone.today + 2.days
      agent2.save!

      # create ticket in group
      ticket2 = Ticket.create!(
        title:         'some notification test out of office',
        group:         group,
        customer:      customer,
        owner_id:      agent2.id,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      Ticket::Article.create!(
        ticket_id:     ticket2.id,
        from:          'some_sender@example.com',
        to:            'some_recipient@example.com',
        subject:       'some subject',
        message_id:    'some@id',
        body:          'some message',
        internal:      false,
        sender:        Ticket::Article::Sender.where(name: 'Customer').first,
        type:          Ticket::Article::Type.where(name: 'email').first,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      expect(ticket2).to be_truthy

      perform_enqueued_jobs commit_transaction: true

      # verify notifications to agent1 + agent2
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(0)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(1)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent3, 'email')).to eq(1)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent4, 'email')).to eq(0)

      # update ticket attributes
      ticket2.title    = "#{ticket2.title} - #2"
      ticket2.priority = Ticket::Priority.lookup(name: '3 high')
      ticket2.save!

      perform_enqueued_jobs commit_transaction: true

      # verify notifications to agent1 + agent2
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(0)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(2)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent3, 'email')).to eq(2)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent4, 'email')).to eq(0)

      agent3.out_of_office = true
      agent3.preferences[:out_of_office_text] = 'at the doctor'
      agent3.out_of_office_replacement_id = agent4.id
      agent3.out_of_office_start_at = Time.zone.today - 2.days
      agent3.out_of_office_end_at = Time.zone.today + 2.days
      agent3.save!

      # update ticket attributes
      ticket2.title    = "#{ticket2.title} - #3"
      ticket2.priority = Ticket::Priority.lookup(name: '3 high')
      ticket2.save!

      perform_enqueued_jobs commit_transaction: true

      # verify notifications to agent1 + agent2
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent1, 'email')).to eq(0)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent2, 'email')).to eq(3)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent3, 'email')).to eq(2)
      expect(NotificationFactory::Mailer.already_sent?(ticket2, agent4, 'email')).to eq(1)
    end

    it 'renders localized notification templates with human-readable changes', :aggregate_failures do # rubocop:disable RSpec/ExampleLength
      # create ticket in group
      ticket1 = Ticket.create!(
        title:         'some notification template test 1 Bobs\'s resumé',
        group:         group,
        customer:      customer,
        state:         Ticket::State.lookup(name: 'new'),
        priority:      Ticket::Priority.lookup(name: '2 normal'),
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      article = Ticket::Article.create!(
        ticket_id:     ticket1.id,
        from:          'some_sender@example.com',
        to:            'some_recipient@example.com',
        subject:       'some subject',
        message_id:    'some@id',
        body:          "some message\nnewline1 abc\nnewline2",
        internal:      false,
        sender:        Ticket::Article::Sender.where(name: 'Customer').first,
        type:          Ticket::Article::Type.where(name: 'email').first,
        updated_by_id: customer.id,
        created_by_id: customer.id,
      )
      expect(ticket1).to be_truthy

      last_changes = {
        'priority_id'  => [1, 2],
        'pending_time' => [nil, Time.zone.parse('2015-01-11 23:33:47 UTC')],
      }

      bg = described_class.new(
        ticket_id:  ticket1.id,
        article_id: article.id,
        type:       'update',
        changes:    last_changes,
        user_id:    ticket1.updated_by_id,
      )

      # check changed attributes
      human_changes = bg.human_changes(last_changes, ticket1, agent2)
      expect(human_changes['Priority']).to be_truthy
      expect(human_changes['Pending till']).to be_truthy
      expect(human_changes['Priority'][0]).to eq('1 low')
      expect(human_changes['Priority'][1]).to eq('2 normal')
      expect(human_changes['Pending till'][0].to_s).to eq('')
      expect(human_changes['Pending till'][1].to_s).to eq('2015-01-11 23:33:47 UTC')
      expect(human_changes['priority_id']).to be_falsey
      expect(human_changes['pending_time']).to be_falsey
      expect(human_changes['pending_till']).to be_falsey

      # en notification
      result = NotificationFactory::Mailer.template(
        locale:   agent2.preferences[:locale],
        timezone: agent2.preferences[:timezone],
        template: 'ticket_update',
        objects:  {
          ticket:    ticket1,
          article:   article,
          recipient: agent2,
          changes:   human_changes,
        },
      )
      expect(result[:subject]).to include("Bobs's resumé")
      expect(result[:body]).to include('Priority')
      expect(result[:body]).to include('1 low')
      expect(result[:body]).to include('2 normal')
      expect(result[:body]).to include('Pending till')
      expect(result[:body]).to include('01/11/2015  7:33 pm (America/St_Lucia)')
      expect(result[:body]).to include('update')
      expect(result[:body]).not_to include('pending_till')
      expect(result[:body]).not_to include('i18n')

      human_changes = bg.human_changes(last_changes, ticket1, agent1)
      expect(human_changes['Priority']).to be_truthy
      expect(human_changes['Pending till']).to be_truthy
      expect(human_changes['Priority'][0]).to eq('1 niedrig')
      expect(human_changes['Priority'][1]).to eq('2 normal')
      expect(human_changes['Pending till'][0].to_s).to eq('')
      expect(human_changes['Pending till'][1].to_s).to eq('2015-01-11 23:33:47 UTC')
      expect(human_changes['priority_id']).to be_falsey
      expect(human_changes['pending_time']).to be_falsey
      expect(human_changes['pending_till']).to be_falsey

      # de & Europe/Berlin notification
      result = NotificationFactory::Mailer.template(
        locale:   agent1.preferences[:locale],
        timezone: agent1.preferences[:timezone],
        template: 'ticket_update',
        objects:  {
          ticket:    ticket1,
          article:   article,
          recipient: agent1,
          changes:   human_changes,
        },
      )

      expect(result[:subject]).to include("Bobs's resumé")
      expect(result[:body]).to include('Priorität')
      expect(result[:body]).to include('1 niedrig')
      expect(result[:body]).to include('2 normal')
      expect(result[:body]).to include('Warten')
      expect(result[:body]).to include('12.01.2015 00:33 (Europe/Berlin)')
      expect(result[:body]).to include('aktualis')
      expect(result[:body]).not_to include('pending_till')
      expect(result[:body]).not_to include('i18n')

      last_changes = {
        title:       ['some notification template test old 1', 'some notification template test 1 #2'],
        priority_id: [2, 3],
      }

      bg = described_class.new(
        ticket_id:  ticket1.id,
        article_id: article.id,
        type:       'update',
        changes:    last_changes,
        user_id:    customer.id,
      )

      # check changed attributes
      human_changes = bg.human_changes(last_changes, ticket1, agent1)
      expect(human_changes['Title']).to be_truthy
      expect(human_changes['Priority']).to be_truthy
      expect(human_changes['Priority'][0]).to eq('2 normal')
      expect(human_changes['Priority'][1]).to eq('3 hoch')
      expect(human_changes['Title'][0]).to eq('some notification template test old 1')
      expect(human_changes['Title'][1]).to eq('some notification template test 1 #2')
      expect(human_changes['priority_id']).to be_falsey
      expect(human_changes['pending_time']).to be_falsey
      expect(human_changes['pending_till']).to be_falsey

      # de notification
      result = NotificationFactory::Mailer.template(
        locale:   agent1.preferences[:locale],
        timezone: agent1.preferences[:timezone],
        template: 'ticket_update',
        objects:  {
          ticket:    ticket1,
          article:   article,
          recipient: agent1,
          changes:   human_changes,
        }
      )

      expect(result[:subject]).to include("Bobs's resumé")
      expect(result[:body]).to include('Titel')
      expect(result[:body]).not_to include('Title')
      expect(result[:body]).to include('some notification template test old 1')
      expect(result[:body]).to include('some notification template test 1 #2')
      expect(result[:body]).to include('Priorität')
      expect(result[:body]).not_to include('Priority')
      expect(result[:body]).to include('3 hoch')
      expect(result[:body]).to include('2 normal')
      expect(result[:body]).to include('aktualisier')

      human_changes = bg.human_changes(last_changes, ticket1, agent2)

      # en notification
      result = NotificationFactory::Mailer.template(
        locale:   agent2.preferences[:locale],
        timezone: agent2.preferences[:timezone],
        template: 'ticket_update',
        objects:  {
          ticket:    ticket1,
          article:   article,
          recipient: agent2,
          changes:   human_changes,
        }
      )

      expect(result[:subject]).to include("Bobs's resumé")
      expect(result[:body]).to include('Title')
      expect(result[:body]).to include('some notification template test old 1')
      expect(result[:body]).to include('some notification template test 1 #2')
      expect(result[:body]).to include('Priority')
      expect(result[:body]).to include('3 high')
      expect(result[:body]).to include('2 normal')
      expect(result[:body]).not_to include('Pending till')
      expect(result[:body]).not_to include('2015-01-11 23:33:47 UTC')
      expect(result[:body]).to include('update')
      expect(result[:body]).not_to include('pending_till')
      expect(result[:body]).not_to include('i18n')

      # en notification
      ticket1.escalation_at = Time.zone.parse('2019-04-01T10:00:00Z')
      result = NotificationFactory::Mailer.template(
        locale:   agent2.preferences[:locale],
        timezone: agent2.preferences[:timezone],
        template: 'ticket_escalation',
        objects:  {
          ticket:    ticket1,
          article:   article,
          recipient: agent2,
        }
      )

      expect(result[:subject]).to include('Escalated ticket (some notification template test 1 Bobs\'s resumé')
      expect(result[:body]).to include('escalated since "04/01/2019  6:00 am (America/St_Lucia)"!')
    end
  end

  def run(ticket, user, type)
    build(ticket, user, type).perform
  end

  def build(ticket, user, type = 'reminder_reached')
    described_class.new(
      object:           ticket.class.name,
      type:             type,
      object_id:        ticket.id,
      interface_handle: 'scheduler',
      changes:          nil,
      created_at:       Time.current,
      user_id:          user.id
    )
  end

  def ooo(instance, user, replacements: Set.new, reasons: [])
    instance.send(:ooo_replacements, user: user, replacements: replacements, ticket: ticket, reasons: reasons)
  end
end
