DEV: correct flaky spec (#475)

We were not properly expiring prompt cache
This commit is contained in:
Sam 2024-02-19 15:21:55 +11:00 committed by GitHub
parent 0fb87b00e2
commit 1f74a77e17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 3 deletions

View File

@ -17,6 +17,8 @@ class CompletionPrompt < ActiveRecord::Base
validates :messages, length: { maximum: 20 }
validate :each_message_length
after_commit { DiscourseAi::AiHelper::Assistant.clear_prompt_cache! }
def self.enabled_by_name(name)
where(enabled: true).find_by(name: name)
end

View File

@ -5,10 +5,14 @@ module DiscourseAi
class Assistant
AI_HELPER_PROMPTS_CACHE_KEY = "ai_helper_prompts"
def self.clear_prompt_cache!
Discourse.cache.delete(AI_HELPER_PROMPTS_CACHE_KEY)
end
def available_prompts
Discourse
.cache
.fetch(AI_HELPER_PROMPTS_CACHE_KEY) do
.fetch(AI_HELPER_PROMPTS_CACHE_KEY, expires_in: 30.minutes) do
prompts = CompletionPrompt.where(enabled: true)
# Hide illustrate_post if disabled

View File

@ -15,7 +15,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
describe("#available_prompts") do
before do
SiteSetting.ai_helper_illustrate_post_model = "disabled"
Discourse.cache.delete(DiscourseAi::AiHelper::Assistant::AI_HELPER_PROMPTS_CACHE_KEY)
DiscourseAi::AiHelper::Assistant.clear_prompt_cache!
end
it "returns all available prompts" do
@ -35,7 +35,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
context "when illustrate post model is enabled" do
before do
SiteSetting.ai_helper_illustrate_post_model = "stable_diffusion_xl"
Discourse.cache.delete(DiscourseAi::AiHelper::Assistant::AI_HELPER_PROMPTS_CACHE_KEY)
DiscourseAi::AiHelper::Assistant.clear_prompt_cache!
end
it "returns the illustrate_post prompt in the list of all prompts" do

View File

@ -32,6 +32,8 @@ describe Plugin::Instance do
SiteSetting.composer_ai_helper_enabled = true
SiteSetting.ai_helper_illustrate_post_model = "disabled"
Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user)
DiscourseAi::AiHelper::Assistant.clear_prompt_cache!
end
let(:serializer) { CurrentUserSerializer.new(user, scope: Guardian.new(user)) }