<commit_before>require "rails_helper"

describe MarkdownHelper, :type => :helper do
  before do
    stub_const("APP_CONFIG", {
      'hostname' => 'example.com',
      'https' => true,
      'items_per_page' => 5,
      'allowed_hosts' => [
        'twitter.com'
      ]
    })
  end

  describe '#markdown' do
    it 'should return the expected text' do
      expect(markdown('**Strong text**')).to eq('<p><strong>Strong text</strong></p>')
    end

    it 'should transform mentions into links' do
      expect(markdown('@jake_weary')).to eq('<p><a href="/jake_weary">@jake_weary</a></p>')
    end
  end

  describe '#strip_markdown' do
    it 'should not return formatted text' do
      expect(strip_markdown('**Strong text**')).to eq('Strong text')
    end
  end

  describe '#twitter_markdown' do
    context 'mentioned user has Twitter connected' do
      let(:user) { FactoryBot.create(:user) }
      let(:service) { Services::Twitter.create(user: user) }
  
      before do
        service.nickname = 'test'
        service.save!
      end

      it 'should transform a internal mention to a Twitter mention' do
        expect(twitter_markdown("@#{user.screen_name}")).to eq('@test')
      end
    end

    context 'mentioned user doesnt have Twitter connected' do
      it 'should not transform the mention' do
        expect(twitter_markdown('@test')).to eq('test')
      end
    end
  end

  describe '#question_markdown' do
    it 'should link allowed links without the linkfilter' do
      expect(question_markdown('https://twitter.com/retrospring')).to eq('<p><a href="https://twitter.com/retrospring" target="_blank" rel="nofollow">https://twitter.com/retrospring</a></p>')
    end

    it 'should link untrusted links with the linkfilter' do
      expect(question_markdown('https://rrerr.net')).to eq('<p><a href="/linkfilter?url=https%3A%2F%2Frrerr.net" target="_blank" rel="nofollow">https://rrerr.net</a></p>')
    end

    it 'should not process any markup aside of links' do
      expect(question_markdown('**your account has been disabled**, [click here to enable it again](https://evil.example.com)')). to eq('<p>**your account has been disabled**, [click here to enable it again](<a href="/linkfilter?url=https%3A%2F%2Fevil.example.com" target="_blank" rel="nofollow">https://evil.example.com</a>)</p>')
    end
  end

  describe '#raw_markdown' do
    it 'should return the expected text' do
      expect(raw_markdown('# Heading')).to eq("<h1>Heading</h1>\n")
    end
  end

  describe '#get_markdown' do
    it 'should return the contents of the specified file' do
      expect(get_markdown('spec/fixtures/markdown/test.md')).to eq("# Heading")
    end<commit_msg>Add test cases for invalid URIs in Markdown<commit_after>require "rails_helper"

describe MarkdownHelper, :type => :helper do
  before do
    stub_const("APP_CONFIG", {
      'hostname' => 'example.com',
      'https' => true,
      'items_per_page' => 5,
      'allowed_hosts' => [
        'twitter.com'
      ]
    })
  end

  describe '#markdown' do
    it 'should return the expected text' do
      expect(markdown('**Strong text**')).to eq('<p><strong>Strong text</strong></p>')
    end

    it 'should transform mentions into links' do
      expect(markdown('@jake_weary')).to eq('<p><a href="/jake_weary">@jake_weary</a></p>')
    end
  end

  describe '#strip_markdown' do
    it 'should not return formatted text' do
      expect(strip_markdown('**Strong text**')).to eq('Strong text')
    end
  end

  describe '#twitter_markdown' do
    context 'mentioned user has Twitter connected' do
      let(:user) { FactoryBot.create(:user) }
      let(:service) { Services::Twitter.create(user: user) }
  
      before do
        service.nickname = 'test'
        service.save!
      end

      it 'should transform a internal mention to a Twitter mention' do
        expect(twitter_markdown("@#{user.screen_name}")).to eq('@test')
      end
    end

    context 'mentioned user doesnt have Twitter connected' do
      it 'should not transform the mention' do
        expect(twitter_markdown('@test')).to eq('test')
      end
    end
  end

  describe '#question_markdown' do
    it 'should link allowed links without the linkfilter' do
      expect(question_markdown('https://twitter.com/retrospring')).to eq('<p><a href="https://twitter.com/retrospring" target="_blank" rel="nofollow">https://twitter.com/retrospring</a></p>')
    end

    it 'should link untrusted links with the linkfilter' do
      expect(question_markdown('https://rrerr.net')).to eq('<p><a href="/linkfilter?url=https%3A%2F%2Frrerr.net" target="_blank" rel="nofollow">https://rrerr.net</a></p>')
    end

    it 'should not process any markup aside of links' do
      expect(question_markdown('**your account has been disabled**, [click here to enable it again](https://evil.example.com)')). to eq('<p>**your account has been disabled**, [click here to enable it again](<a href="/linkfilter?url=https%3A%2F%2Fevil.example.com" target="_blank" rel="nofollow">https://evil.example.com</a>)</p>')
    end

    it 'should not raise an exception if an invalid link is processed' do
      expect{ question_markdown('https://example.com/example.質問') }.not_to raise_error
    end

    it 'should not process invalid links' do
      expect(question_markdown('https://example.com/example.質問')).to eq('<p>https://example.com/example.質問</p>')
    end
  end

  describe '#raw_markdown' do
    it 'should return the expected text' do
      expect(raw_markdown('# Heading')).to eq("<h1>Heading</h1>\n")
    end
  end

  describe '#get_markdown' do
    it 'should return the contents of the specified file' do
      expect(get_markdown('spec/fixtures/markdown/test.md')).to eq("# Heading")
    end
{'fraction_non_alphanumeric': 0.1463372194188272, 'fraction_numerical': 0.0031320689055159214, 'mean_word_length': 3.518867924528302, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 59, 'https://': 17, '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': '7540912', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1764, 'n_words': 476}