ticket_waiting_time_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. require 'rails_helper'
  2. RSpec.describe Stats::TicketWaitingTime do
  3. describe '.generate' do
  4. let(:user) { create(:agent_user, groups: [group]) }
  5. let(:group) { create(:group) }
  6. context 'when given an agent with no tickets' do
  7. it 'returns a hash with 1-day average ticket wait time for user (in minutes)' do
  8. expect(described_class.generate(user)).to include(handling_time: 0)
  9. end
  10. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  11. expect(described_class.generate(user)).to include(average_per_agent: 0)
  12. end
  13. it 'returns a hash with verbal grade for average ticket wait time' do
  14. expect(described_class.generate(user)).to include(state: 'supergood')
  15. end
  16. it 'returns a hash with decimal score (0–1) of user’s risk of falling to a lower grade' do
  17. expect(described_class.generate(user)).to include(percent: 0.0)
  18. end
  19. context 'and who belongs to a group with other tickets' do
  20. let(:ticket) { create(:ticket, group: group) }
  21. before do
  22. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: Time.current + 1.hour)
  23. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: Time.current + 2.hours)
  24. end
  25. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  26. expect(described_class.generate(user)).to include(average_per_agent: 60)
  27. end
  28. end
  29. end
  30. context 'when given an agent with recent (since start-of-day) customer exchanges' do
  31. let(:ticket) { create(:ticket, group: group, owner_id: user.id) }
  32. before do
  33. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: Time.current + 1.hour)
  34. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: Time.current + 2.hours)
  35. end
  36. it 'returns a hash with 1-day average ticket wait time for user (in minutes)' do
  37. expect(described_class.generate(user)).to include(handling_time: 60)
  38. end
  39. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  40. expect(described_class.generate(user)).to include(average_per_agent: 60)
  41. end
  42. it 'returns a hash with verbal grade for average ticket wait time' do
  43. expect(described_class.generate(user)).to include(state: 'supergood')
  44. end
  45. it 'returns a hash with decimal score (0–1) of user’s risk of falling to a lower grade' do
  46. expect(described_class.generate(user)).to include(percent: 1.0)
  47. end
  48. context 'and who belongs to a group with other tickets' do
  49. let(:other_ticket) { create(:ticket, group: group) }
  50. before do
  51. create(:ticket_article, sender_name: 'Customer', ticket: other_ticket, created_at: Time.current + 1.hour)
  52. create(:ticket_article, sender_name: 'Agent', ticket: other_ticket, created_at: Time.current + 3.hours)
  53. end
  54. it 'returns a hash with 1-day average ticket wait time across user’s groups (in minutes)' do
  55. expect(described_class.generate(user)).to include(average_per_agent: 90)
  56. end
  57. end
  58. end
  59. end
  60. describe '.calculate_average' do
  61. let(:ticket) { create(:ticket) }
  62. let(:start_time) { Time.current.beginning_of_day }
  63. context 'with empty tickets (no articles)' do
  64. it 'returns 0' do
  65. expect(described_class.calculate_average(ticket.id, start_time)).to eq(0)
  66. end
  67. end
  68. context 'with old articles (last message predates given start time)' do
  69. before do
  70. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: 1.day.ago)
  71. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: 1.day.ago)
  72. end
  73. it 'returns 0' do
  74. expect(described_class.calculate_average(ticket.id, start_time)).to eq(0)
  75. end
  76. end
  77. context 'with a single exchange' do
  78. before do
  79. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  80. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes)
  81. end
  82. it 'returns elapsed time' do
  83. expect(described_class.calculate_average(ticket.id, start_time)).to eq(1.minute)
  84. end
  85. end
  86. context 'with internal notes' do
  87. before do
  88. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  89. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes, internal: true)
  90. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 3.minutes)
  91. end
  92. it 'ignores them (and measures time to actual response)' do
  93. expect(described_class.calculate_average(ticket.id, start_time)).to eq(2.minutes)
  94. end
  95. end
  96. context 'with multiple exchanges' do
  97. before do
  98. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  99. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes)
  100. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 10.minutes)
  101. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 15.minutes)
  102. end
  103. it 'returns average of elapsed times' do
  104. expect(described_class.calculate_average(ticket.id, start_time)).to eq(3.minutes)
  105. end
  106. end
  107. context 'with all above cases combined' do
  108. before do
  109. # empty ticket
  110. create(:ticket)
  111. # old messages
  112. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: 1.day.ago)
  113. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: 1.day.ago)
  114. # first exchange, with internal notes
  115. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 1.minute)
  116. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 90.seconds, internal: true)
  117. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 2.minutes)
  118. # second exchange
  119. create(:ticket_article, sender_name: 'Customer', ticket: ticket, created_at: start_time + 10.minutes)
  120. create(:ticket_article, sender_name: 'Agent', ticket: ticket, created_at: start_time + 15.minutes)
  121. end
  122. it 'ignores all edge cases and returns only specified average response time' do
  123. expect(described_class.calculate_average(ticket.id, start_time)).to eq(3.minutes)
  124. end
  125. end
  126. end
  127. end