test(reports): stabilize report specs

This commit is contained in:
Sojan Jose
2026-01-24 00:57:08 -08:00
parent 0dadaccb9e
commit eadd831c95
3 changed files with 60 additions and 63 deletions
+42 -41
View File
@@ -10,50 +10,51 @@ describe V2::ReportBuilder do
# Use before_all to share expensive setup across all tests in this describe block
# This runs once instead of 21 times, dramatically speeding up the suite
before_all do
travel_to(Time.zone.today) do
user = create(:user, account: account)
inbox = create(:inbox, account: account)
create(:inbox_member, user: user, inbox: inbox)
RSpec::Mocks.with_temporary_scope do
allow(ActionCableBroadcastJob).to receive(:perform_later)
allow(ActionCableBroadcastJob).to receive(:perform_now)
gravatar_url = 'https://www.gravatar.com'
stub_request(:get, /#{gravatar_url}.*/).to_return(status: 404)
travel_to(Time.zone.today) do
user = create(:user, account: account)
inbox = create(:inbox, account: account)
create(:inbox_member, user: user, inbox: inbox)
perform_enqueued_jobs do
10.times do
conversation = create(:conversation, account: account,
inbox: inbox,
created_at: Time.zone.today)
conversation.reload
conversation.update!(assignee_id: user.id)
create_list(:message, 5, message_type: 'outgoing',
account: account, inbox: inbox,
conversation: conversation, created_at: Time.zone.today + 2.hours)
create_list(:message, 2, message_type: 'incoming',
account: account, inbox: inbox,
conversation: conversation,
created_at: Time.zone.today + 3.hours)
conversation.update_labels('label_1')
conversation.label_list
conversation.save!
end
gravatar_url = 'https://www.gravatar.com'
stub_request(:get, /#{gravatar_url}.*/).to_return(status: 404)
5.times do
conversation = create(:conversation, account: account,
inbox: inbox,
created_at: (Time.zone.today - 2.days))
conversation.reload
conversation.update!(assignee_id: user.id)
create_list(:message, 3, message_type: 'outgoing',
account: account, inbox: inbox,
conversation: conversation,
created_at: (Time.zone.today - 2.days))
create_list(:message, 1, message_type: 'incoming',
account: account, inbox: inbox,
conversation: conversation,
created_at: (Time.zone.today - 2.days))
conversation.update_labels('label_2')
conversation.label_list
conversation.save!
perform_enqueued_jobs do
10.times do
conversation = create(:conversation, account: account,
inbox: inbox, assignee: user,
created_at: Time.zone.today)
create_list(:message, 5, message_type: 'outgoing',
account: account, inbox: inbox,
conversation: conversation, created_at: Time.zone.today + 2.hours)
create_list(:message, 2, message_type: 'incoming',
account: account, inbox: inbox,
conversation: conversation,
created_at: Time.zone.today + 3.hours)
conversation.update_labels('label_1')
conversation.label_list
conversation.save!
end
5.times do
conversation = create(:conversation, account: account,
inbox: inbox, assignee: user,
created_at: (Time.zone.today - 2.days))
create_list(:message, 3, message_type: 'outgoing',
account: account, inbox: inbox,
conversation: conversation,
created_at: (Time.zone.today - 2.days))
create_list(:message, 1, message_type: 'incoming',
account: account, inbox: inbox,
conversation: conversation,
created_at: (Time.zone.today - 2.days))
conversation.update_labels('label_2')
conversation.label_list
conversation.save!
end
end
end
end
@@ -8,6 +8,11 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
let_it_be(:label_2) { create(:label, title: 'label_2', account: account) }
let_it_be(:label_3) { create(:label, title: 'label_3', account: account) }
before do
allow(ActionCableBroadcastJob).to receive(:perform_later)
allow(ActionCableBroadcastJob).to receive(:perform_now)
end
let(:params) do
{
business_hours: business_hours,
@@ -92,10 +97,8 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
# Create conversations with label_1
3.times do
conversation = create(:conversation, account: account,
inbox: inbox,
inbox: inbox, assignee: user,
created_at: Time.zone.today)
conversation.reload
conversation.update!(assignee_id: user.id)
create_list(:message, 2, message_type: 'outgoing',
account: account, inbox: inbox,
conversation: conversation,
@@ -112,10 +115,8 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
# Create conversations with label_2
2.times do
conversation = create(:conversation, account: account,
inbox: inbox,
inbox: inbox, assignee: user,
created_at: Time.zone.today)
conversation.reload
conversation.update!(assignee_id: user.id)
create_list(:message, 1, message_type: 'outgoing',
account: account, inbox: inbox,
conversation: conversation,
@@ -233,10 +234,8 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
perform_enqueued_jobs do
# Conversation within range
conversation_in_range = create(:conversation, account: account,
inbox: inbox,
inbox: inbox, assignee: user,
created_at: 2.days.ago)
conversation_in_range.reload
conversation_in_range.update!(assignee_id: user.id)
conversation_in_range.update_labels('label_1')
conversation_in_range.label_list
conversation_in_range.save!
@@ -250,10 +249,8 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
# Conversation outside range (too old)
conversation_out_of_range = create(:conversation, account: account,
inbox: inbox,
inbox: inbox, assignee: user,
created_at: 1.week.ago)
conversation_out_of_range.reload
conversation_out_of_range.update!(assignee_id: user.id)
conversation_out_of_range.update_labels('label_1')
conversation_out_of_range.label_list
conversation_out_of_range.save!
@@ -294,10 +291,8 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
perform_enqueued_jobs do
conversation = create(:conversation, account: account,
inbox: inbox,
inbox: inbox, assignee: user,
created_at: Time.zone.today)
conversation.reload
conversation.update!(assignee_id: user.id)
conversation.update_labels('label_1')
conversation.label_list
conversation.save!
@@ -353,14 +348,13 @@ RSpec.describe V2::Reports::LabelSummaryBuilder do
perform_enqueued_jobs do
conversation = create(:conversation, account: account2,
inbox: inbox,
inbox: inbox, assignee: user,
created_at: test_date)
conversation.reload
conversation.update!(assignee_id: user.id)
conversation.update_labels(unique_label_name)
conversation.label_list
conversation.save!
# Create reporting events directly to avoid after_commit ordering issues in tests.
resolved_at = Time.zone.local(test_date.year, test_date.month, test_date.day, 10, 0, 0)
create(:reporting_event,
account: account2,
@@ -1,9 +1,11 @@
# frozen_string_literal: true
RSpec.shared_context 'with smtp config' do
around do |example|
# Set SMTP_ADDRESS so mailers build a Mail::Message in test without touching real SMTP.
# Scoped to this shared context to avoid affecting other specs.
with_modified_env('SMTP_ADDRESS' => 'smtp.example.com') { example.run }
before do
# We need to use allow_any_instance_of here because smtp_config_set_or_development?
# is defined in ApplicationMailer and needs to be stubbed for all mailer instances
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(ApplicationMailer).to receive(:smtp_config_set_or_development?).and_return(true)
# rubocop:enable RSpec/AnyInstance
end
end