A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
RSpec.describe Jobs::PublishRoomParticipants do
subject(:job) { described_class.new }
fab!(:room, :live_room)
fab!(:user1, :user)
fab!(:user2, :user)
before { SiteSetting.live_enabled = true }
it "publishes participants for rooms with active participants" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.add(room.id, user2.id)
# Verify participants were added
expect(Live::ParticipantTracker.user_ids(room.id)).to contain_exactly(user1.id, user2.id)
messages = MessageBus.track_publish { job.execute({}) }
room_messages = messages.select { |m| m.channel == Live.room_channel(room.id) }
expect(room_messages.size).to eq(1)
expect(room_messages.first.data[:type]).to eq("participants")
expect(room_messages.first.data[:participants].map { |p| p[:id] }).to contain_exactly(
user1.id,
user2.id,
)
end
it "keeps a mesh room pinned while a member can still resume" do
Live::ParticipantTracker.pin_transport!(room.id, "mesh")
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.update_metadata(room.id, user1.id, { role: "participant" })
Live::ParticipantTracker.create_participant_session!(room.id, user1.id)
key = "#{Live::ParticipantTracker::KEY_NAMESPACE}:#{room.id}:participants"
Discourse.redis.zadd(key, 1.hour.ago.to_f, user1.id)
job.execute({})
expect(Live::ParticipantTracker.pinned_transport(room.id)).to eq("mesh")
Live::ParticipantTracker.revoke_participant_session(room.id, user1.id)
job.execute({})
expect(Live::ParticipantTracker.pinned_transport(room.id)).to be_nil
end
it "reflects TTL-expired participants in broadcast" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.add(room.id, user2.id)
# Set user2's heartbeat to a stale timestamp to simulate TTL expiration
Discourse.redis.zadd(
"#{Live::ParticipantTracker::KEY_NAMESPACE}:#{room.id}:participants",
1.hour.ago.to_f,
user2.id,
)
messages = MessageBus.track_publish { job.execute({}) }
room_messages = messages.select { |m| m.channel == Live.room_channel(room.id) }
expect(room_messages.size).to eq(1)
expect(room_messages.first.data[:participants].map { |p| p[:id] }).to contain_exactly(user1.id)
end
it "does not publish for rooms without recent membership activity" do
messages = MessageBus.track_publish { job.execute({}) }
expect(messages).to be_empty
end
it "publishes an empty list for rooms that recently emptied" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.remove(room.id, user1.id)
messages = MessageBus.track_publish { job.execute({}) }
room_messages = messages.select { |m| m.channel == Live.room_channel(room.id) }
expect(room_messages.size).to eq(1)
expect(room_messages.first.data[:participants]).to be_empty
end
it "stops publishing once a room's last activity leaves the safety window" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.remove(room.id, user1.id)
Discourse.redis.zadd(
Live::ParticipantTracker::RECENTLY_ACTIVE_ROOMS_KEY,
1.hour.ago.to_f,
room.id,
)
messages = MessageBus.track_publish { job.execute({}) }
expect(messages).to be_empty
end
it "handles rooms that no longer exist" do
Live::ParticipantTracker.add(99_999, user1.id)
expect { job.execute({}) }.not_to raise_error
end
it "deletes an emptied livekit-pinned room from the SFU exactly once" do
SiteSetting.live_livekit_url = "wss://livekit.example.com"
SiteSetting.live_livekit_api_key = "lk_api_key"
SiteSetting.live_livekit_api_secret = "lk_api_secret"
Live::ParticipantTracker.pin_transport!(room.id, "livekit")
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.remove(room.id, user1.id)
stub = stub_request(:post, "https://livekit.example.com/twirp/livekit.RoomService/DeleteRoom")
job.execute({})
expect(stub).to have_been_requested.once
expect(Live::ParticipantTracker.pinned_transport(room.id)).to be_nil
# The room stays in the recently-active sweep for a while; with the pin
# gone, later sweeps must not keep re-deleting it.
job.execute({})
expect(stub).to have_been_requested.once
end
describe "deleted room agent cleanup" do
before do
SiteSetting.live_livekit_url = "wss://test.livekit.cloud"
SiteSetting.live_livekit_api_key = "key"
SiteSetting.live_livekit_api_secret = "secret"
SiteSetting.live_livekit_agent_enabled = true
room.update!(public: true)
Live::ParticipantTracker.pin_transport!(room.id, "livekit")
Live::ParticipantTracker.add(room.id, user1.id)
stub_request(
:post,
"https://test.livekit.cloud/twirp/livekit.AgentDispatchService/CreateDispatch",
).to_return(status: 200, body: { id: "AD_test" }.to_json)
Live::AgentDispatcher.dispatch!(room:, agent_name: "assistant")
end
it "retries failed cleanup after the room row and transport pin are gone" do
room.destroy!
delete_dispatch =
stub_request(
:post,
"https://test.livekit.cloud/twirp/livekit.AgentDispatchService/DeleteDispatch",
).to_return(status: 503)
stub_request(
:post,
"https://test.livekit.cloud/twirp/livekit.AgentDispatchService/ListDispatch",
).to_return(status: 503)
delete_room =
stub_request(
:post,
"https://test.livekit.cloud/twirp/livekit.RoomService/DeleteRoom",
).to_return(status: 503)
Live::AgentManager.evict_agents_in_room!(room)
expect(Live::ParticipantTracker.pinned_transport(room.id)).to be_nil
expect(Live::AgentDispatcher.pending?(room.id)).to eq(true)
expect(Live::AgentManager.provider_room_ids).to contain_exactly(room.id)
delete_dispatch.to_return(status: 200, body: "{}")
delete_room.to_return(status: 200, body: "{}")
messages = MessageBus.track_publish { job.execute({}) }
expect(Live::AgentDispatcher.pending?(room.id)).to eq(false)
expect(Live::AgentManager.provider_room_ids).to be_empty
expect(messages.map(&:channel)).not_to include(Live.room_channel(room.id))
expect(delete_room).to have_been_requested.twice
job.execute({})
expect(delete_room).to have_been_requested.twice
end
end
describe "stale status sweep" do
before do
SiteSetting.enable_user_status = true
SiteSetting.live_auto_status_enabled = true
end
# Statuses set within the sweep's grace window are skipped, so specs set
# them in the past and re-add the live user's heartbeat after traveling.
it "clears the status of a participant whose heartbeat lapsed" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::ParticipantTracker.add(room.id, user2.id)
Live::UserStatusManager.set_live_status(user1, room)
Live::UserStatusManager.set_live_status(user2, room)
freeze_time(1.minute.from_now)
Live::ParticipantTracker.add(room.id, user1.id)
Discourse.redis.zadd(
"#{Live::ParticipantTracker::KEY_NAMESPACE}:#{room.id}:participants",
1.hour.ago.to_f,
user2.id,
)
job.execute({})
expect(user1.reload.user_status).to be_present
expect(user2.reload.user_status).to be_nil
end
it "clears lingering statuses of a room that fully emptied" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::UserStatusManager.set_live_status(user1, room)
freeze_time(1.minute.from_now)
Discourse.redis.zadd(
"#{Live::ParticipantTracker::KEY_NAMESPACE}:#{room.id}:participants",
1.hour.ago.to_f,
user1.id,
)
job.execute({})
expect(user1.reload.user_status).to be_nil
end
it "keeps a user's status while they are live in another active room" do
other_room = Fabricate(:live_room)
Live::ParticipantTracker.add(room.id, user1.id)
Live::UserStatusManager.set_live_status(user1, other_room)
freeze_time(1.minute.from_now)
Live::ParticipantTracker.add(other_room.id, user1.id)
Discourse.redis.zadd(
"#{Live::ParticipantTracker::KEY_NAMESPACE}:#{room.id}:participants",
1.hour.ago.to_f,
user1.id,
)
job.execute({})
expect(user1.reload.user_status).to be_present
end
it "keeps a freshly set status even when the user is not yet live" do
Live::ParticipantTracker.add(room.id, user1.id)
Live::UserStatusManager.set_live_status(user2, room)
job.execute({})
expect(user2.reload.user_status).to be_present
end
it "does not touch manually set statuses, even ones using Live emojis" do
Live::ParticipantTracker.add(room.id, user1.id)
user1.set_status!("Sleeping", "zzz")
user2.set_status!("On vacation", "palm_tree")
freeze_time(1.minute.from_now)
job.execute({})
expect(user1.reload.user_status.emoji).to eq("zzz")
expect(user2.reload.user_status.emoji).to eq("palm_tree")
end
end
it "does not publish when plugin is disabled" do
Live::ParticipantTracker.add(room.id, user1.id)
SiteSetting.live_enabled = false
messages = MessageBus.track_publish { job.execute({}) }
expect(messages).to be_empty
end
end