ticket_biz_break_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. require 'rails_helper'
  2. RSpec.describe Escalation::TicketBizBreak, time_zone: 'Europe/Berlin' do
  3. let(:ticket) { create(:ticket) }
  4. let(:calendar) { create(:calendar) }
  5. let(:instance) { described_class.new(ticket, calendar) }
  6. describe '#biz_breaks' do
  7. let(:result) { instance.biz_breaks }
  8. context 'when ticket history is empty' do
  9. it { expect(result).to be_a(Hash) }
  10. it { expect(result).to be_empty }
  11. end
  12. context 'when ticket is opened' do
  13. before do
  14. travel 15.minutes
  15. ticket.update! state: Ticket::State.lookup(name: 'open')
  16. end
  17. it { expect(result).to be_a(Hash) }
  18. it { expect(result).to be_empty }
  19. end
  20. context 'when ticket was opened and closed' do
  21. before do
  22. travel 15.minutes
  23. ticket.update! state: Ticket::State.lookup(name: 'open')
  24. travel 15.minutes
  25. ticket.update! state: Ticket::State.lookup(name: 'closed')
  26. end
  27. it { expect(result).to be_a(Hash) }
  28. it { expect(result).to be_empty }
  29. end
  30. context 'when ticket was started in non-escalated state and closed' do
  31. let(:ticket) { create(:ticket, state: Ticket::State.lookup(name: 'pending reminder')) }
  32. before do
  33. travel_to Time.current.noon
  34. ticket
  35. travel 15.minutes
  36. ticket.update! state: Ticket::State.lookup(name: 'closed')
  37. end
  38. it { expect(result).to be_a(Hash) }
  39. it { expect(result).to be_one }
  40. end
  41. context 'when ticket was suspended and reopened multiple times' do
  42. before do
  43. travel_to Time.current.noon
  44. ticket.update! state: Ticket::State.lookup(name: 'open')
  45. travel 15.minutes
  46. ticket.update! state: Ticket::State.lookup(name: 'pending reminder')
  47. travel 15.minutes
  48. ticket.update! state: Ticket::State.lookup(name: 'open')
  49. travel 15.minutes
  50. ticket.update! state: Ticket::State.lookup(name: 'pending close')
  51. travel 15.minutes
  52. ticket.update! state: Ticket::State.lookup(name: 'closed')
  53. end
  54. let(:first_value) { result.values.first }
  55. it { expect(result).to be_a(Hash) }
  56. it { expect(result.keys).to be_one }
  57. it { expect(result.keys.first).to be_a(Date) }
  58. it { expect(result.keys.first).to eq(Time.current.to_date) }
  59. it { expect(first_value).to be_a(Hash) }
  60. it { expect(first_value.keys).to eq %w[12:15 12:45] }
  61. it { expect(first_value['12:15']).to eq '12:30' }
  62. it { expect(first_value['12:45']).to eq '13:00' }
  63. end
  64. context 'when ticket was suspended over midnight in UTC', time_zone: 'UTC' do
  65. before do
  66. travel_to Time.current.change(month: 11).utc.midnight
  67. travel(-15.minutes)
  68. ticket.update! state: Ticket::State.lookup(name: 'pending reminder')
  69. travel 30.minutes
  70. ticket.update! state: Ticket::State.lookup(name: 'open')
  71. end
  72. let(:first_value) { result.values.first }
  73. it { expect(result.keys).to be_one }
  74. it { expect(result.keys.first).to be_a(Date) }
  75. it { expect(result.keys.first).to eq(Time.current.to_date) }
  76. it { expect(first_value).to be_a(Hash) }
  77. it { expect(first_value.keys).to eq %w[00:45] }
  78. it { expect(first_value['00:45']).to eq '01:15' }
  79. end
  80. context 'when ticket was suspended over midnight in timezone' do
  81. before do
  82. travel_to Time.current.midnight
  83. travel(-15.minutes)
  84. ticket.update! state: Ticket::State.lookup(name: 'pending reminder')
  85. travel 1.hour
  86. ticket.update! state: Ticket::State.lookup(name: 'open')
  87. end
  88. let(:first_value) { result.values.first }
  89. let(:second_value) { result.values.second }
  90. it { expect(result.keys.count).to be(2) }
  91. it { expect(result.keys).to eq [Time.current.yesterday.to_date, Time.current.to_date] }
  92. it { expect(first_value).to be_a(Hash) }
  93. it { expect(first_value.keys).to eq %w[23:45] }
  94. it { expect(first_value['23:45']).to eq '24:00' }
  95. it { expect(second_value).to be_a(Hash) }
  96. it { expect(second_value.keys).to eq %w[00:00] }
  97. it { expect(second_value['00:00']).to eq '00:45' }
  98. end
  99. context 'when ticket was suspended for multiple days' do
  100. before do
  101. travel_to Time.current.noon
  102. ticket.update! state: Ticket::State.lookup(name: 'pending reminder')
  103. travel 5.days
  104. ticket.update! state: Ticket::State.lookup(name: 'open')
  105. end
  106. let(:first_value) { result.values.first }
  107. let(:second_value) { result.values.second }
  108. it { expect(result.keys.count).to be(6) }
  109. it { expect(result.keys).to eq ((Time.current - 5.days).to_date..Time.current).to_a }
  110. it { expect(result.values[0].keys).to eq %w[12:00] }
  111. it { expect(result.values[0]['12:00']).to eq '24:00' }
  112. it { expect(result.values[1].keys).to eq %w[00:00] }
  113. it { expect(result.values[1]['00:00']).to eq '24:00' }
  114. it { expect(result.values[2].keys).to eq %w[00:00] }
  115. it { expect(result.values[2]['00:00']).to eq '24:00' }
  116. it { expect(result.values[3].keys).to eq %w[00:00] }
  117. it { expect(result.values[3]['00:00']).to eq '24:00' }
  118. it { expect(result.values[4].keys).to eq %w[00:00] }
  119. it { expect(result.values[4]['00:00']).to eq '24:00' }
  120. it { expect(result.values[5].keys).to eq %w[00:00] }
  121. it { expect(result.values[5]['00:00']).to eq '12:00' }
  122. end
  123. end
  124. describe '#history_list_states' do
  125. let(:result) { instance.send(:history_list_states) }
  126. it 'empty when history log is empty' do
  127. expect(result).to be_empty
  128. end
  129. it 'empty when history log has non-state records' do
  130. ticket.update! title: '2nd title'
  131. expect(result).to be_empty
  132. end
  133. it 'returns array of Hashes when history log has various records' do
  134. ticket.update! title: '2nd title', state: Ticket::State.lookup(name: 'open')
  135. expect(result.first).to be_a Hash
  136. end
  137. it 'lists changes in specific order when history log has various records' do
  138. ticket.update! title: 'title', state: Ticket::State.lookup(name: 'open')
  139. ticket.update! title: 'another title'
  140. ticket.update! state: Ticket::State.lookup(name: 'closed')
  141. expect(result.pluck('value_to')).to eq %w[open closed]
  142. end
  143. end
  144. describe '#ignored_escalation_state_names' do
  145. let(:result) { instance.send(:ignored_escalation_state_names) }
  146. it { expect(result).to be_a Array }
  147. it { expect(result).to be_all String }
  148. it { expect(result).to include 'closed' }
  149. it { expect(result).not_to include 'open' }
  150. end
  151. describe '#history_list_in_break' do
  152. let(:result) { instance.send(:history_list_in_break) }
  153. it { expect(result).to be_a Array }
  154. it 'empty history returns minutes in timeframe' do
  155. expect(result).to be_empty
  156. end
  157. context 'when contains 4 history points' do
  158. before do
  159. allow(instance).to receive(:history_list_states).and_return(history_list_4)
  160. end
  161. let(:history_list_4) do
  162. [
  163. mock_state_hash(ticket.created_at, nil, 'new'),
  164. mock_state_hash(ticket.created_at + 1.hour, 'new', 'open'),
  165. mock_state_hash(ticket.created_at + 90.minutes, 'open', 'pending close'),
  166. mock_state_hash(ticket.created_at + 2.hours, 'pending close', 'closed')
  167. ]
  168. end
  169. it 'returns one range' do
  170. expect(result).to be_one
  171. end
  172. it 'returns range from pending close' do
  173. expect(result.first.first['value_to']).to eq 'pending close'
  174. end
  175. it 'returns range to closed' do
  176. expect(result.first.second['value_to']).to eq 'closed'
  177. end
  178. it 'calls #range_on_break? thrice' do
  179. allow(instance).to receive(:range_on_break?)
  180. result
  181. expect(instance).to have_received(:range_on_break?).exactly(3).times
  182. end
  183. end
  184. end
  185. describe '#accumulate_breaks' do
  186. let(:input_a) { { Time.current.to_date => { '10:00' => '14:00' }, Time.current.tomorrow.to_date => { '10:00' => '14:05' } } }
  187. let(:input_b) { { Time.current.to_date => { '17:00' => '18:00' } } }
  188. let(:result) { instance.send(:accumulate_breaks, [input_a, input_b]) }
  189. it { expect(result.keys).to eq [Time.current.to_date, Time.current.tomorrow.to_date] }
  190. it { expect(result[Time.current.to_date]).to eq({ '10:00' => '14:00', '17:00' => '18:00' }) }
  191. it { expect(result[Time.current.tomorrow.to_date]).to eq({ '10:00' => '14:05' }) }
  192. end
  193. describe '#history_range_to_breaks' do
  194. before { travel_to Time.current.noon }
  195. let(:result) { instance.send(:history_range_to_breaks, history_from, history_to) }
  196. context 'when fits in a single day' do
  197. let(:history_from) { mock_state_hash(ticket.created_at + 90.minutes, 'open', 'pending close') }
  198. let(:history_to) { mock_state_hash(ticket.created_at + 2.hours, 'pending close', 'closed') }
  199. it { expect(result).to be_a Hash }
  200. it { expect(result.keys).to eq [Time.current.to_date] }
  201. it { expect(result.values.first).to eq({ '13:30' => '14:00' }) }
  202. end
  203. context 'when spills over to multiple days' do
  204. let(:history_from) { mock_state_hash(ticket.created_at + 90.minutes, 'open', 'pending close') }
  205. let(:history_to) { mock_state_hash(ticket.created_at + 2.days, 'pending close', 'closed') }
  206. it { expect(result).to be_a Hash }
  207. it { expect(result.keys).to eq [Time.current.to_date, Time.current.tomorrow.to_date, (Time.current + 2.days).to_date] }
  208. it { expect(result.values.first).to eq({ '13:30' => '24:00' }) }
  209. it { expect(result.values.second).to eq({ '00:00' => '24:00' }) }
  210. it { expect(result.values.third).to eq({ '00:00' => '12:00' }) }
  211. end
  212. end
  213. describe '#mock_initial_state' do
  214. let(:result) { instance.send(:mock_initial_state) }
  215. it { expect(result).to have_key('created_at').and(have_key('value_to')) }
  216. context 'when ticket has no history' do
  217. it { expect(result).to include('created_at' => ticket.created_at) }
  218. it { expect(result).to include('value_to' => ticket.state.name) }
  219. end
  220. shared_context 'when ticket has state changes' do
  221. let(:initial_state_name) { 'pending reminder' }
  222. let(:ticket) { create :ticket, state: Ticket::State.lookup(name: initial_state_name) }
  223. before do
  224. freeze_time
  225. ticket
  226. travel timespan
  227. ticket.update! state: Ticket::State.lookup(name: 'closed')
  228. end
  229. end
  230. context 'when ticket has state changes later' do
  231. let(:timespan) { 30.minutes }
  232. include_examples 'when ticket has state changes'
  233. it { expect(result).to include('created_at' => ticket.created_at) }
  234. it { expect(result).to include('value_to' => initial_state_name) }
  235. end
  236. context 'when ticket has state changes at creation' do
  237. let(:timespan) { 0.minutes }
  238. include_examples 'when ticket has state changes'
  239. it { expect(result).to be_nil }
  240. end
  241. end
  242. def mock_state_hash(time, from, to)
  243. {
  244. 'attribute' => 'state',
  245. 'created_at' => time,
  246. 'value_from' => from,
  247. 'value_to' => to
  248. }
  249. end
  250. end