<commit_before>require 'spec_helper'

describe MailTemplateConcern do
  let(:procedure) { create(:procedure) }
  let(:dossier) { create(:dossier, procedure: procedure) }
  let(:dossier2) { create(:dossier, procedure: procedure) }
  let(:initiated_mail) { create(:initiated_mail, procedure: procedure) }

  shared_examples "can replace tokens in template" do
    describe 'with no token to replace' do
      let(:template) { '[demarches-simplifiees.fr] rien à remplacer' }
      it do
        is_expected.to eq("[demarches-simplifiees.fr] rien à remplacer")
      end
    end

    describe 'with one token to replace' do
      let(:template) { '[demarches-simplifiees.fr] Dossier : --numéro du dossier--' }
      it do
        is_expected.to eq("[demarches-simplifiees.fr] Dossier : #{dossier.id}")
      end
    end

    describe 'with multiples tokens to replace' do
      let(:template) { '[demarches-simplifiees.fr] --numéro du dossier-- --libellé démarche-- --lien dossier--' }
      it do
        expected =
          "[demarches-simplifiees.fr] #{dossier.id} #{dossier.procedure.libelle} " +
          "<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}\">http://localhost:3000/dossiers/#{dossier.id}</a>"

        is_expected.to eq(expected)
      end
    end
  end

  describe '#subject_for_dossier' do
    before { initiated_mail.subject = template }
    subject { initiated_mail.subject_for_dossier(dossier) }

    it_behaves_like "can replace tokens in template"
  end

  describe '#body_for_dossier' do
    before { initiated_mail.body = template }
    subject { initiated_mail.body_for_dossier(dossier) }

    it_behaves_like "can replace tokens in template"
  end

  describe 'tags' do
    describe 'in initiated mail' do
      it "does not treat date de passage en instruction as a tag" do
        expect(initiated_mail.tags).not_to include(include({ libelle: 'date de passage en instruction' }))
      end
    end

    describe 'in received mail' do
      let(:received_mail) { create(:received_mail, procedure: procedure) }

      it "treats date de passage en instruction as a tag" do
        expect(received_mail.tags).to include(include({ libelle: 'date de passage en instruction' }))
      end
    end

    describe '--lien attestation--' do
      let(:attestation_template) { AttestationTemplate.new(activated: true) }
      let(:procedure) { create(:procedure, attestation_template: attestation_template) }

      subject { mail.body_for_dossier(dossier) }

      before do
        dossier.attestation = dossier.build_attestation
        dossier.reload
        mail.body = "--lien attestation--"
      end

      describe "in closed mail" do
        let(:mail) { create(:closed_mail, procedure: procedure) }

        it { is_expected.to eq("<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}/attestation\">http://localhost:3000/dossiers/#{dossier.id}/attestation</a>") }
      end

      describe "in refuse mail" do
        let(:mail) { create(:refused_mail, procedure: procedure) }

        it { is_expected.to eq("--lien attestation--") }
      end
    end
  end

  describe '#replace_tags' do
    before { initiated_mail.body = "n --numéro du dossier--" }
    it "avoids side effects" do
      expect(initiated_mail.body_for_dossier(dossier)).to eq("n #{dossier.id}")
      expect(initiated_mail.body_for_dossier(dossier2)).to eq("n #{dossier2.id}")
    end
  end
end<commit_msg>ajout d'un test<commit_after>require 'spec_helper'

describe MailTemplateConcern do
  let(:procedure) { create(:procedure) }
  let(:dossier) { create(:dossier, procedure: procedure) }
  let(:dossier2) { create(:dossier, procedure: procedure) }
  let(:initiated_mail) { create(:initiated_mail, procedure: procedure) }

  shared_examples "can replace tokens in template" do
    describe 'with no token to replace' do
      let(:template) { '[demarches-simplifiees.fr] rien à remplacer' }
      it do
        is_expected.to eq("[demarches-simplifiees.fr] rien à remplacer")
      end
    end

    describe 'with one token to replace' do
      let(:template) { '[demarches-simplifiees.fr] Dossier : --numéro du dossier--' }
      it do
        is_expected.to eq("[demarches-simplifiees.fr] Dossier : #{dossier.id}")
      end
    end

    describe 'with multiples tokens to replace' do
      let(:template) { '[demarches-simplifiees.fr] --numéro du dossier-- --libellé démarche-- --lien dossier--' }
      it do
        expected =
          "[demarches-simplifiees.fr] #{dossier.id} #{dossier.procedure.libelle} " +
          "<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}\">http://localhost:3000/dossiers/#{dossier.id}</a>"

        is_expected.to eq(expected)
      end
    end
  end

  describe '#subject_for_dossier' do
    before { initiated_mail.subject = template }
    subject { initiated_mail.subject_for_dossier(dossier) }

    it_behaves_like "can replace tokens in template"
  end

  describe '#body_for_dossier' do
    before { initiated_mail.body = template }
    subject { initiated_mail.body_for_dossier(dossier) }

    it_behaves_like "can replace tokens in template"
  end

  describe 'tags' do
    describe 'in initiated mail' do
      it "does not treat date de passage en instruction as a tag" do
        expect(initiated_mail.tags).not_to include(include({ libelle: 'date de passage en instruction' }))
      end
    end

    describe 'in received mail' do
      let(:received_mail) { create(:received_mail, procedure: procedure) }

      it "treats date de passage en instruction as a tag" do
        expect(received_mail.tags).to include(include({ libelle: 'date de passage en instruction' }))
      end
    end

    describe '--lien attestation--' do
      let(:attestation_template) { AttestationTemplate.new(activated: true) }
      let(:procedure) { create(:procedure, attestation_template: attestation_template) }

      subject { mail.body_for_dossier(dossier) }

      before do
        dossier.attestation = dossier.build_attestation
        dossier.reload
        mail.body = "--lien attestation--"
      end

      describe "in closed mail" do
        let(:mail) { create(:closed_mail, procedure: procedure) }

        it { is_expected.to eq("<a target=\"_blank\" rel=\"noopener\" href=\"http://localhost:3000/dossiers/#{dossier.id}/attestation\">http://localhost:3000/dossiers/#{dossier.id}/attestation</a>") }
      end

      describe "in refuse mail" do
        let(:mail) { create(:refused_mail, procedure: procedure) }

        it { is_expected.to eq("--lien attestation--") }
      end

      describe "in without continuation mail" do
        let(:mail) { create(:without_continuation_mail, procedure: procedure) }

        it { is_expected.to eq("--lien attestation--") }
      end
    end
  end

  describe '#replace_tags' do
    before { initiated_mail.body = "n --numéro du dossier--" }
    it "avoids side effects" do
      expect(initiated_mail.body_for_dossier(dossier)).to eq("n #{dossier.id}")
      expect(initiated_mail.body_for_dossier(dossier2)).to eq("n #{dossier2.id}")
    end
  end
end
{'fraction_non_alphanumeric': 0.123046875, 'fraction_numerical': 0.005301339285714286, 'mean_word_length': 3.1559420289855074, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '248260', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2206, 'n_words': 550}