Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit bce8094

Browse files
DEV: Add test to cover proofreading in rich editor
This core PR must be merged for this to work discourse/discourse#32280 We want to make sure we don't have regressions with this functionality in the rich editor, which uses contendeditable and other APIs than the old composer.
1 parent 67e3a61 commit bce8094

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

spec/system/ai_helper/ai_proofreading_spec.rb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
end
1313

1414
let(:composer) { PageObjects::Components::Composer.new }
15+
let(:rich) { composer.rich_editor }
1516
let(:toasts) { PageObjects::Components::Toasts.new }
1617
let(:diff_modal) { PageObjects::Modals::DiffModal.new }
18+
let(:keyboard_shortcut) { [PLATFORM_KEY_MODIFIER, :alt, "p"] }
1719

1820
context "when triggering via keyboard shortcut" do
19-
it "proofreads selected text using" do
21+
it "proofreads selected text and replaces it" do
2022
visit "/new-topic"
2123
composer.fill_content("hello worldd !")
2224

2325
composer.select_range(6, 12)
2426

2527
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)
2729
diff_modal.confirm_changes
2830
expect(composer.composer_input.value).to eq("hello world !")
2931
end
@@ -35,7 +37,7 @@
3537

3638
# Simulate AI response
3739
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)
3941
diff_modal.confirm_changes
4042
expect(composer.composer_input.value).to eq("hello world")
4143
end
@@ -46,9 +48,41 @@
4648

4749
# Simulate AI response
4850
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)
5052
expect(toasts).to have_error(I18n.t("js.discourse_ai.ai_helper.no_content_error"))
5153
end
5254
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
5387
end
5488
end

0 commit comments

Comments
 (0)