|
20 | 20 | let(:target_locale) { "de" } |
21 | 21 | let(:llm_response) { "hur dur hur dur!" } |
22 | 22 | fab!(:post) |
| 23 | + fab!(:topic) { post.topic } |
23 | 24 |
|
24 | 25 | it "creates the correct prompt" do |
25 | 26 | post_translator = |
|
36 | 37 | end |
37 | 38 | end |
38 | 39 |
|
| 40 | + it "creates BotContext with the correct parameters and calls bot.reply with correct args" do |
| 41 | + post_translator = |
| 42 | + DiscourseAi::Translation::PostRawTranslator.new(text:, target_locale:, post:, topic:) |
| 43 | + |
| 44 | + expected_content = { content: text, target_locale: target_locale }.to_json |
| 45 | + |
| 46 | + bot_context = instance_double(DiscourseAi::Personas::BotContext) |
| 47 | + allow(DiscourseAi::Personas::BotContext).to receive(:new).and_return(bot_context) |
| 48 | + |
| 49 | + mock_bot = instance_double(DiscourseAi::Personas::Bot) |
| 50 | + allow(DiscourseAi::Personas::Bot).to receive(:as).and_return(mock_bot) |
| 51 | + allow(mock_bot).to receive(:reply).and_yield(llm_response) |
| 52 | + |
| 53 | + post_translator.translate |
| 54 | + |
| 55 | + expect(DiscourseAi::Personas::BotContext).to have_received(:new).with( |
| 56 | + user: an_instance_of(User), |
| 57 | + skip_tool_details: true, |
| 58 | + feature_name: "translation", |
| 59 | + messages: [{ type: :user, content: expected_content }], |
| 60 | + topic: topic, |
| 61 | + post: post, |
| 62 | + ) |
| 63 | + |
| 64 | + expect(DiscourseAi::Personas::Bot).to have_received(:as) |
| 65 | + expect(mock_bot).to have_received(:reply).with(bot_context, llm_args: { max_tokens: 500 }) |
| 66 | + end |
| 67 | + |
| 68 | + it "sets max_tokens correctly based on text length" do |
| 69 | + test_cases = [ |
| 70 | + ["Short text", 500], # Short text (< 100 chars) |
| 71 | + ["a" * 200, 1000], # Medium text (100-500 chars) |
| 72 | + ["a" * 600, 1200], # Long text (> 500 chars, 600*2=1200) |
| 73 | + ] |
| 74 | + |
| 75 | + test_cases.each do |text, expected_max_tokens| |
| 76 | + translator = DiscourseAi::Translation::PostRawTranslator.new(text: text, target_locale:) |
| 77 | + |
| 78 | + bot_context = instance_double(DiscourseAi::Personas::BotContext) |
| 79 | + allow(DiscourseAi::Personas::BotContext).to receive(:new).and_return(bot_context) |
| 80 | + |
| 81 | + mock_bot = instance_double(DiscourseAi::Personas::Bot) |
| 82 | + allow(DiscourseAi::Personas::Bot).to receive(:as).and_return(mock_bot) |
| 83 | + allow(mock_bot).to receive(:reply).and_yield("translated #{text[0..10]}") |
| 84 | + |
| 85 | + translator.translate |
| 86 | + |
| 87 | + expect(mock_bot).to have_received(:reply).with( |
| 88 | + bot_context, |
| 89 | + llm_args: { |
| 90 | + max_tokens: expected_max_tokens, |
| 91 | + }, |
| 92 | + ) |
| 93 | + end |
| 94 | + end |
| 95 | + |
39 | 96 | it "returns the translation from the llm's response" do |
40 | 97 | DiscourseAi::Completions::Llm.with_prepared_responses([llm_response]) do |
41 | 98 | expect( |
|
0 commit comments