|
6 | 6 | let(:ai_pm_homepage) { PageObjects::Components::AiPmHomepage.new } |
7 | 7 | let(:sidebar) { PageObjects::Components::NavigationMenu::Sidebar.new } |
8 | 8 | let(:header_dropdown) { PageObjects::Components::NavigationMenu::HeaderDropdown.new } |
| 9 | + let(:dialog) { PageObjects::Components::Dialog.new } |
9 | 10 |
|
10 | | - fab!(:user) |
11 | | - fab!(:group) |
| 11 | + fab!(:user) { Fabricate(:user, refresh_auto_groups: true) } |
12 | 12 |
|
| 13 | + fab!(:claude_2) do |
| 14 | + Fabricate( |
| 15 | + :llm_model, |
| 16 | + provider: "anthropic", |
| 17 | + url: "https://api.anthropic.com/v1/messages", |
| 18 | + name: "claude-2", |
| 19 | + ) |
| 20 | + end |
13 | 21 | fab!(:bot_user) do |
14 | | - user = Fabricate(:user) |
15 | | - AiPersona.last.update!(user_id: user.id) |
16 | | - user |
| 22 | + toggle_enabled_bots(bots: [claude_2]) |
| 23 | + SiteSetting.ai_bot_enabled = true |
| 24 | + claude_2.reload.user |
| 25 | + end |
| 26 | + fab!(:bot) do |
| 27 | + persona = |
| 28 | + AiPersona |
| 29 | + .find(DiscourseAi::Personas::Persona.system_personas[DiscourseAi::Personas::General]) |
| 30 | + .class_instance |
| 31 | + .new |
| 32 | + DiscourseAi::Personas::Bot.as(bot_user, persona: persona) |
17 | 33 | end |
18 | | - fab!(:llm_model) { Fabricate(:llm_model, enabled_chat_bot: true) } |
19 | 34 |
|
20 | | - fab!(:pm) { Fabricate(:private_message_topic, title: "AI Conversation Test", user: user) } |
21 | | - fab!(:reply) do |
22 | | - Fabricate(:post, topic: pm, user: user, post_number: 1, raw: "test test test user reply") |
| 35 | + fab!(:pm) do |
| 36 | + Fabricate( |
| 37 | + :private_message_topic, |
| 38 | + title: "This is my special PM", |
| 39 | + user: user, |
| 40 | + topic_allowed_users: [ |
| 41 | + Fabricate.build(:topic_allowed_user, user: user), |
| 42 | + Fabricate.build(:topic_allowed_user, user: bot_user), |
| 43 | + ], |
| 44 | + ) |
| 45 | + end |
| 46 | + fab!(:first_post) do |
| 47 | + Fabricate(:post, topic: pm, user: user, post_number: 1, raw: "This is a reply by the user") |
23 | 48 | end |
24 | | - fab!(:bot_reply) do |
25 | | - Fabricate(:post, topic: pm, user: bot_user, post_number: 2, raw: "test test test bot reply") |
| 49 | + fab!(:second_post) do |
| 50 | + Fabricate(:post, topic: pm, user: bot_user, post_number: 2, raw: "This is a bot reply") |
| 51 | + end |
| 52 | + fab!(:third_post) do |
| 53 | + Fabricate( |
| 54 | + :post, |
| 55 | + topic: pm, |
| 56 | + user: user, |
| 57 | + post_number: 3, |
| 58 | + raw: "This is a second reply by the user", |
| 59 | + ) |
26 | 60 | end |
27 | 61 | fab!(:topic_user) { Fabricate(:topic_user, topic: pm, user: user) } |
28 | 62 | fab!(:topic_bot_user) { Fabricate(:topic_user, topic: pm, user: bot_user) } |
29 | 63 |
|
| 64 | + fab!(:persona) do |
| 65 | + persona = |
| 66 | + AiPersona.create!( |
| 67 | + name: "Test Persona", |
| 68 | + description: "A test persona", |
| 69 | + allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]], |
| 70 | + enabled: true, |
| 71 | + system_prompt: "You are a helpful bot", |
| 72 | + ) |
| 73 | + |
| 74 | + persona.create_user! |
| 75 | + persona.update!( |
| 76 | + default_llm_id: claude_2.id, |
| 77 | + allow_chat_channel_mentions: true, |
| 78 | + allow_topic_mentions: true, |
| 79 | + ) |
| 80 | + persona |
| 81 | + end |
| 82 | + |
30 | 83 | before do |
31 | 84 | SiteSetting.ai_enable_experimental_bot_ux = true |
32 | 85 | SiteSetting.ai_bot_enabled = true |
33 | | - toggle_enabled_bots(bots: [llm_model]) |
34 | | - SiteSetting.ai_bot_allowed_groups = group.id.to_s |
35 | | - sign_in(user) |
36 | | - |
37 | | - group.add(user) |
38 | | - group.save |
39 | | - |
40 | | - allowed_persona = AiPersona.last |
41 | | - allowed_persona.update!(allowed_group_ids: [group.id], enabled: true) |
42 | | - |
| 86 | + Jobs.run_immediately! |
| 87 | + SiteSetting.ai_bot_allowed_groups = "#{Group::AUTO_GROUPS[:trust_level_0]}" |
43 | 88 | sign_in(user) |
44 | 89 | end |
45 | 90 |
|
|
60 | 105 | expect(sidebar).to be_visible |
61 | 106 | end |
62 | 107 |
|
| 108 | + it "displays error when message is too short" do |
| 109 | + visit "/" |
| 110 | + find(".ai-bot-button").click |
| 111 | + |
| 112 | + ai_pm_homepage.input.fill_in(with: "a") |
| 113 | + ai_pm_homepage.submit |
| 114 | + expect(ai_pm_homepage).to have_too_short_dialog |
| 115 | + dialog.click_yes |
| 116 | + expect(composer).to be_closed |
| 117 | + end |
| 118 | + |
63 | 119 | it "renders sidebar even when navigation menu is set to header" do |
64 | 120 | SiteSetting.navigation_menu = "header dropdown" |
65 | 121 | visit "/" |
|
88 | 144 |
|
89 | 145 | expect(ai_pm_homepage).to have_homepage |
90 | 146 | expect(sidebar).to have_section("custom-messages") |
91 | | - expect(sidebar).to have_section_link(pm.title, href: pm.relative_url) |
| 147 | + expect(sidebar).to have_section_link(pm.title) |
| 148 | + expect(sidebar).to have_no_css("button.ai-new-question-button") |
| 149 | + end |
| 150 | + |
| 151 | + it "navigates to the bot conversation when clicked" do |
| 152 | + visit "/" |
| 153 | + find(".ai-bot-button").click |
| 154 | + |
| 155 | + expect(ai_pm_homepage).to have_homepage |
| 156 | + sidebar.find( |
| 157 | + ".sidebar-section[data-section-name='custom-messages'] a.sidebar-section-link", |
| 158 | + ).click |
| 159 | + expect(topic_page).to have_topic_title(pm.title) |
| 160 | + end |
| 161 | + |
| 162 | + it "displays sidebar and 'new question' on the topic page" do |
| 163 | + topic_page.visit_topic(pm) |
| 164 | + expect(sidebar).to be_visible |
| 165 | + expect(sidebar).to have_css("button.ai-new-question-button") |
| 166 | + end |
| 167 | + |
| 168 | + it "redirect to the homepage when 'new question' is clicked" do |
| 169 | + topic_page.visit_topic(pm) |
| 170 | + expect(sidebar).to be_visible |
| 171 | + sidebar.find("button.ai-new-question-button").click |
| 172 | + expect(ai_pm_homepage).to have_homepage |
| 173 | + end |
| 174 | + |
| 175 | + it "can send a new message to the bot" do |
| 176 | + topic_page.visit_topic(pm) |
| 177 | + topic_page.click_reply_button |
| 178 | + expect(composer).to be_opened |
| 179 | + |
| 180 | + composer.fill_in(with: "Hello bot replying to you") |
| 181 | + composer.submit |
| 182 | + expect(page).to have_content("Hello bot replying to you") |
92 | 183 | end |
93 | 184 | end |
94 | 185 | end |
0 commit comments