# frozen_string_literal: true

require "spec_helper"
require "shared_behaviour"

describe Que::Failure::NoRetry do
  before do
    allow(Que::Failure).
      to receive(:on_unhandled_failure).
      with(anything, anything)
  end

  describe "when the job fails" do
    class NoRetryJob < Que::Job
      include Que::Failure::NoRetry

      def run
        raise StandardError, "I broke."
      end
    end

    it_behaves_like "marks the job as failed and non-retryable", NoRetryJob
    it_behaves_like "calls the global failure handler", NoRetryJob
  end

  context "when the job raises an exception that doesn't inherit from StandardError" do
    # rubocop:disable Lint/InheritException
    class ApocalypticException < Exception; end
    # rubocop:enable Lint/InheritException

    class CatastrophicJob < Que::Job
      include Que::Failure::NoRetry

      def run
        raise ApocalypticException, "I don't inherit from StandardError"
      end
    end

    it "is still marked as non-retryable in the database" do
      CatastrophicJob.enqueue
      job = QueJob.first
      expect(job[:retryable]).to eq(true)

      begin
        work_job
      rescue ApocalypticException
      end

      job = QueJob.first
      expect(job[:error_count]).to eq(0)
      expect(job[:retryable]).to eq(false)
    end
  end
end
