require "rails_helper" RSpec.describe MatchmakingChannel, type: :channel do let(:user) { User.create!(nickname: "Blackbeard", password: "password123", rating: 1250) } before do stub_connection current_user: user end describe "#subscribed" do it "subscribes successfully and streams from the user's private channel" do subscribe expect(subscription).to be_confirmed expect(subscription).to have_stream_from("matchmaking:#{user.id}") expect(transmissions).to be_empty end it "transmits a claimed match if one was ready before or during connection" do match_payload = { type: "matched", match_id: "test-match-uuid", game_token: "jwt-game-token", game_server: "ws://localhost:8080", players: [ user.id, 999 ] } allow(MatchmakingService).to receive(:claim).with(user.id).and_return(match_payload) subscribe expect(subscription).to be_confirmed expect(transmissions.last).to eq(match_payload.as_json) end end describe "#start" do it "calls MatchmakingService.enqueue with the user's id and current rating" do subscribe expect(MatchmakingService).to receive(:enqueue).with(user.id, rating: 1250) perform :start end it "accepts extra parameters in data hash without error" do subscribe expect(MatchmakingService).to receive(:enqueue).with(user.id, rating: 1250) perform :start, { "custom" => "data" } end end describe "#stop" do it "calls MatchmakingService.cancel for the current user" do subscribe expect(MatchmakingService).to receive(:cancel).with(user.id) perform :stop end end describe "#unsubscribed" do it "cancels matchmaking search when unsubscribing" do subscribe expect(MatchmakingService).to receive(:cancel).with(user.id) unsubscribe end end end