require 'rspec/rails'
require 'pry-rails'
require 'faker'
require 'factory_bot_rails'
require 'database_cleaner'
require 'sass-rails'
require 'authlogic/test_case'

Dir.glob(File.join(__dir__, 'support', '*.rb')).sort.each { |f| require f }
# ActiveRecord::Migration.maintain_test_schema!

# Paperclip::Attachment.default_options[:path] = "#{Rails.root}/spec/test_files/:class/:id_partition/:style.:extension"
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  # config.infer_spec_type_from_file_location!
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    #     be_bigger_than(2).and_smaller_than(4).description
    #     # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #     # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end

  config.include FactoryBot::Syntax::Methods
  config.include Authlogic::TestCase

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation, except: %w[ar_internal_metadata])
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    Rails.application.config.action_mailer.delivery_method = :test
    Symphonia::User.current = nil
  end

  config.before(:each, type: :feature) do
    # :rack_test driver's Rack app under test shares database connection
    # with the specs, so continue to use transaction strategy for speed.
    unless Capybara.current_driver == :rack_test
      # Driver is probably for an external browser with an app
      # under test that does *not* share a database connection with the
      # specs, so use truncation strategy.
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    ActiveJob::Base.queue_adapter = :test
    DatabaseCleaner.start
  end

  config.after(:suite) do
    FileUtils.rm_rf(Dir["#{Rails.root}/spec/test_files/"])
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
  end

  config.use_transactional_fixtures = false
  config.infer_spec_type_from_file_location!
  config.filter_rails_from_backtrace!
  config.profile_examples = true

  config.before(:each) do |ex|
    meta = ex.metadata
    unless meta[:null]
      case meta[:logged]
      when :admin
        allow(controller).to receive(:current_user).and_return(admin_user) if meta[:type] == :controller
        allow(Symphonia::User).to receive(:current).and_return(admin_user)
      when true, 'true'
        user = regular_user
        allow(controller).to receive(:current_user).and_return(user) if meta[:type] == :controller
        allow(Symphonia::User).to receive(:current).and_return(user)
      end
    end
  end
end