<commit_before>RSpec.feature "Distributions", type: :system do
  before do
    sign_in(@user)
    @url_prefix = "/#{@organization.to_param}"

    @partner = create(:partner, organization: @organization)
    # allow_any_instance_of(StorageLocation).to receive(:geocode).and_return(true)
    @storage_location = create(:storage_location, organization: @organization)
    setup_storage_location(@storage_location)
  end

  context "When creating a new distribution manually" do
    it "Allows a distribution to be created" do
      with_features email_active: true do
        visit @url_prefix + "/distributions/new"

        select @partner.name, from: "Partner"
        select @storage_location.name, from: "From storage location"

        fill_in "Comment", with: "Take my wipes... please"

        expect do
          click_button "Save", match: :first
        end.to change { PartnerMailerJob.jobs.size }.by(1)

        expect(page).to have_content "Distributions"
        expect(page.find(".alert-info")).to have_content "reated"
      end
    end

    it "Displays a complete form after validation errors" do
      with_features email_active: true do
        visit @url_prefix + "/distributions/new"

        # verify line items appear on initial load
        expect(page).to have_selector "#distribution_line_items"

        select @partner.name, from: "Partner"
        expect do
          click_button "Save"
        end.not_to change { PartnerMailerJob.jobs.size }

        # verify line items appear on reload
        expect(page).to have_content "New Distribution"
        expect(page).to have_selector "#distribution_line_items"
      end
    end

    context "when there is insufficient inventory to fulfill the Distribution" do
      it "gracefully handles the error" do
        visit @url_prefix + "/distributions/new"

        select @partner.name, from: "Partner"
        select @storage_location.name, from: "From storage location"

        fill_in "Comment", with: "Take my wipes... please"

        item = @storage_location.inventory_items.first.item
        quantity = @storage_location.inventory_items.first.quantity
        select item.name, from: "distribution_line_items_attributes_0_item_id"
        fill_in "distribution_line_items_attributes_0_quantity", with: quantity * 2

        expect do
          click_button "Save", match: :first
          page.find('.alert')
        end.not_to change { Distribution.count }

        expect(page).to have_content("New Distribution")
        expect(page.find(".alert")).to have_content "exceed"
      end
    end
  end

  it "Does not include inactive items in the line item fields" do
    visit @url_prefix + "/distributions/new"

    item = Item.alphabetized.first

    select @storage_location.name, from: "From storage location"
    expect(page).to have_content(item.name)
    select item.name, from: "distribution_line_items_attributes_0_item_id"

    item.update(active: false)

    page.refresh
    select @storage_location.name, from: "From storage location"
    expect(page).to have_no_content(item.name)
  end

  it "errors if user does not fill storage_location" do
    visit @url_prefix + "/distributions/new"

    select @partner.name, from: "Partner"
    select "", from: "From storage location"

    click_button "Save", match: :first
    page.find('.alert')
    expect(page).to have_css('.alert.error', text: /storage location/i)
  end

  context "With an existing distribution" do
    let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: @user.organization) }

    before do
      visit @url_prefix + "/distributions"
    end

    it "the user can make changes" do
      click_on "Edit", match: :first
      expect do
        fill_in "Agency representative", with: "SO<NAME>"
        click_on "Save", match: :first
        distribution.reload
      end.to change { distribution.agency_rep }.to("SOMETHING DIFFERENT")
    end

    it "allows the user can change the issued_at date" do
      click_on "Edit", match: :first
      expect do
        select('2018', from: 'distribution_issued_at_1i')
        select('May', from: 'distribution_issued_at_2i')
        select('7', from: 'distribution_issued_at_3i')
        select('02 PM', from: 'distribution_issued_at_4i')
        select('15', from: 'distribution_issued_at_5i')
        click_on "Save", match: :first
        distribution.reload
      end.to change { distribution.issued_at }.to(Time.zone.parse("2018-05-07 14:15:00"))
    end

    it "disallows the user from changing the quantity above the inventory quantity" do
      click_on "Edit", match: :first
      expect do
        fill_in 'distribution_line_items_attributes_0_quantity', with: distribution.line_items.first.quantity + 300
        click_on "Save", match: :first
      end.not_to change { distribution.reload.line_items.first.quantity }
      expect(page).to have_content "Distribution could not be updated!"
    end

    it "the user can reclaim it" do
      expect do
        accept_confirm do
          click_on "Reclaim"
        end
        expect(page).to have_content "reclaimed"
      end.to change { Distribution.count }.by(-1)
    end

    context "when one of the items has been 'deleted'" do
      it "the user can still reclaim it and it reactivates the item", js: true do
        item = distribution.line_items.first.item
        item.destroy
        expect do
          accept_confirm do
            click_on "Reclaim"
          end
          page.find ".alert"
        end.to change { Distribution.count }.by(-1).and change { Item.active.count }.by(1)<commit_msg>Removes out of scope change<commit_after>RSpec.feature "Distributions", type: :system do
  before do
    sign_in(@user)
    @url_prefix = "/#{@organization.to_param}"

    @partner = create(:partner, organization: @organization)
    # allow_any_instance_of(StorageLocation).to receive(:geocode).and_return(true)
    @storage_location = create(:storage_location, organization: @organization)
    setup_storage_location(@storage_location)
  end

  context "When creating a new distribution manually" do
    it "Allows a distribution to be created" do
      with_features email_active: true do
        visit @url_prefix + "/distributions/new"

        select @partner.name, from: "Partner"
        select @storage_location.name, from: "From storage location"

        fill_in "Comment", with: "Take my wipes... please"

        expect do
          click_button "Save", match: :first
        end.to change { PartnerMailerJob.jobs.size }.by(1)

        expect(page).to have_content "Distributions"
        expect(page.find(".alert-info")).to have_content "reated"
      end
    end

    it "Displays a complete form after validation errors" do
      with_features email_active: true do
        visit @url_prefix + "/distributions/new"

        # verify line items appear on initial load
        expect(page).to have_selector "#distribution_line_items"

        select @partner.name, from: "Partner"
        expect do
          click_button "Save"
        end.not_to change { PartnerMailerJob.jobs.size }

        # verify line items appear on reload
        expect(page).to have_content "New Distribution"
        expect(page).to have_selector "#distribution_line_items"
      end
    end

    context "when there is insufficient inventory to fulfill the Distribution" do
      it "gracefully handles the error" do
        visit @url_prefix + "/distributions/new"

        select @partner.name, from: "Partner"
        select @storage_location.name, from: "From storage location"

        fill_in "Comment", with: "Take my wipes... please"

        item = @storage_location.inventory_items.first.item
        quantity = @storage_location.inventory_items.first.quantity
        select item.name, from: "distribution_line_items_attributes_0_item_id"
        fill_in "distribution_line_items_attributes_0_quantity", with: quantity * 2

        expect do
          click_button "Save", match: :first
          page.find('.alert')
        end.not_to change { Distribution.count }

        expect(page).to have_content("New Distribution")
        expect(page.find(".alert")).to have_content "exceed"
      end
    end
  end

  it "Does not include inactive items in the line item fields" do
    visit @url_prefix + "/distributions/new"

    item = Item.alphabetized.first

    select @storage_location.name, from: "From storage location"
    expect(page).to have_content(item.name)
    select item.name, from: "distribution_line_items_attributes_0_item_id"

    item.update(active: false)

    page.refresh
    select @storage_location.name, from: "From storage location"
    expect(page).to have_no_content(item.name)
  end

  it "errors if user does not fill storage_location" do
    visit @url_prefix + "/distributions/new"

    select @partner.name, from: "Partner"
    select "", from: "From storage location"

    click_button "Save", match: :first
    page.find('.alert')
    expect(page).to have_css('.alert.error', text: /storage location/i)
  end

  context "With an existing distribution" do
    let!(:distribution) { create(:distribution, :with_items, agency_rep: "A Person", organization: @user.organization) }

    before do
      visit @url_prefix + "/distributions"
    end

    it "the user can make changes" do
      click_on "Edit", match: :first
      expect do
        fill_in "Agency representative", with: "<NAME>FFERENT"
        click_on "Save", match: :first
        distribution.reload
      end.to change { distribution.agency_rep }.to("SOMETHING DIFFERENT")
    end

    it "allows the user can change the issued_at date" do
      click_on "Edit", match: :first
      expect do
        select('2018', from: 'distribution_issued_at_1i')
        select('May', from: 'distribution_issued_at_2i')
        select('7', from: 'distribution_issued_at_3i')
        select('02 PM', from: 'distribution_issued_at_4i')
        select('15', from: 'distribution_issued_at_5i')
        click_on "Save", match: :first
        distribution.reload
      end.to change { distribution.issued_at }.to(Time.zone.parse("2018-05-07 14:15:00"))
    end

    it "disallows the user from changing the quantity above the inventory quantity" do
      click_on "Edit", match: :first
      expect do
        fill_in 'distribution_line_items_attributes_0_quantity', with: distribution.line_items.first.quantity + 300
        click_on "Save", match: :first
      end.not_to change { distribution.line_items.first.quantity }
      expect(page).to have_content "Distribution could not be updated!"
    end

    it "the user can reclaim it" do
      expect do
        accept_confirm do
          click_on "Reclaim"
        end
        expect(page).to have_content "reclaimed"
      end.to change { Distribution.count }.by(-1)
    end

    context "when one of the items has been 'deleted'" do
      it "the user can still reclaim it and it reactivates the item", js: true do
        item = distribution.line_items.first.item
        item.destroy
        expect do
          accept_confirm do
            click_on "Reclaim"
          end
          page.find ".alert"
        end.to change { Distribution.count }.by(-1).and change { Item.active.count }.by(1)
{'fraction_non_alphanumeric': 0.0954537382347718, 'fraction_numerical': 0.007103534008169064, 'mean_word_length': 2.9574841883345044, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6257292', 'n_tokens_mistral': 3353, 'n_tokens_neox': 3181, 'n_words': 945}