require 'rails_helper' RSpec.describe SignatureDetection do describe '.find_signature' do context 'when given an array of hashes' do let(:messages) do raw_message_files.map do |f| { content: File.read(f), content_type: content_type } end end context 'with plain text messages in their :content keys (sample input 1)' do let(:content_type) { 'text/plain' } let(:raw_message_files) do [ Rails.root.join('test/data/email_signature_detection/client_a_1.txt'), Rails.root.join('test/data/email_signature_detection/client_a_2.txt'), Rails.root.join('test/data/email_signature_detection/client_a_3.txt') ] end it 'returns the first 5–10-line substring they share in common' do expect(described_class.find_signature(messages)).to eq(<<~SIG.chomp) Mit freundlichen Grüßen Bob Smith Berechtigungen und dez. Department ________________________________ Musik AG Berechtigungen und dez. Department (ITPBM) Kastanien 2 SIG end end context 'with plain text messages in their :content keys (sample input 2)' do let(:content_type) { 'text/plain' } let(:raw_message_files) do [ Rails.root.join('test/data/email_signature_detection/client_b_1.txt'), Rails.root.join('test/data/email_signature_detection/client_b_2.txt'), Rails.root.join('test/data/email_signature_detection/client_b_3.txt') ] end it 'returns the first 5–10-line substring they share in common' do expect(described_class.find_signature(messages)).to eq(<<~SIG.chomp) Freundliche Grüße Günter Lässig Lokale Daten Music GmbH Baustraße 123, 12345 Max City Telefon 0123 5432114 Telefax 0123 5432139 SIG end end context 'with HTML messages in their :content keys' do let(:content_type) { 'text/html' } let(:raw_message_files) do [ Rails.root.join('test/data/email_signature_detection/client_c_1.html'), Rails.root.join('test/data/email_signature_detection/client_c_2.html'), Rails.root.join('test/data/email_signature_detection/client_c_3.html') ] end it 'converts messages (via #html2text) then returns the first 5–10-line substring they share in common' do expect(described_class.find_signature(messages)).to eq(<<~SIG.chomp) ChristianSmith Technik Tel: +49 12 34 56 78 441 Fax: +49 12 34 56 78 499 Email: Christian.Smith@example.com Web: www.example.com ABC KFZ- und Flugzeug B.V. & Co. KG Hauptverwaltung SIG end end end context 'when input messages do not share 5-line common substrings' do let(:messages) do Array.new(2) { { content: <<~RAW, content_type: 'text/plain' } } Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ut tincidunt nunc. Sed mattis aliquam tellus sit amet lacinia. Mauris fermentum dictum aliquet. Nam ex risus, gravida et ornare ut, mollis non sapien. RAW end it 'doesn’t break' do expect { described_class.find_signature(messages) }.not_to raise_error end end context 'when message contains exchange warning (#3571)' do let(:content_type) { 'text/html' } let(:messages) do [ { content: '
actual content
actual content 2
actual quote
actual quote
333 content
4452134123 content 2
123124123 quote
123141 144234
333 content
4452134123 content 2
9999 quote
999 144234