<commit_before># frozen_string_literal: true

require "rails_helper"

RSpec.describe "WorkBatchs", type: :request do
  describe "GET /collections/:id/batch/" do
    describe "Collection defense" do
      it "shouldn't be publicly accessible!" do
        collection = collections(:collection1)
        get collection_batch_path(collection)
        expect(response).to have_http_status(302)
      end
      it "should be accessible when logged in as admin" do
        user = users(:admin)
        collection = collections(:collection1)
        sign_in user
        get collection_batch_path(collection)
        expect(response).to have_http_status(200)
      end
      it "should not be accessible when logged in as an anonymous user" do
        sign_in users(:user_with_no_rights)
        get collection_batch_path(collections(:collection1))
        expect(response).to have_http_status(302)
        expect(response).to redirect_to root_path
      end
      it "should not be accessible when logged in as an registrator user" do
        sign_in users(:qkunst)
        get collection_batch_path(collections(:collection1))
        expect(response).to have_http_status(302)
        expect(response).to redirect_to root_path
      end
      it "should not allow accesss to the batch editor for non qkunst user has access to" do
        user = users(:read_only)
        sign_in user
        collection = collections(:collection3)
        get collection_batch_path(collection)
        expect(response).to have_http_status(302)
      end

      it "should redirect to the root when accessing anohter collection" do
        user = users(:read_only)
        sign_in user
        collection = collections(:collection1)
        get collection_batch_path(collection)
        expect(response).to have_http_status(302)
        expect(response).to redirect_to root_path
      end
    end
    describe "Field-accessibility" do
      it "describe facility should only be able to edit location" do
        sign_in users(:facility_manager)
        get collection_batch_path(collections(:collection1))
        response_body = response.body
        expect(response_body).to match('Adres en\/of gebouw\(deel\)\<\/label\>')
        expect(response_body).not_to match('Overige opmerkingen<\/label>')
        expect(response_body).not_to match('Aankoopprijs<\/label>')
        expect(response_body).not_to match('Marktwaardecategorie \(€\)<\/label>')
        expect(response_body).not_to match('Overige opmerkingen<\/label>')
      end<commit_msg>added additional specs for filtering works in batch mode<commit_after># frozen_string_literal: true

require "rails_helper"

RSpec.describe "WorkBatchs", type: :request do
  describe "GET /collections/:id/batch/" do
    describe "Collection defense" do
      it "shouldn't be publicly accessible!" do
        collection = collections(:collection1)
        get collection_batch_path(collection)
        expect(response).to have_http_status(302)
      end
      it "should be accessible when logged in as admin" do
        user = users(:admin)
        collection = collections(:collection1)
        sign_in user
        get collection_batch_path(collection)
        expect(response).to have_http_status(200)
      end
      it "should not be accessible when logged in as an anonymous user" do
        sign_in users(:user_with_no_rights)
        get collection_batch_path(collections(:collection1))
        expect(response).to have_http_status(302)
        expect(response).to redirect_to root_path
      end
      it "should not be accessible when logged in as an registrator user" do
        sign_in users(:qkunst)
        get collection_batch_path(collections(:collection1))
        expect(response).to have_http_status(302)
        expect(response).to redirect_to root_path
      end
      it "should not allow accesss to the batch editor for non qkunst user has access to" do
        user = users(:read_only)
        sign_in user
        collection = collections(:collection3)
        get collection_batch_path(collection)
        expect(response).to have_http_status(302)
      end

      it "should redirect to the root when accessing anohter collection" do
        user = users(:read_only)
        sign_in user
        collection = collections(:collection1)
        get collection_batch_path(collection)
        expect(response).to have_http_status(302)
        expect(response).to redirect_to root_path
      end
    end
    describe "Selection of works" do
      let(:user) { users(:admin) }
      let(:collection) { collections(:collection1) }
      let(:works) { [] }

      let(:expectations_checker) do
        works.each do |work|
          expect(response.body).to match(work.title)
          expect(response.body).to match(work.stock_number)
        end
      end

      before do
        sign_in user
      end

      after do
        expectations_checker
      end

      describe "by ids" do
        let(:works) { collection.works_including_child_works.limit(2) }

        it "should work for get with selected_works array" do
          get collection_batch_path(collection, params: {selected_works: works.map(&:id)})
        end

        it "should work for post with selected_works array" do
          post collection_batch_path(collection, params: {selected_works: works.map(&:id)})
        end
      end

      describe "with filter" do
        let(:theme) { themes(:wind)}
        let(:works) { collection.works_including_child_works.joins(:themes).where(themes: theme) }

        it "off" do
          expect(works.count).to be == 2
          post collection_batch_path(collection, params: { selected_work_groups: {themes: [theme.id]} })
        end

        describe "on" do
          let(:works) { collection.works_including_child_works.joins(:themes).where(themes: theme).where(market_value: 50) }

          it "works" do
            expect(works.count).to be == 1
            post collection_batch_path(collection, params: { selected_work_groups: {themes: [theme.id]}, filter: {market_value: [50]} })

            other_works_stock_number = (collection.works_including_child_works.joins(:themes).where(themes: theme).map(&:stock_number) - works.pluck(:stock_number))

            expect(response.body).not_to match(other_works_stock_number[0])
          end
        end
      end

      describe "by cluster group" do
        let(:cluster) { clusters(:cluster1)}
        let(:works) { collection.works_including_child_works.where(cluster: clusters(:cluster1)) }

        it "should work for post with cluster ids" do
          expect(works.count).to be >= 1
          post collection_batch_path(collection, params: { selected_work_groups: {cluster: [cluster.id]} })
        end


      end
    end
    describe "Field-accessibility" do
      it "describe facility should only be able to edit location" do
        sign_in users(:facility_manager)
        get collection_batch_path(collections(:collection1))
        response_body = response.body
        expect(response_body).to match('Adres en\/of gebouw\(deel\)\<\/label\>')
        expect(response_body).not_to match('Overige opmerkingen<\/label>')
        expect(response_body).not_to match('Aankoopprijs<\/label>')
        expect(response_body).not_to match('Marktwaardecategorie \(€\)<\/label>')
        expect(response_body).not_to match('Overige opmerkingen<\/label>')
      end
{'fraction_non_alphanumeric': 0.09087215601300108, 'fraction_numerical': 0.008396533044420368, 'mean_word_length': 2.930282064928153, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, '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': '7192124', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2033, 'n_words': 534}