discourse/plugins/discourse-ai/spec/lib/translation/language_detector_spec.rb
Sam e3fae646d4
DEV: AI persona to agent migration (#38319)
Co-authored-by: Keegan George <kgeorge13@gmail.com>
2026-03-10 15:59:45 +11:00

73 lines
2.5 KiB
Ruby
Vendored

# frozen_string_literal: true
describe DiscourseAi::Translation::LanguageDetector do
let!(:agent) do
AiAgent.find(DiscourseAi::Agents::Agent.system_agents[DiscourseAi::Agents::LocaleDetector])
end
before do
enable_current_plugin
assign_fake_provider_to(:ai_default_llm_model)
SiteSetting.ai_translation_enabled = true
end
describe ".detect" do
let(:locale_detector) { described_class.new("meow") }
let(:llm_response) { "en-US" }
it "creates the correct prompt" do
expected_system_prompt = DiscourseAi::Agents::LocaleDetector.new.system_prompt
allow(DiscourseAi::Completions::Prompt).to receive(:new).with(
expected_system_prompt,
messages: [
{ type: :user, content: "Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?" },
{ type: :model, content: "en" },
{
type: :user,
content:
"[quote]\nNon smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.\nOgni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.\n[/quote]\n¿Cuál es el mensaje principal de esta cita?",
},
{ type: :model, content: "es" },
{ type: :user, content: "meow" },
],
post_id: nil,
topic_id: nil,
).and_call_original
DiscourseAi::Completions::Llm.with_prepared_responses([llm_response]) do
locale_detector.detect
end
end
it "returns the language from the llm's response in the language tag" do
DiscourseAi::Completions::Llm.with_prepared_responses([llm_response]) do
locale_detector.detect
end
end
[
["not a language code", nil],
["", nil],
["1234", nil],
["en-US-INCORRECT", nil],
%w[en-US en-US],
%w[en en],
%w[sr-Latn sr-Latn],
].each do |llm_response, expected_locale|
it "returns #{expected_locale.inspect} when the llm responds with #{llm_response.inspect}" do
DiscourseAi::Completions::Llm.with_prepared_responses([llm_response]) do
expect(locale_detector.detect).to eq(expected_locale)
end
end
end
it "skips detection when provided blank text" do
blank_detector = described_class.new(" ")
allow(AiAgent).to receive(:find_by).and_call_original
expect(blank_detector.detect).to eq(nil)
expect(AiAgent).not_to have_received(:find_by)
end
end
end