<commit_before># Copyright 2013 Square Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

require 'spec_helper'

describe Commit do
  context "[validations]" do
    it "should truncate commit messages" do
      FactoryGirl.create(:commit, message: 'a'*300).message.should eql('a'*253 + '...')
    end
  end

  describe "#recalculate_ready!" do
    before :all do
      Project.where(repository_url: Rails.root.join('spec', 'fixtures', 'repository.git').to_s).delete_all
      @project = FactoryGirl.create(:project, repository_url: Rails.root.join('spec', 'fixtures', 'repository.git').to_s)
    end

    before(:each) { @commit = @project.commit!('HEAD') }

    it "should set ready to false for commits with unready keys" do
      @commit.keys << FactoryGirl.create(:key, ready: false)
      @commit.keys << FactoryGirl.create(:key, ready: true)
      @commit.recalculate_ready!
      @commit.should_not be_ready
    end

    it "should set ready to true for commits with all ready keys" do
      @commit.keys << FactoryGirl.create(:key, ready: true)
      @commit.recalculate_ready!
      @commit.should be_ready
    end

    it "should set ready to true for commits with no keys" do
      @commit.recalculate_ready!
      @commit.should be_ready
    end
  end

  context "[hooks]" do

    context "webhooks" do
      before { @commit = FactoryGirl.create(:commit, ready: false) }
      context "if ready" do
        it "enqueues a webhook job if the state changed" do
          @commit.should_not be_ready
          @commit.ready = true
          WebhookPinger.should_receive(:perform_once)
          @commit.save!
        end

        it "doesn't enqueue a webhook job if the state did not change" do
          @commit.ready = true
          @commit.save!
          @commit.ready = true
          WebhookPinger.should_not_receive(:perform_once)
          @commit.save!
        end

      end

      context "if not ready" do
        it "doesn't enqueue a webhook job even if state has changed" do
          @commit.ready = true
          @commit.save!
          @commit.ready = false
          WebhookPinger.should_not_receive(:perform_once)
          @commit.save!
        end

        it "doesn't enqueue a webhook job if state has not changed" do
          WebhookPinger.should_not_receive(:perform_once)
          @commit.save!
        end
      end
    end

    context "[mail hooks]" do
      it "sends an email to the translators and cc's the user when loading changes to false from true" do
        @commit = FactoryGirl.create(:commit, loading: true, user: FactoryGirl.create(:user))
        ActionMailer::Base.deliveries.clear
        @commit.loading = false
        @commit.save!
        ActionMailer::Base.deliveries.size.should eql(1)
        email = ActionMailer::Base.deliveries.first
        email.to.should eql([Shuttle::Configuration.mailer.translators_list])
        email.cc.should eql([@commit.user.email])
        email.subject.should eql('[Shuttle] New commit ready for translation')
        email.body.to_s.should include("http://test.host/?project_id=#{@commit.project_id}&status=uncompleted")
      end

      it "sends one email to the translators when loading changes to false if the commit has no user" do
        @commit = FactoryGirl.create(:commit, loading: true)
        ActionMailer::Base.deliveries.clear
        @commit.loading = false
        @commit.save!
        ActionMailer::Base.deliveries.size.should eql(1)
        email = ActionMailer::Base.deliveries.first
        email.to.should eql([Shuttle::Configuration.mailer.translators_list])
        email.subject.should eql('[Shuttle] New commit ready for translation')
        email.body.to_s.should include("http://test.host/?project_id=#{@commit.project_id}&status=uncompleted")
      end

      it "sends an email when ready changes to true from false" do
        @commit = FactoryGirl.create(:commit, ready: false, user: FactoryGirl.create(:user))
        ActionMailer::Base.deliveries.clear
        @commit.ready = true

        @commit.project.key_inclusions.push("inc_key_1", "inc_key_2")
        @commit.project.key_exclusions.push("exc_key_1", "exc_key_2", "exc_key_3")
        
        @commit.project.key_locale_inclusions = {"fr" => ["fr_exc_key_1", "fr_exc_key_2", "fr_exc_key_3"], "aa" => ["aa_exc_key_1", "aa_exc_key_2"]}
        @commit.project.key_locale_exclusions = {"ja" => ["ja_inc_key_1", "ja_inc_key_2", "ja_inc_key_3"]}

        @commit.project.only_paths.push("only_path_1", "only_path_2", "only_path_1")
        @commit.project.skip_paths.push("skip_path_1", "skip_path_2")
        
        @commit.project.skip_importer_paths = {"Android XML" => ["an_skip_key_1", "an_skip_key_2", "an_skip_key_3"]}
        @commit.project.only_importer_paths = {"Ember.js" => ["em_only_key_1", "em_only_key_2", "em_only_key_3"], "ERb File" => ["erb_only_key_1", "erb_only_key_2"]}
        
        @commit.save!
        ActionMailer::Base.deliveries.size.should eql(1)
        email = ActionMailer::Base.deliveries.first
        email.to.should eql([@commit.user.email])<commit_msg>Spec fix<commit_after># Copyright 2013 Square Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.

require 'spec_helper'

