<commit_before>require 'spec_helper'
require 'sneakers'
require 'airbrake/sneakers'

RSpec.describe Airbrake::Sneakers::ErrorReporter do
  let(:worker) do
    queue = instance_double('queue')
    allow(queue).to receive_messages(
      name: 'test-queue',
      opts: {},
      exchange: instance_double('exchange')
    )

    Class.new do
      include Sneakers::Worker
      from_queue 'defaults'

      def work(_)
        raise 'oops error'
      end
    end.new(queue)
  end

  let(:error) { StandardError.new('Something is wrong') }

  let(:endpoint) { 'https://airbrake.io/api/v3/projects/113743/notices' }

  def wait_for_a_request_with_body(body)
    wait_for(a_request(:post, endpoint).with(body: body)).to have_been_made.once
  end

  before do
    stub_request(:post, endpoint).to_return(status: 201, body: '{}')

    Sneakers.configure(daemonize: true, log: '/dev/null')
    Sneakers::Worker.configure_metrics
  end

  after do
    Sneakers.clear!
  end

  it "should send a notice" do
    handler = instance_double('handler')
    expect(handler).to receive(:error).once
    allow(worker.logger).to receive(:error)
    worker.do_work(nil, nil, "msg", handler)

    wait_for_a_request_with_body(/"message":"oops\serror"/)
    wait_for_a_request_with_body(/test-queue/)
    wait_for_a_request_with_body(/"component":"sneakers"/)
  end

  it "should support call without worker" do
    Airbrake::Sneakers::ErrorReporter.new.call(error, message: 'my_glorious_message')
    wait_for_a_request_with_body(/"message":"Something is wrong"/)
    wait_for_a_request_with_body(/"params":{"message":"my_glorious_message"}/)
    # worker class is nil so action is NilClass
    wait_for_a_request_with_body(/"action":"NilClass"/)
    wait_for_a_request_with_body(/"component":"sneakers"/)
  end

  it "should support call with worker" do
    Airbrake::Sneakers::ErrorReporter.new.call(error, '', message: 'my_special_message')
    wait_for_a_request_with_body(/"message":"Something is wrong"/)
    wait_for_a_request_with_body(/"params":{"message":"my_special_message"}/)
    # worker class is a String so action is String
    wait_for_a_request_with_body(/"action":"String"/)
    wait_for_a_request_with_body(/"component":"sneakers"/)
  end
end<commit_msg>spec/unit/sneakers: cleanup<commit_after>require 'spec_helper'
require 'sneakers'
require 'airbrake/sneakers'

RSpec.describe Airbrake::Sneakers::ErrorReporter do
  let(:worker) do
    queue = instance_double('queue')
    allow(queue).to receive_messages(
      name: 'test-queue',
      opts: {},
      exchange: instance_double('exchange')
    )

    Class.new do
      include Sneakers::Worker
      from_queue 'defaults'

      def work(_)
        raise 'oops error'
      end
    end.new(queue)
  end

  let(:error) { StandardError.new('Something is wrong') }
  let(:endpoint) { 'https://airbrake.io/api/v3/projects/113743/notices' }

  def wait_for_a_request_with_body(body)
    wait_for(a_request(:post, endpoint).with(body: body)).to have_been_made.once
  end

  before do
    stub_request(:post, endpoint).to_return(status: 201, body: '{}')

    Sneakers.configure(daemonize: true, log: '/dev/null')
    Sneakers::Worker.configure_metrics
  end

  after { Sneakers.clear! }

  it "should send a notice" do
    handler = instance_double('handler')
    expect(handler).to receive(:error).once
    allow(worker.logger).to receive(:error)
    worker.do_work(nil, nil, "msg", handler)

    wait_for_a_request_with_body(/"message":"oops error"/)
    wait_for_a_request_with_body(/test-queue/)
    wait_for_a_request_with_body(/"component":"sneakers"/)
  end

  it "should support call without worker" do
    subject.call(error, message: 'my_glorious_message')
    wait_for_a_request_with_body(/"message":"Something is wrong"/)
    wait_for_a_request_with_body(/"params":{"message":"my_glorious_message"}/)
    # worker class is nil so action is NilClass
    wait_for_a_request_with_body(/"action":"NilClass"/)
    wait_for_a_request_with_body(/"component":"sneakers"/)
  end

  it "should support call with worker" do
    subject.call(error, '', message: 'my_special_message')
    wait_for_a_request_with_body(/"message":"Something is wrong"/)
    wait_for_a_request_with_body(/"params":{"message":"my_special_message"}/)
    # worker class is a String so action is String
    wait_for_a_request_with_body(/"action":"String"/)
    wait_for_a_request_with_body(/"component":"sneakers"/)
  end
end
{'fraction_non_alphanumeric': 0.12935099932629687, 'fraction_numerical': 0.0044913541432741975, 'mean_word_length': 4.411907654921021, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5801272', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1486, 'n_words': 269}