<commit_before>require 'test_helper'

require 'active_support'

require 'blacklight_facet_browse/controller_mixin'
require 'blacklight_facet_browse/key_generator_v1'



# We do NOT currently integration test in a real Blacklight app,
# or really a real Rails app. This is just mocked unit testing
# in isolation, with a mocked application environment provided by
# our mock_controller_class. 
#
# Experiment to see how this does us -- it's definitely got some
# problems, but I just couldn't deal with actual integration testing
# inside a rails app.
describe BlacklightFacetBrowse::ControllerMixin do
  # We tried to use a real Blacklight::Configuration object, but BL
  # made it too hard, we'll just make a Hashie that simulates it,
  # can be used nested. 
  require 'hashie'
  class Conf < Hash
    include Hashie::Extensions::MergeInitializer
    include Hashie::Extensions::MethodAccess
    include Hashie::Extensions::IndifferentAccess
  end
  before do 
    # It's not even close to a Rails controller, just the
    # bare minimum to support the ControllerMixin
    mock_controller_class = Class.new do 
      # to make it easy for tests to set it, set it to our
      # internal Conf class for faking a BL configuration. 
      attr_accessor :blacklight_config
      attr_writer :params, :original_output

      # some things we use to see what was called on the mock
      attr_reader :super_facet_called
      
      # include anon module pretending to be superclass,
      # so our mixin can override and call super
      include( Module.new do 
        # a base implementation for the mixin to call super
        def solr_facet_params(facet_field, user_params=params || {}, extra_controller_params={})
          return original_output
        end

        # base implementation so we can call super, but we'll
        # do nothing but register it. 
        def facet
          @super_facet_called = true
        end

        # superclass imp so module can call it, no-op
        def get_facet_pagination(*args)
        end
        def respond_to(*args)
        end

        def params
          @params ||= Conf.new
        end

        def original_output
          @original_output ||= Conf.new(:"facet.field" => "original", "other" => "other")
        end
      end)

      include BlacklightFacetBrowse::ControllerMixin
    end


    @controller = mock_controller_class.new
    @keygen = BlacklightFacetBrowse::KeyGeneratorV1.new
    @controller.blacklight_config = Conf.new(
      :facet_browse => {
        :browse_key_generator => @keygen
      },
      :facet_fields => {
        :subject_facet => {
          :browse_field => "subject_browse_facet"
        }
      }
    )    
  end

  describe "solr_facet_params" do
    it "raises with no key generator set" do
      @controller.blacklight_config[:facet_browse][:browse_key_generator] = nil

      assert_raises(ArgumentError) do<commit_msg>our mock bl config needs a qt; still not working; mocking up the entire bl config is not working as a path, we're gonna need a real rails app, bah<commit_after>require 'test_helper'

require 'active_support'

require 'blacklight_facet_browse/controller_mixin'
require 'blacklight_facet_browse/key_generator_v1'



# We do NOT currently integration test in a real Blacklight app,
# or really a real Rails app. This is just mocked unit testing
# in isolation, with a mocked application environment provided by
# our mock_controller_class. 
#
# Experiment to see how this does us -- it's definitely got some
# problems, but I just couldn't deal with actual integration testing
# inside a rails app.
describe BlacklightFacetBrowse::ControllerMixin do
  # We tried to use a real Blacklight::Configuration object, but BL
  # made it too hard, we'll just make a Hashie that simulates it,
  # can be used nested. 
  require 'hashie'
  class Conf < Hash
    include Hashie::Extensions::MergeInitializer
    include Hashie::Extensions::MethodAccess
    include Hashie::Extensions::IndifferentAccess
  end
  before do 
    # It's not even close to a Rails controller, just the
    # bare minimum to support the ControllerMixin
    mock_controller_class = Class.new do 
      # to make it easy for tests to set it, set it to our
      # internal Conf class for faking a BL configuration. 
      attr_accessor :blacklight_config
      attr_writer :params, :original_output

      # some things we use to see what was called on the mock
      attr_reader :super_facet_called
      
      # include anon module pretending to be superclass,
      # so our mixin can override and call super
      include( Module.new do 
        # a base implementation for the mixin to call super
        def solr_facet_params(facet_field, user_params=params || {}, extra_controller_params={})
          return original_output
        end

        # base implementation so we can call super, but we'll
        # do nothing but register it. 
        def facet
          @super_facet_called = true
        end

        # superclass imp so module can call it, no-op
        def get_facet_pagination(*args)
        end
        def respond_to(*args)
        end

        def params
          @params ||= Conf.new
        end

        def original_output
          @original_output ||= Conf.new(:"facet.field" => "original", "other" => "other")
        end
      end)

      include BlacklightFacetBrowse::ControllerMixin
    end


    @controller = mock_controller_class.new
    @keygen = BlacklightFacetBrowse::KeyGeneratorV1.new
    @controller.blacklight_config = Conf.new(
      :qt => "search",      
      :facet_browse => {
        :browse_key_generator => @keygen
      },
      :facet_fields => {
        :subject_facet => {
          :browse_field => "subject_browse_facet"
        }
      }
    )    
  end

  describe "solr_facet_params" do
    it "raises with no key generator set" do
      @controller.blacklight_config[:facet_browse][:browse_key_generator] = nil

      assert_raises(ArgumentError) do
{'fraction_non_alphanumeric': 0.06589923256589923, 'fraction_numerical': 0.000667334000667334, 'mean_word_length': 2.8652482269503547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '945059', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1665, 'n_words': 595}