# frozen_string_literal: true

require "selenium-webdriver"

Capybara.default_max_wait_time = 2
Capybara.server = :puma, { Silent: true }
Capybara.disable_animation = true # injects CSP-incompatible CSS and JS

module SystemTestHelpers
  [
    :accept_alert,
    :dismiss_alert,
    :accept_confirm,
    :dismiss_confirm,
    :accept_prompt,
    :dismiss_prompt,
    :accept_modal,
    :dismiss_modal
  ].each do |driver_method|
    define_method(driver_method) do |text = nil, **options, &blk|
      super(text, **options, &blk)
    rescue Capybara::NotSupportedByDriverError
      blk.call
    end
  end
end

RSpec.configure do |config|
  config.include ActionView::RecordIdentifier, type: :system
  config.include SystemTestHelpers, type: :system

  config.before(:each, type: :system) do |example|
    ActiveRecord::Base.connection.disable_query_cache!

    if ENV['SHOW_BROWSER']
      example.metadata[:js] = true
      driven_by :selenium, using: :chrome, screen_size: [ 1024, 800 ]
    else
      driven_by :rack_test
    end
  end

  config.before(:each, :js, type: :system) do
    # Chrome's no-sandbox option is required for running in Docker
    driven_by :selenium, using: (ENV['SHOW_BROWSER'] ? :chrome : :headless_chrome), screen_size: [ 1024, 800 ] do |driver_options|
      driver_options.add_argument("--disable-dev-shm-usage")
      driver_options.add_argument("--no-sandbox")
    end
  end
end
