require 'simplecov'
require 'coveralls'
Coveralls.wear!
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
  SimpleCov::Formatter::HTMLFormatter,
  Coveralls::SimpleCov::Formatter
])
SimpleCov.start do
  add_filter '/spec'
end

require 'pry'

$:.unshift File.expand_path('../lib', __dir__)
require 'aws/xray'
require 'aws/xray/hooks/all'
require 'aws/xray/hooks/active_record'

require 'fileutils'
require 'rack/test'
require 'webmock'
require 'sentry-raven'

Aws::Xray.config.name = 'test-app'
Aws::Xray.config.version = -> { 'deadbeef' }
Aws::Xray.config.worker = Aws::Xray::Worker::Configuration.new(num: 1)
Aws::Xray.config.sampling_rate = 1
Aws::Xray.config.solr_hook_name = 'solr-test'
Aws::Xray.config.record_caller_of_http_requests = true
Aws::Xray.config.default_annotation = Aws::Xray.config.default_annotation.merge('a' => 'b')

require 'json-schema'
# Json schema for `cause` object is invalid now.
# We don't have to set `cause` and `skipped` but it requires them.
# So we can't use json schema validation if segment contains errors.
module SegmentValidator
  path = File.expand_path('schema/xray-segmentdocument-schema-v1.0.0.json', __dir__)
  @schema = JSON.parse(File.read(path))

  def self.call(json)
    JSON::Validator.validate!(@schema, json)
  end
end

require 'active_record'
ActiveRecord::Base.establish_connection(
  adapter: 'sqlite3',
  database: 'db/test.sqlite3',
  host: 'localhost',
  username: 'root',
  password: 'pass',
  timeout: 1,
)

class Item < ActiveRecord::Base
end

RSpec.configure do |config|
  config.before(:suite) do
    FileUtils.rm_rf(File.expand_path('../db', __dir__))
    FileUtils.mkdir_p(File.expand_path('../db', __dir__))

    Item.connection.execute(<<-SQL
      create table items (
        id integer primary key autoincrement,
        name text not null
      )
    SQL
    )
  end

  config.after(:each) do
    Item.delete_all
  end

  config.after(:suite) do
    FileUtils.rm_rf(File.expand_path('../db', __dir__))
  end

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups

  config.filter_run_when_matching :focus

  config.example_status_persistence_file_path = 'tmp/spec_examples.txt'

  config.disable_monkey_patching!

  config.warnings = false # because of rack-timeout

  if config.files_to_run.one?
    config.default_formatter = 'doc'
  end

  config.profile_examples = 10
  config.order = :random
  Kernel.srand config.seed
end
