From 2edd7c569f72518a7d26aa495da66f5d0155f73b Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 10 Mar 2025 14:50:00 +0530 Subject: [PATCH] test: ensure event timestamp is sent across --- spec/listeners/action_cable_listener_spec.rb | 119 +++++++++++++++++-- 1 file changed, 108 insertions(+), 11 deletions(-) diff --git a/spec/listeners/action_cable_listener_spec.rb b/spec/listeners/action_cable_listener_spec.rb index b353f330d..ba32b97c5 100644 --- a/spec/listeners/action_cable_listener_spec.rb +++ b/spec/listeners/action_cable_listener_spec.rb @@ -30,7 +30,8 @@ describe ActionCableListener do agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token ), 'message.created', - message.push_event_data.merge(account_id: account.id) + message.push_event_data.merge(account_id: account.id), + nil ) listener.message_created(event) end @@ -48,7 +49,8 @@ describe ActionCableListener do agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token, verified_contact_inbox.pubsub_token ), 'message.created', - message.push_event_data.merge(account_id: account.id) + message.push_event_data.merge(account_id: account.id), + nil ) listener.message_created(event) end @@ -68,7 +70,8 @@ describe ActionCableListener do 'conversation.typing_on', { conversation: conversation.push_event_data, user: agent.push_event_data, account_id: account.id, - is_private: false } + is_private: false }, + nil ) listener.conversation_typing_on(event) end @@ -88,7 +91,8 @@ describe ActionCableListener do 'conversation.typing_on', { conversation: conversation.push_event_data, user: conversation.contact.push_event_data, account_id: account.id, - is_private: false } + is_private: false }, + nil ) listener.conversation_typing_on(event) end @@ -108,7 +112,8 @@ describe ActionCableListener do 'conversation.typing_off', { conversation: conversation.push_event_data, user: agent.push_event_data, account_id: account.id, - is_private: false } + is_private: false }, + nil ) listener.conversation_typing_off(event) end @@ -125,7 +130,8 @@ describe ActionCableListener do agent.pubsub_token, admin.pubsub_token ), 'contact.deleted', - contact.push_event_data.merge(account_id: account.id) + contact.push_event_data.merge(account_id: account.id), + nil ) listener.contact_deleted(event) end @@ -147,7 +153,8 @@ describe ActionCableListener do }, unread_count: 1, count: 1 - } + }, + nil ) listener.notification_deleted(event) @@ -168,7 +175,8 @@ describe ActionCableListener do notification: notification.push_event_data, unread_count: 1, count: 1 - } + }, + nil ) listener.notification_updated(event) @@ -177,7 +185,8 @@ describe ActionCableListener do describe '#conversation_updated' do let(:event_name) { :'conversation.updated' } - let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) } + let(:timestamp) { Time.zone.now } + let!(:event) { Events::Base.new(event_name, timestamp, conversation: conversation, user: agent, is_private: false) } before do conversation.add_labels(['support']) @@ -189,7 +198,8 @@ describe ActionCableListener do expect(ActionCableBroadcastJob).to receive(:perform_later).with( [agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], 'conversation.updated', - conversation.push_event_data.merge(account_id: account.id) + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f ) listener.conversation_updated(event) end @@ -200,9 +210,96 @@ describe ActionCableListener do expect(ActionCableBroadcastJob).to receive(:perform_later).with( [agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], 'conversation.updated', - conversation.push_event_data.merge(account_id: account.id) + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f ) listener.conversation_updated(event) end end + + context 'with event timestamps' do + let(:timestamp) { Time.zone.now } + + describe '#conversation_created' do + let(:event_name) { :'conversation.created' } + let!(:event) { Events::Base.new(event_name, timestamp, conversation: conversation) } + + it 'sends event with timestamp' do + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + [agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], + 'conversation.created', + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f + ) + listener.conversation_created(event) + end + end + + describe '#conversation_status_changed' do + let(:event_name) { :'conversation.status_changed' } + let!(:event) { Events::Base.new(event_name, timestamp, conversation: conversation) } + + it 'sends event with timestamp' do + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + [agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], + 'conversation.status_changed', + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f + ) + listener.conversation_status_changed(event) + end + end + + describe '#assignee_changed' do + let(:event_name) { :'conversation.assignee_changed' } + let!(:event) { Events::Base.new(event_name, timestamp, conversation: conversation) } + + it 'sends event with timestamp' do + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + [agent.pubsub_token, admin.pubsub_token], + 'assignee.changed', + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f + ) + listener.assignee_changed(event) + end + end + + describe '#team_changed' do + let(:event_name) { :'conversation.team_changed' } + let!(:event) { Events::Base.new(event_name, timestamp, conversation: conversation) } + + it 'sends event with timestamp' do + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + [agent.pubsub_token, admin.pubsub_token], + 'team.changed', + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f + ) + listener.team_changed(event) + end + end + + # Update the existing conversation_updated test to include timestamp verification + describe '#conversation_updated' do + let(:event_name) { :'conversation.updated' } + let!(:event) { Events::Base.new(event_name, timestamp, conversation: conversation, user: agent, is_private: false) } + + before do + conversation.add_labels(['support']) + end + + it 'sends update to inbox members with timestamp' do + expect(conversation.inbox.reload.inbox_members.count).to eq(1) + + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + [agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], + 'conversation.updated', + conversation.push_event_data.merge(account_id: account.id), + timestamp.to_f + ) + listener.conversation_updated(event) + end + end + end end