discourse-ai/spec/lib/modules/nsfw/entry_point_spec.rb
Sam 6ddc17fd61
DEV: port directory structure to Zeitwerk (#319)
Previous to this change we relied on explicit loading for a files in Discourse AI.

This had a few downsides:

- Busywork whenever you add a file (an extra require relative)
- We were not keeping to conventions internally ... some places were OpenAI others are OpenAi
- Autoloader did not work which lead to lots of full application broken reloads when developing.

This moves all of DiscourseAI into a Zeitwerk compatible structure.

It also leaves some minimal amount of manual loading (automation - which is loading into an existing namespace that may or may not be there)

To avoid needing /lib/discourse_ai/... we mount a namespace thus we are able to keep /lib pointed at ::DiscourseAi

Various files were renamed to get around zeitwerk rules and minimize usage of custom inflections

Though we can get custom inflections to work it is not worth it, will require a Discourse core patch which means we create a hard dependency.
2023-11-29 15:17:46 +11:00

66 lines
2.0 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
describe DiscourseAi::Nsfw::EntryPoint do
fab!(:user) { Fabricate(:user) }
describe "registering event callbacks" do
fab!(:image_upload) { Fabricate(:upload) }
let(:raw_with_upload) { "A public post with an image.\n![](#{image_upload.short_path})" }
before { SiteSetting.ai_nsfw_detection_enabled = true }
context "when creating a post" do
let(:creator) do
PostCreator.new(user, raw: raw_with_upload, title: "this is my new topic title")
end
it "queues a job on create if sentiment analysis is enabled" do
expect { creator.create }.to change(Jobs::EvaluatePostUploads.jobs, :size).by(1)
end
it "does nothing if sentiment analysis is disabled" do
SiteSetting.ai_nsfw_detection_enabled = false
expect { creator.create }.not_to change(Jobs::EvaluatePostUploads.jobs, :size)
end
it "does nothing if the post has no uploads" do
creator_2 =
PostCreator.new(user, raw: "this is a test", title: "this is my new topic title")
expect { creator_2.create }.not_to change(Jobs::EvaluatePostUploads.jobs, :size)
end
end
context "when editing a post" do
fab!(:post) { Fabricate(:post, user: user) }
let(:revisor) { PostRevisor.new(post) }
it "queues a job on update if sentiment analysis is enabled" do
expect { revisor.revise!(user, raw: raw_with_upload) }.to change(
Jobs::EvaluatePostUploads.jobs,
:size,
).by(1)
end
it "does nothing if sentiment analysis is disabled" do
SiteSetting.ai_nsfw_detection_enabled = false
expect { revisor.revise!(user, raw: raw_with_upload) }.not_to change(
Jobs::EvaluatePostUploads.jobs,
:size,
)
end
it "does nothing if the new raw has no uploads" do
expect { revisor.revise!(user, raw: "this is a test") }.not_to change(
Jobs::EvaluatePostUploads.jobs,
:size,
)
end
end
end
end