<commit_before>$:<< File.expand_path(File.dirname(__FILE__) + '/../..')
require 'test_helper.rb'

class RequestGenerationTest < ActionDispatch::IntegrationTest

  def setup
    RequestGenerationTest.app = Rails.application
    QBWC.clear_jobs
  end

  test "worker with nothing" do
    QBWC.add_job(:integration_test, true, '', QBWC::Worker)
    session = QBWC::Session.new('foo', '')
    assert_nil session.next_request
  end

  class NilRequestWorker < QBWC::Worker
    def requests(job, session, data)
      nil
    end
  end

  test "worker with nil" do
    QBWC.add_job(:integration_test, true, '', NilRequestWorker)
    session = QBWC::Session.new('foo', '')
    assert_nil session.next_request
    simulate_response(session)
    assert_nil session.next_request
  end

  class SingleRequestWorker < QBWC::Worker
    def requests(job, session, data)
      $SINGLE_REQUESTS_INVOKED_COUNT += 1 if $SINGLE_REQUESTS_INVOKED_COUNT.is_a?(Integer)
      {:customer_query_rq => {:full_name => '<NAME>'}}
    end
  end

  test "simple request worker" do
    QBWC.add_job(:integration_test, true, '', SingleRequestWorker)
    QBWC.jobs.each {|job| assert job.requests_provided_when_job_added == false}
    session = QBWC::Session.new('foo', '')
    nr = session.next_request
    assert_not_nil nr
    assert_match(/FullName.*<NAME>.*FullName/, nr.request)
    simulate_response(session)
    assert_nil session.next_request
  end

  class SingleStringRequestWorker < QBWC::Worker
    def requests(job, session, data)
      $SINGLE_REQUESTS_INVOKED_COUNT += 1 if $SINGLE_REQUESTS_INVOKED_COUNT.is_a?(Integer)
      QBWC_CUSTOMER_QUERY_RQ
    end
  end

  test "simple string request worker" do
    QBWC.add_job(:integration_test, true, '', SingleStringRequestWorker)
    QBWC.jobs.each {|job| assert job.requests_provided_when_job_added == false}
    session = QBWC::Session.new('foo', '')
    nr = session.next_request
    assert_not_nil nr
    assert_match(/FullName.*#{QBWC_USERNAME}.*FullName/, nr.request)
    simulate_response(session)<commit_msg>Cleanup warnings: global variable not initialized<commit_after>$:<< File.expand_path(File.dirname(__FILE__) + '/../..')
require 'test_helper.rb'

class RequestGenerationTest < ActionDispatch::IntegrationTest

  def setup
    RequestGenerationTest.app = Rails.application
    QBWC.clear_jobs
  end

  test "worker with nothing" do
    QBWC.add_job(:integration_test, true, '', QBWC::Worker)
    session = QBWC::Session.new('foo', '')
    assert_nil session.next_request
  end

  class NilRequestWorker < QBWC::Worker
    def requests(job, session, data)
      nil
    end
  end

  test "worker with nil" do
    QBWC.add_job(:integration_test, true, '', NilRequestWorker)
    session = QBWC::Session.new('foo', '')
    assert_nil session.next_request
    simulate_response(session)
    assert_nil session.next_request
  end

  class SingleRequestWorker < QBWC::Worker
    def requests(job, session, data)
      $SINGLE_REQUESTS_INVOKED_COUNT += 1 if defined?($SINGLE_REQUESTS_INVOKED_COUNT) && $SINGLE_REQUESTS_INVOKED_COUNT.is_a?(Integer)
      {:customer_query_rq => {:full_name => '<NAME>'}}
    end
  end

  test "simple request worker" do
    QBWC.add_job(:integration_test, true, '', SingleRequestWorker)
    QBWC.jobs.each {|job| assert job.requests_provided_when_job_added == false}
    session = QBWC::Session.new('foo', '')
    nr = session.next_request
    assert_not_nil nr
    assert_match(/FullName.*<NAME>.*FullName/, nr.request)
    simulate_response(session)
    assert_nil session.next_request
  end

  class SingleStringRequestWorker < QBWC::Worker
    def requests(job, session, data)
      $SINGLE_REQUESTS_INVOKED_COUNT += 1 if defined?($SINGLE_REQUESTS_INVOKED_COUNT) && $SINGLE_REQUESTS_INVOKED_COUNT.is_a?(Integer)
      QBWC_CUSTOMER_QUERY_RQ
    end
  end

  test "simple string request worker" do
    QBWC.add_job(:integration_test, true, '', SingleStringRequestWorker)
    QBWC.jobs.each {|job| assert job.requests_provided_when_job_added == false}
    session = QBWC::Session.new('foo', '')
    nr = session.next_request
    assert_not_nil nr
    assert_match(/FullName.*#{QBWC_USERNAME}.*FullName/, nr.request)
    simulate_response(session)
{'fraction_non_alphanumeric': 0.11219165085388995, 'fraction_numerical': 0.0009487666034155598, 'mean_word_length': 4.297738693467337, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '3219303', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1374, 'n_words': 266}