Skip to content

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

2.0 KB
Raw Blame
# frozen_string_literal: true

describe "AppendLastEditedBy" do
  fab!(:post) { Fabricate(:post, raw: "this is a post with no edit") }
  fab!(:moderator)

  fab!(:automation) do
    Fabricate(:automation, script: DiscourseAutomation::Scripts::APPEND_LAST_EDITED_BY)
  end

  describe "#trigger!" do
    it "works for newly created post" do
      freeze_time

      cooked = automation.trigger!("post" => post, "cooked" => post.cooked)
      updated_at = post.updated_at
      expect(
        cooked.include?(
          PrettyText.cook(
            I18n.t(
              "discourse_automation.scriptables.append_last_edited_by.text",
              username: post.user.username,
              date_time:
                "[date=#{updated_at.to_date} time=#{updated_at.strftime("%H:%M:%S")} timezone=UTC]",
            ),
          ),
        ),
      ).to be_truthy
    end

    it "works for existing post with last edited by detail" do
      freeze_time

      cooked = automation.trigger!("post" => post, "cooked" => post.cooked)
      updated_at = post.updated_at
      expect(
        cooked.include?(
          PrettyText.cook(
            I18n.t(
              "discourse_automation.scriptables.append_last_edited_by.text",
              username: post.user.username,
              date_time:
                "[date=#{updated_at.to_date} time=#{updated_at.strftime("%H:%M:%S")} timezone=UTC]",
            ),
          ),
        ),
      ).to be_truthy

      PostRevisor.new(post).revise!(moderator, raw: "this is a post with edit")

      post.reload
      cooked = automation.trigger!("post" => post, "cooked" => post.cooked)
      updated_at = post.updated_at
      expect(
        cooked.include?(
          PrettyText.cook(
            I18n.t(
              "discourse_automation.scriptables.append_last_edited_by.text",
              username: moderator.username,
              date_time:
                "[date=#{updated_at.to_date} time=#{updated_at.strftime("%H:%M:%S")} timezone=UTC]",
            ),
          ),
        ),
      ).to be_truthy
    end
  end
end