|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +RSpec.describe DiscourseAi::Configuration::Feature do |
| 6 | + fab!(:llm_model) |
| 7 | + fab!(:ai_persona) { Fabricate(:ai_persona, default_llm_id: llm_model.id) } |
| 8 | + |
| 9 | + def allow_configuring_setting(&block) |
| 10 | + DiscourseAi::Completions::Llm.with_prepared_responses(["OK"]) { block.call } |
| 11 | + end |
| 12 | + |
| 13 | + describe "#llm_model" do |
| 14 | + context "when persona is not found" do |
| 15 | + it "returns nil when persona_id is invalid" do |
| 16 | + ai_feature = |
| 17 | + described_class.new( |
| 18 | + "topic_summaries", |
| 19 | + "ai_summarization_persona", |
| 20 | + DiscourseAi::Configuration::Module::SUMMARIZATION_ID, |
| 21 | + DiscourseAi::Configuration::Module::SUMMARIZATION, |
| 22 | + ) |
| 23 | + |
| 24 | + SiteSetting.ai_summarization_persona = 999_999 |
| 25 | + expect(ai_feature.llm_model).to be_nil |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + context "with summarization module" do |
| 30 | + let(:ai_feature) do |
| 31 | + described_class.new( |
| 32 | + "topic_summaries", |
| 33 | + "ai_summarization_persona", |
| 34 | + DiscourseAi::Configuration::Module::SUMMARIZATION_ID, |
| 35 | + DiscourseAi::Configuration::Module::SUMMARIZATION, |
| 36 | + ) |
| 37 | + end |
| 38 | + |
| 39 | + it "returns the configured llm model" do |
| 40 | + SiteSetting.ai_summarization_persona = ai_persona.id |
| 41 | + allow_configuring_setting { SiteSetting.ai_summarization_model = "custom:#{llm_model.id}" } |
| 42 | + expect(ai_feature.llm_model).to eq(llm_model) |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + context "with AI helper module" do |
| 47 | + let(:ai_feature) do |
| 48 | + described_class.new( |
| 49 | + "proofread", |
| 50 | + "ai_helper_proofreader_persona", |
| 51 | + DiscourseAi::Configuration::Module::AI_HELPER_ID, |
| 52 | + DiscourseAi::Configuration::Module::AI_HELPER, |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + it "returns the persona's default llm when no specific helper model is set" do |
| 57 | + SiteSetting.ai_helper_proofreader_persona = ai_persona.id |
| 58 | + SiteSetting.ai_helper_model = "" |
| 59 | + |
| 60 | + expect(ai_feature.llm_model).to eq(llm_model) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context "with translation module" do |
| 65 | + fab!(:translation_model) { Fabricate(:llm_model) } |
| 66 | + |
| 67 | + let(:ai_feature) do |
| 68 | + described_class.new( |
| 69 | + "locale_detector", |
| 70 | + "ai_translation_locale_detector_persona", |
| 71 | + DiscourseAi::Configuration::Module::TRANSLATION_ID, |
| 72 | + DiscourseAi::Configuration::Module::TRANSLATION, |
| 73 | + ) |
| 74 | + end |
| 75 | + |
| 76 | + it "uses translation model when configured" do |
| 77 | + SiteSetting.ai_translation_locale_detector_persona = ai_persona.id |
| 78 | + ai_persona.update!(default_llm_id: nil) |
| 79 | + allow_configuring_setting do |
| 80 | + SiteSetting.ai_translation_model = "custom:#{translation_model.id}" |
| 81 | + end |
| 82 | + |
| 83 | + expect(ai_feature.llm_model).to eq(translation_model) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "#enabled?" do |
| 89 | + it "returns true when no enabled_by_setting is specified" do |
| 90 | + ai_feature = |
| 91 | + described_class.new( |
| 92 | + "topic_summaries", |
| 93 | + "ai_summarization_persona", |
| 94 | + DiscourseAi::Configuration::Module::SUMMARIZATION_ID, |
| 95 | + DiscourseAi::Configuration::Module::SUMMARIZATION, |
| 96 | + ) |
| 97 | + |
| 98 | + expect(ai_feature.enabled?).to be true |
| 99 | + end |
| 100 | + |
| 101 | + it "respects the enabled_by_setting when specified" do |
| 102 | + ai_feature = |
| 103 | + described_class.new( |
| 104 | + "gists", |
| 105 | + "ai_summary_gists_persona", |
| 106 | + DiscourseAi::Configuration::Module::SUMMARIZATION_ID, |
| 107 | + DiscourseAi::Configuration::Module::SUMMARIZATION, |
| 108 | + enabled_by_setting: "ai_summary_gists_enabled", |
| 109 | + ) |
| 110 | + |
| 111 | + SiteSetting.ai_summary_gists_enabled = false |
| 112 | + expect(ai_feature.enabled?).to be false |
| 113 | + |
| 114 | + SiteSetting.ai_summary_gists_enabled = true |
| 115 | + expect(ai_feature.enabled?).to be true |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + describe "#persona_id" do |
| 120 | + it "returns the persona id from site settings" do |
| 121 | + ai_feature = |
| 122 | + described_class.new( |
| 123 | + "topic_summaries", |
| 124 | + "ai_summarization_persona", |
| 125 | + DiscourseAi::Configuration::Module::SUMMARIZATION_ID, |
| 126 | + DiscourseAi::Configuration::Module::SUMMARIZATION, |
| 127 | + ) |
| 128 | + |
| 129 | + SiteSetting.ai_summarization_persona = ai_persona.id |
| 130 | + expect(ai_feature.persona_id).to eq(ai_persona.id) |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + describe ".find_features_using" do |
| 135 | + it "returns all features using a specific persona" do |
| 136 | + SiteSetting.ai_summarization_persona = ai_persona.id |
| 137 | + SiteSetting.ai_helper_proofreader_persona = ai_persona.id |
| 138 | + SiteSetting.ai_translation_locale_detector_persona = 999 |
| 139 | + |
| 140 | + features = described_class.find_features_using(persona_id: ai_persona.id) |
| 141 | + |
| 142 | + expect(features.map(&:name)).to include("topic_summaries", "proofread") |
| 143 | + expect(features.map(&:name)).not_to include("locale_detector") |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments