add match history
This commit is contained in:
@@ -91,4 +91,129 @@ RSpec.describe "Match details API", type: :request do
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user