<commit_before># frozen_string_literal: true

require "spec_helper"

describe OrderTaxAdjustmentsFetcher do
  describe "#totals" do
    let(:zone) { create(:zone_with_member) }
    let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) }

    let(:tax_rate10) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.1,
                        zone: zone)
    end
    let(:tax_rate15) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.15,
                        zone: zone)
    end
    let(:tax_rate20) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.2,
                        zone: zone)
    end
    let(:tax_rate25) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.25,
                        zone: zone)
    end
    let(:tax_category10) { create(:tax_category, tax_rates: [tax_rate10]) }
    let(:tax_category15) { create(:tax_category, tax_rates: [tax_rate15]) }
    let(:tax_category20) { create(:tax_category, tax_rates: [tax_rate20]) }
    let(:tax_category25) { create(:tax_category, tax_rates: [tax_rate25]) }

    let(:variant) do
      create(:variant, product: create(:product, tax_category: tax_category10))
    end
    let(:enterprise_fee) do
      create(:enterprise_fee, enterprise: coordinator,
                              tax_category: tax_category20,
                              calculator: Calculator::FlatRate.new(preferred_amount: 48.0))
    end
    let(:additional_adjustment) do
      create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0))
    end

    let(:order_cycle) do
      create(:simple_order_cycle, coordinator: coordinator,
                                  coordinator_fees: [enterprise_fee],
                                  distributors: [coordinator],
                                  variants: [variant])
    end
    let(:line_item) { create(:line_item, variant: variant, price: 44.0) }
    let(:order) do
      create(
        :order,
        line_items: [line_item],
        bill_address: create(:address),
        order_cycle: order_cycle,
        distributor: coordinator,
        adjustments: [additional_adjustment]
      )
    end

    before do
      allow(Spree::Config).to receive(:shipment_inc_vat).and_return(true)
      allow(Spree::Config).to receive(:shipping_tax_rate).and_return(tax_rate15.amount)
    end

    let(:shipping_method) do
      create(:shipping_method, calculator: Calculator::FlatRate.new(preferred_amount: 46.0))
    end
    let!(:shipment) do
      create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order)
    end

    before do
      order.create_tax_charge!
      order.update_distribution_charge!
    end

    subject { OrderTaxAdjustmentsFetcher.new(order).totals }

    it "returns a hash with all 3 taxes" do
      expect(subject.size).to eq(4)
    end

    it "contains tax on line_item" do
      expect(subject[tax_rate10]).to eq(4.0)
    end

    it "contains tax on shipping_fee" do
      expect(subject[tax_rate15]).to eq(6.0)
    end

    it "contains tax on enterprise_fee" do
      expect(subject[tax_rate20]).to eq(8.0)
    end

    it "contains tax on order adjustment" do
      expect(subject[tax_rate25]).to eq(10.0)
    end
  end
end
<commit_msg>Add failing spec for OrderTaxAdjustmentsFetcher with multiple line items<commit_after># frozen_string_literal: true

require "spec_helper"

describe OrderTaxAdjustmentsFetcher do
  describe "#totals" do
    let(:zone) { create(:zone_with_member) }
    let(:coordinator) { create(:distributor_enterprise, charges_sales_tax: true) }

    let(:tax_rate10) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.1,
                        zone: zone)
    end
    let(:tax_rate15) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.15,
                        zone: zone)
    end
    let(:tax_rate20) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.2,
                        zone: zone)
    end
    let(:tax_rate25) do
      create(:tax_rate, included_in_price: true,
                        calculator: Calculator::DefaultTax.new,
                        amount: 0.25,
                        zone: zone)
    end
    let(:tax_category10) { create(:tax_category, tax_rates: [tax_rate10]) }
    let(:tax_category15) { create(:tax_category, tax_rates: [tax_rate15]) }
    let(:tax_category20) { create(:tax_category, tax_rates: [tax_rate20]) }
    let(:tax_category25) { create(:tax_category, tax_rates: [tax_rate25]) }

    let(:variant) do
      create(:variant, product: create(:product, tax_category: tax_category10))
    end
    let(:enterprise_fee) do
      create(:enterprise_fee, enterprise: coordinator,
                              tax_category: tax_category20,
                              calculator: Calculator::FlatRate.new(preferred_amount: 48.0))
    end
    let(:additional_adjustment) do
      create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0))
    end

    let(:order_cycle) do
      create(:simple_order_cycle, coordinator: coordinator,
                                  coordinator_fees: [enterprise_fee],
                                  distributors: [coordinator],
                                  variants: [variant])
    end
    let(:line_item1) { create(:line_item, variant: variant, price: 44.0) }
    let(:line_item2) { create(:line_item, variant: variant, price: 44.0) }
    let(:order) do
      create(
        :order,
        line_items: [line_item1, line_item2],
        bill_address: create(:address),
        order_cycle: order_cycle,
        distributor: coordinator,
        adjustments: [additional_adjustment]
      )
    end

    before do
      allow(Spree::Config).to receive(:shipment_inc_vat).and_return(true)
      allow(Spree::Config).to receive(:shipping_tax_rate).and_return(tax_rate15.amount)
    end

    let(:shipping_method) do
      create(:shipping_method, calculator: Calculator::FlatRate.new(preferred_amount: 46.0))
    end
    let!(:shipment) do
      create(:shipment_with, :shipping_method, shipping_method: shipping_method, order: order)
    end

    before do
      order.create_tax_charge!
      order.update_distribution_charge!
    end

    subject { OrderTaxAdjustmentsFetcher.new(order).totals }

    it "returns a hash with all 3 taxes" do
      expect(subject.size).to eq(4)
    end

    xit "contains tax on line_item" do
      # This should be the sum of tax on all line items, but only returns tax for one.
      expect(subject[tax_rate10]).to eq(8.0)
    end

    it "contains tax on shipping_fee" do
      expect(subject[tax_rate15]).to eq(6.0)
    end

    it "contains tax on enterprise_fee" do
      expect(subject[tax_rate20]).to eq(8.0)
    end

    it "contains tax on order adjustment" do
      expect(subject[tax_rate25]).to eq(10.0)
    end
  end
end

{'fraction_non_alphanumeric': 0.09860850949959861, 'fraction_numerical': 0.021273748996521274, 'mean_word_length': 2.3490143369175627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2136030', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2198, 'n_words': 476}