# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require "spec_helper"
require "factory_bot_rails"
FB = FactoryBot
require "factories/test_users"

ENV["RAILS_ENV"] ||= "test"

require_relative "dummy/config/environment"

puts "Rails version: #{Rails.version}" if ENV["GITHUB_ACTIONS"] == "true"

abort("The Rails environment is running in production mode!") if Rails.env.production?

require "rspec/rails"
require "setup/vcr"
require "setup/capybara"
require "raif/evals"

ActiveRecord::Migrator.migrations_paths = [File.expand_path("../spec/dummy/db/migrate", __dir__)]

Raif::Engine.root.glob("spec/support/**/*.rb").sort_by(&:to_s).each { |f| require f }
Raif::Engine.root.glob("spec/shared_examples/**/*.rb").sort_by(&:to_s).each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  abort e.to_s.strip
end

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_paths = [
    Rails.root.join("spec/fixtures")
  ]

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # You can uncomment this line to turn off ActiveRecord support entirely.
  # config.use_active_record = false

  # RSpec Rails uses metadata to mix in different behaviours to your tests,
  # for example enabling you to call `get` and `post` in request specs. e.g.:
  #
  #     RSpec.describe UsersController, type: :request do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://rspec.info/features/7-1/rspec-rails
  #
  # You can also this infer these behaviours automatically by location, e.g.
  # /spec/models would pull in the same behaviour as `type: :model` but this
  # behaviour is considered legacy and will be removed in a future version.
  #
  # To enable this behaviour uncomment the line below.
  # config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")

  config.include Raif::RspecHelpers
  config.include ActiveJob::TestHelper

  config.before(:each) do
    # If we're using a VCR cassette, we always want to allow LLM API requests.
    if VCR.current_cassette.present?
      allow(Raif.config).to receive(:llm_api_requests_enabled){ true }
    end
  end
end
