class RubyIndexer::EnhancementTest

def test_enhancing_indexing_included_hook

def test_enhancing_indexing_included_hook
  Class.new(Enhancement) do
    def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
      owner = @listener.current_owner
      return unless owner
      return unless call_node.name == :extend
      arguments = call_node.arguments&.arguments
      return unless arguments
      arguments.each do |node|
        next unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
        module_name = node.full_name
        next unless module_name == "ActiveSupport::Concern"
        @listener.register_included_hook do |index, base|
          class_methods_name = "#{owner.name}::ClassMethods"
          if index.indexed?(class_methods_name)
            singleton = index.existing_or_new_singleton_class(base.name)
            singleton.mixin_operations << Entry::Include.new(class_methods_name)
          end
        end
        @listener.add_method(
          "new_method",
          call_node.location,
          [Entry::Signature.new([Entry::RequiredParameter.new(name: :a)])],
        )
      rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError,
             Prism::ConstantPathNode::MissingNodesInConstantPathError
        # Do nothing
      end
    end
  end
  index(<<~RUBY)
    module ActiveSupport
      module Concern
        def self.extended(base)
          base.class_eval("def new_method(a); end")
        end
      end
    end
    module ActiveRecord
      module Associations
        extend ActiveSupport::Concern
        module ClassMethods
          def belongs_to(something); end
        end
      end
      class Base
        include Associations
      end
    end
    class User < ActiveRecord::Base
    end
  RUBY
  assert_equal(
    [
      "User::<Class:User>",
      "ActiveRecord::Base::<Class:Base>",
      "ActiveRecord::Associations::ClassMethods",
      "Object::<Class:Object>",
      "BasicObject::<Class:BasicObject>",
      "Class",
      "Module",
      "Object",
      "Kernel",
      "BasicObject",
    ],
    @index.linearized_ancestors_of("User::<Class:User>"),
  )
  assert_entry("new_method", Entry::Method, "/fake/path/foo.rb:10-4:10-33")
end