REFACTOR: refactor pretty text spec and move in namespace dir

REFACTOR: refactor pretty text spec and move in namespace dir

diff --git a/spec/lib/discourse_post_event/event_finder_spec.rb b/spec/lib/discourse_post_event/event_finder_spec.rb
new file mode 100644
index 0000000..5dc0e53
--- /dev/null
+++ b/spec/lib/discourse_post_event/event_finder_spec.rb
@@ -0,0 +1,91 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+require_relative '../../fabricators/event_fabricator'
+
+describe DiscoursePostEvent::EventFinder do
+  let(:current_user) { Fabricate(:user) }
+  let(:user) { Fabricate(:user) }
+
+  subject { DiscoursePostEvent::EventFinder }
+
+  before do
+    Jobs.run_immediately!
+    SiteSetting.discourse_post_event_enabled = true
+  end
+
+  context 'the event is associated to a visible post' do
+    let(:post1) {
+      PostCreator.create!(
+        user,
+        title: 'We should buy a boat',
+        raw: 'The boat market is quite active lately.'
+      )
+    }
+    let!(:event) { Fabricate(:event, post: post1) }
+
+    it 'returns the event' do
+      expect(subject.search(current_user)).to match_array([event])
+    end
+  end
+
+  context 'the event is associated to a visible PM' do
+    let(:post1) {
+      PostCreator.create!(
+        user,
+        title: 'We should buy a boat',
+        raw: 'The boat market is quite active lately.',
+        archetype: Archetype.private_message,
+        target_usernames: "#{current_user.username}"
+      )
+    }
+    let!(:event) { Fabricate(:event, post: post1) }
+
+    it 'returns the event' do
+      expect(subject.search(current_user)).to match_array([event])
+    end
+  end
+
+  context 'the event is associated to a not visible PM' do
+    let(:another_user) { Fabricate(:user) }
+    let(:post1) {
+      PostCreator.create!(
+        user,
+        title: 'We should buy a boat',
+        raw: 'The boat market is quite active lately.',
+        archetype: Archetype.private_message,
+        target_usernames: "#{another_user.username}"
+      )
+    }
+    let!(:event) { Fabricate(:event, post: post1) }
+
+    it 'doesn’t return the event' do
+      expect(subject.search(current_user)).to match_array([])
+    end
+  end
+
+  context 'events are filtered' do
+    context 'by post_id' do
+      let(:post1) {
+        PostCreator.create!(
+          user,
+          title: 'We should buy a boat',
+          raw: 'The boat market is quite active lately.'
+        )
+      }
+      let(:post2) {
+        PostCreator.create!(
+          user,
+          title: 'We should buy another boat',
+          raw: 'The boat market is very active lately.'
+        )
+      }
+      let!(:event1) { Fabricate(:event, post: post1) }
+      let!(:event2) { Fabricate(:event, post: post2) }
+
+      it 'returns only the specified event' do
+        expect(subject.search(current_user, { post_id: post2.id })).to match_array([event2])
+      end
+    end
+  end
+end
diff --git a/spec/lib/discourse_post_event/event_parser_spec.rb b/spec/lib/discourse_post_event/event_parser_spec.rb
new file mode 100644
index 0000000..737ce29
--- /dev/null
+++ b/spec/lib/discourse_post_event/event_parser_spec.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+
+def build_post(user, raw)
+  Post.new(user: user, raw: raw)
+end
+
+describe DiscoursePostEvent::EventParser do
+  subject { DiscoursePostEvent::EventParser }
+
+  let(:user) { Fabricate(:user) }
+
+  it 'works with no event' do
+    events = subject.extract_events(build_post(user, 'this could be a nice event'))
+    expect(events.length).to eq(0)
+  end
+
+  it 'finds one event' do
+    events = subject.extract_events(build_post(user, '[event start="foo" end="bar"]\n[/event]'))
+    expect(events.length).to eq(1)
+  end
+
+  it 'finds multiple events' do
+    post_event = build_post user, <<~TXT
+      [event start="2020"]
+      [/event]
+
+      [event start="2021"]
+      [/event]
+    TXT
+
+    events = subject.extract_events(post_event)
+    expect(events.length).to eq(2)
+  end
+
+  it 'parses options' do
+    events = subject.extract_events(build_post(user, '[event start="foo" end="bar"]\n[/event]'))
+    expect(events[0][:start]).to eq("foo")
+    expect(events[0][:end]).to eq("bar")
+  end
+
+  it 'works with escaped string' do
+    events = subject.extract_events(build_post(user, "I am going to get that fixed.\n\n[event start=\"bar\"]\n[/event]"))
+    expect(events[0][:start]).to eq("bar")
+  end
+
+  it 'parses options where value has spaces' do
+    events = subject.extract_events(build_post(user, '[event start="foo" name="bar baz"]\n[/event]'))
+    expect(events[0][:name]).to eq("bar baz")
+  end
+
+  it 'doesn’t parse invalid options' do
+    events = subject.extract_events(build_post(user, "I am going to get that fixed.\n\n[event start=\"foo\" something=\"bar\"]\n[/event]"))
+    expect(events[0][:something]).to be(nil)
+
+    events = subject.extract_events(build_post(user, "I am going to get that fixed.\n\n[event something=\"bar\"]\n[/event]"))
+    expect(events).to eq([])
+  end
+
+  it 'doesn’t parse an event in codeblock' do
+    post_event = build_post user, <<-TXT
+      Example event:
+      `‍``
+      [event start=\"bar\"]\n[/event]
+      `‍``
+    TXT
+
+    events = subject.extract_events(post_event)
+
+    expect(events).to eq([])
+  end
+
+  it 'doesn’t parse in blockquote' do
+    post_event = build_post user, <<-TXT
+      [event start="2020"][/event]
+    TXT
+
+    events = subject.extract_events(post_event)
+    expect(events).to eq([])
+  end
+end
diff --git a/spec/lib/discourse_post_event/pretty_text_spec.rb b/spec/lib/discourse_post_event/pretty_text_spec.rb
new file mode 100644
index 0000000..5cc7cb8
--- /dev/null
+++ b/spec/lib/discourse_post_event/pretty_text_spec.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+require_relative '../../fabricators/event_fabricator'
+
+describe PrettyText do
+  before do
+    freeze_time Time.utc(2018, 6, 5, 18, 40)
+
+    SiteSetting.calendar_enabled = true
+    SiteSetting.discourse_post_event_enabled = true
+  end
+
+  context 'public event' do
+    describe 'An event is displayed in an email' do
+      let(:user_1) { Fabricate(:user, admin: true) }
+
+      context 'The event has no name' do
+        let(:post_1) { create_post_with_event(user_1) }
+
+        it 'displays the topic title' do
+          cooked = PrettyText.cook(post_1.raw)
+
+          expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)
+            <div style='border:1px solid #dedede'>
+              <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>
+              <p>2018-06-05T18:39:50.000Z (UTC)</p>
+            </div>
+          HTML
+        end
+      end
+
+      context 'The event has a name' do
+        let(:post_1) { create_post_with_event(user_1, 'name="Pancakes event"') }
+
+        it 'displays the event name' do
+          cooked = PrettyText.cook(post_1.raw)
+
+          expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)
+            <div style='border:1px solid #dedede'>
+              <p><a href="#{Discourse.base_url}#{post_1.url}">Pancakes event</a></p>
+              <p>2018-06-05T18:39:50.000Z (UTC)</p>
+            </div>
+          HTML
+        end
+      end
+
+      context 'The event has an end date' do
+        let(:post_1) { create_post_with_event(user_1, 'end="2018-06-22"') }
+
+        it 'displays the end date' do
+          cooked = PrettyText.cook(post_1.raw)
+
+          expect(PrettyText.format_for_email(cooked, post_1)).to match_html(<<~HTML)
+            <div style='border:1px solid #dedede'>
+              <p><a href="#{Discourse.base_url}#{post_1.url}">#{post_1.topic.title}</a></p>
+              <p>2018-06-05T18:39:50.000Z (UTC) → 2018-06-22 (UTC)</p>
+            </div>
+          HTML
+        end
+      end
+    end
+  end
+end
diff --git a/spec/lib/discourse_post_event/rrule_generator_spec.rb b/spec/lib/discourse_post_event/rrule_generator_spec.rb
new file mode 100644

[... diff too long, it was truncated ...]

GitHub sha: ba7be7aa