|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +describe ::DiscourseTranslator::TranslatorSelectionValidator do |
| 6 | + subject { described_class.new } |
| 7 | + |
| 8 | + fab!(:llm_model) { Fabricate(:llm_model) } |
| 9 | + |
| 10 | + describe "#valid_value?" do |
| 11 | + context "when value is blank" do |
| 12 | + it "returns true" do |
| 13 | + expect(subject.valid_value?(nil)).to eq(true) |
| 14 | + expect(subject.valid_value?("")).to eq(true) |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + context "when value is 'DiscourseAi'" do |
| 19 | + context "when DiscourseAutomation is not defined" do |
| 20 | + it "returns false" do |
| 21 | + hide_const("DiscourseAutomation") |
| 22 | + expect(subject.valid_value?("DiscourseAi")).to eq(false) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + context "when DiscourseAutomation is defined but ai_helper_enabled is false" do |
| 27 | + it "returns false" do |
| 28 | + SiteSetting.ai_helper_enabled = false |
| 29 | + expect(subject.valid_value?("DiscourseAi")).to eq(false) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context "when DiscourseAutomation is defined and ai_helper_enabled is true" do |
| 34 | + it "returns true" do |
| 35 | + DiscourseAi::Completions::Llm.with_prepared_responses(["OK"]) do |
| 36 | + SiteSetting.ai_helper_model = "custom:#{llm_model.id}" |
| 37 | + SiteSetting.ai_helper_enabled = true |
| 38 | + end |
| 39 | + expect(subject.valid_value?("DiscourseAi")).to eq(true) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context "when value is not 'DiscourseAi'" do |
| 45 | + it "returns true" do |
| 46 | + expect(subject.valid_value?("googly")).to eq(true) |
| 47 | + expect(subject.valid_value?("poopy")).to eq(true) |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + describe "#error_message" do |
| 53 | + context "when DiscourseAutomation is not defined" do |
| 54 | + it "returns the not_installed error message" do |
| 55 | + hide_const("DiscourseAutomation") |
| 56 | + expect(subject.error_message).to eq(I18n.t("translator.discourse_ai.not_installed")) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context "when DiscourseAutomation is defined but ai_helper_enabled is false" do |
| 61 | + it "returns the ai_helper_required error message" do |
| 62 | + SiteSetting.ai_helper_enabled = false |
| 63 | + expect(subject.error_message).to eq(I18n.t("translator.discourse_ai.ai_helper_required")) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context "when DiscourseAutomation is defined and ai_helper_enabled is true" do |
| 68 | + it "returns nil" do |
| 69 | + DiscourseAi::Completions::Llm.with_prepared_responses(["OK"]) do |
| 70 | + SiteSetting.ai_helper_model = "custom:#{llm_model.id}" |
| 71 | + SiteSetting.ai_helper_enabled = true |
| 72 | + end |
| 73 | + expect(subject.error_message).to be_nil |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments