1843200
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þZ(YÇÌc![ùÂ„ïº×Á_�uÐb&=LyZËcõÁÀ     ˆÄ�þB gÈHàÈFŸÃƒh¹ÂÁÀ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'models/concerns/has_xss_sanitized_note_examples'

RSpec.describe Chat, type: :model do
  it_behaves_like 'HasXssSanitizedNote', model_factory: :chat

  describe 'website allowing' do
    let(:chat) { create(:chat, allowed_websites: 'zammad.org') }

    it 'detects allowed website' do
      result = chat.website_allowed?('https://www.zammad.org')
      expect(result).to be true
    end

    it 'detects non-allowed website' do
      result = chat.website_allowed?('https://www.zammad.com')
      expect(result).to be false
    end

    it 'detects missing website as non-allowed', :aggregate_failures do
      expect(chat.website_allowed?(nil)).to be false
      expect(chat.website_allowed?('')).to be false
    end

    it 'allows missing website without allowed websites' do
      chat.update!(allowed_websites: nil)
      expect(chat.website_allowed?(nil)).to be true
    end
  end

  describe '#destroy' do
    let(:chat) { create(:chat) }
    let(:session) { create(:'chat/session', chat: chat) }

    before do
      session
    end

    it 'does delete chats properly if they have sessions' do
      expect { chat.destroy! }.not_to raise_error
    end
  end

  describe 'customer and agent state' do
    let(:chat)   { create(:chat) }
    let(:agent1) { create(:agent, firstname: 'Notification', lastname: 'Agent1') }
    let(:agent2) { create(:agent, firstname: 'Notification', lastname: 'Agent2') }

    def activate_chat_agent(user, concurrent)
      create(:'chat/agent', concurrent: concurrent, updated_by: user, created_by: user)
    end

    def expect_agent_state(user, expected_state)
      expect(described_class.agent_state_with_sessions(user.id)).to include(expected_state)
    end

    before do
      Setting.set('chat', true)

      [agent1, agent2].each do |agent|
        agent.preferences[:chat] = { active: { chat.id => 'on' } }
        agent.save!
      end
    end

    context 'when the chat feature is disabled' do
      before do
        Setting.set('chat', false)
      end

      it 'reports chat_disabled state to the customer' do
        expect(chat.customer_state[:state]).to eq('chat_disabled')
      end

      it 'reports chat_disabled state to the agent' do
        expect(described_class.agent_state(agent1.id)[:state]).to eq('chat_disabled')
      end
    end

    context 'when no chat agent is active' do
      it 'reports offline state to the customer' do
        expect(chat.customer_state[:state]).to eq('offline')
      end

      it 'reports no seats and no sessions to the agent' do
        expect_agent_state(agent1,
                           waiting_chat_count: 0,
                           running_chat_count: 0,
                           active_sessions:    [],
                           seads_available:    0,
                           seads_total:        0,
                           active:             false)
      end
    end

    context 'with one active chat agent' do
      before do
        activate_chat_agent(agent1, 4)
      end

      it 'reports online state to the customer' do
        expect(chat.customer_state[:state]).to eq('online')
      end

      it 'reports available seats to the agent' do
        expect_agent_state(agent1,
                           waiting_chat_count: 0,
                           running_chat_count: 0,
                           active_sessions:    [],
                           seads_available:    4,
                           seads_total:        4,
                           active:             true)
      end

      context 'with a waiting chat session' do
        let(:chat_session) { create(:'chat/session', chat: chat, user: agent1) }

        before do
          chat_session
        end

        it 'generates a session id' do
          expect(chat_session.session_id).to be_present
        end

        it 'reports online state t