require 'spec_helper'

describe 'processSummaryQueue.php' do

  before :all do
    @devbox = DevBox.instance
    @script_name = 'processSummaryQueue.php'
  end

  context 'with wrong params' do
    it 'requires a Month and a Year' do
      output = @devbox.run_acc_script(@script_name)
      expect(output).to include('Params Month and Year are required')

      output = @devbox.run_acc_script("#{@script_name} -y 2018")
      expect(output).to include('Params Month and Year are required')

      output = @devbox.run_acc_script("#{@script_name} -m 5")
      expect(output).to include('Params Month and Year are required')
    end

    it 'refuses any extra params' do
      output = @devbox.run_acc_script("#{@script_name} -m 5 -y 2018 -e extra_param")
      expect(output).to include("Usage: #{@script_name}")
    end
  end

  context 'with right params' do

    before :all do
      @conf = ConfigHelper.instance
      @active_mq = ActiveMQHelper.new
      @accounting_db = DataBase::AccountingDB.new
      @s3_helper = S3BucketHelper.new
      # Making sure that queue is empty
      @active_mq.clean_up
      # Creating a queue using ACCOUNTING_RUN_ENV env var
      @year = @conf[:accounting_year]
      @month = @conf[:accounting_month]
      @period_id = @accounting_db.acc_period_id
      publish_queue_script = "php publishAccountingQueue.php -m #{@month} -y #{@year} -b 1"
      @devbox.run("cd #{@conf[:devbox][:scripts_path]} && ACCOUNTING_RUN_ENV=test #{publish_queue_script}")
      # Cleaning emails
      @s3_helper.clean_up_emails

      # Clean up vendor_accounting/release_accounting tables
      @vendor_ids = @accounting_db.get_vendor_ids(limit: 100)
      @vendor_upcs = @accounting_db.get_vendor_upcs(@vendor_ids)
      @accounting_db.delete_vendor_accounting(@period_id, @vendor_ids)
      @accounting_db.delete_release_accounting(@period_id, @vendor_upcs)

      # Running processSummaryQueue script
      @output = @devbox.run_acc_script("#{@script_name} -m #{@conf[:accounting_month]} -y #{@conf[:accounting_year]}")
    end

    it 'processes vendor_list_<period_id> queue' do
      expect(@output).to be_empty
      # Making sure that the queue was processed
      EventuallyHelper.eventually do
        vendor_list_queue = @active_mq.queue_by_name("vendor_list_#{@period_id}")
        expect(vendor_list_queue['QueueSize']).to eq(0)
        expect(vendor_list_queue['DispatchCount']).to eq(@vendor_ids.count/10)
      end
      # Making sure that new vendor_accounting data were added
      expect(@accounting_db.get_vendor_accounting_count(@vendor_ids, @period_id)).to be > 0
      # Making sure that release_accounting table has new data
      release_accounting = @accounting_db.get_release_accounting(@period_id, @vendor_upcs)
      expect(release_accounting).not_to be_empty
    end

    it 'sends an email using SNS' do
      email_subject = 'Vendor Accounting'
      EventuallyHelper.eventually do
        @s3_helper.email_by_subject(email_subject) || raise("#{email_subject} email not found")
      end
      email_text = @s3_helper.read_email(@s3_helper.email_by_subject(email_subject))
      expect(email_text).to include("Processed #{@vendor_ids.count} vendors")
    end
  end
end
