<commit_before>require 'spec_helper'

describe Comment do

  describe "factories" do
    it "should generate a valid comment" do
      @project = Factory(:project)
      @user = @project.user
      comment = Factory.build(:comment, :project => @project, :user => @user, :target => @project)
      comment.save.should be_true
      comment.user.should == @user
      @project.comments.last.should == comment
      @user.comments.last.should == comment
    end
  end
  
  it "should not allow comment creation with a blank title" do
    comment = Factory.build(:comment, :body => nil)
    comment.should_not be_valid
  end
  
  describe "copying ownership" do
    before do
      @target = Factory.build(:simple_conversation, :body => nil)
      @target.save(:validate => false)
      @comment = Factory.build(:comment, :target => @target, :user => nil, :project => nil)
    end
    
    it "inherits project and user from target" do
      @comment.save.should be_true
      @comment.user.should == @target.user
      @comment.project.should == @target.project
    end
    
    it "works even when mentioning user" do
      @comment.body = "Hello @kitty"
      lambda { @comment.save }.should_not raise_error
    end
    
    it "doesn't happen when updating" do
      @comment.save
      @comment.should_not be_new_record
      @comment.user = @comment.project = nil
      @comment.save!
      @comment.reload
      @comment.user_id.should be_nil
      @comment.project_id.should be_nil
      @comment.body = "I am anonymous"
      @comment.save
      @comment.reload.user.should be_nil
      @comment.project.should be_nil
    end
  end
  
  describe "posting to a task" do
    before do
      @task = Factory(:task)
    end
    
    it "should update counter cache" do
      lambda {
        Factory(:comment, :project => @task.project, :user => @task.user, :target => @task)
        @task.reload
      }.should change(@task, :comments_count).by(1)
    end
  end

  describe "mentioning @user" do
    before do
      @project = Factory(:project)
      @user = Factory(:confirmed_user, :login => 'existing')
    end

    it "should link to users page when mentioning @existing if they are in the project" do
      @project.add_user(@user)
      body = "@existing, hey, @existing"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><a href="/users/existing" class="mention">@existing</a>, hey, <a href="/users/existing" class="mention">@existing</a></p>\n}
      comment.mentioned.to_a.should == [@user]
    end

    it "should not link a user if his username is part of an email address" do
      @project.add_user(@user)
      body = "@existing links, but not <EMAIL> email"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><a href=\"/users/existing\" class=\"mention\">@existing</a> links, but not <a href=\"mailto:<EMAIL>\"><EMAIL></a> email</p>\n}
      comment.mentioned.to_a.should == [@user]
    end

    it "should link to all the mentioned users if they are in the project" do
      pablo = Factory(:confirmed_user, :login => "pablo")
      james = Factory(:confirmed_user, :login => "james")
      @project.add_user(pablo)
      @project.add_user(james)
      body = "@pablo @james Check this out!"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><a href="/users/pablo" class="mention">@pablo</a> <a href="/users/james" class="mention">@james</a> Check this out!</p>\n}
      comment.mentioned.should include(pablo)
      comment.mentioned.should include(james)
    end

    it "should add everyone to watchers if @all is mentioned" do
      pablo = Factory(:confirmed_user, :login => "pablo")
      james = Factory(:confirmed_user, :login => "james")
      @project.add_user(pablo)
      @project.add_user(james)
      body = "@all hands on deck this Friday"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><span class="mention">@all</span> hands on deck this Friday</p>\n}
      comment.mentioned.should include(pablo)
      comment.mentioned.should include(james)
      comment.mentioned.should_not include(@user)
    end

    describe "commenting adds you as a watcher" do
      before do
        @project = Factory(:project)
        @pablo = Factory(:confirmed_user)
        @project.add_user(@pablo)
      end

      it "on a conversation" do
        conversation = Factory(:conversation, :project => @project, :user => @project.user)
        conversation.watchers_ids.should_not include(@pablo.id)
        comment = Factory(:comment, :project => @project, :user => @pablo, :target => conversation)
        conversation.reload.watchers_ids.should include(@pablo.id)
      end

      it "on a task" do
        @task = Factory(:task, :project => @project, :user => @project.user)
        @task.watchers_ids.should_not include(@pablo.id)
        comment = Factory(:comment, :project => @project, :user => @pablo, :target => @task)
        @task.reload.watchers_ids.should include(@pablo.id)
      end
    end
    
    describe "mentioning @user" do
      before do
        @project.add_user(@user)
      end

      it "should add him to conversation" do
        @conversation = Factory(:conversation, :project => @project, :user => @project.user)
        @conversation.watchers.should_not include(@user)

        body = "I would like to add @existing to this, but not @unexisting."
        comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @conversation)
        
        comment.mentioned.to_a.should == [@user]
        @conversation.reload.watchers.should include(@user)
      end

      it "should add him to task" do
        @task = Factory(:task, :project => @project, :user => @project.user)
        @task.watchers.should_not include(@user)

        body = "I would like to add @existing to this, but not @unexisting."
        comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @task)
        
        comment.mentioned.to_a.should == [@user]
        @task.reload.watchers.should include(@user)
      end

      it "should add him to task list" do
        @task_list = Factory(:task_list, :project => @project, :user => @project.user)
        @task_list.watchers.should_not include(@user)

        body = "I would like to add @existing to this, but not @unexisting."
        comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @task_list)
        
        comment.mentioned.to_a.should == [@user]
        @task_list.reload.watchers.should include(@user)
      end
    end

    it "should not link to users page when mentioning @existing if they are not in the project" do
      body = "@existing is a cool guy, but he is not in this project"
      comment = Factory.create(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == "<p>@existing is a cool guy, but he is not in this project</p>\n"
      comment.mentioned.should == nil
    end

    it "should not link to users page when typing @unexisting" do
      body = "Hey, @unexisting, take a look at this!"
      comment = Factory.create(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == "<p>Hey, @unexisting, take a look at this!</p>\n"
      comment.mentioned.should == nil
    end
  end
  
  describe "duplicates" do
    before do
      @project = Factory(:project)<commit_msg>There is no watcher on TaskList<commit_after>require 'spec_helper'

describe Comment do

  describe "factories" do
    it "should generate a valid comment" do
      @project = Factory(:project)
      @user = @project.user
      comment = Factory.build(:comment, :project => @project, :user => @user, :target => @project)
      comment.save.should be_true
      comment.user.should == @user
      @project.comments.last.should == comment
      @user.comments.last.should == comment
    end
  end
  
  it "should not allow comment creation with a blank title" do
    comment = Factory.build(:comment, :body => nil)
    comment.should_not be_valid
  end
  
  describe "copying ownership" do
    before do
      @target = Factory.build(:simple_conversation, :body => nil)
      @target.save(:validate => false)
      @comment = Factory.build(:comment, :target => @target, :user => nil, :project => nil)
    end
    
    it "inherits project and user from target" do
      @comment.save.should be_true
      @comment.user.should == @target.user
      @comment.project.should == @target.project
    end
    
    it "works even when mentioning user" do
      @comment.body = "Hello @kitty"
      lambda { @comment.save }.should_not raise_error
    end
    
    it "doesn't happen when updating" do
      @comment.save
      @comment.should_not be_new_record
      @comment.user = @comment.project = nil
      @comment.save!
      @comment.reload
      @comment.user_id.should be_nil
      @comment.project_id.should be_nil
      @comment.body = "I am anonymous"
      @comment.save
      @comment.reload.user.should be_nil
      @comment.project.should be_nil
    end
  end
  
  describe "posting to a task" do
    before do
      @task = Factory(:task)
    end
    
    it "should update counter cache" do
      lambda {
        Factory(:comment, :project => @task.project, :user => @task.user, :target => @task)
        @task.reload
      }.should change(@task, :comments_count).by(1)
    end
  end

  describe "mentioning @user" do
    before do
      @project = Factory(:project)
      @user = Factory(:confirmed_user, :login => 'existing')
    end

    it "should link to users page when mentioning @existing if they are in the project" do
      @project.add_user(@user)
      body = "@existing, hey, @existing"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><a href="/users/existing" class="mention">@existing</a>, hey, <a href="/users/existing" class="mention">@existing</a></p>\n}
      comment.mentioned.to_a.should == [@user]
    end

    it "should not link a user if his username is part of an email address" do
      @project.add_user(@user)
      body = "@existing links, but not <EMAIL> email"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><a href=\"/users/existing\" class=\"mention\">@existing</a> links, but not <a href=\"mailto:<EMAIL>\"><EMAIL></a> email</p>\n}
      comment.mentioned.to_a.should == [@user]
    end

    it "should link to all the mentioned users if they are in the project" do
      pablo = Factory(:confirmed_user, :login => "pablo")
      james = Factory(:confirmed_user, :login => "james")
      @project.add_user(pablo)
      @project.add_user(james)
      body = "@pablo @james Check this out!"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><a href="/users/pablo" class="mention">@pablo</a> <a href="/users/james" class="mention">@james</a> Check this out!</p>\n}
      comment.mentioned.should include(pablo)
      comment.mentioned.should include(james)
    end

    it "should add everyone to watchers if @all is mentioned" do
      pablo = Factory(:confirmed_user, :login => "pablo")
      james = Factory(:confirmed_user, :login => "james")
      @project.add_user(pablo)
      @project.add_user(james)
      body = "@all hands on deck this Friday"
      comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == %Q{<p><span class="mention">@all</span> hands on deck this Friday</p>\n}
      comment.mentioned.should include(pablo)
      comment.mentioned.should include(james)
      comment.mentioned.should_not include(@user)
    end

    describe "commenting adds you as a watcher" do
      before do
        @project = Factory(:project)
        @pablo = Factory(:confirmed_user)
        @project.add_user(@pablo)
      end

      it "on a conversation" do
        conversation = Factory(:conversation, :project => @project, :user => @project.user)
        conversation.watchers_ids.should_not include(@pablo.id)
        comment = Factory(:comment, :project => @project, :user => @pablo, :target => conversation)
        conversation.reload.watchers_ids.should include(@pablo.id)
      end

      it "on a task" do
        @task = Factory(:task, :project => @project, :user => @project.user)
        @task.watchers_ids.should_not include(@pablo.id)
        comment = Factory(:comment, :project => @project, :user => @pablo, :target => @task)
        @task.reload.watchers_ids.should include(@pablo.id)
      end
    end
    
    describe "mentioning @user" do
      before do
        @project.add_user(@user)
      end

      it "should add him to conversation" do
        @conversation = Factory(:conversation, :project => @project, :user => @project.user)
        @conversation.watchers.should_not include(@user)

        body = "I would like to add @existing to this, but not @unexisting."
        comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @conversation)
        
        comment.mentioned.to_a.should == [@user]
        @conversation.reload.watchers.should include(@user)
      end

      it "should add him to task" do
        @task = Factory(:task, :project => @project, :user => @project.user)
        @task.watchers.should_not include(@user)

        body = "I would like to add @existing to this, but not @unexisting."
        comment = Factory(:comment, :body => body, :project => @project, :user => @project.user, :target => @task)
        
        comment.mentioned.to_a.should == [@user]
        @task.reload.watchers.should include(@user)
      end
    end

    it "should not link to users page when mentioning @existing if they are not in the project" do
      body = "@existing is a cool guy, but he is not in this project"
      comment = Factory.create(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == "<p>@existing is a cool guy, but he is not in this project</p>\n"
      comment.mentioned.should == nil
    end

    it "should not link to users page when typing @unexisting" do
      body = "Hey, @unexisting, take a look at this!"
      comment = Factory.create(:comment, :body => body, :project => @project, :user => @project.user, :target => @project)
      comment.body_html.should == "<p>Hey, @unexisting, take a look at this!</p>\n"
      comment.mentioned.should == nil
    end
  end
  
  describe "duplicates" do
    before do
      @project = Factory(:project)
{'fraction_non_alphanumeric': 0.1360330253587576, 'fraction_numerical': 0.00013105301094292642, 'mean_word_length': 2.950815428423505, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 193, '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': '489663', 'n_tokens_mistral': 4627, 'n_tokens_neox': 4545, 'n_words': 1327}