# frozen_string_literal: true

unless File.exist?("spec/test_app")
  puts "Missing testing app in spec/test_app !" # FIXME: 'Run `bundle exec bin/calagator new spec/dummy --dummy` to generate one.'
  exit 1
end

require "simplecov"

require "rails_helper"
require "rspec-activemodel-mocks"
require "rspec/its"
require "rspec-rails"
require "rspec/collection_matchers"
require "factory_bot_rails"
require "capybara"
require "capybara/cuprite"
require "capybara/rspec"
require "database_cleaner"
require "timecop"
require "webmock"

if Object.const_defined? :SimpleCov
  SimpleCov.command_name "rspec"
end

# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, make a
# separate helper file that requires this one and then use it only in the specs
# that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
  config.use_transactional_fixtures = false

  config.before(:suite) do |_example|
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before do |example|
    DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
    DatabaseCleaner.start
  end

  config.after do
    DatabaseCleaner.clean
  end

  # Capybara.register_driver :chrome_headless do |app|
  #   args = Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu no-sandbox --window-size=1240,1400])
  #   Capybara::Selenium::Driver.new(
  #     app,
  #     browser: :chrome,
  #     desired_capabilities: args
  #   )
  # end

  Capybara.current_driver = :cuprite

  Capybara.server = :webrick
  Capybara.default_driver = :rack_test
  Capybara.default_max_wait_time = 5
  Capybara.javascript_driver = :cuprite
  Capybara.always_include_port = true
  Capybara.register_driver(:cuprite) do |app|
    Capybara::Cuprite::Driver.new(
      app,
      window_size: [1200, 800],
      inspector: ENV["INSPECTOR"]
    )
  end

  # These two settings work together to allow you to limit a spec run
  # to individual examples or groups you care about by tagging them with
  # `:focus` metadata. When nothing is tagged with `:focus`, all examples
  # get run.
  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  # Many RSpec users commonly either run the entire suite or an individual
  # file, and it's useful to allow more verbose output when running an
  # individual spec file.
  if config.files_to_run.one?
    # Use the documentation formatter for detailed output,
    # unless a formatter has already been configured
    # (e.g. via a command-line flag).
    config.default_formatter = "doc"
  end

  # Print the 10 slowest examples and example groups at the
  # end of the spec run, to help surface which specs are running
  # particularly slow.
  # config.profile_examples = 10

  # 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

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed

  # 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|
    # Enable only the newer, non-monkey-patching expect syntax.
    # For more details, see:
    #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
    expectations.syntax = %i[should expect]
  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|
    # Enable only the newer, non-monkey-patching expect syntax.
    # For more details, see:
    #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
    mocks.syntax = %i[should expect]

    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended.
    # mocks.verify_partial_doubles = true
  end

  # Cleanup of seleniunm deprecation warning when running test suite
  # Selenium generating this redundant warning.
  # WARN Selenium [:clear_local_storage] [DEPRECATION] clear_local_storage is deprecated and will be removed in a future release.
  # Will occur until capybarra addresses it
  # Workaround is to ignore these from the logger
  # https://github.com/teamcapybara/capybara/issues/2779
  # If you can remove the line below and running `rspec spec` doesnt blow up with the above warning, then you can remove this config item.
  # Selenium::WebDriver.logger.ignore(:clear_local_storage, :clear_session_storage)

  # Reset to the Sql search engine before each test. Individual tests that
  # override this use "around"; we'll use "around" here too to ensure that
  # this block runs before the individual test's override.
  config.around do |example|
    Calagator::Event::SearchEngine.use(:sql)
    Calagator::Venue::SearchEngine.use(:sql)
    example.run
  end

  config.alias_example_to :fscenario, focus: true

  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include Devise::Test::IntegrationHelpers, type: :request
end
