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