add tests for controllers and ActionCable channels; improve main backend docs

This commit is contained in:
2026-10-27 10:32:40 +03:00
parent b122cbb652
commit 50e8aacfa9
17 changed files with 1127 additions and 63 deletions
@@ -0,0 +1,44 @@
require "rails_helper"
RSpec.describe ApplicationCable::Connection, type: :channel do
include ActionCable::Connection::TestCase::Behavior
let(:user) { User.create!(nickname: "CaptainCook", password: "password123") }
let(:token) { JwtService.encode(user_id: user.id) }
describe "#connect" do
it "connects successfully with token in query params" do
connect "/cable", params: { token: token }
expect(connection.current_user).to eq(user)
end
it "connects successfully with Bearer token in Authorization header" do
connect "/cable", headers: { "Authorization" => "Bearer #{token}" }
expect(connection.current_user).to eq(user)
end
it "rejects connection when no token is provided" do
expect { connect "/cable" }.to have_rejected_connection
end
it "rejects connection when token is invalid" do
expect {
connect "/cable", params: { token: "invalid.jwt.token" }
}.to have_rejected_connection
end
it "rejects connection when token is expired" do
expired_token = JwtService.encode(user_id: user.id, exp: 1.hour.ago.to_i)
expect {
connect "/cable", params: { token: expired_token }
}.to have_rejected_connection
end
it "rejects connection when user does not exist in the database" do
orphan_token = JwtService.encode(user_id: 999_999)
expect {
connect "/cable", params: { token: orphan_token }
}.to have_rejected_connection
end
end
end
@@ -0,0 +1,68 @@
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