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

Commit e81108a

Browse files
DEV: Add test to cover proofreading in rich editor (#1259)
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 1300cc8 commit e81108a

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

spec/system/ai_helper/ai_proofreading_spec.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
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
1921
it "proofreads selected text using" do
@@ -24,7 +26,7 @@
2426
composer.select_range(6, 12)
2527

2628
DiscourseAi::Completions::Llm.with_prepared_responses(["world"]) do
27-
composer.composer_input.send_keys([PLATFORM_KEY_MODIFIER, :alt, "p"])
29+
composer.composer_input.send_keys(keyboard_shortcut)
2830
diff_modal.confirm_changes
2931
expect(composer.composer_input.value).to eq("hello world !")
3032
end
@@ -37,7 +39,7 @@
3739

3840
# Simulate AI response
3941
DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do
40-
composer.composer_input.send_keys([PLATFORM_KEY_MODIFIER, :alt, "p"])
42+
composer.composer_input.send_keys(keyboard_shortcut)
4143
diff_modal.confirm_changes
4244
expect(composer.composer_input.value).to eq("hello world")
4345
end
@@ -48,9 +50,41 @@
4850

4951
# Simulate AI response
5052
DiscourseAi::Completions::Llm.with_prepared_responses(["hello world"]) do
51-
composer.composer_input.send_keys([PLATFORM_KEY_MODIFIER, :alt, "p"])
53+
composer.composer_input.send_keys(keyboard_shortcut)
5254
expect(toasts).to have_error(I18n.t("js.discourse_ai.ai_helper.no_content_error"))
5355
end
5456
end
57+
58+
context "when using rich text editor" do
59+
before { SiteSetting.rich_editor = true }
60+
61+
it "proofreads selected text and replaces it" do
62+
visit "/new-topic"
63+
expect(composer).to be_opened
64+
composer.toggle_rich_editor
65+
composer.type_content("hello worldd !")
66+
67+
# NOTE: The rich text editor cannot use select_range on the page object since it is
68+
# a contenteditable element. It would be hard to make this generic enough to put in
69+
# the page object, maybe at some point in the future we can refactor this.
70+
execute_script(<<~JS, text)
71+
const composer = document.querySelector("#reply-control .d-editor-input");
72+
const startNode = composer.firstChild.firstChild;
73+
composer.focus();
74+
const range = document.createRange();
75+
range.setStart(startNode, 6);
76+
range.setEnd(startNode, 12);
77+
const selection = window.getSelection();
78+
selection.removeAllRanges();
79+
selection.addRange(range);
80+
JS
81+
82+
DiscourseAi::Completions::Llm.with_prepared_responses(["world"]) do
83+
composer.composer_input.send_keys(keyboard_shortcut)
84+
diff_modal.confirm_changes
85+
expect(rich).to have_css("p", text: "hello world !")
86+
end
87+
end
88+
end
5589
end
5690
end

0 commit comments

Comments
 (0)