<commit_before>require 'rails_helper'

describe Agents::TriggerAgent do
  before do
    @valid_params = {
      'name' => "my trigger agent",
      'options' => {
        'expected_receive_period_in_days' => 2,
        'rules' => [{
                      'type' => "regex",
                      'value' => "a\\db",
                      'path' => "foo.bar.baz",
                    }],
        'message' => "I saw '{{foo.bar.baz}}' from {{name}}"
      }
    }

    @checker = Agents::TriggerAgent.new(@valid_params)
    @checker.user = users(:bob)
    @checker.save!

    @event = Event.new
    @event.agent = agents(:bob_rain_notifier_agent)
    @event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" }},
                       'name' => "Joe" }
  end

  describe "validation" do
    before do
      expect(@checker).to be_valid
    end

    it "should validate presence of message" do
      @checker.options['message'] = nil
      expect(@checker).not_to be_valid

      @checker.options['message'] = ''
      expect(@checker).not_to be_valid
    end

    it "should be valid without a message when 'keep_event' is set" do
      @checker.options['keep_event'] = 'true'
      @checker.options['message'] = ''
      expect(@checker).to be_valid
    end

    it "if present, 'keep_event' must equal true or false" do
      @checker.options['keep_event'] = 'true'
      expect(@checker).to be_valid

      @checker.options['keep_event'] = 'false'
      expect(@checker).to be_valid

      @checker.options['keep_event'] = ''
      expect(@checker).to be_valid

      @checker.options['keep_event'] = 'tralse'
      expect(@checker).not_to be_valid
    end

    it "validates that 'must_match' is a positive integer, not greater than the number of rules, if provided" do
      @checker.options['must_match'] = '1'
      expect(@checker).to be_valid

      @checker.options['must_match'] = '0'
      expect(@checker).not_to be_valid

      @checker.options['must_match'] = 'wrong'
      expect(@checker).not_to be_valid

      @checker.options['must_match'] = ''
      expect(@checker).to be_valid

      @checker.options.delete('must_match')
      expect(@checker).to be_valid

      @checker.options['must_match'] = '2'
      expect(@checker).not_to be_valid
      expect(@checker.errors[:base].first).to match(/equal to or less than the number of rules/)
    end

    it "should validate the three fields in each rule" do
      @checker.options['rules'] << { 'path' => "foo", 'type' => "fake", 'value' => "6" }
      expect(@checker).not_to be_valid
      @checker.options['rules'].last['type'] = "field>=value"
      expect(@checker).to be_valid
      @checker.options['rules'].last.delete('value')
      expect(@checker).not_to be_valid
    end<commit_msg>Earn some coverage<commit_after>require 'rails_helper'

describe Agents::TriggerAgent do
  before do
    @valid_params = {
      'name' => "my trigger agent",
      'options' => {
        'expected_receive_period_in_days' => 2,
        'rules' => [{
                      'type' => "regex",
                      'value' => "a\\db",
                      'path' => "foo.bar.baz",
                    }],
        'message' => "I saw '{{foo.bar.baz}}' from {{name}}"
      }
    }

    @checker = Agents::TriggerAgent.new(@valid_params)
    @checker.user = users(:bob)
    @checker.save!

    @event = Event.new
    @event.agent = agents(:bob_rain_notifier_agent)
    @event.payload = { 'foo' => { "bar" => { 'baz' => "a2b" }},
                       'name' => "Joe" }
  end

  describe "validation" do
    before do
      expect(@checker).to be_valid
    end

    it "should validate presence of message" do
      @checker.options['message'] = nil
      expect(@checker).not_to be_valid

      @checker.options['message'] = ''
      expect(@checker).not_to be_valid
    end

    it "should be valid without a message when 'keep_event' is set" do
      @checker.options['keep_event'] = 'true'
      @checker.options['message'] = ''
      expect(@checker).to be_valid
    end

    it "if present, 'keep_event' must equal true or false" do
      @checker.options['keep_event'] = 'true'
      expect(@checker).to be_valid

      @checker.options['keep_event'] = 'false'
      expect(@checker).to be_valid

      @checker.options['keep_event'] = ''
      expect(@checker).to be_valid

      @checker.options['keep_event'] = 'tralse'
      expect(@checker).not_to be_valid
    end

    it "validates that 'must_match' is a positive integer, not greater than the number of rules, if provided" do
      @checker.options['must_match'] = '1'
      expect(@checker).to be_valid

      @checker.options['must_match'] = '0'
      expect(@checker).not_to be_valid

      @checker.options['must_match'] = 'wrong'
      expect(@checker).not_to be_valid

      @checker.options['must_match'] = ''
      expect(@checker).to be_valid

      @checker.options.delete('must_match')
      expect(@checker).to be_valid

      @checker.options['must_match'] = '2'
      expect(@checker).not_to be_valid
      expect(@checker.errors[:base].first).to match(/equal to or less than the number of rules/)
    end

    it "should validate the three fields in each rule" do
      @checker.options['rules'] << { 'path' => "foo", 'type' => "fake", 'value' => "6" }
      expect(@checker).not_to be_valid
      @checker.options['rules'].last['type'] = "field>=value"
      expect(@checker).to be_valid
      @checker.options['rules'].last.delete('value')
      expect(@checker).not_to be_valid
    end

    it "should validate non-hash rules" do
      @checker.options['rules'] << "{% if status == 'ok' %}true{% endif %}"
      expect(@checker).to be_valid

      @checker.options['rules'] << []
      expect(@checker).not_to be_valid
    end
{'fraction_non_alphanumeric': 0.15279223031564343, 'fraction_numerical': 0.002081165452653486, 'mean_word_length': 2.627044025157233, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '2353307', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1825, 'n_words': 399}