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 it "transmits current invite code if one was active for the user" do allow(MatchmakingService).to receive(:claim).with(user.id).and_return(nil) allow(MatchmakingService).to receive(:current_invite_code).with(user.id).and_return("ABCDE") subscribe expect(subscription).to be_confirmed expect(transmissions.last).to eq({ "type" => "invite_created", "code" => "ABCDE" }) 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 "#create_invite" do it "calls MatchmakingService.create_invite and transmits invite_created" do subscribe expect(MatchmakingService).to receive(:create_invite).with(user.id).and_return("XYZAB") perform :create_invite expect(transmissions.last).to eq({ "type" => "invite_created", "code" => "XYZAB" }) end end describe "#join_invite" do it "calls MatchmakingService.join_invite with code" do subscribe expect(MatchmakingService).to receive(:join_invite).with(user.id, "XYZAB").and_return({ ok: true }) perform :join_invite, { "code" => "XYZAB" } end it "transmits invite_error if join fails" do subscribe expect(MatchmakingService).to receive(:join_invite).with(user.id, "WRONG").and_return( { error: "not_found", message: "Приглашение не найдено или истекло" } ) perform :join_invite, { "code" => "WRONG" } expect(transmissions.last).to eq( { "type" => "invite_error", "error" => "not_found", "message" => "Приглашение не найдено или истекло" } ) end end describe "#cancel_invite" do it "calls MatchmakingService.cancel for the current user" do subscribe expect(MatchmakingService).to receive(:cancel).with(user.id) perform :cancel_invite 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