require 'test_helper'

class PentestTeamTest < ActiveSupport::TestCase
  def setup
    @game = create(:active_game)
    @division = create(:division)
    @defensive_team = create(:team, division: @division)
    @offensive_team = create(:team, division: @division)
    @challenge = create(:pentest_challenge,
                        defense_period: 4, # Hours
                        defense_points: 1000,
                        flag_count: 0
                       )
  end

  test 'calc defensive points in a pentest division' do
    create(:defense_flag, challenge: @challenge, team: @defensive_team)
    # calc_defensive_points returns results in a hash in the form {"challenge_name"=>point_value}
    assert_equal 0, @defensive_team.calc_defensive_points[@challenge.name]
    @game.update(start: 2.hours.ago)
    assert_equal 500, @defensive_team.calc_defensive_points[@challenge.name]
    # Solved challenge happens back an hour in time causing the defense points to decrease
    create(:pentest_solved_challenge, team: @offensive_team, challenge: @challenge, created_at: 1.hour.ago)
    assert_equal 250, @defensive_team.reload.calc_defensive_points[@challenge.name]
  end

end
