# frozen_string_literal: true

require "simplecov"
require "webdrivers/geckodriver"

SimpleCov.start("rails") do
  add_filter "app/admin"
end

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../config/environment", __dir__)
require "rspec/rails"
require "email_spec"
require "rspec/active_model/mocks"
require "pundit/rspec"
require "axe-rspec"
require "capybara/rails"
require "percy/capybara"
require "flipper/adapters/pstore"

Capybara.javascript_driver = :selenium_headless

VCR.configure do |c|
  c.cassette_library_dir = "spec/fixtures/vcr_cassettes"
  c.hook_into :webmock
  # Ignore requests to github and s3 for the benefit of the webdrivers gem
  # which automatically downloads the webdriver for headless testing
  c.ignore_hosts "github.com", "release-assets.githubusercontent.com"
  c.ignore_request do |request|
    URI(request.uri).host =~ /objects\.githubusercontent\.com/
    # false
    # URI(request.uri).port == 7777
  end
  c.ignore_localhost = true
  # This will by default record new web requests in VCR. We can see
  # that this is happened because the files in spec/fixtures/vcr_cassettes
  # get appended to
  c.default_cassette_options = { record: :new_episodes }
end

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_all_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # rspec-rails 3 will no longer automatically infer an example group's spec type
  # from the file location. You can explicitly opt-in to this feature using this
  # snippet:
  config.infer_spec_type_from_file_location!

  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # 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"

  # This is a workaround for a strange thing where ActionMailer::Base.deliveries isn't being
  # cleared out correctly in feature specs. So, do it here for everything.
  config.before do
    ActionMailer::Base.deliveries = []
  end

  require "sidekiq/testing"
  # Make everything sent to sidekiq get run immediately during tests.
  # Makes everything a little easier to reason about
  Sidekiq::Testing.inline!

  config.include EmailSpec::Helpers
  config.include EmailSpec::Matchers
  config.include FactoryBot::Syntax::Methods
  config.include Devise::Test::ControllerHelpers, type: :view
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include SessionHelpers, type: :feature
  config.include MockLocationHelpers
  config.include AutocompleteHelpers

  # Disable searchkick during testing so that we don't need to run
  # elasticsearch locally which is a pain
  # Also disable rack attack because we're logging in quite a bit during testing
  config.before(:suite) do
    Searchkick.disable_callbacks
    Rack::Attack.enabled = false
  end

  config.before do
    # See https://github.com/flippercloud/flipper/issues/261#issuecomment-427138651
    FileUtils.rm "tmp/flipper.pstore", force: true
  end
end

# For testing use a pstore adapter
# Using this over the standard in-memory adapter for testing so that flipper
# will still work with capybara with js: true.
Flipper.configure do |config|
  config.adapter do
    pstore_path = Rails.root.join("tmp/flipper.pstore")
    Flipper::Adapters::PStore.new(pstore_path)
  end
end