describe Commit do
  context "[validations]" do
    it "should truncate commit messages" do
      FactoryGirl.create(:commit, message: 'a'*300).message.should eql('a'*253 + '...')
    end
  end

  describe "#recalculate_ready!" do
    before :all do
      Project.where(repository_url: Rails.root.join('spec', 'fixtures', 'repository.git').to_s).delete_all
      @project = FactoryGirl.create(:project, repository_url: Rails.root.join('spec', 'fixtures', 'repository.git').to_s)
    end

    before(:each) { @commit = @project.commit!('HEAD') }

    it "should set ready to false for commits with unready keys" do
      @commit.keys << FactoryGirl.create(:key, ready: false)
      @commit.keys << FactoryGirl.create(:key, ready: true)
      @commit.recalculate_ready!
      @commit.should_not be_ready
    end

    it "should set ready to true for commits with all ready keys" do
      @commit.keys << FactoryGirl.create(:key, ready: true)
      @commit.recalculate_ready!
      @commit.should be_ready
    end

    it "should set ready to true for commits with no keys" do
      @commit.recalculate_ready!
      @commit.should be_ready
    end
  end

  context "[hooks]" do

    context "webhooks" do
      before { @commit = FactoryGirl.create(:commit, ready: false) }
      context "if ready" do
        it "enqueues a webhook job if the state changed" do
          @commit.should_not be_ready
          @commit.ready = true
          WebhookPinger.should_receive(:perform_once)
          @commit.save!
        end

        it "doesn't enqueue a webhook job if the state did not change" do
          @commit.ready = true
          @commit.save!
          @commit.ready = true
          WebhookPinger.should_not_receive(:perform_once)
          @commit.save!
        end

      end

      context "if not ready" do
        it "doesn't enqueue a webhook job even if state has changed" do
          @commit.ready = true
          @commit.save!
          @commit.ready = false
          WebhookPinger.should_not_receive(:perform_once)
          @commit.save!
        end

        it "doesn't enqueue a webhook job if state has not changed" do
          WebhookPinger.should_not_receive(:perform_once)
          @commit.save!
        end
      end
    end

    context "[mail hooks]" do
      it "sends an email to the translators and cc's the user when loading changes to false from true" do
        @commit = FactoryGirl.create(:commit, loading: true, user: FactoryGirl.create(:user))
        ActionMailer::Base.deliveries.clear
        @commit.loading = false
        @commit.save!
        ActionMailer::Base.deliveries.size.should eql(1)
        email = ActionMailer::Base.deliveries.first
        email.to.should eql([Shuttle::Configuration.mailer.translators_list])
        email.cc.should eql([@commit.user.email])
        email.subject.should eql('[Shuttle] New commit ready for translation')
        email.body.to_s.should include("http://test.host/?project_id=#{@commit.project_id}&status=uncompleted")
      end

      it "sends one email to the translators when loading changes to false if the commit has no user" do
        @commit = FactoryGirl.create(:commit, loading: true)
        ActionMailer::Base.deliveries.clear
        @commit.loading = false
        @commit.save!
        ActionMailer::Base.deliveries.size.should eql(1)
        email = ActionMailer::Base.deliveries.first
        email.to.should eql([Shuttle::Configuration.mailer.translators_list])
        email.subject.should eql('[Shuttle] New commit ready for translation')
        email.body.to_s.should include("http://test.host/?project_id=#{@commit.project_id}&status=uncompleted")
      end

      it "sends an email when ready changes to true from false" do
        @commit = FactoryGirl.create(:commit, ready: false, user: FactoryGirl.create(:user))
        ActionMailer::Base.deliveries.clear
        @commit.ready = true

        @commit.project.key_inclusions += %w(inc_key_1 inc_key_2)
        @commit.project.key_exclusions += %w(exc_key_1 exc_key_2 exc_key_3)
        
        @commit.project.key_locale_inclusions = {"fr" => ["fr_exc_key_1", "fr_exc_key_2", "fr_exc_key_3"], "aa" => ["aa_exc_key_1", "aa_exc_key_2"]}
        @commit.project.key_locale_exclusions = {"ja" => ["ja_inc_key_1", "ja_inc_key_2", "ja_inc_key_3"]}

        @commit.project.only_paths += %w(only_path_1 only_path_2 only_path_1)
        @commit.project.skip_paths += %w(skip_path_1 skip_path_2)
        
        @commit.project.skip_importer_paths = {"Android XML" => ["an_skip_key_1", "an_skip_key_2", "an_skip_key_3"]}
        @commit.project.only_importer_paths = {"Ember.js" => ["em_only_key_1", "em_only_key_2", "em_only_key_3"], "ERb File" => ["erb_only_key_1", "erb_only_key_2"]}
        
        @commit.save!
        ActionMailer::Base.deliveries.size.should eql(1)
        email = ActionMailer::Base.deliveries.first
        email.to.should eql([@commit.user.email])
{'fraction_non_alphanumeric': 0.1104666726153297, 'fraction_numerical': 0.007673775318997055, 'mean_word_length': 2.974468085106383, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2236708', 'n_tokens_mistral': 3536, 'n_tokens_neox': 3417, 'n_words': 886}