# Part of Odoo. See LICENSE file for full copyright and licensing details. import json from markupsafe import Markup from odoo.addons.ai.tests.common import TestAICommon from odoo.tests import tagged, users @tagged("post_install", "-at_install") class TestAIPromptButton(TestAICommon): @classmethod def setUpClass(cls): super().setUpClass() cls.composer_id = cls.env['ai.composer'].create({ 'name': 'test composer', 'interface_key': 'chatter_ai_button', 'focused_model_id': cls.env.ref('ai.model_ai_agent').id, }) cls.rendering_record = cls.env['ai.agent'].create({ 'name': 'Test record', }) @users('user_internal') def test_button_without_prompt(self): """ When the prompt field of the prompt button is empty, the name field is used as the prompt. """ button = self.env['ai.prompt.button'].sudo().create({ 'name': 'Button without prompt', 'composer_id': self.composer_id.id, }) message_parts = button.sudo(False)._render_prompt(self.rendering_record.id) self.assertEqual(message_parts, [{'type': 'text', 'text': 'Button without prompt'}]) self.composer_id.focused_model_id = self.env.ref('ai.model_ai_agent').id button = self.env['ai.prompt.button'].sudo().create({ 'name': 'Button with empty prompt', 'composer_id': self.composer_id.id, 'prompt': Markup('
') }) message_parts = button.sudo(False)._render_prompt(self.rendering_record.id) self.assertEqual(message_parts, [{'type': 'text', 'text': 'Button with empty prompt'}]) @users('user_internal') def test_render_basic_prompt(self): """ The prompt field of the button has a value, but it is simple text and doesn't contain any field references. """ self.composer_id.focused_model_id = self.env.ref("ai.model_ai_agent").id button = self.env['ai.prompt.button'].sudo().create({ 'name': 'Basic Prompt', 'prompt': 'This is a basic prompt
', 'composer_id': self.composer_id.id, }) message_parts = button.sudo(False)._render_prompt(self.rendering_record.id) self.assertEqual(len(message_parts), 1) self.assertEqual(message_parts[0]['type'], 'text') self.assertIn('This is a basic prompt', message_parts[0]['text']) self.assertIn('# field values\n{}', message_parts[0]['text']) @users('user_internal') def test_render_prompt_with_fields(self): """ The prompt field of the button contains field references. """ self.composer_id.focused_model_id = self.env.ref('ai.model_ai_agent').id # Use the specific span format required by parse_ai_prompt_values button = self.env['ai.prompt.button'].sudo().create({ 'name': 'Field Prompt', 'prompt': """Agent name: Name
Agent image: Image
Partner name: Partner Name