@@ -123,4 +123,84 @@ def add_automation_field(name, value, type: "text")
123123 last_post = post . topic . reload . posts . order ( :post_number ) . last
124124 expect ( last_post . raw ) . to eq post . raw
125125 end
126+
127+ it "can respond using an AI persona when configured" do
128+ bot_user = Fabricate ( :user , username : "ai_assistant" )
129+ ai_persona =
130+ Fabricate (
131+ :ai_persona ,
132+ name : "Help Bot" ,
133+ description : "AI assistant for forum help" ,
134+ system_prompt : "You are a helpful forum assistant" ,
135+ default_llm : llm_model ,
136+ user_id : bot_user . id ,
137+ )
138+
139+ # Configure the automation to use the persona instead of canned reply
140+ add_automation_field ( "canned_reply" , nil , type : "message" ) # Clear canned reply
141+ add_automation_field ( "reply_persona" , ai_persona . id , type : "choices" )
142+ add_automation_field ( "whisper" , true , type : "boolean" )
143+
144+ post = Fabricate ( :post , raw : "I need help with a problem" )
145+
146+ ai_response = "I'll help you with your problem!"
147+
148+ # Set up the test to provide both the triage and the persona responses
149+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ "bad" , ai_response ] ) do
150+ automation . running_in_background!
151+ automation . trigger! ( { "post" => post } )
152+ end
153+
154+ # Verify the response was created
155+ topic = post . topic . reload
156+ last_post = topic . posts . order ( :post_number ) . last
157+
158+ # Verify the AI persona's user created the post
159+ expect ( last_post . user_id ) . to eq ( bot_user . id )
160+
161+ # Verify the content matches the AI response
162+ expect ( last_post . raw ) . to eq ( ai_response )
163+
164+ # Verify it's a whisper post (since we set whisper: true)
165+ expect ( last_post . post_type ) . to eq ( Post . types [ :whisper ] )
166+ end
167+
168+ it "does not create replies when the action is edit" do
169+ # Set up bot user and persona
170+ bot_user = Fabricate ( :user , username : "helper_bot" )
171+ ai_persona =
172+ Fabricate (
173+ :ai_persona ,
174+ name : "Edit Helper" ,
175+ description : "AI assistant for editing" ,
176+ system_prompt : "You help with editing" ,
177+ default_llm : llm_model ,
178+ user_id : bot_user . id ,
179+ )
180+
181+ # Configure the automation with both reply methods
182+ add_automation_field ( "canned_reply" , "This is a canned reply" , type : "message" )
183+ add_automation_field ( "reply_persona" , ai_persona . id , type : "choices" )
184+
185+ # Create a post and capture its topic
186+ post = Fabricate ( :post , raw : "This needs to be evaluated" )
187+ topic = post . topic
188+
189+ # Get initial post count
190+ initial_post_count = topic . posts . count
191+
192+ # Run automation with action: :edit and a matching response
193+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ "bad" ] ) do
194+ automation . running_in_background!
195+ automation . trigger! ( { "post" => post , "action" => :edit } )
196+ end
197+
198+ # Topic should be updated (if configured) but no new posts
199+ topic . reload
200+ expect ( topic . posts . count ) . to eq ( initial_post_count )
201+
202+ # Verify no replies were created
203+ last_post = topic . posts . order ( :post_number ) . last
204+ expect ( last_post . id ) . to eq ( post . id )
205+ end
126206end
0 commit comments