mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 19:17:48 +00:00 
			
		
		
		
	chore: fix flaky spec (#7686)
- fix flaky reporting specs - fix flaky export spec
This commit is contained in:
		| @@ -9,46 +9,48 @@ describe V2::ReportBuilder do | |||||||
|   # Update this spec to use travel_to |   # Update this spec to use travel_to | ||||||
|   # This spec breaks in certain timezone |   # This spec breaks in certain timezone | ||||||
|   describe '#timeseries' do |   describe '#timeseries' do | ||||||
|     before_all do |     before do | ||||||
|       user = create(:user, account: account) |       travel_to(Time.zone.today) do | ||||||
|       inbox = create(:inbox, account: account) |         user = create(:user, account: account) | ||||||
|       create(:inbox_member, user: user, inbox: inbox) |         inbox = create(:inbox, account: account) | ||||||
|  |         create(:inbox_member, user: user, inbox: inbox) | ||||||
|  |  | ||||||
|       gravatar_url = 'https://www.gravatar.com' |         gravatar_url = 'https://www.gravatar.com' | ||||||
|       stub_request(:get, /#{gravatar_url}.*/).to_return(status: 404) |         stub_request(:get, /#{gravatar_url}.*/).to_return(status: 404) | ||||||
|  |  | ||||||
|       perform_enqueued_jobs do |         perform_enqueued_jobs do | ||||||
|         10.times do |           10.times do | ||||||
|           conversation = create(:conversation, account: account, |             conversation = create(:conversation, account: account, | ||||||
|                                                inbox: inbox, assignee: user, |                                                  inbox: inbox, assignee: user, | ||||||
|                                                created_at: Time.zone.today) |                                                  created_at: Time.zone.today) | ||||||
|           create_list(:message, 5, message_type: 'outgoing', |             create_list(:message, 5, message_type: 'outgoing', | ||||||
|                                    account: account, inbox: inbox, |                                      account: account, inbox: inbox, | ||||||
|                                    conversation: conversation, created_at: Time.zone.today + 2.hours) |                                      conversation: conversation, created_at: Time.zone.today + 2.hours) | ||||||
|           create_list(:message, 2, message_type: 'incoming', |             create_list(:message, 2, message_type: 'incoming', | ||||||
|                                    account: account, inbox: inbox, |                                      account: account, inbox: inbox, | ||||||
|                                    conversation: conversation, |                                      conversation: conversation, | ||||||
|                                    created_at: Time.zone.today + 3.hours) |                                      created_at: Time.zone.today + 3.hours) | ||||||
|           conversation.update_labels('label_1') |             conversation.update_labels('label_1') | ||||||
|           conversation.label_list |             conversation.label_list | ||||||
|           conversation.save! |             conversation.save! | ||||||
|         end |           end | ||||||
|  |  | ||||||
|         5.times do |           5.times do | ||||||
|           conversation = create(:conversation, account: account, |             conversation = create(:conversation, account: account, | ||||||
|                                                inbox: inbox, assignee: user, |                                                  inbox: inbox, assignee: user, | ||||||
|                                                created_at: (Time.zone.today - 2.days)) |                                                  created_at: (Time.zone.today - 2.days)) | ||||||
|           create_list(:message, 3, message_type: 'outgoing', |             create_list(:message, 3, message_type: 'outgoing', | ||||||
|                                    account: account, inbox: inbox, |                                      account: account, inbox: inbox, | ||||||
|                                    conversation: conversation, |                                      conversation: conversation, | ||||||
|                                    created_at: (Time.zone.today - 2.days)) |                                      created_at: (Time.zone.today - 2.days)) | ||||||
|           create_list(:message, 1, message_type: 'incoming', |             create_list(:message, 1, message_type: 'incoming', | ||||||
|                                    account: account, inbox: inbox, |                                      account: account, inbox: inbox, | ||||||
|                                    conversation: conversation, |                                      conversation: conversation, | ||||||
|                                    created_at: (Time.zone.today - 2.days)) |                                      created_at: (Time.zone.today - 2.days)) | ||||||
|           conversation.update_labels('label_2') |             conversation.update_labels('label_2') | ||||||
|           conversation.label_list |             conversation.label_list | ||||||
|           conversation.save! |             conversation.save! | ||||||
|  |           end | ||||||
|         end |         end | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
| @@ -100,28 +102,30 @@ describe V2::ReportBuilder do | |||||||
|       end |       end | ||||||
|  |  | ||||||
|       it 'return resolutions count' do |       it 'return resolutions count' do | ||||||
|         params = { |         travel_to(Time.zone.today) do | ||||||
|           metric: 'resolutions_count', |           params = { | ||||||
|           type: :account, |             metric: 'resolutions_count', | ||||||
|           since: (Time.zone.today - 3.days).to_time.to_i.to_s, |             type: :account, | ||||||
|           until: Time.zone.today.end_of_day.to_time.to_i.to_s |             since: (Time.zone.today - 3.days).to_time.to_i.to_s, | ||||||
|         } |             until: Time.zone.today.end_of_day.to_time.to_i.to_s | ||||||
|  |           } | ||||||
|  |  | ||||||
|         conversations = account.conversations.where('created_at < ?', 1.day.ago) |           conversations = account.conversations.where('created_at < ?', 1.day.ago) | ||||||
|         perform_enqueued_jobs do |           perform_enqueued_jobs do | ||||||
|           # Resolve all 5 conversations |             # Resolve all 5 conversations | ||||||
|           conversations.each(&:resolved!) |             conversations.each(&:resolved!) | ||||||
|  |  | ||||||
|           # Reopen 1 conversation |             # Reopen 1 conversation | ||||||
|           conversations.first.open! |             conversations.first.open! | ||||||
|  |           end | ||||||
|  |  | ||||||
|  |           builder = described_class.new(account, params) | ||||||
|  |           metrics = builder.timeseries | ||||||
|  |  | ||||||
|  |           # 4 conversations are resolved | ||||||
|  |           expect(metrics[Time.zone.today]).to be 4 | ||||||
|  |           expect(metrics[Time.zone.today - 2.days]).to be 0 | ||||||
|         end |         end | ||||||
|  |  | ||||||
|         builder = described_class.new(account, params) |  | ||||||
|         metrics = builder.timeseries |  | ||||||
|  |  | ||||||
|         # 4 conversations are resolved |  | ||||||
|         expect(metrics[Time.zone.today]).to be 4 |  | ||||||
|         expect(metrics[Time.zone.today - 2.days]).to be 0 |  | ||||||
|       end |       end | ||||||
|  |  | ||||||
|       it 'returns average first response time' do |       it 'returns average first response time' do | ||||||
| @@ -218,30 +222,32 @@ describe V2::ReportBuilder do | |||||||
|       end |       end | ||||||
|  |  | ||||||
|       it 'return resolutions count' do |       it 'return resolutions count' do | ||||||
|         params = { |         travel_to(Time.zone.today) do | ||||||
|           metric: 'resolutions_count', |           params = { | ||||||
|           type: :label, |             metric: 'resolutions_count', | ||||||
|           id: label_2.id, |             type: :label, | ||||||
|           since: (Time.zone.today - 3.days).to_time.to_i.to_s, |             id: label_2.id, | ||||||
|           until: (Time.zone.today + 1.day).to_time.to_i.to_s |             since: (Time.zone.today - 3.days).to_time.to_i.to_s, | ||||||
|         } |             until: (Time.zone.today + 1.day).to_time.to_i.to_s | ||||||
|  |           } | ||||||
|  |  | ||||||
|         conversations = account.conversations.where('created_at < ?', 1.day.ago) |           conversations = account.conversations.where('created_at < ?', 1.day.ago) | ||||||
|  |  | ||||||
|         perform_enqueued_jobs do |           perform_enqueued_jobs do | ||||||
|           # ensure 5 reporting events are created |             # ensure 5 reporting events are created | ||||||
|           conversations.each(&:resolved!) |             conversations.each(&:resolved!) | ||||||
|  |  | ||||||
|           # open one of the conversations to check if it is not counted |             # open one of the conversations to check if it is not counted | ||||||
|           conversations.last.open! |             conversations.last.open! | ||||||
|  |           end | ||||||
|  |  | ||||||
|  |           builder = described_class.new(account, params) | ||||||
|  |           metrics = builder.timeseries | ||||||
|  |  | ||||||
|  |           # this should count only 4 since the last conversation was reopened | ||||||
|  |           expect(metrics[Time.zone.today]).to be 4 | ||||||
|  |           expect(metrics[Time.zone.today - 2.days]).to be 0 | ||||||
|         end |         end | ||||||
|  |  | ||||||
|         builder = described_class.new(account, params) |  | ||||||
|         metrics = builder.timeseries |  | ||||||
|  |  | ||||||
|         # this should count only 4 since the last conversation was reopened |  | ||||||
|         expect(metrics[Time.zone.today]).to be 4 |  | ||||||
|         expect(metrics[Time.zone.today - 2.days]).to be 0 |  | ||||||
|       end |       end | ||||||
|  |  | ||||||
|       it 'returns average first response time' do |       it 'returns average first response time' do | ||||||
|   | |||||||
| @@ -15,7 +15,7 @@ RSpec.describe 'Reports API', type: :request do | |||||||
|  |  | ||||||
|   before do |   before do | ||||||
|     create_list(:conversation, 10, account: account, inbox: inbox, |     create_list(:conversation, 10, account: account, inbox: inbox, | ||||||
|                                    assignee: user, created_at: Time.zone.today) |                                    assignee: user, created_at: Time.current.in_time_zone(default_timezone).to_date) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   describe 'GET /api/v2/accounts/:account_id/reports/account' do |   describe 'GET /api/v2/accounts/:account_id/reports/account' do | ||||||
|   | |||||||
| @@ -37,15 +37,13 @@ RSpec.describe Account::ContactsExportJob do | |||||||
|       described_class.perform_now(account.id, []) |       described_class.perform_now(account.id, []) | ||||||
|  |  | ||||||
|       csv_data = CSV.parse(account.contacts_export.download, headers: true) |       csv_data = CSV.parse(account.contacts_export.download, headers: true) | ||||||
|       first_row = csv_data[0] |       emails = csv_data.pluck('email') | ||||||
|       last_row = csv_data[csv_data.length - 1] |       phone_numbers = csv_data.pluck('phone_number') | ||||||
|       first_contact = account.contacts.find_by(email: 'test1@text.example') |  | ||||||
|       last_contact = account.contacts.find_by(email: 'test2@text.example') |  | ||||||
|  |  | ||||||
|       expect(csv_data.length).to eq(account.contacts.count) |       expect(csv_data.length).to eq(account.contacts.count) | ||||||
|  |  | ||||||
|       expect([first_row['email'], last_row['email']]).to contain_exactly(first_contact.email, last_contact.email) |       expect(emails).to include('test1@text.example', 'test2@text.example') | ||||||
|       expect([first_row['phone_number'], last_row['phone_number']]).to contain_exactly(first_contact.phone_number, last_contact.phone_number) |       expect(phone_numbers).to include('+910808080818', '+910808080808') | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Sojan Jose
					Sojan Jose