mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-16 04:56:58 +00:00
New stats page, scoreboard, and live scores pages.
This commit is contained in:
110
test/channels/mat_scoreboard_channel_test.rb
Normal file
110
test/channels/mat_scoreboard_channel_test.rb
Normal file
@@ -0,0 +1,110 @@
|
||||
require "test_helper"
|
||||
|
||||
class MatScoreboardChannelTest < ActionCable::Channel::TestCase
|
||||
setup do
|
||||
@mat = mats(:one)
|
||||
@match = matches(:tournament_1_bout_1000)
|
||||
@alternate_match = matches(:tournament_1_bout_1001)
|
||||
Rails.cache.clear
|
||||
@mat.update!(queue1: @match.id, queue2: @alternate_match.id, queue3: nil, queue4: nil)
|
||||
@mat.set_selected_scoreboard_match!(@match)
|
||||
@mat.set_last_match_result!("106 lbs - Example Winner Decision Example Loser 3-1")
|
||||
end
|
||||
|
||||
test "subscribes to a valid mat stream and transmits scoreboard payload" do
|
||||
subscribe(mat_id: @mat.id)
|
||||
|
||||
assert subscription.confirmed?
|
||||
assert_has_stream_for @mat
|
||||
assert_equal(
|
||||
{
|
||||
"mat_id" => @mat.id,
|
||||
"queue1_bout_number" => @match.bout_number,
|
||||
"queue1_match_id" => @match.id,
|
||||
"selected_bout_number" => @match.bout_number,
|
||||
"selected_match_id" => @match.id,
|
||||
"last_match_result" => "106 lbs - Example Winner Decision Example Loser 3-1"
|
||||
},
|
||||
transmissions.last
|
||||
)
|
||||
end
|
||||
|
||||
test "rejects subscription for an invalid mat" do
|
||||
subscribe(mat_id: -1)
|
||||
|
||||
assert subscription.rejected?
|
||||
end
|
||||
|
||||
test "transmits payload with queue1 and no selected match" do
|
||||
@mat.set_selected_scoreboard_match!(nil)
|
||||
|
||||
subscribe(mat_id: @mat.id)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
"mat_id" => @mat.id,
|
||||
"queue1_bout_number" => @match.bout_number,
|
||||
"queue1_match_id" => @match.id,
|
||||
"selected_bout_number" => nil,
|
||||
"selected_match_id" => nil,
|
||||
"last_match_result" => "106 lbs - Example Winner Decision Example Loser 3-1"
|
||||
},
|
||||
transmissions.last
|
||||
)
|
||||
end
|
||||
|
||||
test "transmits payload when selected match differs from queue1" do
|
||||
@mat.set_selected_scoreboard_match!(@alternate_match)
|
||||
|
||||
subscribe(mat_id: @mat.id)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
"mat_id" => @mat.id,
|
||||
"queue1_bout_number" => @match.bout_number,
|
||||
"queue1_match_id" => @match.id,
|
||||
"selected_bout_number" => @alternate_match.bout_number,
|
||||
"selected_match_id" => @alternate_match.id,
|
||||
"last_match_result" => "106 lbs - Example Winner Decision Example Loser 3-1"
|
||||
},
|
||||
transmissions.last
|
||||
)
|
||||
end
|
||||
|
||||
test "transmits payload when no queue1 match exists" do
|
||||
@mat.update!(queue1: nil, queue2: nil, queue3: nil, queue4: nil)
|
||||
@mat.set_selected_scoreboard_match!(nil)
|
||||
|
||||
subscribe(mat_id: @mat.id)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
"mat_id" => @mat.id,
|
||||
"queue1_bout_number" => nil,
|
||||
"queue1_match_id" => nil,
|
||||
"selected_bout_number" => nil,
|
||||
"selected_match_id" => nil,
|
||||
"last_match_result" => "106 lbs - Example Winner Decision Example Loser 3-1"
|
||||
},
|
||||
transmissions.last
|
||||
)
|
||||
end
|
||||
|
||||
test "transmits payload with blank last match result" do
|
||||
@mat.set_last_match_result!(nil)
|
||||
|
||||
subscribe(mat_id: @mat.id)
|
||||
|
||||
assert_equal(
|
||||
{
|
||||
"mat_id" => @mat.id,
|
||||
"queue1_bout_number" => @match.bout_number,
|
||||
"queue1_match_id" => @match.id,
|
||||
"selected_bout_number" => @match.bout_number,
|
||||
"selected_match_id" => @match.id,
|
||||
"last_match_result" => nil
|
||||
},
|
||||
transmissions.last
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -1,8 +1,152 @@
|
||||
require "test_helper"
|
||||
|
||||
class MatchChannelTest < ActionCable::Channel::TestCase
|
||||
# test "subscribes" do
|
||||
# subscribe
|
||||
# assert subscription.confirmed?
|
||||
# end
|
||||
setup do
|
||||
@match = matches(:tournament_1_bout_1000)
|
||||
Rails.cache.clear
|
||||
end
|
||||
|
||||
test "subscribes to a valid match stream" do
|
||||
subscribe(match_id: @match.id)
|
||||
|
||||
assert subscription.confirmed?
|
||||
assert_has_stream_for @match
|
||||
end
|
||||
|
||||
test "invalid match subscription confirms but does not stream" do
|
||||
subscribe(match_id: -1)
|
||||
|
||||
assert subscription.confirmed?
|
||||
assert_empty subscription.streams
|
||||
end
|
||||
|
||||
test "send_stat updates the match and broadcasts stats" do
|
||||
subscribe(match_id: @match.id)
|
||||
|
||||
assert_broadcast_on(@match, { w1_stat: "T3", w2_stat: "E1" }) do
|
||||
perform :send_stat, {
|
||||
new_w1_stat: "T3",
|
||||
new_w2_stat: "E1"
|
||||
}
|
||||
end
|
||||
|
||||
@match.reload
|
||||
assert_equal "T3", @match.w1_stat
|
||||
assert_equal "E1", @match.w2_stat
|
||||
end
|
||||
|
||||
test "send_stat updates only w1 stat when only w1 is provided" do
|
||||
subscribe(match_id: @match.id)
|
||||
|
||||
assert_broadcast_on(@match, { w1_stat: "T3", w2_stat: nil }.compact) do
|
||||
perform :send_stat, { new_w1_stat: "T3" }
|
||||
end
|
||||
|
||||
@match.reload
|
||||
assert_equal "T3", @match.w1_stat
|
||||
assert_nil @match.w2_stat
|
||||
end
|
||||
|
||||
test "send_stat updates only w2 stat when only w2 is provided" do
|
||||
subscribe(match_id: @match.id)
|
||||
|
||||
assert_broadcast_on(@match, { w1_stat: nil, w2_stat: "E1" }.compact) do
|
||||
perform :send_stat, { new_w2_stat: "E1" }
|
||||
end
|
||||
|
||||
@match.reload
|
||||
assert_nil @match.w1_stat
|
||||
assert_equal "E1", @match.w2_stat
|
||||
end
|
||||
|
||||
test "send_stat with empty payload does not update or broadcast" do
|
||||
subscribe(match_id: @match.id)
|
||||
stream = MatchChannel.broadcasting_for(@match)
|
||||
ActionCable.server.pubsub.broadcasts(stream).clear
|
||||
|
||||
perform :send_stat, {}
|
||||
|
||||
@match.reload
|
||||
assert_nil @match.w1_stat
|
||||
assert_nil @match.w2_stat
|
||||
assert_empty ActionCable.server.pubsub.broadcasts(stream)
|
||||
end
|
||||
|
||||
test "send_scoreboard caches and broadcasts scoreboard state" do
|
||||
subscribe(match_id: @match.id)
|
||||
scoreboard_state = {
|
||||
"participantScores" => { "w1" => 2, "w2" => 0 },
|
||||
"metadata" => { "boutNumber" => @match.bout_number }
|
||||
}
|
||||
|
||||
assert_broadcast_on(@match, { scoreboard_state: scoreboard_state }) do
|
||||
perform :send_scoreboard, { scoreboard_state: scoreboard_state }
|
||||
end
|
||||
|
||||
cached_state = Rails.cache.read("tournament:#{@match.tournament_id}:match:#{@match.id}:scoreboard_state")
|
||||
assert_equal scoreboard_state, cached_state
|
||||
end
|
||||
|
||||
test "send_scoreboard with blank payload does not cache or broadcast" do
|
||||
subscribe(match_id: @match.id)
|
||||
stream = MatchChannel.broadcasting_for(@match)
|
||||
ActionCable.server.pubsub.broadcasts(stream).clear
|
||||
|
||||
perform :send_scoreboard, { scoreboard_state: nil }
|
||||
|
||||
assert_nil Rails.cache.read("tournament:#{@match.tournament_id}:match:#{@match.id}:scoreboard_state")
|
||||
assert_empty ActionCable.server.pubsub.broadcasts(stream)
|
||||
end
|
||||
|
||||
test "request_sync transmits match data and cached scoreboard state" do
|
||||
@match.update!(
|
||||
w1_stat: "T3",
|
||||
w2_stat: "E1",
|
||||
score: "3-1",
|
||||
win_type: "Decision",
|
||||
winner_id: @match.w1,
|
||||
finished: 1
|
||||
)
|
||||
scoreboard_state = {
|
||||
"participantScores" => { "w1" => 3, "w2" => 1 },
|
||||
"metadata" => { "boutNumber" => @match.bout_number }
|
||||
}
|
||||
Rails.cache.write(
|
||||
"tournament:#{@match.tournament_id}:match:#{@match.id}:scoreboard_state",
|
||||
scoreboard_state
|
||||
)
|
||||
|
||||
subscribe(match_id: @match.id)
|
||||
perform :request_sync
|
||||
|
||||
assert_equal({
|
||||
"w1_stat" => "T3",
|
||||
"w2_stat" => "E1",
|
||||
"score" => "3-1",
|
||||
"win_type" => "Decision",
|
||||
"winner_name" => @match.wrestler1.name,
|
||||
"winner_id" => @match.w1,
|
||||
"finished" => 1,
|
||||
"scoreboard_state" => scoreboard_state
|
||||
}, transmissions.last)
|
||||
end
|
||||
|
||||
test "request_sync transmits unfinished match data without scoreboard cache" do
|
||||
@match.update!(
|
||||
w1_stat: "T3",
|
||||
w2_stat: "E1",
|
||||
score: nil,
|
||||
win_type: nil,
|
||||
winner_id: nil,
|
||||
finished: nil
|
||||
)
|
||||
|
||||
subscribe(match_id: @match.id)
|
||||
perform :request_sync
|
||||
|
||||
assert_equal({
|
||||
"w1_stat" => "T3",
|
||||
"w2_stat" => "E1"
|
||||
}, transmissions.last)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user