# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Channel::EmailParser::StructureCheck do
  subject(:check) { described_class.new(Mail.new(raw_mail)) }

  describe '#verify!' do
    context 'with a single part mail' do
      let(:raw_mail) { "From: foo@example.com\r\nSubject: Foo\r\n\r\nLorem ipsum" }

      it 'passes' do
        expect(check.verify!).to be(true)
      end
    end

    context 'with as many nested levels as allowed' do
      let(:raw_mail) { nested_multipart_mail(described_class::MAX_DEPTH) }

      it 'passes' do
        expect(check.verify!).to be(true)
      end
    end

    context 'with one nested level more than allowed' do
      let(:raw_mail) { nested_multipart_mail(described_class::MAX_DEPTH + 1) }

      it 'raises' do
        expect { check.verify! }.to raise_error(described_class::LimitExceeded, 'The email has more than 30 levels of nested parts.')
      end
    end

    context 'with nesting deep enough to overflow the stack of a recursive walk' do
      let(:raw_mail) { nested_multipart_mail(10_000) }

      it 'raises without walking past the limit' do
        expect { check.verify! }.to raise_error(described_class::LimitExceeded)
      end
    end

    context 'with as many parts as allowed' do
      let(:raw_mail) { wide_multipart_mail(described_class::MAX_PARTS) }

      it 'passes' do
        expect(check.verify!).to be(true)
      end
    end

    context 'with one part more than allowed' do
      let(:raw_mail) { wide_multipart_mail(described_class::MAX_PARTS + 1) }

      it 'raises' do
        expect { check.verify! }.to raise_error(described_class::LimitExceeded, 'The email has more than 500 parts.')
      end
    end

    context 'with a level of far too many parts' do
      let(:mail) { Mail.new(wide_multipart_mail(100_000)) }

      it 'raises before splitting it', :aggregate_failures do
        allow(mail).to receive(:parts).and_call_original

        expect { described_class.new(mail).verify! }.to raise_error(described_class::LimitExceeded, 'The email has more than 500 parts.')
        expect(mail).not_to have_received(:parts)
      end
    end

    context 'with a level of far too many parts and no boundary' do
      let(:mail) { Mail.new("From: foo@example.com\r\nContent-Type: multipart/mixed\r\n\r\n#{"--\r\n\r\nx\r\n" * 100_000}") }

      it 'raises before splitting it', :aggregate_failures do
        allow(mail).to receive(:parts).and_call_original

        expect { described_class.new(mail).verify! }.to raise_error(described_class::LimitExceeded, 'The email has more than 500 parts.')
        expect(mail).not_to have_received(:parts)
      end
    end

    context 'with a level of far too many parts behind closing delimiters' do
      let(:mail) { Mail.new("From: foo@example.com\r\nContent-Type: multipart/mixed; boundary=\"b\"\r\n\r\n#{"--b--\r\n\r\nx\r\n" * 100_000}") }

      it 'raises before splitting it', :aggregate_failures do
        allow(mail).to receive(:parts).and_call_original

        expect { described_class.new(mail).verify! }.to raise_error(described_class::LimitExceeded, 'The email has more than 500 parts.')
        expect(mail).not_to have_received(:parts)
      end
    end

    context 'with parts allowed on each level but too many in total' do
      let(:raw_mail) do
        inner = wide_multipart_mail(described_class::MAX_PARTS - 1).sub(%r{\A.*?(?=Content-Type)}m, '')

        "From: foo@example.com\r\nContent-Type: multipart/mixed; boundary=\"outer\"\r\n\r\n--outer\r\n#{inner}\r\n--outer\r\nContent-Type: text/plain\r\n\r\nLorem ipsum\r\n--outer--\r\n"
      end

      it 'raises' do
        expect { check.verify! }.to raise_error(described_class::LimitExceeded, 'The email has more than 500 parts.')
      end
    end
  end
end
