# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module TransactionIgnoringTestCases
  include MultiverseHelpers

  TXN_PREFIX = 'Controller/'

  setup_and_teardown_agent do |collector|
    collector.stub('connect', {
      'transaction_name_rules' => [{'match_expression' => 'ignored_transaction',
                                    'ignore' => true}],
      'agent_run_id' => 1
    })
  end

  # Test classes that include this module are expected to define:
  #   trigger_transaction(txn_name)
  #   trigger_transaction_with_error(txn_name, error_msg)
  #   trigger_transaction_with_slow_sql(txn_name)

  def test_does_not_record_metrics_for_ignored_transaction
    trigger_transaction('accepted_transaction')
    trigger_transaction('ignored_transaction')

    NewRelic::Agent.instance.send(:harvest_and_send_timeslice_data)

    stats = $collector.reported_stats_for_metric(TXN_PREFIX + 'accepted_transaction')

    assert_equal(1, stats.size)

    stats = $collector.reported_stats_for_metric(TXN_PREFIX + 'ignored_transaction')

    assert_equal(0, stats.size)
  end

  def test_does_not_record_traced_errors_for_ignored_transactions
    trigger_transaction_with_error('ignored_transaction', 'Buffy dies :(')
    trigger_transaction_with_error('accepted_transaction', 'Buffy lives :)')

    NewRelic::Agent.instance.send(:harvest_and_send_errors)

    post = first_call_for('error_data')
    errors = post.errors

    assert_equal(1, errors.size)
    assert_equal('Buffy lives :)', errors.first.message)
  end

  def test_does_not_record_transaction_trace_for_ignored_transactions
    with_config(:'transaction_tracer.transaction_threshold' => 0) do
      trigger_transaction('accepted_transaction')
      NewRelic::Agent.instance.send(:harvest_and_send_transaction_traces)

      first_call_for('transaction_sample_data')

      trigger_transaction('ignored_transaction')
      NewRelic::Agent.instance.send(:harvest_and_send_transaction_traces)

      first_call_for('transaction_sample_data')
    end
  end

  def test_does_not_record_analytics_for_ignored_transactions
    trigger_transaction('ignored_transaction')
    trigger_transaction('accepted_transaction')

    NewRelic::Agent.instance.send(:harvest_and_send_analytic_event_data)

    post = first_call_for('analytic_event_data')

    events = post.events

    assert_equal(1, events.size)
    assert_equal(TXN_PREFIX + 'accepted_transaction', events.first[0]['name'])
  end

  def test_does_not_record_sql_traces_for_ignored_transactions
    trigger_transaction_with_slow_sql('ignored_transaction')
    trigger_transaction_with_slow_sql('accepted_transaction')

    NewRelic::Agent.instance.send(:harvest_and_send_slowest_sql)

    post = first_call_for('sql_trace_data')

    traces = post.traces

    assert_equal(1, traces.size)

    trace = traces.first

    # From SqlTrace#to_collector_array
    # 0 -> path
    # 5 -> call_count
    assert_equal(TXN_PREFIX + 'accepted_transaction', trace[0])
    assert_equal(1, trace[5])
  end
end
