<commit_before>require 'test_helper'

class EmailTest < ActiveSupport::TestCase
  context "An email" do
    setup do
      @email = Mailhopper::Email.new
    end

    should "require a from address" do
      assert !@email.valid?
      @email.from_address = '<EMAIL>'
      assert @email.valid?
    end

    should "be generated when mail is sent" do
      headers = {
        :from => '<EMAIL>',
        :to => '<EMAIL>',
        :cc => '<EMAIL>',
        :bcc => '<EMAIL>',
        :reply_to => '<EMAIL>',
        :subject => 'Hiya!'
      }
      content = 'Papaya'

      assert_equal Mailhopper::Email.count, 0
      SampleMailer.hello(headers, content).deliver
      assert_equal Mailhopper::Email.count, 1

      email = Mailhopper::Email.first

      assert_equal headers[:from],      email.from_address
      assert_equal headers[:to],        email.to_address
      assert_equal headers[:cc],        email.cc_address
      assert_equal headers[:bcc],       email.bcc_address
      assert_equal headers[:reply_to],  email.reply_to_address
      assert_equal headers[:subject],   email.subject

      # Email content will include headers as well as content
      assert email.content.include?(content)

      assert email.sent_at.nil?
    end
  end
end<commit_msg>added test coverage for blank addresses and arrays of addresses<commit_after>require 'test_helper'

class EmailTest < ActiveSupport::TestCase
  context "An email" do
    setup do
      @email = Mailhopper::Email.new
    end

    should "require a from address" do
      assert !@email.valid?
      @email.from_address = '<EMAIL>'
      assert @email.valid?
    end

    should "be generated when mail is sent to individual addresses" do
      headers = {
        :from     => '<EMAIL>',
        :to       => '<EMAIL>',
        :cc       => '<EMAIL>',
        :bcc      => '<EMAIL>',
        :reply_to => 'reply_<EMAIL>',
        :subject  => 'Hiya!'
      }
      content = 'Papaya'

      generate_and_verify_email(headers, content)
    end

    should "be generated when mail is sent to multiple addresses" do
      headers = {
        :from     => '<EMAIL>',
        :to       => ['<EMAIL>', '<EMAIL>'],
        :cc       => ['<EMAIL>', '<EMAIL>'],
        :bcc      => ['<EMAIL>', '<EMAIL>'],
        :reply_to => '<EMAIL>',
        :subject  => 'Hiya!'
      }
      content = 'Papaya'

      generate_and_verify_email(headers, content)
    end

    should "be generated when mail is sent to blank addresses" do
      headers = {
        :from     => '<EMAIL>',
        :to       => '<EMAIL>',
        :cc       => nil,
        :bcc      => nil,
        :reply_to => nil,
        :subject  => 'Hiya!'
      }
      content = 'Papaya'

      generate_and_verify_email(headers, content)
    end
  end

  private

  def generate_and_verify_email(headers, content)
    assert_equal Mailhopper::Email.count, 0
    SampleMailer.hello(headers, content).deliver
    assert_equal Mailhopper::Email.count, 1

    email = Mailhopper::Email.first

    assert_email_matches_headers(headers, email)

    # Email content will include headers as well as content
    assert email.content.include?(content)

    assert email.sent_at.nil?
  end

  def assert_email_matches_headers(headers, email)
    assert_address_matches_header headers[:from],      email.from_address
    assert_address_matches_header headers[:to],        email.to_address
    assert_address_matches_header headers[:cc],        email.cc_address
    assert_address_matches_header headers[:bcc],       email.bcc_address
    assert_equal                  headers[:reply_to],  email.reply_to_address
    assert_equal                  headers[:subject],   email.subject
  end

  def assert_address_matches_header(header, address)
    if header.kind_of? Array
      assert_equal header.join(','), address
    else
      assert_equal header, address
    end
  end
end
{'fraction_non_alphanumeric': 0.11062516079238487, 'fraction_numerical': 0.0010290712631849757, 'mean_word_length': 2.4346289752650176, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 49, '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': '3065668', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1150, 'n_words': 288}