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

require 'rails_helper'

RSpec.describe CreateFailedEmails, db_strategy: :reset, type: :db_migration do
  before do
    ActiveRecord::Migration[7.0].drop_table :failed_emails

    # The table created by the migration has more columns than the one of freshly set up
    #   systems (see create_base.rb), so cached column information must not be reused.
    FailedEmail.reset_column_information

    # make sure folder does not exist
    FileUtils.rm_rf(dir)
  end

  describe '#up', system_init_done: true do
    let(:dir) { described_class::OLD_FAILED_EMAIL_DIRECTORY }

    context 'when unprocessable email files exist' do
      let(:content) { attributes_for(:failed_email)[:data] }

      before do
        FileUtils.mkdir_p dir

        File.binwrite dir.join('test.eml'), content
      end

      it 'imports unprocessable email form files' do
        migrate

        expect(FailedEmail.first).to have_attributes(data: content)
      end

      it 'removes unprocessable email directory' do
        expect { migrate }
          .to change { Dir.exist? dir }
          .to false
      end
    end

    context 'when unprocessable emails directory does not exist' do
      it 'does not crash' do
        expect { migrate }.not_to raise_error
      end
    end
  end
end
