require 'rails_helper'
describe PostAnalyzer do
let(:default_topic_id) { 12 }
let(:url) { 'https://twitter.com/evil_trout/status/345954894420787200' }
describe '#cook' do
let(:post_analyzer) {PostAnalyzer.new nil, nil }
let(:raw) { "Here's a tweet:\n#{url}" }
let(:options) { {} }
let(:args) { [raw, options] }
before { Oneboxer.stubs(:onebox) }
it 'fetches the cached onebox for any urls in the post' do
Oneboxer.expects(:cached_onebox).with url
post_analyzer.cook(*args)
expect(post_analyzer.found_oneboxes?).to be(true)
end
it 'does not invalidate the onebox cache' do
Oneboxer.expects(:invalidate).with(url).never
post_analyzer.cook(*args)
end
context 'when invalidating oneboxes' do
let(:options) {{ invalidate_oneboxes: true }}
it 'invalidates the oneboxes for urls in the post' do
Oneboxer.expects(:invalidate).with url
post_analyzer.cook(*args)
end
end
end
context "links" do
let(:raw_no_links) { "hello world my name is evil trout" }
let(:raw_one_link_md) { "[jlawr](http://www.imdb.com/name/nm2225369)" }
let(:raw_two_links_html) { "disney reddit" }
let(:raw_three_links) { "http://discourse.org and http://discourse.org/another_url and http://www.imdb.com/name/nm2225369" }
let(:raw_elided) { "···
\nhttp://discourse.org\n
···
\ndiscourse.org\n
" }
let(:raw_post_with_avatars) { '
' }
let(:raw_post_with_favicon) { '
' }
let(:raw_post_with_two_classy_images) { "
" }
it "returns 0 images for an empty post" do
post_analyzer = PostAnalyzer.new("Hello world", nil)
expect(post_analyzer.image_count).to eq(0)
end
it "finds images from markdown" do
post_analyzer = PostAnalyzer.new(raw_post_one_image_md, default_topic_id)
expect(post_analyzer.image_count).to eq(1)
end
it "finds images from HTML" do
post_analyzer = PostAnalyzer.new(raw_post_two_images_html, default_topic_id)
expect(post_analyzer.image_count).to eq(2)
end
it "doesn't count avatars as images" do
post_analyzer = PostAnalyzer.new(raw_post_with_avatars, default_topic_id)
expect(post_analyzer.image_count).to eq(0)
end
it "doesn't count favicons as images" do
post_analyzer = PostAnalyzer.new(raw_post_with_favicon, default_topic_id)
expect(post_analyzer.image_count).to eq(0)
end
it "doesn't count thumbnails as images" do
post_analyzer = PostAnalyzer.new(raw_post_with_thumbnail, default_topic_id)
expect(post_analyzer.image_count).to eq(0)
end
it "doesn't count whitelisted images" do
Post.stubs(:white_listed_image_classes).returns(["classy"])
post_analyzer = PostAnalyzer.new(raw_post_with_two_classy_images, default_topic_id)
expect(post_analyzer.image_count).to eq(0)
end
end
describe "link_count" do
let(:raw_post_one_link_md) { "[sherlock](http://www.bbc.co.uk/programmes/b018ttws)" }
let(:raw_post_two_links_html) { "discourse twitter" }
let(:raw_post_with_mentions) { "hello @novemberkilo how are you doing?" }
it "returns 0 links for an empty post" do
post_analyzer = PostAnalyzer.new("Hello world", nil)
expect(post_analyzer.link_count).to eq(0)
end
it "returns 0 links for a post with mentions" do
post_analyzer = PostAnalyzer.new(raw_post_with_mentions, default_topic_id)
expect(post_analyzer.link_count).to eq(0)
end
it "finds links from markdown" do
Oneboxer.stubs :onebox
post_analyzer = PostAnalyzer.new(raw_post_one_link_md, default_topic_id)
expect(post_analyzer.link_count).to eq(1)
end
it "finds links from HTML" do
post_analyzer = PostAnalyzer.new(raw_post_two_links_html, default_topic_id)
post_analyzer.cook(raw_post_two_links_html, {})
expect(post_analyzer.found_oneboxes?).to be(false)
expect(post_analyzer.link_count).to eq(2)
end
end
describe "raw_mentions" do
it "returns an empty array with no matches" do
post_analyzer = PostAnalyzer.new("Hello Jake and Finn!", default_topic_id)
expect(post_analyzer.raw_mentions).to eq([])
end
it "returns lowercase unique versions of the mentions" do
post_analyzer = PostAnalyzer.new("@Jake @Finn @Jake", default_topic_id)
expect(post_analyzer.raw_mentions).to eq(['jake', 'finn'])
end
it "ignores pre" do
post_analyzer = PostAnalyzer.new("@Jake@Finn", default_topic_id) expect(post_analyzer.raw_mentions).to eq(['finn']) end it "catches content between pre tags" do post_analyzer = PostAnalyzer.new("
hello@Finn ", default_topic_id) expect(post_analyzer.raw_mentions).to eq(['finn']) end it "ignores code" do post_analyzer = PostAnalyzer.new("@Jake `@Finn`", default_topic_id) expect(post_analyzer.raw_mentions).to eq(['jake']) end it "ignores code in markdown-formatted code blocks" do post_analyzer = PostAnalyzer.new(" @Jake @Finn\n@Ryan", default_topic_id) expect(post_analyzer.raw_mentions).to eq(['ryan']) end it "ignores quotes" do post_analyzer = PostAnalyzer.new("[quote=\"Evil Trout\"]@Jake[/quote] @Finn", default_topic_id) expect(post_analyzer.raw_mentions).to eq(['finn']) end it "ignores oneboxes" do post_analyzer = PostAnalyzer.new("Hello @Jake\n#{url}", default_topic_id) post_analyzer.stubs(:cook).returns("
Hello @Jake
@Finn