period_working_minutes_spec.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Escalation::PeriodWorkingMinutes do
  4. let(:instance) { described_class.new start_time, end_time, ticket, biz }
  5. let(:start_time) { 1.week.ago }
  6. let(:end_time) { 1.day.ago }
  7. let(:ticket) { create(:ticket) }
  8. let(:calendar) { create(:calendar, :'24/7') }
  9. let(:biz) { calendar.biz breaks: Escalation::TicketBizBreak.new(ticket, calendar).biz_breaks }
  10. describe '#period_working_minutes' do
  11. subject(:result) { instance.send(:period_working_minutes) }
  12. before do
  13. freeze_time
  14. ticket.update! state: Ticket::State.lookup(name: 'new')
  15. travel 1.hour
  16. ticket.update! state: Ticket::State.lookup(name: 'open')
  17. travel 30.minutes
  18. ticket.update! state: Ticket::State.lookup(name: 'pending close')
  19. travel 30.minutes
  20. ticket.update! state: Ticket::State.lookup(name: 'closed'), close_at: Time.current
  21. end
  22. context 'when timeframe takes whole ticket life' do
  23. let(:start_time) { ticket.created_at }
  24. let(:end_time) { ticket.close_at + 2.hours }
  25. it { is_expected.to be 90 }
  26. end
  27. context 'when timeframe starts before and ends after ticket life' do
  28. let(:start_time) { ticket.created_at - 1.week }
  29. let(:end_time) { ticket.close_at + 1.week }
  30. it { is_expected.to be 90 }
  31. end
  32. context 'when timeframe starts in the middle of ticket life' do
  33. let(:start_time) { ticket.created_at + 30.minutes }
  34. let(:end_time) { ticket.close_at }
  35. it { is_expected.to be 60 }
  36. end
  37. context 'when timeframe end in the middle of ticket life' do
  38. let(:start_time) { ticket.created_at }
  39. let(:end_time) { ticket.created_at + 30.minutes }
  40. it { is_expected.to be 30 }
  41. end
  42. end
  43. describe '#timeframe_start' do
  44. subject(:result) { instance.send(:timeframe_start) }
  45. context 'when start_time is early' do
  46. it { is_expected.to eq ticket.created_at }
  47. end
  48. context 'when start_time is later than #created_at' do
  49. let(:start_time) { 1.week.from_now }
  50. it { is_expected.to eq start_time }
  51. end
  52. end
  53. describe '#timeframe_end' do
  54. subject(:result) { instance.send(:timeframe_end) }
  55. context 'when end_time is late' do
  56. let(:end_time) { 1.week.from_now }
  57. before do
  58. ticket.update! close_at: 1.day.from_now
  59. end
  60. it { is_expected.to eq ticket.close_at }
  61. end
  62. context 'when end_time is before closing' do
  63. let(:end_time) { 1.week.from_now }
  64. before do
  65. ticket.update! close_at: 2.weeks.from_now
  66. end
  67. it { is_expected.to eq end_time }
  68. end
  69. context 'when #close_at is not set' do
  70. let(:end_time) { 1.week.ago }
  71. it { is_expected.to eq end_time }
  72. end
  73. end
  74. end