Skip to content

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

3.7 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseGamification::Solutions do
  fab!(:category)
  fab!(:topic) { Fabricate(:topic_with_op, category: category) }
  fab!(:question_user, :user)
  fab!(:answer_user, :user)
  fab!(:answer_post) { Fabricate(:post, topic: topic, user: answer_user) }

  before do
    SiteSetting.allow_solved_on_all_topics = true
    SiteSetting.solution_score_value = 5
  end

  it "is enabled when score value is positive" do
    expect(described_class).to be_enabled

    SiteSetting.solution_score_value = 0
    expect(described_class).not_to be_enabled
  end

  describe "scoring query" do
    def query_results
      DB.query(described_class.query, since: 2.days.ago)
    end

    it "scores accepted answers correctly" do
      freeze_time DateTime.parse("2024-01-01 12:00")

      DiscourseSolved::AcceptAnswer.call!(
        params: {
          post_id: answer_post.id,
        },
        guardian: Discourse.system_user.guardian,
      )

      expect(query_results).to contain_exactly(
        have_attributes(user_id: answer_user.id, date: Time.current.beginning_of_day, points: 5.0),
      )

      DiscourseSolved::UnacceptAnswer.call!(
        params: {
          post_id: answer_post.id,
        },
        guardian: Discourse.system_user.guardian,
      )
      expect(query_results).to be_empty
    end

    it "doesn't score self-accepted answers" do
      topic.update!(user: answer_user)
      DiscourseSolved::AcceptAnswer.call!(
        params: {
          post_id: answer_post.id,
        },
        guardian: Discourse.system_user.guardian,
      )

      expect(query_results).to be_empty
    end

    context "with multiple solutions enabled" do
      before { SiteSetting.solved_allow_multiple_solutions = true }

      it "scores additional solutions when querying with :since after first solution" do
        freeze_time DateTime.parse("2024-01-01 12:00")

        DiscourseSolved::AcceptAnswer.call!(
          params: {
            post_id: answer_post.id,
          },
          guardian: Discourse.system_user.guardian,
        )

        freeze_time DateTime.parse("2024-01-03 12:00")
        answer_post_2 = Fabricate(:post, topic: topic, user: answer_user)
        DiscourseSolved::AcceptAnswer.call!(
          params: {
            post_id: answer_post_2.id,
          },
          guardian: Discourse.system_user.guardian,
        )

        expect(DB.query(described_class.query, since: 1.day.ago)).to contain_exactly(
          have_attributes(
            user_id: answer_user.id,
            date: Date.parse("2024-01-03").beginning_of_day,
            points: 5.0,
          ),
        )
      end

      it "scores multiple accepted solutions by the same user" do
        freeze_time DateTime.parse("2024-01-01 12:00")

        answer_post_2 = Fabricate(:post, topic: topic, user: answer_user)

        DiscourseSolved::AcceptAnswer.call!(
          params: {
            post_id: answer_post.id,
          },
          guardian: Discourse.system_user.guardian,
        )
        DiscourseSolved::AcceptAnswer.call!(
          params: {
            post_id: answer_post_2.id,
          },
          guardian: Discourse.system_user.guardian,
        )

        expect(query_results).to contain_exactly(
          have_attributes(
            user_id: answer_user.id,
            date: Time.current.beginning_of_day,
            points: 10.0,
          ),
        )
      end
    end
  end

  it "is disabled when solved plugin is disabled" do
    SiteSetting.solved_enabled = false
    expect(described_class).not_to be_enabled

    SiteSetting.solved_enabled = true
    SiteSetting.solution_score_value = 0
    expect(described_class).not_to be_enabled

    SiteSetting.solution_score_value = 1
    expect(described_class).to be_enabled
  end
end