discourse/plugins/chat/spec/lib/service_runner_spec.rb
Joffrey JAFFEUX 12a18d4d55
DEV: properly namespace chat (#20690)
This commit main goal was to comply with Zeitwerk and properly rely on autoloading. To achieve this, most resources have been namespaced under the `Chat` module.

- Given all models are now namespaced with `Chat::` and would change the stored types in DB when using polymorphism or STI (single table inheritance), this commit uses various Rails methods to ensure proper class is loaded and the stored name in DB is unchanged, eg: `Chat::Message` model will be stored as `"ChatMessage"`, and `"ChatMessage"` will correctly load `Chat::Message` model.
- Jobs are now using constants only, eg: `Jobs::Chat::Foo` and should only be enqueued this way

Notes:
- This commit also used this opportunity to limit the number of registered css files in plugin.rb
- `discourse_dev` support has been removed within this commit and will be reintroduced later

<!-- NOTE: All pull requests should have tests (rspec in Ruby, qunit in JavaScript). If your code does not include test coverage, please include an explanation of why it was omitted. -->
2023-03-17 14:24:38 +01:00

301 lines
6.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ServiceRunner do
class SuccessService
include Service::Base
end
class FailureService
include Service::Base
step :fail_step
def fail_step
fail!("error")
end
end
class FailedPolicyService
include Service::Base
policy :test
def test
false
end
end
class SuccessPolicyService
include Service::Base
policy :test
def test
true
end
end
class FailedContractService
include Service::Base
class Contract
attribute :test
validates :test, presence: true
end
contract
end
class SuccessContractService
include Service::Base
contract
end
class FailureWithModelService
include Service::Base
model :fake_model, :fetch_fake_model
private
def fetch_fake_model
nil
end
end
class SuccessWithModelService
include Service::Base
model :fake_model, :fetch_fake_model
private
def fetch_fake_model
:model_found
end
end
class FailureWithCollectionModelService
include Service::Base
model :fake_model, :fetch_fake_model
private
def fetch_fake_model
[]
end
end
class SuccessWithCollectionModelService
include Service::Base
model :fake_model, :fetch_fake_model
private
def fetch_fake_model
[:models_found]
end
end
describe ".call(service, &block)" do
subject(:runner) { described_class.call(service, object, &actions_block) }
let(:result) { object.result }
let(:actions_block) { object.instance_eval(actions) }
let(:service) { SuccessService }
let(:actions) { "proc {}" }
let(:object) do
Class
.new(Chat::ApiController) do
def request
OpenStruct.new
end
def params
ActionController::Parameters.new
end
def guardian
end
end
.new
end
it "runs the provided service in the context of a controller" do
runner
expect(result).to be_a Service::Base::Context
expect(result).to be_a_success
end
context "when using the on_success action" do
let(:actions) { <<-BLOCK }
proc do
on_success { :success }
end
BLOCK
context "when the service succeeds" do
it "runs the provided block" do
expect(runner).to eq :success
end
end
context "when the service does not succeed" do
let(:service) { FailureService }
it "does not run the provided block" do
expect(runner).not_to eq :success
end
end
end
context "when using the on_failure action" do
let(:actions) { <<-BLOCK }
proc do
on_failure { :fail }
end
BLOCK
context "when the service fails" do
let(:service) { FailureService }
it "runs the provided block" do
expect(runner).to eq :fail
end
end
context "when the service does not fail" do
let(:service) { SuccessService }
it "does not run the provided block" do
expect(runner).not_to eq :fail
end
end
end
context "when using the on_failed_policy action" do
let(:actions) { <<-BLOCK }
proc do
on_failed_policy(:test) { :policy_failure }
end
BLOCK
context "when the service policy fails" do
let(:service) { FailedPolicyService }
it "runs the provided block" do
expect(runner).to eq :policy_failure
end
end
context "when the service policy does not fail" do
let(:service) { SuccessPolicyService }
it "does not run the provided block" do
expect(runner).not_to eq :policy_failure
end
end
end
context "when using the on_failed_contract action" do
let(:actions) { <<-BLOCK }
proc do
on_failed_contract { :contract_failure }
end
BLOCK
context "when the service contract fails" do
let(:service) { FailedContractService }
it "runs the provided block" do
expect(runner).to eq :contract_failure
end
end
context "when the service contract does not fail" do
let(:service) { SuccessContractService }
it "does not run the provided block" do
expect(runner).not_to eq :contract_failure
end
end
end
context "when using the on_model_not_found action" do
let(:actions) { <<-BLOCK }
->(*) do
on_model_not_found(:fake_model) { :no_model }
end
BLOCK
context "when fetching a single model" do
context "when the service fails without a model" do
let(:service) { FailureWithModelService }
it "runs the provided block" do
expect(runner).to eq :no_model
end
end
context "when the service does not fail with a model" do
let(:service) { SuccessWithModelService }
it "does not run the provided block" do
expect(runner).not_to eq :no_model
end
end
end
context "when fetching a collection" do
context "when the service fails without a model" do
let(:service) { FailureWithCollectionModelService }
it "runs the provided block" do
expect(runner).to eq :no_model
end
end
context "when the service does not fail with a model" do
let(:service) { SuccessWithCollectionModelService }
it "does not run the provided block" do
expect(runner).not_to eq :no_model
end
end
end
end
context "when using several actions together" do
let(:service) { FailureService }
let(:actions) { <<-BLOCK }
proc do
on_success { :success }
on_failure { :failure }
on_failed_policy { :policy_failure }
end
BLOCK
it "runs the first matching action" do
expect(runner).to eq :failure
end
end
context "when running in the context of a job" do
let(:object) { Class.new(ServiceJob).new }
let(:actions) { <<-BLOCK }
proc do
on_success { :success }
on_failure { :failure }
end
BLOCK
it "runs properly" do
expect(runner).to eq :success
end
end
end
end