<commit_before># frozen_string_literal: true

# A shared module for testing ArrayConnection, RelationConnection,
# DatasetConnection and MongoRelationConnection.
#
# The test must implement `TestSchema` to serve the queries below with the expected results.
module ConnectionAssertions
  MAX_PAGE_SIZE = 6
  NAMES = [
    "Avocado",
    "Beet",
    "Cucumber",
    "Dill",
    "Eggplant",
    "Fennel",
    "Ginger",
    "Horseradish",
    "I Can't Believe It's Not Butter",
    "Jicama",
  ]

  def self.build_schema(get_items:, connection_class:, total_count_connection_class:)
    Class.new(GraphQL::Schema) do
      use GraphQL::Pagination::Connections
      use GraphQL::Execution::Interpreter

      default_max_page_size ConnectionAssertions::MAX_PAGE_SIZE

      # Make a way to get local variables (passed in as args)
      # into method resolvers below
      class << self
        attr_accessor :get_items, :connection_class, :total_count_connection_class
      end

      self.get_items = get_items
      self.connection_class = connection_class
      self.total_count_connection_class = total_count_connection_class

      item = Class.new(GraphQL::Schema::Object) do
        graphql_name "Item"
        field :name, String, null: false
      end

      custom_item_edge = Class.new(GraphQL::Types::Relay::BaseEdge) do
        node_type item
        graphql_name "CustomItemEdge"
      end

      custom_item_connection = Class.new(GraphQL::Types::Relay::BaseConnection) do
        graphql_name "CustomItemConnection"
        edge_type custom_item_edge
        field :total_count, Integer, null: false
      end

      query = Class.new(GraphQL::Schema::Object) do
        graphql_name "Query"
        field :items, item.connection_type, null: false do
          argument :max_page_size_override, Integer, required: false
        end

        def items(max_page_size_override: nil)
          context.schema.class.connection_class.new(get_items, max_page_size: max_page_size_override)
        end

        field :custom_items, custom_item_connection, null: false

        def custom_items
          context.schema.class.total_count_connection_class.new(get_items)
        end

        field :limited_items, item.connection_type, null: false, max_page_size: 2

        def limited_items
          get_items
        end

        private

        def get_items
          context.schema.class.get_items.call
        end
      end

      query(query)
    end
  end


  def self.included(child_module)
    child_module.class_exec do
      def exec_query(query_str, variables)
        TestSchema.execute(query_str, variables: variables)
      end

      def get_page_info(result, page_info_field)
        result["data"]["items"]["pageInfo"][page_info_field]
      end

      def assert_names(expected_names, result)
        nodes_names = result["data"]["items"]["nodes"].map { |n| n["name"] }
        assert_equal expected_names, nodes_names, "The nodes shortcut field has expected names"
        edges_names = result["data"]["items"]["edges"].map { |n| n["node"]["name"] }
        assert_equal expected_names, edges_names, "The edges.node has expected names"
      end

      describe "cursor-based pagination" do
        let(:query_str) { <<-GRAPHQL
          query($first: Int, $after: String, $last: Int, $before: String) {
            items(first: $first, after: $after, last: $last, before: $before) {
              nodes {
                name<commit_msg>Fix lint error<commit_after># frozen_string_literal: true

# A shared module for testing ArrayConnection, RelationConnection,
# DatasetConnection and MongoRelationConnection.
#
# The test must implement `TestSchema` to serve the queries below with the expected results.
module ConnectionAssertions
  MAX_PAGE_SIZE = 6
  NAMES = [
    "Avocado",
    "Beet",
    "Cucumber",
    "Dill",
    "Eggplant",
    "Fennel",
    "Ginger",
    "Horseradish",
    "I Can't Believe It's Not Butter",
    "Jicama",
  ]

  def self.build_schema(get_items:, connection_class:, total_count_connection_class:)
    Class.new(GraphQL::Schema) do
      use GraphQL::Pagination::Connections
      use GraphQL::Execution::Interpreter

      default_max_page_size ConnectionAssertions::MAX_PAGE_SIZE

      # Make a way to get local variables (passed in as args)
      # into method resolvers below
      class << self
        attr_accessor :get_items, :connection_class, :total_count_connection_class
      end

      self.get_items = get_items
      self.connection_class = connection_class
      self.total_count_connection_class = total_count_connection_class

      item = Class.new(GraphQL::Schema::Object) do
        graphql_name "Item"
        field :name, String, null: false
      end

      custom_item_edge = Class.new(GraphQL::Types::Relay::BaseEdge) do
        node_type item
        graphql_name "CustomItemEdge"
      end

      custom_item_connection = Class.new(GraphQL::Types::Relay::BaseConnection) do
        graphql_name "CustomItemConnection"
        edge_type custom_item_edge
        field :total_count, Integer, null: false
      end

      query = Class.new(GraphQL::Schema::Object) do
        graphql_name "Query"
        field :items, item.connection_type, null: false do
          argument :max_page_size_override, Integer, required: false
        end

        def items(max_page_size_override: nil)
          context.schema.class.connection_class.new(get_items, max_page_size: max_page_size_override)
        end

        field :custom_items, custom_item_connection, null: false

        def custom_items
          context.schema.class.total_count_connection_class.new(get_items)
        end

        field :limited_items, item.connection_type, null: false, max_page_size: 2

        def limited_items
          get_items
        end

        private

        def get_items
          context.schema.class.get_items.call
        end
      end

      query(query)
    end
  end

  def self.included(child_module)
    child_module.class_exec do
      def exec_query(query_str, variables)
        TestSchema.execute(query_str, variables: variables)
      end

      def get_page_info(result, page_info_field)
        result["data"]["items"]["pageInfo"][page_info_field]
      end

      def assert_names(expected_names, result)
        nodes_names = result["data"]["items"]["nodes"].map { |n| n["name"] }
        assert_equal expected_names, nodes_names, "The nodes shortcut field has expected names"
        edges_names = result["data"]["items"]["edges"].map { |n| n["node"]["name"] }
        assert_equal expected_names, edges_names, "The edges.node has expected names"
      end

      describe "cursor-based pagination" do
        let(:query_str) { <<-GRAPHQL
          query($first: Int, $after: String, $last: Int, $before: String) {
            items(first: $first, after: $after, last: $last, before: $before) {
              nodes {
                name
{'fraction_non_alphanumeric': 0.09062049062049062, 'fraction_numerical': 0.0005772005772005772, 'mean_word_length': 2.9425483503981797, 'pattern_counts': {'":': 0, '<': 11, '<?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': '5884458', 'n_tokens_mistral': 2069, 'n_tokens_neox': 2027, 'n_words': 485}