<commit_before>require 'fast_spec_helper'
require 'interpol/configuration'
require 'yaml'
require 'psych'

module Interpol
  describe DefinitionFinder do
    describe '#find_definition' do
      def endpoint_def(message_type, status_codes, *versions)
        {
          'versions' => versions,
          'message_type' => message_type,
          'status_codes' => status_codes,
          'schema' => {},
          'examples' => {}
        }
      end

      def endpoint(name, method, route, *endpoint_defs)
        Endpoint.new \
          'name' => name,
          'route' => route,
          'method' => method,
          'definitions' => endpoint_defs
      end

      let(:endpoint_def_1a) { endpoint_def('response', ['2xx'], '1.3') }
      let(:endpoint_def_1b) { endpoint_def('response', nil, '1.3') }
      let(:endpoint_def_2a) { endpoint_def('request', nil, '2.3', '2.7') }

      let(:endpoint_1) do
        endpoint 'e1', 'GET', '/users/:user_id/overview', endpoint_def_1a, endpoint_def_1b
      end
      let(:endpoint_2)    { endpoint 'e2', 'POST', '/foo/bar', endpoint_def_2a}
      let(:all_endpoints) { [endpoint_1, endpoint_2].extend(DefinitionFinder) }

      def find(options)
        find_with_status_code(nil, options)
      end

      def find_with_status_code(status_code, options)
        all_endpoints.find_definition(options[:method], options[:path],
          options[:message_type], status_code) { |e| options[:version] }
      end

      it 'finds a matching endpoint definition' do
        found = find(:method => 'POST', :path => '/foo/bar',
          :version => '2.3', :message_type => 'request')
        expect(found.endpoint_name).to eq(endpoint_2.name)
        expect(found.version).to eq('2.3')
      end

      it 'finds the correct versioned definition of the endpoint' do
        found = find(:method => 'POST', :path => '/foo/bar',
          :version => '2.7', :message_type => 'request')
        expect(found.version).to eq('2.7')
      end

      it 'calls the version block with the endpoint' do
        endpoint = nil
        all_endpoints.find_definition('POST', '/foo/bar', 'request') do |e|
          endpoint = e
        end

        expect(endpoint).to be(endpoint_2)
      end

      it 'returns NoDefinitionFound if it cannot find a matching route' do
        result = find(:method => 'POST', :path => '/goo/bar',
          :version => '2.7', :message_type => 'request')
        expect(result).to be(DefinitionFinder::NoDefinitionFound)
      end

      it 'returns nil if the endpoint does not have a matching version' do
        result = find(:method => 'POST', :path => '/foo/bar',
          :version => '13.7', :message_type => 'request')
        expect(result).to be(DefinitionFinder::NoDefinitionFound)
      end

      it 'handles route params properly' do
        found = find_with_status_code('200', :method => 'GET', :path => '/users/17/overview',
          :version => '1.3', :message_type => 'response')
        expect(found.endpoint_name).to be(endpoint_1.name)
        expect(found.status_codes).to eq('2xx')
      end

      it 'handles status code params properly' do
        found = find_with_status_code('403', :method => 'GET', :path => '/users/17/overview',
          :version => '1.3', :message_type => 'response')
        expect(found.endpoint_name).to be(endpoint_1.name)
        expect(found.status_codes).to eq('xxx')
      end
    end
  end

  describe Configuration do
    let(:config) { Configuration.new }

    if defined?(::YAML::ENGINE.yamler)
      old_yamler = nil

      before(:all) do<commit_msg>We can't require psych on 1.8.7.<commit_after>require 'fast_spec_helper'
require 'interpol/configuration'
require 'yaml'

module Interpol
  describe DefinitionFinder do
    describe '#find_definition' do
      def endpoint_def(message_type, status_codes, *versions)
        {
          'versions' => versions,
          'message_type' => message_type,
          'status_codes' => status_codes,
          'schema' => {},
          'examples' => {}
        }
      end

      def endpoint(name, method, route, *endpoint_defs)
        Endpoint.new \
          'name' => name,
          'route' => route,
          'method' => method,
          'definitions' => endpoint_defs
      end

      let(:endpoint_def_1a) { endpoint_def('response', ['2xx'], '1.3') }
      let(:endpoint_def_1b) { endpoint_def('response', nil, '1.3') }
      let(:endpoint_def_2a) { endpoint_def('request', nil, '2.3', '2.7') }

      let(:endpoint_1) do
        endpoint 'e1', 'GET', '/users/:user_id/overview', endpoint_def_1a, endpoint_def_1b
      end
      let(:endpoint_2)    { endpoint 'e2', 'POST', '/foo/bar', endpoint_def_2a}
      let(:all_endpoints) { [endpoint_1, endpoint_2].extend(DefinitionFinder) }

      def find(options)
        find_with_status_code(nil, options)
      end

      def find_with_status_code(status_code, options)
        all_endpoints.find_definition(options[:method], options[:path],
          options[:message_type], status_code) { |e| options[:version] }
      end

      it 'finds a matching endpoint definition' do
        found = find(:method => 'POST', :path => '/foo/bar',
          :version => '2.3', :message_type => 'request')
        expect(found.endpoint_name).to eq(endpoint_2.name)
        expect(found.version).to eq('2.3')
      end

      it 'finds the correct versioned definition of the endpoint' do
        found = find(:method => 'POST', :path => '/foo/bar',
          :version => '2.7', :message_type => 'request')
        expect(found.version).to eq('2.7')
      end

      it 'calls the version block with the endpoint' do
        endpoint = nil
        all_endpoints.find_definition('POST', '/foo/bar', 'request') do |e|
          endpoint = e
        end

        expect(endpoint).to be(endpoint_2)
      end

      it 'returns NoDefinitionFound if it cannot find a matching route' do
        result = find(:method => 'POST', :path => '/goo/bar',
          :version => '2.7', :message_type => 'request')
        expect(result).to be(DefinitionFinder::NoDefinitionFound)
      end

      it 'returns nil if the endpoint does not have a matching version' do
        result = find(:method => 'POST', :path => '/foo/bar',
          :version => '13.7', :message_type => 'request')
        expect(result).to be(DefinitionFinder::NoDefinitionFound)
      end

      it 'handles route params properly' do
        found = find_with_status_code('200', :method => 'GET', :path => '/users/17/overview',
          :version => '1.3', :message_type => 'response')
        expect(found.endpoint_name).to be(endpoint_1.name)
        expect(found.status_codes).to eq('2xx')
      end

      it 'handles status code params properly' do
        found = find_with_status_code('403', :method => 'GET', :path => '/users/17/overview',
          :version => '1.3', :message_type => 'response')
        expect(found.endpoint_name).to be(endpoint_1.name)
        expect(found.status_codes).to eq('xxx')
      end
    end
  end

  describe Configuration do
    let(:config) { Configuration.new }

    if defined?(::YAML::ENGINE.yamler)
      require 'psych'
      old_yamler = nil

      before(:all) do
{'fraction_non_alphanumeric': 0.14116993191607613, 'fraction_numerical': 0.015145199388634152, 'mean_word_length': 2.655662772981209, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, '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': '6875255', 'n_tokens_mistral': 2226, 'n_tokens_neox': 2181, 'n_words': 524}