# frozen_string_literal: true

require 'rails_helper'

describe GoodJob::CronEntriesController do
  around do |example|
    orig_value = ActionController::Base.allow_forgery_protection
    ActionController::Base.allow_forgery_protection = false
    example.call
    ActionController::Base.allow_forgery_protection = orig_value
  end

  describe 'PUT #disable' do
    it 'disables cron' do
      cron_entry = GoodJob::CronEntry.find(:example)
      expect do
        put good_job.disable_cron_entry_path(cron_key: 'example')
      end.to change { cron_entry.enabled? }.from(true).to(false)
      expect(response).to have_http_status(:found)
    end
  end

  describe 'PUT #enable' do
    it 'disables cron' do
      cron_entry = GoodJob::CronEntry.find(:example)
      cron_entry.disable
      expect do
        put good_job.enable_cron_entry_path(cron_key: 'example')
      end.to change { cron_entry.enabled? }.from(false).to(true)
      expect(response).to have_http_status(:found)
    end
  end

  describe 'POST #enqueue' do
    before do
      allow(ExampleJob).to receive(:queue_adapter).and_return(GoodJob::Adapter.new(execution_mode: :external))
    end

    it 'enqueues a job' do
      expect do
        post good_job.enqueue_cron_entry_path(cron_key: 'example')
      end.to change(GoodJob::Job, :count).by(1)
      expect(response).to have_http_status(:found)
    end

    it 'uses the application I18n.default_locale' do
      original_locale = I18n.default_locale
      I18n.default_locale = :de

      post good_job.enqueue_cron_entry_path(cron_key: 'example')
      expect(GoodJob::Job.last.serialized_params).to include("locale" => "de")
    ensure
      I18n.default_locale = original_locale
    end
  end
end
