|
| 1 | +"""Journey: comparing a draft with a revision in the chat input's Compare text view. |
| 2 | +
|
| 3 | +More > Compare text opens the side-by-side compare and merge view with the message being typed on |
| 4 | +the left as the original. Use in prompt puts the revision on the right into the message box and |
| 5 | +sends nothing until the person sends it. In the comparison the view counts the changes ("Change 1 |
| 6 | +of 2") and steps through them with Previous change and Next change, copies the current change from |
| 7 | +the original to the proposed revision or back, and says "No differences" for two identical texts. |
| 8 | +Closing after an edit asks to discard the comparison first and leaves the message as it was; |
| 9 | +closing with nothing edited asks nothing. Opening the view from the `/compare` command is |
| 10 | +e2e/chat/test_slash_commands.py. |
| 11 | +
|
| 12 | +Discriminates: passes on the dev 3b8205505 build (backend at 3b8205505, frontend of 226dca18c, |
| 13 | +which differs only in the chat input's selected-feature pills). In a build of it whose Use in prompt |
| 14 | +also sends the message the Use in prompt test fails, in one whose copy buttons copy the other way |
| 15 | +the copy test fails, in one whose Next change stays put the step-through test fails, in one that |
| 16 | +counts identical texts as one change the no-differences test fails, and in one whose Cancel closes |
| 17 | +without asking the discard test fails. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import re |
| 23 | +import uuid |
| 24 | + |
| 25 | +import pytest |
| 26 | +from playwright.sync_api import Locator, Page, expect |
| 27 | + |
| 28 | +from harness import upstream as reply |
| 29 | +from utils.chat_ui import chat_input, expect_reply, open_input_menu |
| 30 | + |
| 31 | +pytestmark = [pytest.mark.journey, pytest.mark.requires_browser, pytest.mark.requires_source] |
| 32 | + |
| 33 | +ORIGINAL = "alpha\nbravo\ncharlie\ndelta\necho\nfoxtrot\ngolf" |
| 34 | +REVISED = "alpha\nBRAVO\ncharlie\ndelta\necho\nFOXTROT\ngolf" |
| 35 | + |
| 36 | + |
| 37 | +def _type_draft(page: Page, text: str) -> None: |
| 38 | + expect(chat_input(page)).to_be_visible() |
| 39 | + chat_input(page).click() |
| 40 | + page.keyboard.type(text) |
| 41 | + expect(chat_input(page)).to_have_text(text) |
| 42 | + |
| 43 | + |
| 44 | +def _open_compare(page: Page) -> Locator: |
| 45 | + open_input_menu(page).get_by_role("menuitem", name="Compare text").click() |
| 46 | + compare = page.get_by_role("dialog", name="Compare text") |
| 47 | + expect(compare).to_be_visible() |
| 48 | + return compare |
| 49 | + |
| 50 | + |
| 51 | +def _pane(compare: Locator, label: str) -> Locator: |
| 52 | + return compare.get_by_role("textbox", name=label, exact=True) |
| 53 | + |
| 54 | + |
| 55 | +def _write(compare: Locator, label: str, text: str) -> None: |
| 56 | + _pane(compare, label).click() |
| 57 | + compare.page.keyboard.insert_text(text) |
| 58 | + |
| 59 | + |
| 60 | +def _start_comparing(compare: Locator, original: str, revised: str) -> Locator: |
| 61 | + """Write both sides, press Compare and return the change counter.""" |
| 62 | + _write(compare, "Original", original) |
| 63 | + _write(compare, "Proposed revision", revised) |
| 64 | + compare.get_by_role("button", name="Compare", exact=True).click() |
| 65 | + return compare.get_by_role("status") |
| 66 | + |
| 67 | + |
| 68 | +def test_use_in_prompt_puts_the_revision_in_the_input_without_sending( |
| 69 | + page_for, make_user, upstream |
| 70 | +): |
| 71 | + page = page_for(make_user()) |
| 72 | + draft = f"Tell me about the tides {uuid.uuid4().hex[:6]}" |
| 73 | + revised = f"{draft}, in one short sentence" |
| 74 | + _type_draft(page, draft) |
| 75 | + sent_before = len(upstream.chat_requests()) |
| 76 | + |
| 77 | + compare = _open_compare(page) |
| 78 | + expect(_pane(compare, "Original")).to_have_text(draft) |
| 79 | + _pane(compare, "Proposed revision").click() |
| 80 | + page.keyboard.insert_text(revised) |
| 81 | + compare.get_by_role("button", name="Use in prompt").click() |
| 82 | + |
| 83 | + expect(compare).to_have_count(0) |
| 84 | + expect(chat_input(page)).to_have_text(revised) |
| 85 | + # a bounded look that nothing is sent on its own after the revision lands |
| 86 | + page.wait_for_timeout(1000) |
| 87 | + assert len(upstream.chat_requests()) == sent_before, "Use in prompt sent the message itself" |
| 88 | + expect(page).not_to_have_url(re.compile(r"/c/")) |
| 89 | + |
| 90 | + upstream.queue(reply.text("Noted.", match=reply.answering(revised))) |
| 91 | + chat_input(page).press("Enter") |
| 92 | + expect_reply(page, "Noted.") |
| 93 | + [request] = [body for body in upstream.chat_requests() if reply.answering(revised)(body)] |
| 94 | + assert request["messages"][-1]["content"] == revised |
| 95 | + |
| 96 | + |
| 97 | +def test_next_and_previous_change_step_through_the_changes(page_for, make_user): |
| 98 | + page = page_for(make_user()) |
| 99 | + compare = _open_compare(page) |
| 100 | + counter = _start_comparing(compare, ORIGINAL, REVISED) |
| 101 | + previous = compare.get_by_role("button", name="Previous change") |
| 102 | + following = compare.get_by_role("button", name="Next change") |
| 103 | + |
| 104 | + expect(counter).to_have_text("Change 1 of 2") |
| 105 | + expect(previous).to_be_disabled() |
| 106 | + following.click() |
| 107 | + expect(counter).to_have_text("Change 2 of 2") |
| 108 | + expect(following).to_be_disabled() |
| 109 | + previous.click() |
| 110 | + expect(counter).to_have_text("Change 1 of 2") |
| 111 | + |
| 112 | + |
| 113 | +def test_a_change_is_copied_to_the_proposal_or_back_to_the_original(page_for, make_user): |
| 114 | + page = page_for(make_user()) |
| 115 | + compare = _open_compare(page) |
| 116 | + counter = _start_comparing(compare, ORIGINAL, REVISED) |
| 117 | + expect(counter).to_have_text("Change 1 of 2") |
| 118 | + |
| 119 | + compare.get_by_role("button", name="Copy change to proposal").click() |
| 120 | + expect(counter).to_have_text("Change 1 of 1") |
| 121 | + expect(_pane(compare, "Proposed revision")).to_contain_text("bravo") |
| 122 | + expect(_pane(compare, "Proposed revision")).not_to_contain_text("BRAVO") |
| 123 | + |
| 124 | + compare.get_by_role("button", name="Copy change to original").click() |
| 125 | + expect(counter).to_have_text("No differences") |
| 126 | + expect(_pane(compare, "Original")).to_contain_text("FOXTROT") |
| 127 | + expect(_pane(compare, "Original")).not_to_contain_text("foxtrot") |
| 128 | + |
| 129 | + compare.get_by_role("button", name="Use in prompt").click() |
| 130 | + expect(compare).to_have_count(0) |
| 131 | + expect(chat_input(page)).to_contain_text("bravo") |
| 132 | + expect(chat_input(page)).to_contain_text("FOXTROT") |
| 133 | + |
| 134 | + |
| 135 | +def test_identical_texts_show_no_differences(page_for, make_user): |
| 136 | + page = page_for(make_user()) |
| 137 | + same = "The harbour closes at dusk." |
| 138 | + _type_draft(page, same) |
| 139 | + compare = _open_compare(page) |
| 140 | + _write(compare, "Proposed revision", same) |
| 141 | + compare.get_by_role("button", name="Compare", exact=True).click() |
| 142 | + |
| 143 | + expect(compare.get_by_role("status")).to_have_text("No differences") |
| 144 | + expect(compare.get_by_role("button", name="Previous change")).to_be_disabled() |
| 145 | + expect(compare.get_by_role("button", name="Next change")).to_be_disabled() |
| 146 | + expect(compare.get_by_role("button", name="Copy change to proposal")).to_be_disabled() |
| 147 | + |
| 148 | + |
| 149 | +def test_closing_after_an_edit_asks_to_discard_and_keeps_the_message(page_for, make_user): |
| 150 | + page = page_for(make_user()) |
| 151 | + draft = f"Keep this draft {uuid.uuid4().hex[:6]}" |
| 152 | + _type_draft(page, draft) |
| 153 | + |
| 154 | + compare = _open_compare(page) |
| 155 | + compare.get_by_role("button", name="Cancel", exact=True).click() |
| 156 | + expect(compare).to_have_count(0) |
| 157 | + |
| 158 | + compare = _open_compare(page) |
| 159 | + _write(compare, "Proposed revision", "Something else entirely") |
| 160 | + compare.get_by_role("button", name="Cancel", exact=True).click() |
| 161 | + confirm = page.get_by_role("dialog", name="Discard comparison changes?") |
| 162 | + expect(confirm).to_contain_text("Your prompt will stay unchanged.") |
| 163 | + confirm.get_by_role("button", name="Cancel").click() |
| 164 | + expect(confirm).to_have_count(0) |
| 165 | + expect(_pane(compare, "Proposed revision")).to_have_text("Something else entirely") |
| 166 | + |
| 167 | + compare.get_by_role("button", name="Cancel", exact=True).click() |
| 168 | + confirm.get_by_role("button", name="Discard").click() |
| 169 | + expect(compare).to_have_count(0) |
| 170 | + expect(chat_input(page)).to_have_text(draft) |
0 commit comments