<commit_before>require_relative "../test_helper"

class EventsControllerTest < ActionController::TestCase

  def setup
    @profile = create_user('testuser').person
  end
  attr_reader :profile

  should 'list today events by default' do
    profile.events << Event.new(:name => '<NAME>', :start_date => DateTime.now)
    profile.events << Event.new(:name => '<NAME>', :start_date => DateTime.now)

    get :events, :profile => profile.identifier

    today = DateTime.now.strftime("%B %d, %Y").html_safe
    assert_tag :tag => 'div', :attributes => {:id => "agenda-items"},
      :descendant => {:tag => 'h3', :content => "Events for #{today}"},
      :descendant => {:tag => 'tr', :content => "<NAME>"},
      :descendant => {:tag => 'tr', :content => "<NAME>"}
  end

  should 'display calendar of current month' do
    get :events, :profile => profile.identifier

    month = DateTime.now.strftime("%B %Y")
    assert_tag :tag => 'table', :attributes => {:class => /current-month/}, :descendant => {:tag => 'caption', :content => /#{month}/}
  end

  should 'display links to previous and next month' do
    get :events, :profile => profile.identifier

    prev_month = DateTime.now - 1.month
    next_month = DateTime.now + 1.month
    prev_month_name = prev_month.strftime("%B")
    next_month_name = next_month.strftime("%B")
    assert_tag :tag =>'a', :attributes => {:href => "/profile/#{profile.identifier}/events/#{prev_month.year}/#{prev_month.month}"}, :content => prev_month_name
    assert_tag :tag =>'a', :attributes => {:href => "/profile/#{profile.identifier}/events/#{next_month.year}/#{next_month.month}"}, :content => next_month_name
  end

  should 'see the events paginated' do
    30.times do |i|
      profile.events << Event.new(:name => "Lesson #{i}", :start_date => DateTime.now)
    end
    get :events, :profile => profile.identifier
    assert_equal 20, assigns(:events).size
  end

  should "show events for current month only" do
    profile.events << Event.create(:name => '<NAME>', :start_date => DateTime.now.at_end_of_month - 1)
    profile.events << Event.create(:name => '<NAME>', :start_date => DateTime.now + 31)

    get :events, :profile => profile.identifier

    assert_no_tag :tag =>'a', :content => /Joao Birthday/
    assert_tag :tag =>'a', :content => /Maria Birthday/
  end

  should 'show events of specific day' do
    profile.events << Event.new(:name => '<NAME>', :start_date => DateTime.new(2009, 10, 28))

    get :events_by_day, :profile => profile.identifier, :year => 2009, :month => 10, :day => 28

    assert_tag :tag => 'a', :content => /Joao Birthday/
  end

  should 'not show events page to non members of private community' do<commit_msg>Fix unstable test of events controller<commit_after>require_relative "../test_helper"

class EventsControllerTest < ActionController::TestCase

  def setup
    @profile = create_user('testuser').person
  end
  attr_reader :profile

  should 'list today events by default' do
    profile.events << Event.new(:name => '<NAME>', :start_date => DateTime.now)
    profile.events << Event.new(:name => '<NAME>', :start_date => DateTime.now)

    get :events, :profile => profile.identifier

    today = DateTime.now.strftime("%B %d, %Y").html_safe
    assert_tag :tag => 'div', :attributes => {:id => "agenda-items"},
      :descendant => {:tag => 'h3', :content => "Events for #{today}"},
      :descendant => {:tag => 'tr', :content => "Joao Birthday"},
      :descendant => {:tag => 'tr', :content => "<NAME>"}
  end

  should 'display calendar of current month' do
    get :events, :profile => profile.identifier

    month = DateTime.now.strftime("%B %Y")
    assert_tag :tag => 'table', :attributes => {:class => /current-month/}, :descendant => {:tag => 'caption', :content => /#{month}/}
  end

  should 'display links to previous and next month' do
    get :events, :profile => profile.identifier

    prev_month = DateTime.now - 1.month
    next_month = DateTime.now + 1.month
    prev_month_name = prev_month.strftime("%B")
    next_month_name = next_month.strftime("%B")
    assert_tag :tag =>'a', :attributes => {:href => "/profile/#{profile.identifier}/events/#{prev_month.year}/#{prev_month.month}"}, :content => prev_month_name
    assert_tag :tag =>'a', :attributes => {:href => "/profile/#{profile.identifier}/events/#{next_month.year}/#{next_month.month}"}, :content => next_month_name
  end

  should 'see the events paginated' do
    30.times do |i|
      profile.events << Event.new(:name => "Lesson #{i}", :start_date => DateTime.now)
    end
    get :events, :profile => profile.identifier
    assert_equal 20, assigns(:events).size
  end

  should "show events for current month only" do
    profile.events << Event.create(:name => '<NAME>', :start_date => DateTime.now.in_time_zone.at_end_of_month - 1)
    profile.events << Event.create(:name => '<NAME>', :start_date => DateTime.now + 31)

    get :events, :profile => profile.identifier

    assert_no_tag :tag =>'a', :content => /Joao Birthday/
    assert_tag :tag =>'a', :content => /Maria Birthday/
  end

  should 'show events of specific day' do
    profile.events << Event.new(:name => '<NAME>', :start_date => DateTime.new(2009, 10, 28))

    get :events_by_day, :profile => profile.identifier, :year => 2009, :month => 10, :day => 28

    assert_tag :tag => 'a', :content => /<NAME>/
  end

  should 'not show events page to non members of private community' do
{'fraction_non_alphanumeric': 0.17168564083731178, 'fraction_numerical': 0.009548292324641938, 'mean_word_length': 3.885201793721973, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4140890', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1665, 'n_words': 466}