Skip to content

A platform for community discussion. Free, open, simple.

59.9 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseEvents::Events::Event do
  before do
    freeze_time DateTime.parse("2020-04-24 14:10")
    Jobs.run_immediately!
    SiteSetting.discourse_events_enabled = true
    SiteSetting.discourse_post_event_enabled = true
  end

  it do
    is_expected.to validate_length_of(:description).is_at_most(
      DiscourseEvents::Events::Event::MAX_DESCRIPTION_LENGTH,
    )
  end

  it do
    is_expected.to validate_length_of(:name).is_at_least(
      DiscourseEvents::Events::Event::MIN_NAME_LENGTH,
    ).is_at_most(DiscourseEvents::Events::Event::MAX_NAME_LENGTH)
  end

  it do
    is_expected.to validate_length_of(:location).is_at_most(
      DiscourseEvents::Events::Event::MAX_LOCATION_LENGTH,
    )
  end

  it do
    is_expected.to validate_length_of(:url).is_at_most(
      DiscourseEvents::Events::Event::MAX_URL_LENGTH,
    )
  end

  it do
    is_expected.to validate_numericality_of(:max_attendees)
      .only_integer
      .is_greater_than(0)
      .is_less_than_or_equal_to(DiscourseEvents::Events::Event::MAX_ATTENDEES_LIMIT)
      .allow_nil
  end

  describe "#warm_livestream_onebox" do
    let(:livestream_url) { "https://www.youtube.com/live/abc123" }

    fab!(:topic) { Fabricate(:topic, category: nil) }
    fab!(:post) { Fabricate(:post, topic: topic) }

    before { Jobs.run_later! }

    it "enqueues onebox warming for a livestream URL" do
      expect_enqueued_with(
        job: :warm_livestream_onebox,
        args: {
          event_id: post.id,
          url: livestream_url,
        },
      ) { Fabricate(:event, post: post, livestream: true, location: livestream_url) }
    end

    it "skips onebox warming when the onebox is cached" do
      Discourse.cache.write(
        Oneboxer.onebox_cache_key(livestream_url),
        { onebox: "<aside>cached</aside>" },
      )

      expect_not_enqueued_with(job: :warm_livestream_onebox) do
        Fabricate(:event, post: post, livestream: true, location: livestream_url)
      end
    end

    it "skips onebox warming for events that are not livestreams" do
      expect_not_enqueued_with(job: :warm_livestream_onebox) do
        Fabricate(:event, post: post, livestream: false, location: "Room 5")
      end
    end

    it "skips onebox warming when the livestream has no URL" do
      expect_not_enqueued_with(job: :warm_livestream_onebox) do
        Fabricate(:event, post: post, livestream: true)
      end
    end

    it "warms the onebox when the URL changes" do
      event = Fabricate(:event, post: post, livestream: true, location: livestream_url)

      expect_enqueued_with(
        job: :warm_livestream_onebox,
        args: {
          event_id: event.id,
          url: "https://www.youtube.com/live/def456",
        },
      ) { event.update!(location: "https://www.youtube.com/live/def456") }
    end

    it "does not warm the onebox when an unrelated attribute changes" do
      event = Fabricate(:event, post: post, livestream: true, location: livestream_url)

      expect_not_enqueued_with(job: :warm_livestream_onebox) { event.update!(name: "Renamed") }
    end
  end

  describe "#warm_recording_onebox" do
    fab!(:topic) { Fabricate(:topic, category: nil) }
    fab!(:post) { Fabricate(:post, topic: topic) }

    before { Jobs.run_later! }

    it "enqueues onebox warming when a recording is added" do
      event = Fabricate(:event, post: post)

      expect_enqueued_with(
        job: :warm_recording_onebox,
        args: {
          event_id: event.id,
          url: "https://youtu.be/abc",
        },
      ) { event.update!(recording_url: "youtu.be/abc") }
    end

    it "skips onebox warming when the onebox is cached" do
      Discourse.cache.write(
        Oneboxer.onebox_cache_key("https://youtu.be/abc"),
        { onebox: "<aside>cached</aside>" },
      )

      expect_not_enqueued_with(job: :warm_recording_onebox) do
        Fabricate(:event, post: post, recording_url: "https://youtu.be/abc")
      end
    end

    it "does not warm the onebox when an unrelated attribute changes" do
      event = Fabricate(:event, post: post, recording_url: "https://youtu.be/abc")

      expect_not_enqueued_with(job: :warm_recording_onebox) { event.update!(name: "Renamed") }
    end
  end

  describe "#livestream_url" do
    fab!(:topic) { Fabricate(:topic, category: nil) }
    fab!(:post) { Fabricate(:post, topic: topic) }

    it "prefers the location over the url" do
      event =
        Fabricate(
          :event,
          post: post,
          location: "https://zoom.us/j/123456789",
          url: "https://example.com/fallback",
        )

      expect(event.livestream_url).to eq("https://zoom.us/j/123456789")
    end

    it "falls back to the url when there is no location" do
      event = Fabricate(:event, post: post, url: "https://example.com/fallback")

      expect(event.livestream_url).to eq("https://example.com/fallback")
    end

    it "is nil when neither is set" do
      event = Fabricate(:event, post: post)

      expect(event.livestream_url).to be_nil
    end
  end

  describe "#is_zoom_livestream?" do
    fab!(:topic) { Fabricate(:topic, category: nil) }
    fab!(:post) { Fabricate(:post, topic: topic) }

    before do
      SiteSetting.livestream_zoom_enabled = true
      # Creating a livestream event enqueues onebox warming, which would
      # otherwise run inline and try to fetch the URL.
      Jobs.run_later!
    end

    def event_for(location)
      Fabricate(:event, post: post, livestream: true, location: location)
    end

    it "recognises a Zoom URL on a vanity subdomain" do
      expect(event_for("https://us06web.zoom.us/j/123456789?pwd=secret")).to be_is_zoom_livestream
    end

    it "recognises a Zoom URL on the bare host" do
      expect(event_for("https://zoom.us/j/123456789")).to be_is_zoom_livestream
    end

    it "recognises a Zoom webinar URL" do
      expect(event_for("https://zoom.us/w/123456789")).to be_is_zoom_livestream
    end

    it "rejects a host that only ends in the Zoom domain" do
      expect(event_for("https://notzoom.us/j/123456789")).not_to be_is_zoom_livestream
    end

    it "rejects a Zoom URL with no joinable meeting number" do
      expect(event_for("https://zoom.us/about")).not_to be_is_zoom_livestream
    end

    it "rejects a non-Zoom livestream URL" do
      expect(event_for("https://www.youtube.com/live/abc123")).not_to be_is_zoom_livestream
    end

    it "is false when the event is not a livestream" do
      event = Fabricate(:event, post: post, livestream: false, url: "https://zoom.us/j/123456789")

      expect(event).not_to be_is_zoom_livestream
    end

    it "is false when Zoom is disabled" do
      SiteSetting.livestream_zoom_enabled = false

      expect(event_for("https://zoom.us/j/123456789")).not_to be_is_zoom_livestream
    end
  end

  describe "#reopen_livestream_chat_channel" do
    fab!(:category)
    fab!(:topic) { Fabricate(:topic, category:) }
    fab!(:post) { Fabricate(:post, topic:) }

    before do
      SiteSetting.chat_enabled = true
      Jobs.run_later!
    end

    let!(:event) do
      Fabricate(
        :event,
        post:,
        livestream: true,
        location: "https://www.youtube.com/live/abc123",
        original_starts_at: 2.days.ago,
        original_ends_at: 2.days.ago + 1.hour,
      )
    end
    let(:topic_chat_channel) { DiscourseEvents::Livestream::TopicChatChannel.find_by(topic:) }
    let(:chat_channel) { topic_chat_channel.chat_channel }

    before do
      chat_channel.closed!(Discourse.system_user)
      topic_chat_channel.update!(closed_on_event_end: true)
    end

    it "reopens the chat the event closed once it is moved into the future" do
      event.update!(original_starts_at: 1.day.from_now, original_ends_at: 1.day.from_now + 1.hour)

      expect(chat_channel.reload).to be_open
      expect(topic_chat_channel.reload.closed_on_event_end).to eq(false)
    end

    it "reopens the chat once the event is made recurring" do
      event.update!(recurrence: "every_week")

      expect(chat_channel.reload).to be_open
    end

    it "reopens the chat once a finished recurring series is extended" do
      event.update_columns(recurrence: "every_week", recurrence_until: 1.day.ago)

      event.update!(recurrence_until: 1.month.from_now)

      expect(chat_channel.reload).to be_open
    end

    it "keeps the chat closed when the series cutoff falls after its last occurrence" do
      event.update_columns(recurrence: "every_week", recurrence_until: 2.days.from_now)

      event.update!(name: "Renamed event")

      expect(chat_channel.reload).to be_closed
    end

    it "keeps the chat closed while the event is still over" do
      event.update!(name: "Renamed event")

      expect(chat_channel.reload).to be_closed
    end

    it "keeps a status moderators set since the event closed the chat" do
      chat_channel.open!(Discourse.system_user)
      chat_channel.closed!(Discourse.system_user)

      event.update!(original_starts_at: 1.day.from_now, original_ends_at: 1.day.from_now + 1.hour)

      expect(chat_channel.reload).to be_closed
    end
  end

  describe "#create_livestream_chat_channel" do
    fab!(:category)
    fab!(:topic) { Fabricate(:topic, category: category) }
    fab!(:post) { Fabricate(:post, topic: topic) }

    before do
      SiteSetting.chat_enabled = true
      Jobs.run_later!
    end

    it "creates the chat channel after the livestream event is committed" do
      expect {
        Fabricate(
          :event,
          post: post,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )
      }.to change(DiscourseEvents::Livestream::TopicChatChannel, :count).by(1)

      expect(post.topic.topic_chat_channel.chat_channel.chatable).to eq(category)
    end

    it "does not create a chat channel when chat is disabled" do
      SiteSetting.chat_enabled = false

      expect {
        Fabricate(
          :event,
          post: post,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )
      }.not_to change(DiscourseEvents::Livestream::TopicChatChannel, :count)
    end

    it "does not create a chat channel for a livestream event on a reply" do
      reply = Fabricate(:post, topic: topic)

      expect {
        Fabricate(
          :event,
          post: reply,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )
      }.not_to change(DiscourseEvents::Livestream::TopicChatChannel, :count)
    end
  end

  describe "#reset_invalid_livestream" do
    fab!(:post)

    # enqueue (don't run) the onebox-warming job so it doesn't make a real request
    before { Jobs.run_later! }

    it "keeps livestream enabled for an allowed host" do
      event =
        Fabricate(
          :event,
          post: post,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )

      expect(event.reload.livestream).to eq(true)
    end

    it "keeps livestream enabled for a host an admin added" do
      SiteSetting.livestream_allowed_hosts = "stream.example.com"

      event =
        Fabricate(:event, post: post, livestream: true, location: "https://stream.example.com/live")

      expect(event.reload.livestream).to eq(true)
    end

    it "resets livestream when the location is not an https URL" do
      event = Fabricate(:event, post: post, livestream: true, location: "Room 5")

      expect(event.reload.livestream).to eq(false)

      event.update!(location: "http://www.youtube.com/live/abc123")

      expect(event.reload.livestream).to eq(false)
    end

    it "falls back to the url, which is what the onebox and serializers use" do
      event =
        Fabricate(
          :event,
          post: post,
          livestream: true,
          location: nil,
          url: "https://www.youtube.com/live/abc123",
        )

      expect(event.reload.livestream).to eq(true)

      event.update!(url: "https://example.com/live")

      expect(event.reload.livestream).to eq(false)
    end

    it "resets livestream when the location host is not allowed" do
      event = Fabricate(:event, post: post, livestream: true, location: "https://example.com/live")

      expect(event.reload.livestream).to eq(false)
    end

    it "resets livestream when the location only resembles an allowed host" do
      event =
        Fabricate(:event, post: post, livestream: true, location: "https://youtube.com.evil.com/x")

      expect(event.reload.livestream).to eq(false)
    end

    it "resets livestream when an admin removes the host from the allowlist" do
      event =
        Fabricate(
          :event,
          post: post,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )
      SiteSetting.livestream_allowed_hosts = "zoom.us"

      event.update!(name: "Renamed")

      expect(event.reload.livestream).to eq(false)
    end

    it "resets livestream when there is no location" do
      event = Fabricate(:event, post: post, livestream: true)

      expect(event.reload.livestream).to eq(false)
    end

    it "resets livestream when the location is edited away from a URL" do
      event =
        Fabricate(
          :event,
          post: post,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )
      event.update!(location: "Room 5")

      expect(event.reload.livestream).to eq(false)
    end

    it "resets livestream when the event is not on the first post" do
      reply = Fabricate(:post, topic: post.topic)
      expect(reply.is_first_post?).to be(false)

      event =
        Fabricate(
          :event,
          post: reply,
          livestream: true,
          location: "https://www.youtube.com/live/abc123",
        )

      expect(event.reload.livestream).to eq(false)
    end
  end

  describe "#raw_invitees_are_groups" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:post) { Fabricate(:post, topic: topic) }

    it "is invalid when an invitee matches a username that is not a group" do
      Fabricate(:user, username: "not_a_group")
      event =
        Fabricate.build(
          :event,
          post: post,
          original_starts_at: Time.now,
          raw_invitees: ["not_a_group"],
        )

      expect(event).not_to be_valid
      expect(event.errors[:base]).to include(
        I18n.t("discourse_post_event.errors.models.event.raw_invitees.only_group"),
      )
    end

    it "is valid when an invitee is a group whose name collides with a username" do
      Fabricate(:user).update_columns(
        username: DiscourseEvents::Events::Event::PUBLIC_GROUP,
        username_lower: DiscourseEvents::Events::Event::PUBLIC_GROUP,
      )
      event =
        Fabricate.build(
          :event,
          post: post,
          original_starts_at: Time.now,
          raw_invitees: [DiscourseEvents::Events::Event::PUBLIC_GROUP],
        )

      expect(event).to be_valid
    end
  end

  describe "topic custom fields callback" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:notified_user, :user)
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:first_post) { Fabricate(:post, topic: topic) }
    let(:second_post) { Fabricate(:post, topic: topic) }
    let!(:starts_at) { Time.zone.parse("2020-04-24 14:15:00") }
    let!(:ends_at) { Time.zone.parse("2020-04-24 16:15:00") }
    let!(:alt_starts_at) { Time.zone.parse("2020-04-24 14:14:25") }
    let!(:alt_ends_at) { Time.zone.parse("2020-04-24 19:15:25") }
    let(:event) do
      DiscourseEvents::Events::Event.create!(
        id: first_post.id,
        original_starts_at: Time.now + 1.hour,
        original_ends_at: Time.now + 2.hours,
      )
    end
    let(:late_event) do
      DiscourseEvents::Events::Event.create!(
        id: first_post.id,
        original_starts_at: Time.now - 10.hours,
        original_ends_at: Time.now - 8.hours,
      )
    end
    let(:first_post_starts_at) do
      Time.zone.parse(
        first_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT],
      )
    end
    let(:first_post_ends_at) do
      Time.zone.parse(
        first_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT],
      )
    end

    describe "#after_commit[:create, :update]" do
      context "when a post event has been created" do
        context "when the associated post is the OP" do
          it "sets the topic custom field and creates event date" do
            expect(first_post.is_first_post?).to be(true)
            expect(first_post.topic.custom_fields).to be_blank

            expect {
              DiscourseEvents::Events::Event.create!(
                id: first_post.id,
                original_starts_at: starts_at,
                original_ends_at: ends_at,
              )
            }.to change { DiscourseEvents::Events::EventDate.count }
            first_post.topic.reload

            expect(first_post_starts_at).to eq_time(starts_at)
            expect(first_post_ends_at).to eq_time(ends_at)
            expect(DiscourseEvents::Events::EventDate.last.starts_at).to eq_time(starts_at)
            expect(DiscourseEvents::Events::EventDate.last.ends_at).to eq_time(ends_at)
          end
        end

        context "when the associated post is not the OP" do
          it "doesn’t set the topic custom field but still creates event date" do
            expect(second_post.is_first_post?).to be(false)
            expect(second_post.topic.custom_fields).to be_blank

            expect {
              DiscourseEvents::Events::Event.create!(
                id: second_post.id,
                original_starts_at: starts_at,
              )
            }.to change { DiscourseEvents::Events::EventDate.count }
            second_post.topic.reload

            expect(second_post.topic.custom_fields).to be_blank
          end
        end

        describe "notify an user" do
          describe "before the event starts" do
            it "does notify the user" do
              expect { event.create_notification!(notified_user, first_post) }.to change {
                Notification.count
              }.by(1)
            end
          end

          describe "after the event starts" do
            it "doesn't notify the user" do
              expect { late_event.create_notification!(notified_user, first_post) }.not_to change {
                Notification.count
              }
            end
          end

          describe "with private message topics" do
            let(:pm_owner) { Fabricate(:user) }
            let(:allowed_user) { Fabricate(:user) }
            let(:disallowed_user) { Fabricate(:user) }
            let(:pm_topic) do
              Fabricate(:private_message_topic, user: pm_owner, recipient: allowed_user)
            end
            let(:pm_post) { Fabricate(:post, topic: pm_topic, user: pm_owner) }
            let(:pm_event) { Fabricate(:event, post: pm_post) }

            it "does not send notifications to users without access to the PM" do
              expect(Guardian.new(disallowed_user).can_see?(pm_topic)).to be(false)
              expect { pm_event.create_notification!(disallowed_user, pm_post) }.not_to change {
                Notification.count
              }
            end

            it "does send notifications to users with access to the PM" do
              expect(Guardian.new(allowed_user).can_see?(pm_topic)).to be(true)
              expect { pm_event.create_notification!(allowed_user, pm_post) }.to change {
                Notification.count
              }.by(1)
            end
          end
        end
      end

      context "when a post event has been updated" do
        context "when the associated post is the OP" do
          let!(:post_event) do
            Fabricate(
              :event,
              post: first_post,
              original_starts_at: starts_at,
              original_ends_at: ends_at,
            )
          end

          it "sets the topic custom field" do
            first_post.topic.reload

            expect(first_post.is_first_post?).to be(true)
            expect(first_post_starts_at).to eq_time(starts_at)
            expect(first_post_ends_at).to eq_time(ends_at)

            first_event_date = post_event.event_dates.last
            expect(first_event_date.starts_at).to eq_time(starts_at)
            expect(first_event_date.finished_at).to be nil

            post_event.update_with_params!(
              original_starts_at: alt_starts_at,
              original_ends_at: alt_ends_at,
            )
            first_post.topic.reload
            first_event_date.reload

            second_event_date = post_event.event_dates.last

            expect(
              Time.zone.parse(
                first_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT],
              ),
            ).to eq(alt_starts_at)
            expect(
              Time.zone.parse(
                first_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT],
              ),
            ).to eq(alt_ends_at)

            expect(first_event_date.finished_at).not_to be nil
            expect(second_event_date.starts_at).to eq_time(alt_starts_at)

            second_event_date.update_columns(finished_at: Time.current)
            expect(post_event.starts_at).to eq_time(alt_starts_at)
            expect(post_event.ends_at).to eq_time(alt_ends_at)
          end

          it "preserves going invitees and resets other responses when the date changes" do
            going_user = Fabricate(:user)
            interested_user = Fabricate(:user)
            invitee_klass = DiscourseEvents::Events::Invitee

            post_event.create_invitees(
              [
                {
                  user_id: going_user.id,
                  status: invitee_klass.statuses[:going],
                  notified: true,
                  recurring: false,
                },
                {
                  user_id: interested_user.id,
                  status: invitee_klass.statuses[:interested],
                  notified: true,
                  recurring: false,
                },
              ],
            )

            post_event.update_with_params!(
              original_starts_at: alt_starts_at,
              original_ends_at: alt_ends_at,
            )

            going_invitee = post_event.invitees.find_by(user_id: going_user.id)
            interested_invitee = post_event.invitees.find_by(user_id: interested_user.id)

            expect(going_invitee).to have_attributes(
              status: invitee_klass.statuses[:going],
              notified: true,
              recurring: false,
            )
            expect(interested_invitee).to have_attributes(
              status: nil,
              notified: true,
              recurring: false,
            )
          end
        end

        context "when the associated post is not the OP" do
          let(:post_event) { Fabricate(:event, post: second_post, original_starts_at: starts_at) }

          it "doesn’t set the topic custom field" do
            expect(second_post.is_first_post?).to be(false)
            expect(
              second_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT],
            ).to be_blank

            post_event.update_with_params!(original_starts_at: alt_starts_at)
            second_post.topic.reload

            expect(
              second_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT],
            ).to be_blank

            second_event_date = post_event.event_dates.last
            expect(second_event_date.starts_at).to eq_time(alt_starts_at)
          end
        end
      end
    end

    describe "#after_commit[:destroy]" do
      context "when a post event has been destroyed" do
        context "when the associated post is the OP" do
          let!(:post_event) do
            Fabricate(
              :event,
              post: first_post,
              original_starts_at: starts_at,
              original_ends_at: ends_at,
            )
          end

          it "sets the topic custom field" do
            first_post.topic.reload

            expect(first_post.is_first_post?).to be(true)
            expect(first_post_starts_at).to eq_time(starts_at)
            expect(first_post_ends_at).to eq_time(ends_at)

            post_event.destroy!
            first_post.topic.reload

            expect(
              first_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT],
            ).to be_blank
            expect(
              first_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT],
            ).to be_blank
          end
        end

        context "when the associated post is not the OP" do
          let!(:first_post_event) do
            Fabricate(
              :event,
              post: first_post,
              original_starts_at: starts_at,
              original_ends_at: ends_at,
            )
          end
          let!(:second_post_event) do
            Fabricate(
              :event,
              post: second_post,
              original_starts_at: starts_at,
              original_ends_at: ends_at,
            )
          end

          it "doesn’t change the topic custom field" do
            second_post.topic.reload

            expect(first_post.is_first_post?).to be(true)
            expect(
              Time.zone.parse(
                second_post.topic.custom_fields[
                  DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT
                ],
              ),
            ).to eq(starts_at)
            expect(
              Time.zone.parse(
                second_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT],
              ),
            ).to eq(ends_at)
            expect(second_post.is_first_post?).to be(false)

            second_post_event.destroy!
            second_post.topic.reload

            expect(
              Time.zone.parse(
                second_post.topic.custom_fields[
                  DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT
                ],
              ),
            ).to eq(starts_at)
            expect(
              Time.zone.parse(
                second_post.topic.custom_fields[DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT],
              ),
            ).to eq(ends_at)
          end
        end
      end
    end
  end

  describe "#ongoing?" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:first_post) { Fabricate(:post, topic: topic) }

    context "with ends_at" do
      context "with starts_at < current date" do
        context "with ends_at < current date" do
          it "is ongoing" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: 2.hours.ago,
                original_ends_at: 1.hour.ago,
                post: first_post,
              )

            expect(post_event.ongoing?).to be(false)
          end
        end

        context "with ends_at > current date" do
          it "is not ongoing" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: 2.hours.ago,
                original_ends_at: 3.hours.from_now,
                post: first_post,
              )

            expect(post_event.ongoing?).to be(true)
          end
        end
      end

      context "when starts_at > current date" do
        context "when ends_at > current date" do
          it "is not ongoing" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: 1.hour.from_now,
                original_ends_at: 2.hours.from_now,
                post: first_post,
              )

            expect(post_event.ongoing?).to be(false)
          end
        end
      end
    end

    context "without ends_at date" do
      context "when starts_at < current date" do
        it "is ongoing" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: 2.hours.ago,
              post: first_post,
            )

          expect(post_event.ongoing?).to be(true)
        end
      end

      context "when starts_at == current date" do
        it "is ongoing" do
          post_event =
            DiscourseEvents::Events::Event.create!(original_starts_at: Time.now, post: first_post)

          expect(post_event.ongoing?).to be(true)
        end
      end

      context "when starts_at > current date" do
        it "is not ongoing" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: 1.hour.from_now,
              post: first_post,
            )

          expect(post_event.ongoing?).to be(false)
        end
      end
    end
  end

  describe "#livestream_chat_active?" do
    before { Jobs.run_later! }

    # Not `fab!`: the dates must be relative to the time frozen for this file.
    let!(:event) do
      Fabricate(
        :event,
        livestream: true,
        location: "https://www.youtube.com/live/abc123",
        original_starts_at: 1.hour.from_now,
        original_ends_at: 2.hours.from_now,
      )
    end

    it "is true for an upcoming livestream" do
      expect(event.livestream_chat_active?).to eq(true)
    end

    it "is false once the livestream has ended" do
      freeze_time 3.hours.from_now

      expect(event.livestream_chat_active?).to eq(false)
    end

    it "is false for events that aren't livestreams" do
      event.update_columns(livestream: false)

      expect(event.livestream_chat_active?).to eq(false)
    end

    it "is only true around each occurrence of a recurring livestream" do
      event.update_columns(recurrence: "every_week")

      expect(event.livestream_chat_active?).to eq(false)

      freeze_time 50.minutes.from_now
      expect(event.livestream_chat_active?).to eq(true)
    end
  end

  describe "#currently_within_event_timeframe?" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:first_post) { Fabricate(:post, topic: topic) }

    def event_with(starts_at:, ends_at: nil, all_day: false)
      DiscourseEvents::Events::Event.create!(
        original_starts_at: starts_at,
        original_ends_at: ends_at,
        all_day:,
        post: first_post,
      )
    end

    it "is false before the early access window opens" do
      expect(event_with(starts_at: 45.minutes.from_now).currently_within_event_timeframe?).to be(
        false,
      )
    end

    it "is true within the early access window" do
      expect(event_with(starts_at: 15.minutes.from_now).currently_within_event_timeframe?).to be(
        true,
      )
    end

    it "is true while the event is in progress" do
      expect(
        event_with(
          starts_at: 1.hour.ago,
          ends_at: 1.hour.from_now,
        ).currently_within_event_timeframe?,
      ).to be(true)
    end

    it "is true within the grace period after the end" do
      expect(
        event_with(
          starts_at: 2.hours.ago,
          ends_at: 5.minutes.ago,
        ).currently_within_event_timeframe?,
      ).to be(true)
    end

    it "is false once the grace period has passed" do
      expect(
        event_with(
          starts_at: 2.hours.ago,
          ends_at: 15.minutes.ago,
        ).currently_within_event_timeframe?,
      ).to be(false)
    end

    it "is true for a started event without an end time" do
      expect(event_with(starts_at: 1.day.ago).currently_within_event_timeframe?).to be(true)
    end

    context "for an all day event" do
      it "is true when now is between the start + end of the day of the event" do
        expect(
          event_with(
            starts_at: Time.zone.now.beginning_of_day,
            all_day: true,
          ).currently_within_event_timeframe?,
        ).to be(true)
      end

      it "is true on a later day of a multi-day event" do
        expect(
          event_with(
            starts_at: 2.days.ago,
            ends_at: 1.day.from_now,
            all_day: true,
          ).currently_within_event_timeframe?,
        ).to be(true)
      end

      it "is false after the final day of a multi-day event" do
        expect(
          event_with(
            starts_at: 5.days.ago,
            ends_at: 2.days.ago,
            all_day: true,
          ).currently_within_event_timeframe?,
        ).to be(false)
      end
    end
  end

  describe "#expired?" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:first_post) { Fabricate(:post, topic: topic) }

    context "with ends_at" do
      context "when starts_at < current date" do
        context "when ends_at < current date" do
          it "is expired" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: DateTime.parse("2020-04-22 14:05"),
                original_ends_at: DateTime.parse("2020-04-23 14:05"),
                post: first_post,
              )

            expect(post_event.expired?).to be(true)
          end
        end

        context "when ends_at > current date" do
          it "is not expired" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: DateTime.parse("2020-04-24 14:15"),
                original_ends_at: DateTime.parse("2020-04-25 11:05"),
                post: first_post,
              )

            expect(post_event.expired?).to be(false)
          end
        end
      end

      context "when starts_at > current date" do
        it "is not expired" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: DateTime.parse("2020-04-25 14:05"),
              original_ends_at: DateTime.parse("2020-04-26 14:05"),
              post: first_post,
            )

          expect(post_event.expired?).to be(false)
        end
      end
    end

    context "without ends_at date" do
      context "when starts_at < current date" do
        it "is expired" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: DateTime.parse("2020-04-24 14:05"),
              post: first_post,
            )

          expect(post_event.expired?).to be(false)
        end
      end

      context "when starts_at == current date" do
        it "is expired" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: DateTime.parse("2020-04-24 14:10"),
              post: first_post,
            )

          expect(post_event.expired?).to be(false)
        end
      end

      context "when starts_at > current date" do
        it "is not expired" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: DateTime.parse("2020-04-24 14:15"),
              post: first_post,
            )

          expect(post_event.expired?).to be(false)
        end
      end
    end

    context "with recurring events" do
      context "with recurrence_until set" do
        context "when current date is before recurrence_until" do
          it "is not expired" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: DateTime.parse("2020-04-22 14:05"),
                original_ends_at: DateTime.parse("2020-04-22 15:05"),
                recurrence: "FREQ=WEEKLY",
                recurrence_until: DateTime.parse("2020-05-01 00:00"),
                post: first_post,
              )

            expect(post_event.expired?).to be(false)
          end
        end

        context "when current date is after recurrence_until" do
          it "is expired" do
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: DateTime.parse("2020-04-22 14:05"),
                original_ends_at: DateTime.parse("2020-04-22 15:05"),
                recurrence: "FREQ=WEEKLY",
                recurrence_until: DateTime.parse("2020-04-23 00:00"),
                post: first_post,
              )

            expect(post_event.expired?).to be(true)
          end
        end

        context "when current date equals recurrence_until" do
          it "is not expired" do
            current_time = DateTime.parse("2020-04-24 14:10")
            post_event =
              DiscourseEvents::Events::Event.create!(
                original_starts_at: DateTime.parse("2020-04-22 14:05"),
                original_ends_at: DateTime.parse("2020-04-22 15:05"),
                recurrence: "FREQ=WEEKLY",
                recurrence_until: current_time,
                post: first_post,
              )

            expect(post_event.expired?).to be(false)
          end
        end
      end

      context "without recurrence_until set" do
        it "never expires" do
          post_event =
            DiscourseEvents::Events::Event.create!(
              original_starts_at: DateTime.parse("2020-04-22 14:05"),
              original_ends_at: DateTime.parse("2020-04-22 15:05"),
              recurrence: "FREQ=WEEKLY",
              recurrence_until: nil,
              post: first_post,
            )

          expect(post_event.expired?).to be(false)
        end
      end
    end
  end

  describe "#parsed_reminders" do
    fab!(:post_1, :post)

    def event_with_reminders(reminders)
      Fabricate(:event, post: post_1, reminders: reminders)
    end

    it "parses type, value and unit" do
      event = event_with_reminders("notification.15.minutes,bumpTopic.3.days")

      expect(event.parsed_reminders).to eq(
        [
          { type: "notification", value: 15, unit: "minutes" },
          { type: "bumpTopic", value: 3, unit: "days" },
        ],
      )
    end

    it "defaults the type to notification for legacy reminders without one" do
      event = event_with_reminders("15.minutes")

      expect(event.parsed_reminders).to eq([{ type: "notification", value: 15, unit: "minutes" }])
    end

    it "keeps negative values used for reminders after the event" do
      event = event_with_reminders("notification.-30.minutes")

      expect(event.parsed_reminders).to eq([{ type: "notification", value: -30, unit: "minutes" }])
    end

    it "strips padding and plus signs" do
      event = event_with_reminders("notification.15.minutes, bumpTopic.+3.days")

      expect(event.parsed_reminders).to eq(
        [
          { type: "notification", value: 15, unit: "minutes" },
          { type: "bumpTopic", value: 3, unit: "days" },
        ],
      )
    end

    it "skips reminders with an unknown unit" do
      event = event_with_reminders("notification.1.fortnights,notification.1.hours")

      expect(event.parsed_reminders).to eq([{ type: "notification", value: 1, unit: "hours" }])
    end

    it "is empty without reminders" do
      expect(event_with_reminders(nil).parsed_reminders).to eq([])
    end
  end

  describe ".valid_reminder?" do
    it "accepts known types, legacy type-less reminders, padding and signed values" do
      expect(described_class.valid_reminder?("notification.15.minutes")).to eq(true)
      expect(described_class.valid_reminder?("bumpTopic.1.days")).to eq(true)
      expect(described_class.valid_reminder?("15.minutes")).to eq(true)
      expect(described_class.valid_reminder?(" notification.+15.minutes ")).to eq(true)
      expect(described_class.valid_reminder?("notification.-30.minutes")).to eq(true)
    end

    it "rejects unknown units, types, non-numeric values and wrong arity" do
      expect(described_class.valid_reminder?("notification.1.fortnights")).to eq(false)
      expect(described_class.valid_reminder?("notification.abc.minutes")).to eq(false)
      expect(described_class.valid_reminder?("foo.1.minutes")).to eq(false)
      expect(described_class.valid_reminder?("15")).to eq(false)
      expect(described_class.valid_reminder?("a.notification.15.minutes")).to eq(false)
    end
  end

  describe "#duration" do
    fab!(:post_1, :post)

    context "when event has both starts_at and ends_at" do
      it "returns duration in HH:MM:SS format" do
        event =
          DiscourseEvents::Events::Event.create!(
            id: post_1.id,
            original_starts_at: "2022-01-15 10:00:00 UTC",
            original_ends_at: "2022-01-15 11:30:00 UTC",
          )

        expect(event.duration).to eq("01:30:00")
      end
    end

    context "when event only has starts_at" do
      it "returns default duration of 1 hour" do
        event =
          DiscourseEvents::Events::Event.create!(
            id: post_1.id,
            original_starts_at: "2022-01-15 10:00:00 UTC",
          )

        expect(event.duration).to eq("01:00:00")
      end
    end

    context "when event spans multiple days" do
      it "returns correct duration" do
        event =
          DiscourseEvents::Events::Event.create!(
            id: post_1.id,
            original_starts_at: "2022-01-15 10:00:00 UTC",
            original_ends_at: "2022-01-16 12:30:00 UTC",
          )

        expect(event.duration).to eq("26:30:00")
      end
    end
  end

  describe "#update_with_params!" do
    fab!(:post_1, :post)
    fab!(:user_1, :user)
    let(:group_1) do
      Fabricate(:group).tap do |g|
        g.add(user_1)
        g.save!
      end
    end

    before { freeze_time }

    context "when changing recurring event dates" do
      subject(:update_dates) do
        event.update_with_params!(
          original_starts_at: 2.days.ago,
          original_ends_at: 2.days.ago + 1.hour,
        )
      end

      fab!(:user, :user)

      let(:event) do
        Fabricate(
          :event,
          recurrence: "every_week",
          original_starts_at: 1.day.ago,
          original_ends_at: 1.day.ago + 1.hour,
        )
      end
      let(:status) { DiscourseEvents::Events::Invitee.statuses[:going] }
      let(:recurring) { true }
      let!(:invitee) { Fabricate(:invitee, event:, user:, status:, notified: true) }
      let(:reset_attendance) { { "status" => nil, "notified" => false, "recurring" => false } }

      before do
        # Preserve stored combinations that the invitee save callback would normalize.
        invitee.update_columns(recurring:)
      end

      context "with a going recurring invitee" do
        it "preserves attendance and notification state" do
          expect { update_dates }.not_to change {
            invitee.reload.attributes.slice("status", "notified", "recurring")
          }
        end
      end

      context "with a going invitee without recurring attendance" do
        let(:recurring) { false }

        it "resets attendance and notification state" do
          expect { update_dates }.to change {
            invitee.reload.attributes.slice("status", "notified", "recurring")
          }.to(reset_attendance)
        end
      end

      context "with a declined recurring invitee" do
        let(:status) { DiscourseEvents::Events::Invitee.statuses[:not_going] }

        it "resets attendance and notification state" do
          expect { update_dates }.to change {
            invitee.reload.attributes.slice("status", "notified", "recurring")
          }.to(reset_attendance)
        end
      end

      context "with a NULL status and recurring attendance" do
        let(:status) { nil }

        it "preserves attendance and notification state" do
          expect { update_dates }.not_to change {
            invitee.reload.attributes.slice("status", "notified", "recurring")
          }
        end
      end

      context "with a NULL status without recurring attendance" do
        let(:status) { nil }
        let(:recurring) { false }

        it "resets notification state" do
          expect { update_dates }.to change {
            invitee.reload.attributes.slice("status", "notified", "recurring")
          }.to(reset_attendance)
        end
      end
    end

    context "with a private event" do
      let!(:event_1) do
        Fabricate(
          :event,
          post: post_1,
          status: DiscourseEvents::Events::Event.statuses[:private],
          raw_invitees: [group_1.name],
        )
      end

      before do
        freeze_time

        event_1.create_invitees([{ user_id: user_1.id, status: 0 }])
      end

      context "when updating the name" do
        it "doesn’t clear existing invitees" do
          expect(event_1.invitees.count).to eq(1)

          expect { event_1.update_with_params!(name: "The event") }.not_to change {
            event_1.invitees.count
          }
        end
      end

      context "when an invitee is no longer allowed" do
        let(:group_2) { Fabricate(:group) }

        it "resets the pruned invitee's topic tracking" do
          event_1.invitees.find_by(user_id: user_1.id).update_attendance!(:going)
          expect(TopicUser.find_by(user: user_1, topic: post_1.topic).notification_level).to eq(
            TopicUser.notification_levels[:watching],
          )

          event_1.update_with_params!(raw_invitees: [group_2.name])

          expect(event_1.invitees.find_by(user_id: user_1.id)).to be_nil
          expect(TopicUser.find_by(user: user_1, topic: post_1.topic).notification_level).to eq(
            TopicUser.notification_levels[:regular],
          )
        end

        it "unfollows the pruned invitee from the livestream chat channel" do
          channel = Fabricate(:category_channel)
          Fabricate(:topic_chat_channel, topic: post_1.topic, chat_channel: channel)
          membership =
            Fabricate(
              :user_chat_channel_membership,
              user: user_1,
              chat_channel: channel,
              following: true,
            )

          event_1.update_with_params!(raw_invitees: [group_2.name])

          expect(event_1.invitees.find_by(user_id: user_1.id)).to be_nil
          expect(membership.reload.following).to eq(false)
        end
      end
    end
  end

  describe "resetting topic tracking when the event is destroyed" do
    fab!(:user_1, :user)
    fab!(:topic_1, :topic)
    fab!(:post_1) { Fabricate(:post, topic: topic_1) }
    fab!(:event_1) { Fabricate(:event, post: post_1) }

    before { event_1.create_invitees([{ user_id: user_1.id, status: 0 }]) }

    it "resets a watching invitee back to regular, leaving the topic intact" do
      event_1.invitees.find_by(user_id: user_1.id).update_attendance!(:going)
      expect(TopicUser.find_by(user: user_1, topic: topic_1).notification_level).to eq(
        TopicUser.notification_levels[:watching],
      )

      event_1.destroy!

      expect(Topic.exists?(topic_1.id)).to eq(true)
      expect(TopicUser.find_by(user: user_1, topic: topic_1).notification_level).to eq(
        TopicUser.notification_levels[:regular],
      )
    end
  end

  describe "#missing_users" do
    fab!(:post_1, :post)
    fab!(:user_1, :user)
    fab!(:user_2, :user)
    fab!(:user_3, :user)
    fab!(:group_1) do
      Fabricate(:group).tap do |g|
        g.add(user_1)
        g.add(user_2)
        g.add(user_3)
        g.save!
      end
    end
    fab!(:group_2) do
      Fabricate(:group).tap do |g|
        g.add(user_2)
        g.save!
      end
    end
    fab!(:event_1) do
      Fabricate(
        :event,
        post: post_1,
        status: DiscourseEvents::Events::Event.statuses[:private],
        raw_invitees: [group_1.name, group_2.name],
      )
    end

    before { DiscourseEvents::Events::Invitee.create_attendance!(user_3.id, post_1.id, :going) }

    it "doesn't return already attending user" do
      expect(event_1.missing_users.pluck(:id)).to_not include(user_3.id)
    end

    it "return users from groups with no duplicates" do
      expect(event_1.missing_users.pluck(:id)).to match_array([user_1.id, user_2.id])
    end

    context "with private event with empty raw_invitees" do
      let!(:event_without_invitees) do
        Fabricate(
          :event,
          post: Fabricate(:post),
          status: DiscourseEvents::Events::Event.statuses[:private],
          raw_invitees: [],
        )
      end

      it "does not return all site users" do
        expect(event_without_invitees.missing_users.count).to eq(0)
        expect(User.real.activated.not_silenced.not_suspended.not_staged.count).not_to eq(0)
      end
    end
  end

  describe "#calculate_next_date" do
    subject(:next_date) { event.calculate_next_date }

    context "when the event is recurring" do
      context "when the recurring ends on the next day" do
        let(:event) do
          Fabricate(
            :event,
            recurrence: "every_day",
            recurrence_until: "2020-04-25 23:59",
            original_starts_at: "2020-04-20 13:00",
          )
        end

        it "returns the next occurrence within the recurrence period" do
          expect(next_date).not_to be_blank
          expect(next_date).to be_an(Array)
          expect(next_date.length).to eq(2)
          expect(next_date[0]).to eq(Time.utc(2020, 4, 25, 13, 0, 0))
          expect(next_date[1]).to eq(Time.utc(2020, 4, 25, 14, 0, 0))
        end
      end
    end
  end

  describe "#starts_at and #ends_at for expired recurring events" do
    context "when recurring event has expired (past recurrence_until)" do
      let(:expired_recurring_event) do
        Fabricate(
          :event,
          recurrence: "every_week",
          recurrence_until: 1.day.ago,
          original_starts_at: 1.week.ago,
          original_ends_at: 1.week.ago + 2.hours,
        )
      end

      it "keeps the last known dates" do
        expect(expired_recurring_event.starts_at).to eq_time(
          expired_recurring_event.original_starts_at,
        )
        expect(expired_recurring_event.ends_at).to eq_time(expired_recurring_event.original_ends_at)
      end
    end

    context "when recurring event has no recurrence_until (endless)" do
      let(:endless_recurring_event) do
        Fabricate(
          :event,
          recurrence: "every_week",
          recurrence_until: nil,
          original_starts_at: 1.week.ago,
          original_ends_at: 1.week.ago + 2.hours,
        )
      end

      it "still returns starts_at from event_dates" do
        expect(endless_recurring_event.starts_at).not_to be_nil
      end

      it "still returns ends_at from event_dates" do
        expect(endless_recurring_event.starts_at).not_to be_nil
      end
    end

    context "when non-recurring event" do
      let(:non_recurring_event) do
        Fabricate(
          :event,
          recurrence: nil,
          original_starts_at: 1.week.ago,
          original_ends_at: 1.week.ago + 2.hours,
        )
      end

      it "still returns starts_at from event_dates regardless of when it was" do
        expect(non_recurring_event.starts_at).not_to be_nil
      end

      it "still returns ends_at from event_dates regardless of when it was" do
        expect(non_recurring_event.ends_at).not_to be_nil
      end
    end
  end

  describe "syncing from raw" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:post) { Fabricate(:post, topic: topic, user: user) }
    fab!(:upload)

    it "sets livestream from the bbcode attribute" do
      Jobs.run_later!

      post.update!(
        raw:
          "[event start=\"2020-04-24 14:15\" livestream=\"true\" location=\"https://www.youtube.com/live/abc123\"]\n[/event]",
      )
      post.rebake!
      DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
      post.reload

      expect(post.event.livestream).to eq(true)
    end

    it "defaults livestream to false when the attribute is absent" do
      post.update!(raw: "[event start=\"2020-04-24 14:15\"]\n[/event]")
      post.rebake!
      DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
      post.reload

      expect(post.event.livestream).to eq(false)
    end

    it "ignores livestream=true when the location is not an http(s) URL" do
      post.update!(
        raw: "[event start=\"2020-04-24 14:15\" livestream=\"true\" location=\"Room 5\"]\n[/event]",
      )
      post.rebake!
      DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
      post.reload

      expect(post.event.livestream).to eq(false)
    end

    context "with image" do
      before do
        post.update!(
          raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",
        )
        post.rebake!
        DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
        post.reload
      end

      it "resolves image to image_upload_id and creates UploadReference" do
        expect(post.event.image_upload_id).to eq(upload.id)
        expect(UploadReference.exists?(upload_id: upload.id, target: post.event)).to eq(true)
      end

      it "clears image_upload_id when image is removed" do
        post.update!(raw: "[event start=\"2020-04-24 14:15\"]\n[/event]")
        post.rebake!
        DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
        post.reload

        expect(post.event.image_upload_id).to be_nil
        expect(UploadReference.exists?(upload_id: upload.id, target: post.event)).to eq(false)
      end
    end

    describe "post.image_upload_id sync on post_process_cooked" do
      fab!(:topic) { Fabricate(:topic, user: user) }

      it "sets post.image_upload_id from the event image after post processing" do
        post =
          Fabricate(
            :post,
            topic: topic,
            user: user,
            raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",
          )
        post.rebake!
        DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
        post.reload
        CookedPostProcessor.new(post).post_process

        post.reload
        expect(post.image_upload_id).to eq(upload.id)
      end

      it "sets topic.image_upload_id when the post is the first post" do
        post = topic.first_post || Fabricate(:post, topic: topic, user: user, post_number: 1)
        post.update!(
          raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",
        )
        post.rebake!
        DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
        post.reload
        CookedPostProcessor.new(post).post_process

        post.reload
        topic.reload
        expect(topic.image_upload_id).to eq(upload.id)
      end

      it "does not override post.image_upload_id when event has no image" do
        other_upload = Fabricate(:upload)
        post =
          Fabricate(
            :post,
            topic: topic,
            user: user,
            raw: "[event start=\"2020-04-24 14:15\"]\n[/event]\n![image](#{other_upload.url})",
          )
        post.rebake!
        DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
        post.reload
        CookedPostProcessor.new(post).post_process

        post.reload
        expect(post.image_upload_id).not_to eq(nil)
        expect(post.event.image_upload_id).to be_nil
      end
    end

    context "with an image from a different secure post" do
      fab!(:private_upload_owner, :user)
      fab!(:private_upload_recipient, :user)
      fab!(:private_upload_post) do
        Fabricate(
          :private_message_post,
          user: private_upload_owner,
          recipient: private_upload_recipient,
        )
      end
      fab!(:private_upload) do
        Fabricate(
          :secure_upload,
          user: private_upload_owner,
          access_control_post: private_upload_post,
        )
      end

      before do
        setup_s3
        SiteSetting.secure_uploads = true

        post.update!(
          raw: "[event start=\"2020-04-24 14:15\" image=\"#{private_upload.url}\"]\n[/event]",
        )
        post.rebake!
      end

      it "does not associate the upload" do
        DiscourseEvents::Events::Event::SyncFromPost.call(params: { post_id: post.id })
        post.reload

        expect(post.event.image_upload_id).to be_nil
        expect(UploadReference.exists?(upload_id: private_upload.id, target: post.event)).to eq(
          false,
        )
      end
    end
  end

  describe "post/topic image_upload_id sync on post_edited" do
    fab!(:user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
    fab!(:topic) { Fabricate(:topic, user: user) }
    fab!(:post) { Fabricate(:post, topic: topic, user: user, post_number: 1) }
    fab!(:upload)

    it "syncs event image to post and topic when image is added" do
      PostRevisor.new(post).revise!(
        user,
        raw: "[event start=\"2020-04-24 14:15\" image=\"#{upload.short_url}\"]\n[/event]",
      )
      post.reload
      topic.reload

      expect(post.image_upload_id).to eq(upload.id)
      expect(topic.image_upload_id).to eq(upload.id)
    end

    it "does not clear post image_upload_id when event has no image but post has inline images" do
      other_upload = Fabricate(:upload)
      PostRevisor.new(post).revise!(
        user,
        raw: "[event start=\"2020-04-24 14:15\"]\n[/event]\n![image](#{other_upload.short_url})",
      )
      CookedPostProcessor.new(post).post_process
      post.reload

      PostRevisor.new(post).revise!(
        user,
        raw:
          "[event start=\"2020-04-24 14:15\"]\n[/event]\n![image](#{other_upload.short_url})\nupdated text",
      )
      post.reload

      expect(post.image_upload_id).to eq(other_upload.id)
    end
  end
end

describe DiscourseEvents::Events::Event, "#most_likely_going" do
  before do
    Jobs.run_immediately!
    SiteSetting.discourse_events_enabled = true
    SiteSetting.discourse_post_event_enabled = true
  end

  fab!(:event)

  it "orders going invitees by RSVP time (created_at), not by user id" do
    early_rsvp = Fabricate(:user)
    late_rsvp = Fabricate(:user)
    # later-created user (higher id) RSVP'd first, so should come first
    expect(late_rsvp.id).to be > early_rsvp.id

    Fabricate(
      :post_event_invitee,
      event:,
      user: late_rsvp,
      status: DiscourseEvents::Events::Invitee.statuses[:going],
      created_at: 2.hours.ago,
    )
    Fabricate(
      :post_event_invitee,
      event:,
      user: early_rsvp,
      status: DiscourseEvents::Events::Invitee.statuses[:going],
      created_at: 1.hour.ago,
    )

    expect(event.most_likely_going.map(&:user)).to eq([late_rsvp, early_rsvp])
  end

  it "groups going invitees ahead of interested ones regardless of RSVP time" do
    interested = Fabricate(:user)
    going = Fabricate(:user)

    Fabricate(
      :post_event_invitee,
      event:,
      user: interested,
      status: DiscourseEvents::Events::Invitee.statuses[:interested],
      created_at: 2.hours.ago,
    )
    Fabricate(
      :post_event_invitee,
      event:,
      user: going,
      status: DiscourseEvents::Events::Invitee.statuses[:going],
      created_at: 1.hour.ago,
    )

    expect(event.most_likely_going.map(&:user)).to eq([going, interested])
  end
end

describe DiscourseEvents::Events::Event, "#capacity" do
  before do
    Jobs.run_immediately!
    SiteSetting.discourse_events_enabled = true
    SiteSetting.discourse_post_event_enabled = true
  end

  it "detects capacity when max_attendees set" do
    creator = Fabricate(:user)
    topic = Fabricate(:topic, user: creator)
    post = Fabricate(:post, user: creator, topic: topic)
    event = Fabricate(:event, post: post, max_attendees: 1)
    event.create_invitees(
      [{ user_id: creator.id, status: DiscourseEvents::Events::Invitee.statuses[:going] }],
    )
    expect(event.at_capacity?).to eq(true)
  end
end

describe DiscourseEvents::Events::Event, "#url" do
  fab!(:post)

  before { SiteSetting.discourse_post_event_enabled = true }

  it "accepts a URI, with or without a scheme" do
    event = Fabricate.build(:event, post: post, url: "meet.google.com/phv-rbyy-gsh")

    expect(event).to be_valid
  end

  it "rejects a value that would resolve nowhere" do
    event = Fabricate.build(:event, post: post, url: "Room 5")

    expect(event).not_to be_valid
    expect(event.errors.full_messages.join).to include("valid web address")
  end

  it "leaves an existing bad url editable" do
    event = Fabricate(:event, post: post)
    event.update_column(:url, "Room 5")

    event.reload.name = "Renamed"
    expect(event).to be_valid

    event.url = nil
    expect(event).to be_valid
  end
end