1
0
mirror of https://github.com/jcwimer/wrestlingApp synced 2026-04-12 00:09:32 +00:00

New stats page, scoreboard, and live scores pages.

This commit is contained in:
2026-04-10 21:08:03 -04:00
parent 7526148ba5
commit 970f38ed14
60 changed files with 9650 additions and 148 deletions

View File

@@ -1,9 +1,11 @@
require 'test_helper'
require "json"
class MatchesControllerTest < ActionController::TestCase
# Remove Devise helpers since we're no longer using Devise
# include Devise::Test::ControllerHelpers # Needed to sign in
include ActionView::Helpers::DateHelper # Needed for time ago in words
include ActionCable::TestHelper
setup do
@tournament = Tournament.find(1)
@@ -34,6 +36,18 @@ class MatchesControllerTest < ActionController::TestCase
get :stat, params: { id: @match.id }
end
def get_state
get :state, params: { id: @match.id }
end
def get_state_with_params(extra_params = {})
get :state, params: { id: @match.id }.merge(extra_params)
end
def get_spectate(extra_params = {})
get :spectate, params: { id: @match.id }.merge(extra_params)
end
def get_edit_assignment(extra_params = {})
get :edit_assignment, params: { id: @match.id }.merge(extra_params)
end
@@ -106,11 +120,44 @@ class MatchesControllerTest < ActionController::TestCase
redirect
end
test "logged in user should not get state match page if not owner" do
sign_in_non_owner
get_state
redirect
end
test "logged school delegate should not get state match page if not owner" do
sign_in_school_delegate
get_state
redirect
end
test "non logged in user should not get stat match page" do
get_stat
redirect
end
test "non logged in user should not get state match page" do
get_state
redirect
end
test "valid school permission key cannot get state match page" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_state_with_params(school_permission_key: "valid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "invalid school permission key cannot get state match page" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_state_with_params(school_permission_key: "invalid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "non logged in user should get post update match" do
post_update
assert_redirected_to '/static_pages/not_allowed'
@@ -139,6 +186,202 @@ class MatchesControllerTest < ActionController::TestCase
get_stat
success
end
test "logged in tournament owner should get state match page" do
sign_in_owner
get_state
success
end
test "logged in tournament delegate should get state match page" do
sign_in_tournament_delegate
get_state
success
end
test "logged in school delegate cannot get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
sign_in_school_delegate
get_spectate
redirect
end
test "logged in user cannot get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
sign_in_non_owner
get_spectate
redirect
end
test "logged in tournament delegate can get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
sign_in_tournament_delegate
get_spectate
success
end
test "logged in tournament owner can get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
sign_in_owner
get_spectate
success
end
test "non logged in user cannot get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
get_spectate
redirect
end
test "valid school permission key cannot get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_spectate(school_permission_key: "valid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "invalid school permission key cannot get spectate match page when tournament is not public" do
@tournament.update!(is_public: false)
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_spectate(school_permission_key: "invalid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "logged in school delegate can get spectate match page when tournament is public" do
@tournament.update!(is_public: true)
sign_in_school_delegate
get_spectate
success
end
test "logged in user can get spectate match page when tournament is public" do
@tournament.update!(is_public: true)
sign_in_non_owner
get_spectate
success
end
test "logged in tournament delegate can get spectate match page when tournament is public" do
@tournament.update!(is_public: true)
sign_in_tournament_delegate
get_spectate
success
end
test "logged in tournament owner can get spectate match page when tournament is public" do
@tournament.update!(is_public: true)
sign_in_owner
get_spectate
success
end
test "non logged in user can get spectate match page when tournament is public" do
@tournament.update!(is_public: true)
get_spectate
success
end
test "spectate renders embedded scoreboard when match is unfinished" do
@tournament.update!(is_public: true)
@match.update!(finished: nil)
get_spectate
assert_response :success
assert_includes response.body, "data-match-spectate-target=\"scoreboardContainer\""
assert_includes response.body, "data-controller=\"match-scoreboard\""
assert_includes response.body, "data-match-scoreboard-source-mode-value=\"websocket\""
assert_includes response.body, "data-match-scoreboard-display-mode-value=\"embedded\""
assert_includes response.body, "data-match-scoreboard-target=\"greenTimerIndicator\""
assert_includes response.body, "data-match-scoreboard-target=\"redTimerIndicator\""
end
test "spectate hides embedded scoreboard when match is finished" do
@tournament.update!(is_public: true)
@match.update!(finished: 1, winner_id: @match.w1, win_type: "Decision", score: "3-1")
get_spectate
assert_response :success
assert_not_includes response.body, "data-match-spectate-target=\"scoreboardContainer\""
end
test "posting a match update from match state redirects to all matches" do
sign_in_owner
get :state, params: { id: @match.id }
patch :update, params: { id: @match.id, match: { score: "3-1", win_type: "Decision", winner_id: @match.w1, finished: 1 } }
assert_redirected_to "/tournaments/#{@tournament.id}/matches"
end
test "state page renders hidden stat fields for generated stats submission" do
sign_in_owner
get_state
assert_response :success
assert_includes response.body, 'name="match[w1_stat]"'
assert_includes response.body, 'name="match[w2_stat]"'
assert_includes response.body, 'data-match-state-target="w1StatField"'
assert_includes response.body, 'data-match-state-target="w2StatField"'
end
test "posting a match update from match state respects redirect_to param" do
sign_in_owner
get :state, params: { id: @match.id, redirect_to: "/mats/#{@match.mat_id}" }
patch :update, params: {
id: @match.id,
redirect_to: "/mats/#{@match.mat_id}",
match: { score: "3-1", win_type: "Decision", winner_id: @match.w1, finished: 1 }
}
assert_redirected_to "/mats/#{@match.mat_id}"
end
test "posting a match update broadcasts match data and cached scoreboard state" do
sign_in_owner
scoreboard_state = {
"participantScores" => { "w1" => 3, "w2" => 1 },
"metadata" => { "boutNumber" => @match.bout_number }
}
stream = MatchChannel.broadcasting_for(@match)
ActionCable.server.pubsub.clear
ActionCable.server.pubsub.broadcasts(stream).clear
Rails.cache.write(
"tournament:#{@match.tournament_id}:match:#{@match.id}:scoreboard_state",
scoreboard_state
)
patch :update, params: {
id: @match.id,
match: {
w1_stat: "T3",
w2_stat: "E1",
score: "3-1",
win_type: "Decision",
winner_id: @match.w1,
finished: 1
}
}
payload = JSON.parse(ActionCable.server.pubsub.broadcasts(stream).last)
assert_equal(
{
"w1_stat" => "T3",
"w2_stat" => "E1",
"score" => "3-1",
"win_type" => "Decision",
"winner_id" => @match.w1,
"winner_name" => @match.wrestler1.name,
"finished" => 1,
"scoreboard_state" => scoreboard_state
},
payload
)
end
test "logged in tournament delegate should post update match" do
sign_in_tournament_delegate

View File

@@ -31,6 +31,30 @@ class MatsControllerTest < ActionController::TestCase
get :show, params: { id: 1 }
end
def get_state
get :state, params: { id: @mat.id }
end
def get_state_with_params(extra_params = {})
get :state, params: { id: @mat.id }.merge(extra_params)
end
def get_scoreboard
get :scoreboard, params: { id: @mat.id }
end
def get_scoreboard_with_params(extra_params = {})
get :scoreboard, params: { id: @mat.id }.merge(extra_params)
end
def post_select_match(extra_params = {})
post :select_match, params: { id: @mat.id, match_id: @match.id, bout_number: @match.bout_number }.merge(extra_params)
end
def post_select_match_with_params(extra_params = {})
post :select_match, params: { id: @mat.id }.merge(extra_params)
end
def post_update
patch :update, params: { id: @mat.id, mat: {name: @mat.name, tournament_id: @mat.tournament_id} }
end
@@ -211,6 +235,18 @@ class MatsControllerTest < ActionController::TestCase
show
redirect
end
test "logged in user should not get state mat page" do
sign_in_non_owner
get_state
redirect
end
test "logged in user should not get scoreboard mat page" do
sign_in_non_owner
get_scoreboard
redirect
end
test "logged school delegate should not get show mat" do
sign_in_school_delegate
@@ -218,11 +254,116 @@ class MatsControllerTest < ActionController::TestCase
redirect
end
test "logged school delegate should not get state mat page" do
sign_in_school_delegate
get_state
redirect
end
test "logged school delegate should not get scoreboard mat page" do
sign_in_school_delegate
get_scoreboard
redirect
end
test "non logged in user should not get state mat page" do
get_state
redirect
end
test "non logged in user should not get scoreboard mat page" do
get_scoreboard
redirect
end
test "valid school permission key cannot get state mat page" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_state_with_params(school_permission_key: "valid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "invalid school permission key cannot get state mat page" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_state_with_params(school_permission_key: "invalid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "valid school permission key cannot get scoreboard mat page" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_scoreboard_with_params(school_permission_key: "valid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "invalid school permission key cannot get scoreboard mat page" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
get_scoreboard_with_params(school_permission_key: "invalid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "logged in user should not post select_match on mat" do
sign_in_non_owner
post_select_match
redirect
end
test "logged school delegate should not post select_match on mat" do
sign_in_school_delegate
post_select_match
redirect
end
test "non logged in user should not post select_match on mat" do
post_select_match
redirect
end
test "valid school permission key cannot post select_match on mat" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
post_select_match_with_params(school_permission_key: "valid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "invalid school permission key cannot post select_match on mat" do
school = @tournament.schools.first
school.update!(permission_key: "valid-school-key")
post_select_match_with_params(school_permission_key: "invalid-school-key")
assert_redirected_to "/static_pages/not_allowed"
end
test "logged in tournament owner should get show mat" do
sign_in_owner
show
success
end
test "logged in tournament owner should get state mat page" do
sign_in_owner
get_state
success
end
test "logged in tournament owner should get scoreboard mat page" do
sign_in_owner
get_scoreboard
success
end
test "logged in tournament owner can post select_match on mat" do
sign_in_owner
post_select_match
assert_response :no_content
end
test "logged in tournament delegate should get show mat" do
sign_in_tournament_delegate
@@ -230,6 +371,118 @@ class MatsControllerTest < ActionController::TestCase
success
end
test "logged in tournament delegate should get state mat page" do
sign_in_tournament_delegate
get_state
success
end
test "logged in tournament delegate should get scoreboard mat page" do
sign_in_tournament_delegate
get_scoreboard
success
end
test "state mat page renders queue buttons and mat-state controller" do
sign_in_owner
get_state
assert_response :success
assert_includes response.body, "data-controller=\"mat-state\""
assert_includes response.body, "Queue 1:"
assert_includes response.body, "Queue 2:"
assert_includes response.body, "Queue 3:"
assert_includes response.body, "Queue 4:"
end
test "scoreboard mat page renders match-scoreboard controller" do
sign_in_owner
get_scoreboard_with_params(print: true)
assert_response :success
assert_includes response.body, "data-controller=\"match-scoreboard\""
assert_includes response.body, "data-match-scoreboard-source-mode-value=\"localstorage\""
end
test "scoreboard mat page uses selected scoreboard match as initial bout" do
sign_in_owner
alternate_match = @mat.queue2_match
if alternate_match.nil?
alternate_match = @tournament.matches.where(mat_id: nil).first
@mat.assign_match_to_queue!(alternate_match, 2)
alternate_match = @mat.reload.queue2_match
end
@mat.set_selected_scoreboard_match!(alternate_match)
get_scoreboard
assert_response :success
assert_includes response.body, "data-match-scoreboard-initial-bout-number-value=\"#{alternate_match.bout_number}\""
end
test "state mat page renders no matches assigned when queue is empty" do
sign_in_owner
@mat.clear_queue!
get_state
assert_response :success
assert_includes response.body, "No matches assigned to this mat."
end
test "posting a match update from mat state redirects back to mat state" do
sign_in_owner
get :state, params: { id: @mat.id, bout_number: @match.bout_number }
old_controller = @controller
@controller = MatchesController.new
patch :update, params: { id: @match.id, match: { score: "3-1", win_type: "Decision", winner_id: @match.w1, finished: 1 } }
@controller = old_controller
assert_redirected_to "/mats/#{@mat.id}/state"
end
test "logged in tournament delegate can post select_match on mat" do
sign_in_tournament_delegate
post_select_match
assert_response :no_content
end
test "select_match updates selected scoreboard match" do
sign_in_owner
alternate_match = @mat.queue2_match
if alternate_match.nil?
alternate_match = @tournament.matches.where(mat_id: nil).first
@mat.assign_match_to_queue!(alternate_match, 2)
alternate_match = @mat.reload.queue2_match
end
post :select_match, params: { id: @mat.id, match_id: alternate_match.id, bout_number: alternate_match.bout_number }
assert_response :no_content
assert_equal alternate_match.id, @mat.selected_scoreboard_match&.id
end
test "select_match updates last match result without changing selected match" do
sign_in_owner
@mat.set_selected_scoreboard_match!(@match)
post :select_match, params: { id: @mat.id, last_match_result: "106 lbs - Winner Decision Loser 3-1" }
assert_response :no_content
assert_equal @match.id, @mat.selected_scoreboard_match&.id
assert_equal "106 lbs - Winner Decision Loser 3-1", @mat.last_match_result_text
end
test "select_match returns unprocessable entity for a non queued match without last result" do
sign_in_owner
non_queued_match = @tournament.matches.where(mat_id: nil).first
post :select_match, params: { id: @mat.id, match_id: non_queued_match.id, bout_number: non_queued_match.bout_number }
assert_response :unprocessable_entity
end
test "ads are hidden on mat show" do
sign_in_owner
show

View File

@@ -28,6 +28,10 @@ class TournamentsControllerTest < ActionController::TestCase
get :up_matches, params: { id: 1 }
end
def get_live_scores
get :live_scores, params: { id: 1 }
end
def get_qrcode(params = {})
get :qrcode, params: { id: 1 }.merge(params)
end
@@ -591,6 +595,116 @@ class TournamentsControllerTest < ActionController::TestCase
end
# END UP MATCHES PAGE PERMISSIONS
# LIVE SCORES PAGE PERMISSIONS WHEN TOURNAMENT IS NOT PUBLIC
test "logged in school delegate cannot get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
sign_in_school_delegate
get_live_scores
redirect
end
test "logged in user cannot get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
sign_in_non_owner
get_live_scores
redirect
end
test "logged in tournament delegate can get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
sign_in_delegate
get_live_scores
success
end
test "logged in tournament owner can get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
sign_in_owner
get_live_scores
success
end
test "non logged in user cannot get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
get_live_scores
redirect
end
test "non logged in user with valid school permission key cannot get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
@school.update(permission_key: "valid-key")
get :live_scores, params: { id: @tournament.id, school_permission_key: "valid-key" }
redirect
end
test "non logged in user with invalid school permission key cannot get live scores page when tournament is not public" do
@tournament.is_public = false
@tournament.save
@school.update(permission_key: "valid-key")
get :live_scores, params: { id: @tournament.id, school_permission_key: "invalid-key" }
redirect
end
# LIVE SCORES PAGE PERMISSIONS WHEN TOURNAMENT IS PUBLIC
test "logged in school delegate can get live scores page when tournament is public" do
@tournament.is_public = true
@tournament.save
sign_in_school_delegate
get_live_scores
success
end
test "logged in user can get live scores page when tournament is public" do
@tournament.is_public = true
@tournament.save
sign_in_non_owner
get_live_scores
success
end
test "logged in tournament delegate can get live scores page when tournament is public" do
@tournament.is_public = true
@tournament.save
sign_in_delegate
get_live_scores
success
end
test "logged in tournament owner can get live scores page when tournament is public" do
@tournament.is_public = true
@tournament.save
sign_in_owner
get_live_scores
success
end
test "non logged in user can get live scores page when tournament is public" do
@tournament.is_public = true
@tournament.save
get_live_scores
success
end
test "live scores page renders mat cards and scoreboard controller" do
@tournament.is_public = true
@tournament.save
get_live_scores
success
assert_includes response.body, "Live Scores"
assert_includes response.body, "data-controller=\"match-scoreboard\""
assert_includes response.body, "data-match-scoreboard-source-mode-value=\"mat_websocket\""
assert_includes response.body, "data-match-scoreboard-display-mode-value=\"embedded\""
assert_includes response.body, "Last Match Result"
assert_includes response.body, "Stats"
assert_not_includes response.body, "Result</h4>"
end
# ALL_RESULTS PAGE PERMISSIONS WHEN TOURNAMENT IS NOT PUBLIC
test "logged in school delegate cannot get all_results page when tournament is not public" do
@tournament.is_public = false