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

Commit b5d42f3

Browse files
DEV: Re-introduce proofreading specs (#1263)
Followup fe7e73a and 1300cc8 Reintroduces proofreader specs and unskips ones skipped when streaming composer suggestions were added. We just need to make sure the jobs run immediately in specs because the job is what sends the MessageBus event to the UI. Also adds a pageobject method to confirm a diff is shown in the modal before pressing Confirm button.
1 parent 0f34ce9 commit b5d42f3

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

spec/system/ai_helper/ai_proofreading_spec.rb

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,42 @@
88
before do
99
assign_fake_provider_to(:ai_helper_model)
1010
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!
1115
sign_in(admin)
1216
end
1317

1418
let(:composer) { PageObjects::Components::Composer.new }
19+
let(:rich) { composer.rich_editor }
1520
let(:toasts) { PageObjects::Components::Toasts.new }
1621
let(:diff_modal) { PageObjects::Modals::DiffModal.new }
22+
let(:keyboard_shortcut) { [PLATFORM_KEY_MODIFIER, :alt, "p"] }
1723

1824
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
2126
visit "/new-topic"
2227
composer.fill_content("hello worldd !")
2328

2429
composer.select_range(6, 12)
2530

2631
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")
2834
diff_modal.confirm_changes
2935
expect(composer.composer_input.value).to eq("hello world !")
3036
end
3137
end
3238

3339
it "proofreads all text when nothing is selected" do
34-
skip("Message bus updates not appearing in tests")
3540
visit "/new-topic"
3641
composer.fill_content("hello worrld")
3742

3843
# Simulate AI response
3944
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")
4147
diff_modal.confirm_changes
4248
expect(composer.composer_input.value).to eq("hello world")
4349
end
@@ -48,9 +54,42 @@
4854

4955
# Simulate AI response
5056
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)
5258
expect(toasts).to have_error(I18n.t("js.discourse_ai.ai_helper.no_content_error"))
5359
end
5460
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
5594
end
5695
end

spec/system/page_objects/modals/diff_modal.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def old_value
2222
def new_value
2323
find(".composer-ai-helper-modal__new-value").text
2424
end
25+
26+
def has_diff?(old_value, new_value)
27+
has_css?(".inline-diff ins", text: new_value) &&
28+
has_css?(".inline-diff del", text: old_value)
29+
end
2530
end
2631
end
2732
end

0 commit comments

Comments
 (0)