<commit_before>require "spec_helper"

describe ExchangeProductsRenderer do
  let(:order_cycle) { create(:order_cycle) }
  let(:coordinator) { order_cycle.coordinator }
  let(:renderer) { described_class.new(order_cycle, coordinator.owner) }

  describe "#exchange_products" do
    describe "for an incoming exchange" do
      let(:exchange) { order_cycle.exchanges.incoming.first }

      it "loads products" do
        products = renderer.exchange_products(true, exchange.sender)

        expect(products.first.supplier.name).to eq exchange.variants.first.product.supplier.name
      end

      it "loads products in order" do
        sorted_products_names = products.pluck(:name).sort

        expect(products.pluck(:name)).to eq(sorted_products_names)
      end
    end

    describe "for an outgoing exchange" do
      let(:exchange) { order_cycle.exchanges.outgoing.first }

      it "loads products" do
        products = renderer.exchange_products(false, exchange.receiver)

        suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
        expect(suppliers).to include products.first.supplier.name
        expect(suppliers).to include products.second.supplier.name
      end

      it "loads products in order" do
        sorted_products_names = products.pluck(:name).sort

        expect(products.pluck(:name)).to eq(sorted_products_names)
      end

      context "showing products from coordinator inventory only" do
        before { order_cycle.update prefers_product_selection_from_coordinator_inventory_only: true }

        it "loads no products if there are no products from the coordinator inventory" do
          products = renderer.exchange_products(false, exchange.receiver)

          expect(products).to be_empty
        end

        it "loads products from the coordinator inventory" do
          # Add variant already in the exchange to the coordinator's inventory
          exchange.variants.first.inventory_items = [create(:inventory_item, enterprise: order_cycle.coordinator)]

          products = renderer.exchange_products(false, exchange.receiver)

          expect(products).to eq [exchange.variants.first.product]
        end
      end
    end
  end

  describe "#exchange_variants" do
    describe "for an incoming exchange" do
      it "loads variants" do
        exchange = order_cycle.exchanges.incoming.first
        variants = renderer.exchange_variants(true, exchange.sender)<commit_msg>updated implementation<commit_after>require "spec_helper"

describe ExchangeProductsRenderer do
  let(:order_cycle) { create(:order_cycle) }
  let(:coordinator) { order_cycle.coordinator }
  let(:renderer) { described_class.new(order_cycle, coordinator.owner) }

  describe "#exchange_products" do
    describe "for an incoming exchange" do
      let(:exchange) { order_cycle.exchanges.incoming.first }

      it "loads products" do
        products = renderer.exchange_products(true, exchange.sender)

        expect(products.first.supplier.name).to eq exchange.variants.first.product.supplier.name
      end

      it "loads products in order" do
        sorted_products_names = products.map(&:name).sort

        expect(products.map(&:name)).to eq(sorted_products_names)
      end
    end

    describe "for an outgoing exchange" do
      let(:exchange) { order_cycle.exchanges.outgoing.first }

      it "loads products" do
        products = renderer.exchange_products(false, exchange.receiver)

        suppliers = [exchange.variants[0].product.supplier.name, exchange.variants[1].product.supplier.name]
        expect(suppliers).to include products.first.supplier.name
        expect(suppliers).to include products.second.supplier.name
      end

      it "loads products in order" do
        sorted_products_names = products.map(&:name).sort

        expect(products.map(&:name)).to eq(sorted_products_names)
      end

      context "showing products from coordinator inventory only" do
        before { order_cycle.update prefers_product_selection_from_coordinator_inventory_only: true }

        it "loads no products if there are no products from the coordinator inventory" do
          products = renderer.exchange_products(false, exchange.receiver)

          expect(products).to be_empty
        end

        it "loads products from the coordinator inventory" do
          # Add variant already in the exchange to the coordinator's inventory
          exchange.variants.first.inventory_items = [create(:inventory_item, enterprise: order_cycle.coordinator)]

          products = renderer.exchange_products(false, exchange.receiver)

          expect(products).to eq [exchange.variants.first.product]
        end
      end
    end
  end

  describe "#exchange_variants" do
    describe "for an incoming exchange" do
      it "loads variants" do
        exchange = order_cycle.exchanges.incoming.first
        variants = renderer.exchange_variants(true, exchange.sender)
{'fraction_non_alphanumeric': 0.08345091715379964, 'fraction_numerical': 0.0008062890546260835, 'mean_word_length': 3.3072916666666665, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7384433', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1326, 'n_words': 328}