$LOAD_PATH << File.join(File.dirname(__FILE__), '..')

ENV['RACK_ENV'] = 'test'

require 'rspec'
require 'rack/test'
require 'rack/utils'
require 'capybara/rspec'
require 'capybara/webkit'
require 'capybara/node/matchers'
require 'app'

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run :focus => true

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'

  config.include Rack::Test::Methods
  # Capybara.app = Sinatra::Application
  Capybara.app = Rack::ShowExceptions.new(Sinatra::Application)
  Capybara.automatic_reload = false
  Capybara.default_driver    = :webkit
  Capybara.javascript_driver = :webkit

  module Capybara
    module Node
      module Matchers
        # This matcher that will perform a scan of the given keywords
        # in the specified order across the node's content, regardless
        # of what's in between the keywords.
        #
        # Usage example: some HTML node might have the following inner text:
        # => 'The payment method "Cash" has been removed.'
        #
        # That content can be matched by:
        #
        # => has_keywords?('method removed')          # => true
        # => has_keywords?('method payment removed')  # => false
        def has_keywords?(*keywords)
          if keywords.size == 1
            keywords = keywords.first.split(/\s/)
          end
          if self.respond_to?(:has_text?)
            has_text?(Regexp.new(keywords.join('.*')))
          else
            normalize_whitespace(text).match(Regexp.new(keywords.join('.*')))
          end
        end

        alias_method :have_keywords, :has_keywords?
      end
    end
  end

  set :courier_service_enabled, false
end

def mockup_user(q = {})
  User.destroy

  @some_salt = Algol.salt
  @mockup_user_params = {
    name: 'Mysterious Mocker',
    email: 'very@mysterious.com',
    provider: 'algol',
    password:               User.encrypt(@some_salt),
    password_confirmation:  User.encrypt(@some_salt)
  }.merge(q)
  @user = @u = User.create(@mockup_user_params)
end

FlashTypes = [ 'notice', 'error', 'warning' ]

def should_flash(type, keywords)
  page.should have_selector('.flashes.' + type.to_s)
  page.find('.flashes.' + type.to_s).should have_keywords(keywords)
end

def should_only_flash(type, keywords)
  FlashTypes.each { |excluded_type|
    next if excluded_type == type.to_s
    page.should_not have_selector(".flashes.#{excluded_type}")
  }

  should_flash(type, keywords)
end