unsupported
  7   iˆl–Ãa¾”ªi?u8 ¸Û71hß�þD9YÇÌc!Z”"ùõ„qßôà¿ñ *×    i# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'Sessions integration with BackgroundServices::Service::ProcessSessionsJobs' do # rubocop:disable RSpec/DescribeClass
  # The real BackgroundServices::Service::ProcessSessionsJobs worker runs on a separate
  # thread and picks up and cleans up sessions in real wall-clock time, so `travel`
  # does not affect it and its state has to be polled.
  def wait_for(timeout: 20)
    Timeout.timeout(timeout) { sleep 0.1 until yield }
  end

  describe 'processing session client threads' do
    before do
      # The session store persists across examples/processes, so stale
      # queued messages from earlier runs would otherwise leak into these
      # client_id-keyed message queue assertions.
      Sessions.cleanup
      UserInfo.current_user_id = 1
    end

    context 'with websocket and ajax sessions for different users' do
      let(:roles)  { Role.where(name: ['Agent']) }
      let(:groups) { Group.all }

      # Created eagerly (let!, not let) and in this order: each creation
      # broadcasts a client notification to every currently connected
      # session, so agents must all exist before the first Sessions.create
      # below, or later agents' creation messages would leak into earlier
      # clients' message queues.
      let!(:agent1) do
        User.create_or_update(
          login:     'session-agent-1',
          firstname: 'Session',
          lastname:  'Agent 1',
          email:     'session-agent1@example.com',
          password:  'agentpw',
          active:    true,
          roles:     roles,
          groups:    groups,
        ).tap(&:save!)
      end

      let!(:agent2) do
        User.create_or_update(
          login:     'session-agent-2',
          firstname: 'Session',
          lastname:  'Agent 2',
          email:     'session-agent2@example.com',
          password:  'agentpw',
          active:    true,
          roles:     roles,
          groups:    groups,
        ).tap(&:save!)
      end

      let!(:agent3) do
        User.create_or_update(
          login:     'session-agent-3',
          firstname: 'Session',
          lastname:  'Agent 3',
          email:     'session-agent3@example.com',
          password:  'agentpw',
          active:    true,
          roles:     roles,
          groups:    groups,
        ).tap(&:save!)
      end

      it 'delivers messages to connected clients and stops their threads once sessions go idle', :aggregate_failures do # rubocop:disable RSpec/ExampleLength
        client_id1 = 'a1234'
        client_id2 = 'a123456'
        client_id3 = 'aabc'
        Sessions.destroy(client_id1)
        Sessions.destroy(client_id2)
        Sessions.destroy(client_id3)
        Sessions.create(client_id1, agent1.attributes, { type: 'websocket' })
        Sessions.create(client_id2, agent2.attributes, { type: 'ajax' })
        Sessions.create(client_id3, agent3.attributes, { type: 'ajax' })

        expect(Sessions.session_exists?(client_id1)).to be(true)
        expect(Sessions.session_exists?(client_id2)).to be(true)
        expect(Sessions.session_exists?(client_id3)).to be(true)

        # check if session still exists after idle cleanup
        travel 1.second
        Sessions.destroy_idle_sessions(3)
        expect(Sessions.session_exists?(client_id1)).to be(true)
        expect(Sessions.session_exists?(client_id2)).to be(true)
        expect(Sessions.session_exists?(client_id3)).to be(true)

        # check if session still exists after idle cleanup with touched sessions
        travel 4.seconds
        Sessions.touch(client_id1)
        Sessions.touch(client_id2)
        Sessions.touch(client_id3)
        Sessions.destroy_idle_sessions(3)
        expect(Sessions.session_exists?(client_id1)).to be(true)
        expect(Sessions.session_exists?(client_id2)).to be(true)
        expect(Sessions.session_exists?(client_id3)).to be(true)

        # check session data
