<commit_before>require 'spec_helper'

describe RegisterableRouteSet do
  describe '.from_content_item' do
    it "constructs a route set from a non-redirect content item" do
      item = build(:content_item, :base_path => "/path", :rendering_app => "frontend")
      item.routes = [
        { 'path' => '/path', 'type' => 'exact'},
        { 'path' => '/path.json', 'type' => 'exact'},
        { 'path' => '/path/subpath', 'type' => 'prefix'},
      ]
      route_set = RegisterableRouteSet.from_content_item(item)
      expect(route_set.is_redirect).to be_false
      expected_routes = [
        RegisterableRoute.new(:path => '/path',         :type => 'exact'),
        RegisterableRoute.new(:path => '/path.json',    :type => 'exact'),
        RegisterableRoute.new(:path => '/path/subpath', :type => 'prefix'),
      ]
      expect(route_set.registerable_routes).to match_array(expected_routes)
      expect(route_set.registerable_redirects).to eq([])
    end

    it "constructs a route set from a redirect content item" do
      item = build(:redirect_content_item, :base_path => "/path")
      item.redirects = [
        { "path" => "/path", "type" => 'exact', "destination" => "/somewhere" },
        { "path" => "/path/foo", "type" => "prefix", "destination" => "/somewhere-else" },
      ]

      route_set = RegisterableRouteSet.from_content_item(item)
      expect(route_set.is_redirect).to be_true
      expect(route_set.registerable_routes).to eq([])
      expected_redirects = [
        RegisterableRedirect.new(:path => "/path", :type => "exact", :destination => "/somewhere"),
        RegisterableRedirect.new(:path => "/path/foo", :type => "prefix", :destination => "/somewhere-else"),
      ]
      expect(route_set.registerable_redirects).to match_array(expected_redirects)
    end
  end

  describe "validations" do
    context "for a non-redirect item" do
      before :each do
        @route_set = build(:registerable_route_set, :is_redirect => false)
      end

      it 'is valid with a valid set of registerable routes' do
        @route_set.registerable_routes = [
          RegisterableRoute.new(:path => "#{@route_set.base_path}", :type => 'exact'),
          RegisterableRoute.new(:path => "#{@route_set.base_path}.json", :type => 'exact'),
          RegisterableRoute.new(:path => "#{@route_set.base_path}/exact-subpath", :type => 'exact'),
          RegisterableRoute.new(:path => "#{@route_set.base_path}/sub/path-prefix", :type => 'prefix'),
        ]
        expect(@route_set).to be_valid
      end

      it "requires some routes" do
        @route_set.registerable_routes = []
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end

      it "requires all routes to be valid" do
        @route_set.registerable_routes.first.type = "not_a_valid_type"
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end

      it "requires any redirects to be valid"

      it "requires all routes to be beneath the base path" do
        @route_set.registerable_routes << build(:registerable_route, :path => "/another-path")
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)

        # string prefix of base_path is not under the base path.
        @route_set.registerable_routes.last.path = "#{@route_set.base_path}-foo"
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end

      it "requires any redirects to be beneath the base path"

      it "requires the routes to include the base path" do
        @route_set.registerable_routes.first.path = "#{@route_set.base_path}/foo"
        expect(@route_set).to_not be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end
    end

    context "for a redirect item" do
      before :each do<commit_msg>Remove cruft<commit_after>require 'spec_helper'

describe RegisterableRouteSet do
  describe '.from_content_item' do
    it "constructs a route set from a non-redirect content item" do
      item = build(:content_item, :base_path => "/path", :rendering_app => "frontend")
      item.routes = [
        { 'path' => '/path', 'type' => 'exact'},
        { 'path' => '/path.json', 'type' => 'exact'},
        { 'path' => '/path/subpath', 'type' => 'prefix'},
      ]
      route_set = RegisterableRouteSet.from_content_item(item)
      expect(route_set.is_redirect).to be_false
      expected_routes = [
        RegisterableRoute.new(:path => '/path',         :type => 'exact'),
        RegisterableRoute.new(:path => '/path.json',    :type => 'exact'),
        RegisterableRoute.new(:path => '/path/subpath', :type => 'prefix'),
      ]
      expect(route_set.registerable_routes).to match_array(expected_routes)
      expect(route_set.registerable_redirects).to eq([])
    end

    it "constructs a route set from a redirect content item" do
      item = build(:redirect_content_item, :base_path => "/path")
      item.redirects = [
        { "path" => "/path", "type" => 'exact', "destination" => "/somewhere" },
        { "path" => "/path/foo", "type" => "prefix", "destination" => "/somewhere-else" },
      ]

      route_set = RegisterableRouteSet.from_content_item(item)
      expect(route_set.is_redirect).to be_true
      expect(route_set.registerable_routes).to eq([])
      expected_redirects = [
        RegisterableRedirect.new(:path => "/path", :type => "exact", :destination => "/somewhere"),
        RegisterableRedirect.new(:path => "/path/foo", :type => "prefix", :destination => "/somewhere-else"),
      ]
      expect(route_set.registerable_redirects).to match_array(expected_redirects)
    end
  end

  describe "validations" do
    context "for a non-redirect item" do
      before :each do
        @route_set = build(:registerable_route_set, :is_redirect => false)
      end

      it 'is valid with a valid set of registerable routes' do
        @route_set.registerable_routes = [
          RegisterableRoute.new(:path => "#{@route_set.base_path}", :type => 'exact'),
          RegisterableRoute.new(:path => "#{@route_set.base_path}.json", :type => 'exact'),
          RegisterableRoute.new(:path => "#{@route_set.base_path}/exact-subpath", :type => 'exact'),
          RegisterableRoute.new(:path => "#{@route_set.base_path}/sub/path-prefix", :type => 'prefix'),
        ]
        expect(@route_set).to be_valid
      end

      it "requires some routes" do
        @route_set.registerable_routes = []
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end

      it "requires all routes to be valid" do
        @route_set.registerable_routes.first.type = "not_a_valid_type"
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end

      it "requires all routes to be beneath the base path" do
        @route_set.registerable_routes << build(:registerable_route, :path => "/another-path")
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)

        # string prefix of base_path is not under the base path.
        @route_set.registerable_routes.last.path = "#{@route_set.base_path}-foo"
        expect(@route_set).not_to be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end

      it "requires the routes to include the base path" do
        @route_set.registerable_routes.first.path = "#{@route_set.base_path}/foo"
        expect(@route_set).to_not be_valid
        expect(@route_set).to have(1).error_on(:registerable_routes)
      end
    end

    context "for a redirect item" do
      before :each do
{'fraction_non_alphanumeric': 0.14105021080873897, 'fraction_numerical': 0.0012776287210936501, 'mean_word_length': 3.1048767697954904, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 77, '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': '7475847', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2399, 'n_words': 513}