# frozen_string_literal: true

require "simplecov"
SimpleCov.start do
  coverage_dir "coverage/#{ENV.fetch("CIRCLE_JOB", "test")}/#{ENV.fetch("CIRCLE_NODE_INDEX", 0)}"
end

ENV["AAA_FAKE_MODE"] = "true"

require "cucumber/rails"
require "sidekiq/testing"
require "selenium-webdriver"
require "webdrivers" unless File.exist?("/usr/bin/chromedriver")
require "percy"
require "uri"

ActionController::Base.allow_rescue = false

begin
  DatabaseCleaner.allow_remote_database_url = true
  DatabaseCleaner.strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

Cucumber::Rails::Database.javascript_strategy = :truncation

World(FactoryBot::Syntax::Methods)
FactoryBot.find_definitions

# Capybara ====================================================================
require Rails.root.join("features", "support", "helpers", "capybara_helper")

CapybaraHelper.configure

Before do
  CapybaraHelper.reset_browser
end

# Webmock =====================================================================

Before do
  sales_tax_url = ENV.fetch("TEACHABLE_API_BASE_URL", "http://marketing-api.worksonmy.computer:3000") + "/calculate_sales_tax"
  stub_request(
    :get,
    /#{Regexp.quote(sales_tax_url)}\?amount=(\d+)&state=([a-zA-Z]+)&zip=(.*)/
  ).to_return(
    status: 200,
    body: { tax_amount: 0, tax_rate: 0 }.to_json
  )

end

# VCR =========================================================================

require Rails.root.join("features", "support", "helpers", "vcr_helper")

VcrHelper.configure
Around do |scenario, block|
  VcrHelper.around_scenario(scenario, &block)
end

# Sidekiq =========================================================================
# default testing environment is Sidekiq::Testing.fake! when you require Sidekiq::Testing

After do
  Sidekiq::Worker.clear_all
end

# AAA  ======================================================================

Before do
  AAA.client.connection.reset!
end
