<commit_before>require "test/test_helper"

class UseVanityController < ActionController::Base
  attr_accessor :current_user

  def index
    render :text=>ab_test(:pie_or_cake)
  end
end

# Pages accessible to everyone, e.g. sign in, community search.
class UseVanityControllerTest < ActionController::TestCase
  tests UseVanityController

  def setup
    super
    metric :sugar_high
    new_ab_test :pie_or_cake do
      metrics :sugar_high
    end
    UseVanityController.class_eval do
      use_vanity :current_user
    end
    if ::Rails.respond_to?(:application) # Rails 3 configuration
      ::Rails.application.config.session_options[:domain] = '.foo.bar'
    end
  end

  def test_chooses_sets_alternatives_for_rails_tests
    experiment(:pie_or_cake).chooses(true)
    get :index
    assert_equal 'true', @response.body
 
    experiment(:pie_or_cake).chooses(false)
    get :index
    assert_equal 'false', @response.body
  end


  def test_vanity_cookie_is_persistent
    get :index
    cookie = @response["Set-Cookie"].to_s
    assert_match cookie, /vanity_id=[a-f0-9]{32};/
    expires = cookie[/expires=(.*)(;|$)/, 1]
    assert expires
    assert_in_delta Time.parse(expires), Time.now + 1.month, 1.day
  end

  def test_vanity_cookie_default_id
    get :index
    assert cookies['vanity_id'] =~ /^[a-f0-9]{32}$/
  end

  def test_vanity_cookie_retains_id
    @request.cookies['vanity_id'] = "from_last_time"
    get :index
    assert_equal "from_last_time", cookies['vanity_id']
  end

  def test_vanity_identity_set_from_cookie
    @request.cookies['vanity_id'] = "from_last_time"
    get :index<commit_msg>fix 1.8 compatibility<commit_after>require "test/test_helper"

class UseVanityController < ActionController::Base
  attr_accessor :current_user

  def index
    render :text=>ab_test(:pie_or_cake)
  end
end

# Pages accessible to everyone, e.g. sign in, community search.
class UseVanityControllerTest < ActionController::TestCase
  tests UseVanityController

  def setup
    super
    metric :sugar_high
    new_ab_test :pie_or_cake do
      metrics :sugar_high
    end
    UseVanityController.class_eval do
      use_vanity :current_user
    end
    if ::Rails.respond_to?(:application) # Rails 3 configuration
      ::Rails.application.config.session_options[:domain] = '.foo.bar'
    end
  end

  def test_chooses_sets_alternatives_for_rails_tests
    experiment(:pie_or_cake).chooses(true)
    get :index
    assert_equal 'true', @response.body
 
    experiment(:pie_or_cake).chooses(false)
    get :index
    assert_equal 'false', @response.body
  end


  def test_vanity_cookie_is_persistent
    get :index
    cookie = @response["Set-Cookie"].to_s
    assert_match /vanity_id=[a-f0-9]{32};/, cookie
    expires = cookie[/expires=(.*)(;|$)/, 1]
    assert expires
    assert_in_delta Time.parse(expires), Time.now + 1.month, 1.day
  end

  def test_vanity_cookie_default_id
    get :index
    assert cookies['vanity_id'] =~ /^[a-f0-9]{32}$/
  end

  def test_vanity_cookie_retains_id
    @request.cookies['vanity_id'] = "from_last_time"
    get :index
    assert_equal "from_last_time", cookies['vanity_id']
  end

  def test_vanity_identity_set_from_cookie
    @request.cookies['vanity_id'] = "from_last_time"
    get :index
{'fraction_non_alphanumeric': 0.10104422604422604, 'fraction_numerical': 0.007985257985257985, 'mean_word_length': 3.9498480243161094, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7177132', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1105, 'n_words': 223}