From d94392a27770e327a387224b96f419f86bd95949 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Sun, 25 Jan 2026 23:44:00 -0800 Subject: [PATCH] test(telegram): stabilize contact assertions --- .../telegram/incoming_message_service_spec.rb | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/spec/services/telegram/incoming_message_service_spec.rb b/spec/services/telegram/incoming_message_service_spec.rb index 528161afe..3d63e6a8f 100644 --- a/spec/services/telegram/incoming_message_service_spec.rb +++ b/spec/services/telegram/incoming_message_service_spec.rb @@ -42,6 +42,11 @@ describe Telegram::IncomingMessageService do } end + def contact_for(source_id = nil) + source_id ||= message_params.dig('from', 'id') + ContactInbox.find_by!(inbox: telegram_channel.inbox, source_id: source_id).contact + end + describe '#perform' do context 'when valid text message params' do it 'creates appropriate conversations, message and contacts' do @@ -51,7 +56,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.content).to eq('test') end end @@ -64,9 +69,9 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') - expect(Contact.all.first.additional_attributes['social_telegram_user_id']).to eq(23) - expect(Contact.all.first.additional_attributes['social_telegram_user_name']).to eq('sojan') + expect(contact_for.name).to eq('Sojan Jose') + expect(contact_for.additional_attributes['social_telegram_user_id']).to eq(23) + expect(contact_for.additional_attributes['social_telegram_user_name']).to eq('sojan') expect(telegram_channel.inbox.messages.first.content).to eq('test') end end @@ -107,7 +112,7 @@ describe Telegram::IncomingMessageService do expect(telegram_channel.inbox.conversations.count).not_to eq(0) expect(telegram_channel.inbox.conversations.last.additional_attributes).to include({ 'chat_id' => 23, 'business_connection_id' => 'eooW3KF5WB5HxTD7T826' }) - contact = Contact.all.first + contact = contact_for expect(contact.name).to eq('Sojan Jose') expect(contact.additional_attributes['language_code']).to eq('en') message = telegram_channel.inbox.messages.first @@ -131,7 +136,7 @@ describe Telegram::IncomingMessageService do expect(telegram_channel.inbox.conversations.count).not_to eq(0) expect(telegram_channel.inbox.conversations.last.additional_attributes).to include({ 'chat_id' => 23, 'business_connection_id' => 'eooW3KF5WB5HxTD7T826' }) - contact = Contact.all.first + contact = contact_for expect(contact.name).to eq('Sojan Jose') # TODO: The language code is not present when we send the first message to the client. # Should we update it when the user replies? @@ -161,9 +166,9 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') - expect(Contact.all.first.additional_attributes['social_telegram_user_id']).to eq(23) - expect(Contact.all.first.additional_attributes['social_telegram_user_name']).to eq('sojan') + expect(contact_for.name).to eq('Sojan Jose') + expect(contact_for.additional_attributes['social_telegram_user_id']).to eq(23) + expect(contact_for.additional_attributes['social_telegram_user_name']).to eq('sojan') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('audio') end end @@ -182,7 +187,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('image') end end @@ -207,7 +212,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('image') end end @@ -229,7 +234,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('video') end end @@ -258,7 +263,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('video') end end @@ -277,7 +282,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('audio') end end @@ -298,7 +303,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('file') end end @@ -336,7 +341,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('location') end @@ -355,7 +360,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') attachment = telegram_channel.inbox.messages.first.attachments.first expect(attachment.file_type).to eq('location') @@ -388,8 +393,8 @@ describe Telegram::IncomingMessageService do described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') - expect(Contact.all.first.additional_attributes['social_telegram_user_id']).to eq(5_171_248) + expect(contact_for(5_171_248).name).to eq('Sojan Jose') + expect(contact_for(5_171_248).additional_attributes['social_telegram_user_id']).to eq(5_171_248) expect(telegram_channel.inbox.messages.first.content).to eq('Option 1') end end @@ -406,7 +411,7 @@ describe Telegram::IncomingMessageService do }.with_indifferent_access described_class.new(inbox: telegram_channel.inbox, params: params).perform expect(telegram_channel.inbox.conversations.count).not_to eq(0) - expect(Contact.all.first.name).to eq('Sojan Jose') + expect(contact_for.name).to eq('Sojan Jose') expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('contact') end end