135 lines
5.6 KiB
Ruby
135 lines
5.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
require 'securerandom'
|
|
require_relative '../../../e2e/lib/api_client'
|
|
require_relative '../../../e2e/lib/action_cable_client'
|
|
|
|
RSpec.describe 'Tournament timer websocket HTTP E2E' do
|
|
required_env_vars = %w[
|
|
TURNIERE_E2E_BASE_URL
|
|
TURNIERE_E2E_EMAIL
|
|
TURNIERE_E2E_PASSWORD
|
|
].freeze
|
|
|
|
before do
|
|
missing_vars = required_env_vars.reject { |name| ENV.key?(name) }
|
|
skip("HTTP E2E requires #{missing_vars.join(', ')}") unless missing_vars.empty?
|
|
end
|
|
|
|
let(:base_url) { ENV.fetch('TURNIERE_E2E_BASE_URL') }
|
|
let(:owner_email) { ENV.fetch('TURNIERE_E2E_EMAIL') }
|
|
let(:owner_password) { ENV.fetch('TURNIERE_E2E_PASSWORD') }
|
|
let(:client) { login_client }
|
|
|
|
it 'broadcasts timer state changes over websocket' do
|
|
tournament = create_playoff_tournament(name_prefix: 'Ws Timer')
|
|
|
|
cable_client = TurniereE2E::ActionCableClient.new(base_url: base_url)
|
|
cable_client.subscribe_timer!(tournament_id: tournament.fetch(:id))
|
|
|
|
initial_payload = cable_client.wait_for_timer_payload!
|
|
expect(initial_payload.fetch('timestamp')).to be_nil
|
|
expect(initial_payload.fetch('timer_mode')).to be_nil
|
|
expect(initial_payload.fetch('timer_reason')).to be_nil
|
|
expect(initial_payload.fetch('timer_reason_text')).to be_nil
|
|
|
|
countdown = client.patch("/tournaments/#{tournament.fetch(:id)}/timer", body: {
|
|
timestamp_seconds: 120,
|
|
timer_reason: 'playoff_start'
|
|
})
|
|
expect(countdown[:status]).to eq(200)
|
|
|
|
countdown_payload = wait_for_timer_state!(cable_client, timer_mode: 'countdown')
|
|
expect(countdown_payload.fetch('timestamp')).to eq(countdown.dig(:json, :timestamp))
|
|
expect(countdown_payload.fetch('timer_reason')).to eq('playoff_start')
|
|
expect(countdown_payload.fetch('timer_reason_text')).to be_nil
|
|
|
|
reason_update = client.patch("/tournaments/#{tournament.fetch(:id)}/timer", body: {
|
|
timer_reason: 'custom',
|
|
timer_reason_text: 'Reason changed after start'
|
|
})
|
|
expect(reason_update[:status]).to eq(200)
|
|
|
|
reason_payload = wait_for_timer_payload_matching!(cable_client) do |payload|
|
|
payload.fetch('timestamp') == countdown.dig(:json, :timestamp) &&
|
|
payload.fetch('timer_mode') == 'countdown' &&
|
|
payload.fetch('timer_reason') == 'custom' &&
|
|
payload.fetch('timer_reason_text') == 'Reason changed after start'
|
|
end
|
|
expect(reason_payload.fetch('timestamp')).to eq(countdown.dig(:json, :timestamp))
|
|
|
|
reason_clear = client.patch("/tournaments/#{tournament.fetch(:id)}/timer", body: {
|
|
timer_reason: nil
|
|
})
|
|
expect(reason_clear[:status]).to eq(200)
|
|
|
|
reason_cleared_payload = wait_for_timer_payload_matching!(cable_client) do |payload|
|
|
payload.fetch('timestamp') == countdown.dig(:json, :timestamp) &&
|
|
payload.fetch('timer_mode') == 'countdown' &&
|
|
payload.fetch('timer_reason').nil? &&
|
|
payload.fetch('timer_reason_text').nil?
|
|
end
|
|
expect(reason_cleared_payload.fetch('timestamp')).to eq(countdown.dig(:json, :timestamp))
|
|
|
|
countup = client.patch("/tournaments/#{tournament.fetch(:id)}/timer", body: {
|
|
timer_mode: 'countup',
|
|
timer_reason: 'custom',
|
|
timer_reason_text: 'Manual overtime'
|
|
})
|
|
expect(countup[:status]).to eq(200)
|
|
|
|
countup_payload = wait_for_timer_state!(cable_client, timer_mode: 'countup')
|
|
expect(countup_payload.fetch('timestamp')).to eq(countup.dig(:json, :timestamp))
|
|
expect(countup_payload.fetch('timer_reason')).to eq('custom')
|
|
expect(countup_payload.fetch('timer_reason_text')).to eq('Manual overtime')
|
|
|
|
clear = client.patch("/tournaments/#{tournament.fetch(:id)}/timer", body: { clear_timer: true })
|
|
expect(clear[:status]).to eq(200)
|
|
|
|
cleared_payload = wait_for_timer_state!(cable_client, timer_mode: nil)
|
|
expect(cleared_payload.fetch('timestamp')).to be_nil
|
|
expect(cleared_payload.fetch('timer_reason')).to be_nil
|
|
expect(cleared_payload.fetch('timer_reason_text')).to be_nil
|
|
ensure
|
|
cable_client&.close
|
|
end
|
|
|
|
def login_client
|
|
api_client = TurniereE2E::ApiClient.new(base_url: base_url)
|
|
response = api_client.login!(email: owner_email, password: owner_password)
|
|
|
|
expect(response[:status]).to eq(200)
|
|
api_client
|
|
end
|
|
|
|
def create_playoff_tournament(name_prefix:)
|
|
response = client.post('/tournaments', body: {
|
|
name: "#{name_prefix} #{SecureRandom.hex(3)}",
|
|
description: 'Websocket timer tournament',
|
|
public: false,
|
|
teams: 4.times.map do |index|
|
|
{ name: "#{name_prefix.tr(' ', '')}-#{index + 1}" }
|
|
end
|
|
})
|
|
expect(response[:status]).to eq(201)
|
|
response.fetch(:json)
|
|
end
|
|
|
|
def wait_for_timer_state!(cable_client, timer_mode:, timeout: 20)
|
|
wait_for_timer_payload_matching!(cable_client, timeout: timeout) do |payload|
|
|
payload.fetch('timer_mode') == timer_mode
|
|
end
|
|
end
|
|
|
|
def wait_for_timer_payload_matching!(cable_client, timeout: 20)
|
|
deadline = Time.now + timeout
|
|
loop do
|
|
payload = cable_client.wait_for_timer_payload!(timeout: [deadline - Time.now, 0.1].max)
|
|
return payload if yield(payload)
|
|
|
|
raise "timer websocket payload not received within #{timeout}s" if Time.now >= deadline
|
|
end
|
|
end
|
|
end
|