<commit_before># frozen_string_literal: true

require 'spec_helper'

describe CheckoutHelper, type: :helper do
  it "generates html for validated inputs" do
    expect(helper).to receive(:render).with(
      "shared/validated_input",
      name: "test",
      path: "foo",
      attributes: { :required => true, :type => :email, :name => "foo", :id => "foo",
                    "ng-model" => "foo", "ng-class" => "{error: !fieldValid('foo')}" }
    )

    helper.validated_input("test", "foo", type: :email)
  end

  describe "displaying the tax total for an order" do
    let(:order) { double(:order, total_tax: 123.45, currency: 'AUD') }

    it "retrieves the total tax on the order" do
      expect(helper.display_checkout_tax_total(order)).to eq(Spree::Money.new(123.45,
                                                                              currency: 'AUD'))
    end
  end

  describe "#display_checkout_taxes_hash" do
    let(:order) { build(:order_with_totals) }
    let(:tax_rate10) { build(:tax_rate, amount: 0.1) }
    let(:tax_rate20) { build(:tax_rate, amount: 0.2) }
    let(:other_tax_rate20) { build(:tax_rate, amount: 0.2) }
    let(:adjustment1) {
      build(:adjustment, amount: 1, label: "10% tax", originator: tax_rate10)
    }
    let(:adjustment2) {
      build(:adjustment, amount: 2, label: "20% tax", originator: tax_rate20)
    }
    let(:other_adjustment2) {
      build(:adjustment, amount: 2, label: "20% tax", originator: other_tax_rate20)
    }

    it "produces an empty array without taxes" do
      expect(helper.display_checkout_taxes_hash(order)).to eq([])
    end

    it "shows a single tax adjustment" do
      order.all_adjustments << adjustment1
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(1, currency: order.currency),
          percentage: "10.0%",
          rate_amount: 0.1,
        }
      ]
    end

    it "shows multiple tax adjustments" do
      order.all_adjustments << adjustment1
      order.all_adjustments << adjustment2
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(1, currency: order.currency),
          percentage: "10.0%",
          rate_amount: 0.1,
        },
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
      ]
    end

    it "sorts adjustments by percentage" do
      order.all_adjustments << adjustment2
      order.all_adjustments << adjustment1
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(1, currency: order.currency),
          percentage: "10.0%",
          rate_amount: 0.1,
        },
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
      ]
    end

    it "shows multiple tax adjustments with same percentage" do
      order.all_adjustments << adjustment2
      order.all_adjustments << other_adjustment2
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
      ]

      expect(helper.display_checkout_taxes_hash(order).size).to eq 2
    end
  end

  it "knows if guests can checkout" do
    distributor = create(:distributor_enterprise)
    order = create(:order, distributor: distributor)
    allow(helper).to receive(:current_order) { order }
    expect(helper.guest_checkout_allowed?).to be true

    order.distributor.allow_guest_orders = false
    expect(helper.guest_checkout_allowed?).to be false
  end

  describe "#checkout_adjustments_for" do
    let(:order) { create(:order_with_totals_and_distribution) }
    let(:enterprise_fee) { create(:enterprise_fee, amount: 123) }
    let!(:fee_adjustment) {
      create(:adjustment, originator: enterprise_fee, adjustable: order,
                          order: order)
    }

    before do
      order.update_order!
      # Sanity check initial adjustments state
      expect(order.shipment_adjustments.count).to eq 1
      expect(order.adjustments.enterprise_fee.count).to eq 1
    end

    it "collects adjustments on the order" do
      adjustments = helper.checkout_adjustments_for(order)

      shipping_adjustment = order.shipment_adjustments.first
      expect(adjustments).to include shipping_adjustment

      admin_fee_summary = adjustments.reject { |a| a.id == shipping_adjustment.id }.first
      expect(admin_fee_summary.label).to eq "Shipping"
      expect(admin_fee_summary.amount).to eq 123
    end

    context "tax rate adjustments" do
      let!(:tax_rate) { create(:tax_rate, amount: 0.1, calculator: ::Calculator::DefaultTax.new) }
      let!(:line_item_fee_adjustment) {
        create(:adjustment, originator: enterprise_fee, adjustable: order.line_items.first,
                            order: order)<commit_msg>Test against unique fee name<commit_after># frozen_string_literal: true

require 'spec_helper'

describe CheckoutHelper, type: :helper do
  it "generates html for validated inputs" do
    expect(helper).to receive(:render).with(
      "shared/validated_input",
      name: "test",
      path: "foo",
      attributes: { :required => true, :type => :email, :name => "foo", :id => "foo",
                    "ng-model" => "foo", "ng-class" => "{error: !fieldValid('foo')}" }
    )

    helper.validated_input("test", "foo", type: :email)
  end

  describe "displaying the tax total for an order" do
    let(:order) { double(:order, total_tax: 123.45, currency: 'AUD') }

    it "retrieves the total tax on the order" do
      expect(helper.display_checkout_tax_total(order)).to eq(Spree::Money.new(123.45,
                                                                              currency: 'AUD'))
    end
  end

  describe "#display_checkout_taxes_hash" do
    let(:order) { build(:order_with_totals) }
    let(:tax_rate10) { build(:tax_rate, amount: 0.1) }
    let(:tax_rate20) { build(:tax_rate, amount: 0.2) }
    let(:other_tax_rate20) { build(:tax_rate, amount: 0.2) }
    let(:adjustment1) {
      build(:adjustment, amount: 1, label: "10% tax", originator: tax_rate10)
    }
    let(:adjustment2) {
      build(:adjustment, amount: 2, label: "20% tax", originator: tax_rate20)
    }
    let(:other_adjustment2) {
      build(:adjustment, amount: 2, label: "20% tax", originator: other_tax_rate20)
    }

    it "produces an empty array without taxes" do
      expect(helper.display_checkout_taxes_hash(order)).to eq([])
    end

    it "shows a single tax adjustment" do
      order.all_adjustments << adjustment1
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(1, currency: order.currency),
          percentage: "10.0%",
          rate_amount: 0.1,
        }
      ]
    end

    it "shows multiple tax adjustments" do
      order.all_adjustments << adjustment1
      order.all_adjustments << adjustment2
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(1, currency: order.currency),
          percentage: "10.0%",
          rate_amount: 0.1,
        },
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
      ]
    end

    it "sorts adjustments by percentage" do
      order.all_adjustments << adjustment2
      order.all_adjustments << adjustment1
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(1, currency: order.currency),
          percentage: "10.0%",
          rate_amount: 0.1,
        },
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
      ]
    end

    it "shows multiple tax adjustments with same percentage" do
      order.all_adjustments << adjustment2
      order.all_adjustments << other_adjustment2
      order.save!

      expect(helper.display_checkout_taxes_hash(order)).to eq [
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
        {
          amount: Spree::Money.new(2, currency: order.currency),
          percentage: "20.0%",
          rate_amount: 0.2,
        },
      ]

      expect(helper.display_checkout_taxes_hash(order).size).to eq 2
    end
  end

  it "knows if guests can checkout" do
    distributor = create(:distributor_enterprise)
    order = create(:order, distributor: distributor)
    allow(helper).to receive(:current_order) { order }
    expect(helper.guest_checkout_allowed?).to be true

    order.distributor.allow_guest_orders = false
    expect(helper.guest_checkout_allowed?).to be false
  end

  describe "#checkout_adjustments_for" do
    let(:order) { create(:order_with_totals_and_distribution) }
    let(:enterprise_fee) { create(:enterprise_fee, amount: 123) }
    let!(:fee_adjustment) {
      create(:adjustment, originator: enterprise_fee, adjustable: order,
                          order: order, label: "Enterprise Fee")
    }

    before do
      order.update_order!
      # Sanity check initial adjustments state
      expect(order.shipment_adjustments.count).to eq 1
      expect(order.adjustments.enterprise_fee.count).to eq 1
    end

    it "collects adjustments on the order" do
      adjustments = helper.checkout_adjustments_for(order)

      shipping_adjustment = order.shipment_adjustments.first
      expect(adjustments).to include shipping_adjustment

      admin_fee_summary = adjustments.reject { |a| a.id == shipping_adjustment.id }.first
      expect(admin_fee_summary.label).to eq "Enterprise Fee"
      expect(admin_fee_summary.amount).to eq 123
    end

    context "tax rate adjustments" do
      let!(:tax_rate) { create(:tax_rate, amount: 0.1, calculator: ::Calculator::DefaultTax.new) }
      let!(:line_item_fee_adjustment) {
        create(:adjustment, originator: enterprise_fee, adjustable: order.line_items.first,
                            order: order)
{'fraction_non_alphanumeric': 0.1174573055028463, 'fraction_numerical': 0.018975332068311195, 'mean_word_length': 2.60129825760164, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '232182', 'n_tokens_mistral': 3417, 'n_tokens_neox': 3219, 'n_words': 705}