chat_agent_state_spec.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Sessions::Event::ChatAgentState do
  4. let(:client_id) { SecureRandom.uuid }
  5. let(:chat) { Chat.first }
  6. let(:user) do
  7. create(:agent, preferences: {
  8. chat: {
  9. active: {
  10. chat.id.to_s => 'on'
  11. }
  12. }
  13. })
  14. end
  15. let!(:instance) do
  16. Sessions.create(client_id, { 'id' => user.id }, {})
  17. Sessions.queue(client_id)
  18. described_class.new(
  19. payload: {
  20. 'data' => {
  21. 'active' => active
  22. },
  23. },
  24. user_id: user.id,
  25. client_id: client_id,
  26. clients: {},
  27. session: {
  28. 'id' => user.id
  29. },
  30. )
  31. end
  32. let(:record) { create(:'chat/agent', updated_by: user) }
  33. before do
  34. Setting.set('chat', true)
  35. end
  36. context 'when state changes' do
  37. let(:active) { !record.active }
  38. it 'broadcasts agent state update' do
  39. allow(Chat).to receive(:broadcast_agent_state_update)
  40. instance.run
  41. expect(Chat).to have_received(:broadcast_agent_state_update)
  42. end
  43. end
  44. context "when state doesn't change" do
  45. let(:active) { record.active }
  46. it "doesn't broadcasts agent state update" do
  47. allow(Chat).to receive(:broadcast_agent_state_update)
  48. instance.run
  49. expect(Chat).not_to have_received(:broadcast_agent_state_update)
  50. end
  51. end
  52. end