require "rails_helper" RSpec.describe "Match details API", type: :request do let(:player1) { User.create!(nickname: "player_one", password: "secret123") } let(:player2) { User.create!(nickname: "player_two", password: "secret123") } let(:outsider) { User.create!(nickname: "outsider", password: "secret123") } let(:match) do Match.create!( player1: player1, player2: player2, status: :player1_won, game_server: "ws://game-server:8080/ws", started_at: 10.minutes.ago, finished_at: Time.current, duration_seconds: 600, player1_rating_before: player1.rating, player2_rating_before: player2.rating, player1_rating_change: 12, player2_rating_change: -12 ) end def token_for(user) JwtService.encode(user_id: user.id) end describe "GET /api/matches/:uuid" do it "returns match details with player1's rating delta for player1" do get "/api/matches/#{match.uuid}", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["match"]["id"]).to eq(match.uuid) expect(json["match"]["status"]).to eq("player1_won") expect(json["match"]["duration_seconds"]).to eq(600) expect(json["match"]["game_server"]).to eq("ws://game-server:8080/ws") expect(json["match"]["your_rating_change"]).to eq(12) expect(json["match"]["your_rating_before"]).to eq(player1.rating) expect(json["match"]["player1"]["nickname"]).to eq("player_one") expect(json["match"]["player2"]["rating_change"]).to eq(-12) end it "returns player2's own rating delta for player2" do get "/api/matches/#{match.uuid}", headers: { "Authorization" => "Bearer #{token_for(player2)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["match"]["your_rating_change"]).to eq(-12) expect(json["match"]["your_rating_before"]).to eq(player2.rating) end it "returns 404 for a user who is not a participant" do get "/api/matches/#{match.uuid}", headers: { "Authorization" => "Bearer #{token_for(outsider)}" } expect(response).to have_http_status(:not_found) end it "returns 404 for an unknown match uuid" do get "/api/matches/00000000-0000-0000-0000-000000000000", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:not_found) end it "returns 401 without a token" do get "/api/matches/#{match.uuid}" expect(response).to have_http_status(:unauthorized) end it "returns the current state for an ongoing match" do ongoing = Match.create!( player1: player1, player2: player2, status: :ongoing, game_server: "ws://game-server:8080/ws", started_at: Time.current, player1_rating_before: player1.rating, player2_rating_before: player2.rating, player1_rating_change: 0, player2_rating_change: 0 ) get "/api/matches/#{ongoing.uuid}", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["match"]["status"]).to eq("ongoing") expect(json["match"]["finished_at"]).to be_nil expect(json["match"]["duration_seconds"]).to be_nil end end describe "GET /api/matches" do let!(:match1) do Match.create!( player1: player1, player2: player2, status: :player1_won, game_server: "ws://game-server:8080/ws", started_at: 20.minutes.ago, finished_at: 10.minutes.ago, duration_seconds: 600, player1_rating_before: 100, player2_rating_before: 100, player1_rating_change: 12, player2_rating_change: -12 ) end let!(:match2) do Match.create!( player1: player2, player2: player1, status: :player2_won, game_server: "ws://game-server:8080/ws", started_at: 10.minutes.ago, finished_at: 5.minutes.ago, duration_seconds: 300, player1_rating_before: 88, player2_rating_before: 112, player1_rating_change: -10, player2_rating_change: 10 ) end let!(:other_match) do Match.create!( player1: player2, player2: outsider, status: :player1_won, game_server: "ws://game-server:8080/ws", started_at: 5.minutes.ago, finished_at: Time.current, duration_seconds: 300, player1_rating_before: 100, player2_rating_before: 100, player1_rating_change: 15, player2_rating_change: -15 ) end it "returns 401 without authentication" do get "/api/matches" expect(response).to have_http_status(:unauthorized) end it "returns the list of matches for current_user newest first" do get "/api/matches", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["total"]).to eq(2) expect(json["matches"].size).to eq(2) expect(json["matches"].first["id"]).to eq(match2.uuid) expect(json["matches"].second["id"]).to eq(match1.uuid) first_item = json["matches"].first expect(first_item["your_rating_change"]).to eq(10) expect(first_item["player1"]["nickname"]).to eq("player_two") expect(first_item["player2"]["nickname"]).to eq("player_one") end it "paginates results using start and count" do get "/api/matches?start=1&count=1", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["total"]).to eq(2) expect(json["start"]).to eq(1) expect(json["count"]).to eq(1) expect(json["matches"].size).to eq(1) expect(json["matches"].first["id"]).to eq(match1.uuid) end it "supports from parameter as alias for start" do get "/api/matches?from=1&count=1", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["start"]).to eq(1) expect(json["matches"].size).to eq(1) expect(json["matches"].first["id"]).to eq(match1.uuid) end it "returns an empty list when user has no matches" do get "/api/matches", headers: { "Authorization" => "Bearer #{token_for(outsider)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) expect(json["total"]).to eq(1) # outsider has other_match expect(json["matches"].size).to eq(1) expect(json["matches"].first["id"]).to eq(other_match.uuid) end it "includes player avatar_url if attached" do player1.avatar.attach( io: StringIO.new("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15c4"), filename: "avatar.png", content_type: "image/png" ) get "/api/matches", headers: { "Authorization" => "Bearer #{token_for(player1)}" } expect(response).to have_http_status(:ok) json = JSON.parse(response.body) first_item = json["matches"].first # in match2, player1 is player2 and player2 is player1 expect(first_item["player2"]["avatar_url"]).to include("avatar.png") expect(first_item["player1"]["avatar_url"]).to be_nil end end end