disabled
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þ\#YÇÌc!Z�%ùÎ„¬²ÿÍ_�uÐb&=LyZËcõÄÌ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Escalation do
  let(:instance)              { described_class.new ticket, force: force }
  let(:instance_with_history) { described_class.new ticket_with_history, force: force }
  let(:instance_with_open)    { described_class.new open_ticket_with_history, force: force }

  let(:ticket)   { create(:ticket) }
  let(:force)    { false }
  let(:calendar) { create(:calendar, :'24/7') }

  let(:sla_247)          { create(:sla, :condition_blank, solution_time: 75, calendar: calendar) }
  let(:sla_247_response) { create(:sla, :condition_blank, first_response_time: 30, response_time: 45, solution_time: 75, calendar: calendar) }
  let(:sla_247_update)   { create(:sla, :condition_blank, first_response_time: 30, update_time: 60, solution_time: 75, calendar: calendar) }

  let(:ticket_with_history) do
    freeze_time
    ticket = create(:ticket)
    ticket.update! state: Ticket::State.lookup(name: 'new')
    travel 1.hour
    ticket.update! state: Ticket::State.lookup(name: 'open')
    travel 30.minutes
    ticket.update! state: Ticket::State.lookup(name: 'pending close')
    travel 30.minutes
    ticket.update! state: Ticket::State.lookup(name: 'closed'), close_at: Time.current
    ticket
  end

  let(:open_ticket_with_history) do
    freeze_time
    article = create(:ticket_article, :inbound_email)

    travel 10.minutes
    article.ticket.update! state: Ticket::State.lookup(name: 'pending close')
    travel 10.minutes
    article.ticket.update! state: Ticket::State.lookup(name: 'open')

    article.ticket
  end

  before do
    travel_to '2025-07-30 12:00'
  end

  describe '#preferences' do
    it { expect(instance.preferences).to be_a Escalation::TicketPreferences }
  end

  describe '#escalation_disabled?' do
    it 'true when ticket is not open' do
      ticket.update! state: Ticket::State.lookup(name: 'pending close')
      expect(instance).to be_escalation_disabled
    end

    it 'false when ticket is open' do
      expect(instance).not_to be_escalation_disabled
    end
  end

  describe '#calculatable?' do
    it 'false when ticket is not open' do
      ticket.update! state: Ticket::State.lookup(name: 'pending close')
      expect(instance).not_to be_calculatable
    end

    it 'true when ticket is open' do
      expect(instance).to be_calculatable
    end

    # https://github.com/zammad/zammad/issues/2579
    it 'true when ticket was just closed' do
      ticket
      travel 30.minutes

      without_update_escalation_information_callback { ticket.update close_at: Time.current, state: Ticket::State.lookup(name: 'closed') }

      expect(instance).to be_calculatable
    end

    it 'true when response to ticket comes while ticket has pending reminder' do
      ticket.update(state: Ticket::State.find_by(name: 'pending reminder'))

      without_update_escalation_information_callback { create(:'ticket/article', :outbound_email, ticket: ticket) }

      expect(instance).to be_calculatable
    end
  end

  describe '#calculate' do
    it 'works and updates' do
      ticket
      sla_247
      expect { instance.calculate }.to change(ticket, :has_changes_to_save?).to(true)
    end

    it 'exit early when escalation is disabled' do
      allow(instance).to receive(:escalation_disabled?).and_return(true)
      allow(instance).to receive(:calendar) # next method called after checking escalation state
      instance.calculate
      expect(instance).not_to have_received(:calendar)
    end

    it 'recalculate when escalation is disabled but it is forced' do
      instance_forced = described_class.new ticket, force: true
      allow(instance_forced).to receive(:escalation_disabled?).and_return(true)
      allow(instance_forced).to receive(:calendar) # next method called after checking escalation state
      instance_forced.calculate
      expect(instance_forced).to have_received(:calendar)
    end

    it 'no calendar is ear