<commit_before>require "test_helper"
require "account_mailer"

class AccountMailerTest < UnitTestCase
  FIXTURES_PATH = File.dirname(__FILE__) + "/../account_mailer"

  def setup
    I18n.locale = "en"
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    @expected = Mail.new
    @expected.mime_version = "1.0"
    super
  end

  # Run off an email in both HTML and text form.
  def run_mail_test(name, user = nil)
    text_files = Dir.glob("#{FIXTURES_PATH}/#{name}.text*").
                 reject { |x| x.match(/\.new$/) }
    html_files = Dir.glob("#{FIXTURES_PATH}/#{name}.html*").
                 reject { |x| x.match(/\.new$/) }

    assert(text_files.any? || html_files.any?)

    if text_files.any?
      user.email_html = false if user
      yield
      email = ActionMailer::Base.deliveries.last.encoded
      email = remove_extraneous_email_headers(email)
      assert_string_equal_file(email, *text_files)
    end

    if html_files.any?
      user.email_html = true if user
      yield
      email = ActionMailer::Base.deliveries.last.encoded
      email = remove_extraneous_email_headers(email)
      fix_mac_vs_pc!(email)
      assert_string_equal_file(email, *html_files)
    end
  end

  # At the moment at least Redcloth produces slightly different output on
  # Nathan's laptop than on Jason's.  I'm trying to reduce both responses to a
  # common form so that we don't need to continue to tweak two separate copies
  # of every email response.  But I'm failing...
  def fix_mac_vs_pc!(email)
    email.gsub!(%r{<br />\n}, "<br/>")
    email.gsub!(/&#38;/, "&amp;")
    email.gsub!(/ &#8212;/, "&#8212;")
    email.gsub!(/^\s+/, "")
    email.gsub!(/\r\n?/, "\n")
  end

  def remove_extraneous_email_headers(str)
    in_header = true
    keep_wrapped_line = false
    str.split("\n").select do |line|
      in_header = false if line =~ /^\s*$|<html>/
      if !in_header
        true
      elsif line =~ /^(To|From|Reply-To|Cc|Bcc|Subject):/
        keep_wrapped_line = true
      elsif keep_wrapped_line && line !~ /^[A-Z][\w\-]+:/
        true
      else
        keep_wrapped_line = false
      end
    end.join("\n")
  end

  ##############################################################################<commit_msg>Fix AccountMailerTest failures [Finishes #154110059]<commit_after>require "test_helper"
require "account_mailer"

class AccountMailerTest < UnitTestCase
  FIXTURES_PATH = File.dirname(__FILE__) + "/../account_mailer"

  def setup
    I18n.locale = "en"
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    @expected = Mail.new
    @expected.mime_version = "1.0"
    super
  end

  # Run off an email in both HTML and text form.
  def run_mail_test(name, user = nil)
    text_files = Dir.glob("#{FIXTURES_PATH}/#{name}.text*").
                 reject { |x| x.match(/\.new$/) }
    html_files = Dir.glob("#{FIXTURES_PATH}/#{name}.html*").
                 reject { |x| x.match(/\.new$/) }

    assert(text_files.any? || html_files.any?)

    if text_files.any?
      user.email_html = false if user
      yield
      email = ActionMailer::Base.deliveries.last.encoded
      email = remove_extraneous_email_headers(email)
      assert_string_equal_file(email, *text_files)
    end

    if html_files.any?
      user.email_html = true if user
      yield
      email = ActionMailer::Base.deliveries.last.encoded
      email = remove_extraneous_email_headers(email)
      fix_mac_vs_pc!(email)
      assert_string_equal_file(email, *html_files)
    end
  end

  # At the moment at least Redcloth produces slightly different output on
  # Nathan's laptop than on Jason's.  I'm trying to reduce both responses to a
  # common form so that we don't need to continue to tweak two separate copies
  # of every email response.  But I'm failing...
  def fix_mac_vs_pc!(email)
    email.gsub!(/&#38;/, "&amp;")
    email.gsub!(/ &#8212;/, "&#8212;")
    email.gsub!(/^\s+/, "")
    email.gsub!(/\r\n?/, "\n")
  end

  def remove_extraneous_email_headers(str)
    in_header = true
    keep_wrapped_line = false
    str.split("\n").select do |line|
      in_header = false if line =~ /^\s*$|<html>/
      if !in_header
        true
      elsif line =~ /^(To|From|Reply-To|Cc|Bcc|Subject):/
        keep_wrapped_line = true
      elsif keep_wrapped_line && line !~ /^[A-Z][\w\-]+:/
        true
      else
        keep_wrapped_line = false
      end
    end.join("\n")
  end

  ##############################################################################
{'fraction_non_alphanumeric': 0.162440911044263, 'fraction_numerical': 0.007950150408250967, 'mean_word_length': 2.911764705882353, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '3813582', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1542, 'n_words': 347}