<commit_before>require "rails_helper"

describe EmailDigest do

  describe "notifications" do

    it "returns notifications for a user" do
      user1 = create(:user)
      user2 = create(:user)

      proposal_notification = create(:proposal_notification)
      notification1 = create(:notification, notifiable: proposal_notification, user: user1)
      notification2 = create(:notification, notifiable: proposal_notification, user: user2)

      email_digest = EmailDigest.new(user1)

      expect(email_digest.notifications).to eq [notification1]
      expect(email_digest.notifications).not_to include(notification2)
    end

    it "returns only proposal notifications" do
      user = create(:user)

      notification1 = create(:notification, :for_proposal_notification, user: user)
      notification2 = create(:notification, :for_comment, user: user)

      email_digest = EmailDigest.new(user)

      expect(email_digest.notifications).to eq [notification1]
      expect(email_digest.notifications).not_to include(notification2)
    end

  end

  describe "pending_notifications?" do

    it "returns true when notifications have not been emailed" do
      user = create(:user)

      notification = create(:notification, :for_proposal_notification, user: user)

      email_digest = EmailDigest.new(user)
      expect(email_digest.pending_notifications?).to be
    end

    it "returns false when notifications have been emailed" do
      user = create(:user)

      notification = create(:notification, :for_proposal_notification, user: user, emailed_at: Time.current)

      email_digest = EmailDigest.new(user)
      expect(email_digest.pending_notifications?).not_to be
    end

    it "returns false when there are no notifications for a user" do
      user = create(:user)
      email_digest = EmailDigest.new(user)
      expect(email_digest.pending_notifications?).not_to be
    end

  end

  describe "deliver" do

    it "delivers email if notifications pending" do
      user = create(:user)

      notification = create(:notification, :for_proposal_notification, user: user)

      reset_mailer
      email_digest = EmailDigest.new(user)
      email_digest.deliver(Time.current)

      email = open_last_email
      expect(email).to have_subject("Proposal notifications in CONSUL")
    end

    it "does not deliver email if no notifications pending" do
      user = create(:user)

      create(:notification, :for_proposal_notification, user: user, emailed_at: Time.current)

      reset_mailer
      email_digest = EmailDigest.new(user)
      email_digest.deliver(Time.current)

      expect(all_emails).to be_empty
    end

  end

  describe "mark_as_emailed" do

    it "marks notifications as emailed" do
      user1 = create(:user)
      user2 = create(:user)

      proposal_notification = create(:proposal_notification)
      notification1 = create(:notification, notifiable: proposal_notification, user: user1)
      notification2 = create(:notification, notifiable: proposal_notification, user: user1)
      notification3 = create(:notification, notifiable: proposal_notification, user: user2)

      expect(notification1.emailed_at).not_to be
      expect(notification2.emailed_at).not_to be
      expect(notification3.emailed_at).not_to be

      email_digest = EmailDigest.new(user1)
      email_digest.mark_as_emailed
<commit_msg>Remove duplication in email specs using `let`<commit_after>require "rails_helper"

describe EmailDigest do

  describe "notifications" do

    it "returns notifications for a user" do
      user1 = create(:user)
      user2 = create(:user)

      proposal_notification = create(:proposal_notification)
      notification1 = create(:notification, notifiable: proposal_notification, user: user1)
      notification2 = create(:notification, notifiable: proposal_notification, user: user2)

      email_digest = EmailDigest.new(user1)

      expect(email_digest.notifications).to eq [notification1]
      expect(email_digest.notifications).not_to include(notification2)
    end

    it "returns only proposal notifications" do
      user = create(:user)

      notification1 = create(:notification, :for_proposal_notification, user: user)
      notification2 = create(:notification, :for_comment, user: user)

      email_digest = EmailDigest.new(user)

      expect(email_digest.notifications).to eq [notification1]
      expect(email_digest.notifications).not_to include(notification2)
    end

  end

  describe "pending_notifications?" do
    let(:user) { create(:user) }

    it "returns true when notifications have not been emailed" do
      create(:notification, :for_proposal_notification, user: user)

      email_digest = EmailDigest.new(user)
      expect(email_digest.pending_notifications?).to be
    end

    it "returns false when notifications have been emailed" do
      create(:notification, :for_proposal_notification, user: user, emailed_at: Time.current)

      email_digest = EmailDigest.new(user)
      expect(email_digest.pending_notifications?).not_to be
    end

    it "returns false when there are no notifications for a user" do
      email_digest = EmailDigest.new(user)
      expect(email_digest.pending_notifications?).not_to be
    end

  end

  describe "deliver" do
    let(:user) { create(:user) }

    it "delivers email if notifications pending" do
      create(:notification, :for_proposal_notification, user: user)

      reset_mailer
      email_digest = EmailDigest.new(user)
      email_digest.deliver(Time.current)

      email = open_last_email
      expect(email).to have_subject("Proposal notifications in CONSUL")
    end

    it "does not deliver email if no notifications pending" do
      create(:notification, :for_proposal_notification, user: user, emailed_at: Time.current)

      reset_mailer
      email_digest = EmailDigest.new(user)
      email_digest.deliver(Time.current)

      expect(all_emails).to be_empty
    end

  end

  describe "mark_as_emailed" do

    it "marks notifications as emailed" do
      user1 = create(:user)
      user2 = create(:user)

      proposal_notification = create(:proposal_notification)
      notification1 = create(:notification, notifiable: proposal_notification, user: user1)
      notification2 = create(:notification, notifiable: proposal_notification, user: user1)
      notification3 = create(:notification, notifiable: proposal_notification, user: user2)

      expect(notification1.emailed_at).not_to be
      expect(notification2.emailed_at).not_to be
      expect(notification3.emailed_at).not_to be

      email_digest = EmailDigest.new(user1)
      email_digest.mark_as_emailed

{'fraction_non_alphanumeric': 0.08167870036101083, 'fraction_numerical': 0.007521058965102286, 'mean_word_length': 3.832122093023256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3926996', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1814, 'n_words': 463}