0x00000000fea18000 0x00000000fea18fff 0x0000000000040200
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000007000 0x0000000000007fff 0x0000000000000100
0x00000000fdc00000 0x00000000fddfffff 0x0000000000000200
0x00000000fc200000 0x00000000fc3fffff 0x0000000000102201
0x0000000000000000 0x0000000000000000 0x0000000000000000
     GˆÊ�þB	@³Ž˜ÆB¶ ÿçÌƒi–\ÐË¿Ó  
   I�236Õ¿Ó ð    G# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

RSpec.shared_examples 'Whatsapp::Webhook::Message media' do |type:, mime_type:, extension:, caption: nil|
  describe '#process' do
    let(:channel) { create(:whatsapp_channel, welcome: 'Hey there!') }

    let(:from) do
      {
        phone: Faker::PhoneNumber.cell_phone_in_e164.delete('+'),
        name:  Faker::Name.unique.name,
      }
    end

    let(:json) do
      {
        object: 'whatsapp_business_account',
        entry:  [{
          id:      '222259550976437',
          changes: [{
            value: {
              messaging_product: 'whatsapp',
              metadata:          {
                display_phone_number: '15551340563',
                phone_number_id:      channel.options[:phone_number_id],
              },
              contacts:          [{
                profile: {
                  name: from[:name],
                },
                wa_id:   from[:phone],
              }],
              messages:          [{
                from:      from[:phone],
                id:        'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
                timestamp: '1707921703',
                type.to_sym => {
                  mime_type: mime_type,
                  sha256:    'mi4AElysqJQ1Oc4pugVtrLKyiB+GLE/pzPEgtBsrcLk=',
                  id:        '1870770316696531',
                  caption:   caption,
                }.compact,
                type:         type,
              }],
            },
            field: 'messages',
          }],
        }],
      }.to_json
    end

    let(:data) { JSON.parse(json).deep_symbolize_keys }

    context 'when all data is valid' do
      let(:url)            { Faker::Internet.unique.url }
      let(:media_content)  { SecureRandom.uuid }
      let(:media_file)     { Tempfile.create("sample.#{extension}").tap { |f| File.write(f, media_content) } }
      let(:valid_checksum) { Digest::SHA2.new(256).hexdigest(media_content) }

      let(:internal_response1) do
        Struct.new(:url, :mime_type, :sha256).new(url, mime_type, valid_checksum)
      end

      let(:internal_response2) do
        Struct.new(:success).new(true)
      end

      before do
        allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:get).and_return(internal_response1)
        allow_any_instance_of(WhatsappSdk::Api::Medias).to receive(:download).and_return(internal_response2)

        allow_any_instance_of(Whatsapp::Incoming::Media).to receive(:with_tmpfile).and_yield(media_file)
      end

      after do
        File.unlink(media_file)
      end

      it 'creates a user' do
        expect { described_class.new(data:, channel:).process }.to change(User, :count).by(1)
      end

      it 'creates a ticket + an article', :aggregate_failures do
        described_class.new(data:, channel:).process

        expect(Ticket.last).to have_attributes(
          title:    "#{from[:name]} (+#{from[:phone]}) via WhatsApp",
          group_id: channel.group_id,
        )
  