disabled
  5   ˆl–Ãa¾”J_)â€vãn\jbÑ¿�þB g	Ëet£?ÏÃƒh¹ÂÁÀ      disabled
  /   ˆ{‹„„-i[D<†ªoZƒ›Ù«Ä441_‘I|¥‰ÓMdœv ©ƒ†ü+=ÄÃ ¹    ‹      •“_oÛ Åßû)îxØ[|S'Ýª3uv¤EÊ?¥ž¦iš&lˆ�jC´^÷égÇ©šV‰º=Ãï\Î¹ }—,ãôûj_ÒùV_?Ï¦1�â·QŒ˜¤I¿0†çˆ“Rz¿½Blš&hF�±¦k,}]�Ñy«r/;£Ý;ZJ.Ú/P¯|%ÙTùÌÐÜ{§„c¥2,x%-§ØC­÷:šñØ•;KÚ»mx¶«Ð-£¾„^©BGÄ›-aTÕ8›GUn´Ã¬âú.(Ô† ¯|D~LãåÏ–C_vjF9”Vn"ò)Ž×Ëè†°¯%E~œ™÷ÌŒ;µj£¤8	ßöð­úsº`Ò3‰t¹U[¯Œ~F±�x57•Ûò6ëE› ´‡T�ˆ·ºÁóÍXÝ¬'‹4™®û–ˆ®Ä³»—AØŠ[©=$ÊÊÜû¸7Ú«ÞëÌm¯Ÿ~aoÃª¢ô„à(ùÞïõ�6�>´ß¶æ¨ñÖnm~¹ºœ!ìõÌ¡õW^Ãax1^ÂŽ®ÆN�A8?ê�÷šâî%PìÖ_¶½nR»    5   	ˆl–Ãa¾”J_)â€rãmÜiå1hß�þB g	nHÝ+ägùÇƒh¹ÆÅÄ      	disabled
  5   ˆl–Ãa¾”J_)â€vãLÜ TÅ£�þB g	m–J?çÈƒh¹ÇÆÅ      disabled
     ˆ¾�þB g	m–J?çÈƒh¹ÇÆÅ      disabled
  D   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þBËÑ¬ã�f1�­ŠQ�ùÉƒl gÈ_�uÐb&=LyZËcõÈÇ ‹    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'Ticket: #auto_assign' do # rubocop:disable RSpec/DescribeClass
  before do
    Setting.set('ticket_auto_assignment', ticket_auto_assignment)
    Setting.set('ticket_auto_assignment_selector', { condition: ticket_auto_assignment_condition })
  end

  context 'when auto assignment is enabled' do
    let(:ticket_auto_assignment) { true }

    context 'when conditions with states are used' do
      let(:ticket) { create(:ticket, group: Group.first, state: Ticket::State.find_by(name: 'closed')) }
      let(:agent)  { create(:agent, groups: [Group.first]) }

      context 'when the condition does match' do
        let(:ticket_auto_assignment_condition) { { 'ticket.state_id' => { operator: 'is', value: Ticket::State.pluck(:id) } } }

        it 'does auto assign' do
          ticket.auto_assign(agent)
          expect(ticket.reload.owner_id).to eq(agent.id)
        end
      end

      context 'when the condition does not match' do
        let(:ticket_auto_assignment_condition) { { 'ticket.state_id' => { operator: 'is', value: Ticket::State.by_category_ids(:work_on) } } }

        it 'does not auto assign' do
          ticket.auto_assign(agent)
          expect(ticket.reload.owner_id).to eq(1)
        end
      end
    end

    context 'when "ticket_auto_assignment_user_ids_ignore" is set' do
      let(:ticket)                           { create(:ticket, group: Group.first) }
      let(:agent)                            { create(:agent, groups: [Group.first]) }
      let(:ticket_auto_assignment_condition) { { 'ticket.state_id' => { operator: 'is', value: Ticket::State.pluck(:id) } } }

      before do
        Setting.set('ticket_auto_assignment_user_ids_ignore', user_ids_ignore.call(agent))
      end

      {
        'an integer'          => ->(agent) { agent.id },
        'a string'            => ->(agent) { agent.id.to_s },
        'an array of integer' => ->(agent) { [agent.id] },
        'an array of string'  => ->(agent) { [agent.id.to_s] },
      }.each do |format, value|
        context "with the user id as #{format}" do
          let(:user_ids_ignore) { value }

          it 'does not auto assign' do
            ticket.auto_assign(agent)
            expect(ticket.reload.owner_id).to eq(1)
          end
        end
      end

      context 'with other user ids' do
        let(:user_ids_ignore) { ->(_agent) { [99_999, 999_999] } }

        it 'does auto assign' do
          ticket.auto_assign(agent)
          expect(ticket.reload.owner_id).to eq(agent.id)
        end
      end
    end

    context 'when conditions with title are used' do
      let(:ticket) { create(:ticket, group: Group.first, title: 'Welcome to Zammad') }
      let(:agent)  { create(:agent, groups: [Group.first]) }

      context 'when the condition does match' do
        let(:ticket_auto_assignment_condition) { { 'ticket.title' => { operator: 'matches regex', value: '^welcome' } } }

        it 'does auto assign' do
          ticket.auto_assign(agent)
          expect(ticket.reload.owner_id).to eq(agent.id)
        end
      end

      context 'when the cond