|
8 | 8 | before do |
9 | 9 | assign_fake_provider_to(:ai_helper_model) |
10 | 10 | SiteSetting.ai_helper_enabled = true |
| 11 | + |
| 12 | + # This needs to be done because the streaming suggestions for composer |
| 13 | + # happen in a background job, which sends the MessageBus event to the client. |
| 14 | + Jobs.run_immediately! |
11 | 15 | sign_in(admin) |
12 | 16 | end |
13 | 17 |
|
14 | 18 | let(:composer) { PageObjects::Components::Composer.new } |
| 19 | + let(:rich) { composer.rich_editor } |
15 | 20 | let(:toasts) { PageObjects::Components::Toasts.new } |
16 | 21 | let(:diff_modal) { PageObjects::Modals::DiffModal.new } |
| 22 | + let(:keyboard_shortcut) { [PLATFORM_KEY_MODIFIER, :alt, "p"] } |
17 | 23 |
|
18 | 24 | context "when triggering via keyboard shortcut" do |
19 | | - it "proofreads selected text using" do |
20 | | - skip("Message bus updates not appearing in tests") |
| 25 | + it "proofreads selected text" do |
21 | 26 | visit "/new-topic" |
22 | 27 | composer.fill_content("hello worldd !") |
23 | 28 |
|
24 | 29 | composer.select_range(6, 12) |
25 | 30 |
|
26 | 31 | DiscourseAi::Completions::Llm.with_prepared_responses(["world"]) do |
27 | | - composer.composer_input.send_keys([PLATFORM_KEY_MODIFIER, :alt, "p"]) |
| 32 | + composer.composer_input.send_keys(keyboard_shortcut) |
| 33 | + expect(diff_modal).to have_diff("worldd", "world") |
28 | 34 | diff_modal.confirm_changes |
29 | 35 | expect(composer.composer_input.value).to eq("hello world !") |
30 | 36 | end |
31 | 37 | end |
32 | 38 |
|
33 | 39 | it "proofreads all text when nothing is selected" do |
34 | | - skip("Message bus updates not appearing in tests") |
35 | 40 | visit "/new-topic" |
36 | 41 | composer.fill_content("hello worrld") |
37 | 42 |
|
38 | 43 | # Simulate AI response |
39 | 44 | DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do |
40 | | - composer.composer_input.send_keys([PLATFORM_KEY_MODIFIER, :alt, "p"]) |
| 45 | + composer.composer_input.send_keys(keyboard_shortcut) |
| 46 | + expect(diff_modal).to have_diff("worrld", "world") |
41 | 47 | diff_modal.confirm_changes |
42 | 48 | expect(composer.composer_input.value).to eq("hello world") |
43 | 49 | end |
|
48 | 54 |
|
49 | 55 | # Simulate AI response |
50 | 56 | DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do |
51 | | - composer.composer_input.send_keys([PLATFORM_KEY_MODIFIER, :alt, "p"]) |
| 57 | + composer.composer_input.send_keys(keyboard_shortcut) |
52 | 58 | expect(toasts).to have_error(I18n.t("js.discourse_ai.ai_helper.no_content_error")) |
53 | 59 | end |
54 | 60 | end |
| 61 | + |
| 62 | + context "when using rich text editor" do |
| 63 | + before { SiteSetting.rich_editor = true } |
| 64 | + |
| 65 | + it "proofreads selected text and replaces it" do |
| 66 | + visit "/new-topic" |
| 67 | + expect(composer).to be_opened |
| 68 | + composer.toggle_rich_editor |
| 69 | + composer.type_content("hello worldd !") |
| 70 | + |
| 71 | + # NOTE: The rich text editor cannot use select_range on the page object since it is |
| 72 | + # a contenteditable element. It would be hard to make this generic enough to put in |
| 73 | + # the page object, maybe at some point in the future we can refactor this. |
| 74 | + execute_script(<<~JS, text) |
| 75 | + const composer = document.querySelector("#reply-control .d-editor-input"); |
| 76 | + const startNode = composer.firstChild.firstChild; |
| 77 | + composer.focus(); |
| 78 | + const range = document.createRange(); |
| 79 | + range.setStart(startNode, 6); |
| 80 | + range.setEnd(startNode, 12); |
| 81 | + const selection = window.getSelection(); |
| 82 | + selection.removeAllRanges(); |
| 83 | + selection.addRange(range); |
| 84 | + JS |
| 85 | + |
| 86 | + DiscourseAi::Completions::Llm.with_prepared_responses(["world"]) do |
| 87 | + composer.composer_input.send_keys(keyboard_shortcut) |
| 88 | + expect(diff_modal).to have_diff("worldd", "world") |
| 89 | + diff_modal.confirm_changes |
| 90 | + expect(rich).to have_css("p", text: "hello world !") |
| 91 | + end |
| 92 | + end |
| 93 | + end |
55 | 94 | end |
56 | 95 | end |
0 commit comments