|
1 | 1 | import { defaultModeSlug } from "@roo/modes" |
2 | 2 |
|
3 | | -import { render, fireEvent, screen } from "@src/utils/test-utils" |
| 3 | +import { render, fireEvent, screen, act } from "@src/utils/test-utils" |
4 | 4 | import { useExtensionState } from "@src/context/ExtensionStateContext" |
5 | 5 | import { vscode } from "@src/utils/vscode" |
6 | 6 | import * as pathMentions from "@src/utils/path-mentions" |
@@ -1252,4 +1252,89 @@ describe("ChatTextArea", () => { |
1252 | 1252 | expect(sendButton).toHaveClass("pointer-events-none") |
1253 | 1253 | }) |
1254 | 1254 | }) |
| 1255 | + |
| 1256 | + describe("string operations on the normalized input (issue #1226)", () => { |
| 1257 | + const getTextarea = (container: HTMLElement) => { |
| 1258 | + const textarea = container.querySelector("textarea") |
| 1259 | + if (!textarea) { |
| 1260 | + throw new Error("expected the chat textarea to be rendered") |
| 1261 | + } |
| 1262 | + return textarea |
| 1263 | + } |
| 1264 | + |
| 1265 | + it("inserts command text at the cursor via the insertTextIntoTextarea message", () => { |
| 1266 | + render(<ChatTextArea {...defaultProps} inputValue="hello " />) |
| 1267 | + |
| 1268 | + act(() => { |
| 1269 | + window.dispatchEvent( |
| 1270 | + new MessageEvent("message", { data: { type: "insertTextIntoTextarea", text: "/command" } }), |
| 1271 | + ) |
| 1272 | + }) |
| 1273 | + |
| 1274 | + // The cursor starts at 0, so the command is prepended with a trailing space. |
| 1275 | + expect(defaultProps.setInputValue).toHaveBeenCalledWith("/command hello ") |
| 1276 | + }) |
| 1277 | + |
| 1278 | + it("inspects the characters around the cursor on Backspace without crashing", () => { |
| 1279 | + const { container } = render(<ChatTextArea {...defaultProps} inputValue="some text" />) |
| 1280 | + const textarea = getTextarea(container) |
| 1281 | + |
| 1282 | + fireEvent.keyDown(textarea, { key: "Backspace" }) |
| 1283 | + |
| 1284 | + // Plain text: no mention manipulation, so the input is untouched. |
| 1285 | + expect(defaultProps.setInputValue).not.toHaveBeenCalled() |
| 1286 | + }) |
| 1287 | + |
| 1288 | + it("removes a mention on the second Backspace after deleting the space after it", () => { |
| 1289 | + const { container } = render(<ChatTextArea {...defaultProps} inputValue="hello @problems " />) |
| 1290 | + const textarea = getTextarea(container) |
| 1291 | + |
| 1292 | + // Position the cursor after the trailing space and tell the component about it. |
| 1293 | + textarea.setSelectionRange(16, 16) |
| 1294 | + fireEvent.mouseUp(textarea) |
| 1295 | + |
| 1296 | + // First Backspace: drops the space after the mention and arms the pending flag. |
| 1297 | + fireEvent.keyDown(textarea, { key: "Backspace" }) |
| 1298 | + |
| 1299 | + // Second Backspace: removes the mention itself. |
| 1300 | + fireEvent.keyDown(textarea, { key: "Backspace" }) |
| 1301 | + |
| 1302 | + expect(defaultProps.setInputValue).toHaveBeenCalledWith("hello ") |
| 1303 | + }) |
| 1304 | + |
| 1305 | + it("resets the pending mention flag without changing the input when the cursor is not after a mention", () => { |
| 1306 | + const { container } = render(<ChatTextArea {...defaultProps} inputValue="hello @problems " />) |
| 1307 | + const textarea = getTextarea(container) |
| 1308 | + |
| 1309 | + // Arm the pending flag with the first Backspace right after the mention. |
| 1310 | + textarea.setSelectionRange(16, 16) |
| 1311 | + fireEvent.mouseUp(textarea) |
| 1312 | + fireEvent.keyDown(textarea, { key: "Backspace" }) |
| 1313 | + |
| 1314 | + // Move the cursor away from the mention before the next Backspace. |
| 1315 | + textarea.setSelectionRange(2, 2) |
| 1316 | + fireEvent.mouseUp(textarea) |
| 1317 | + fireEvent.keyDown(textarea, { key: "Backspace" }) |
| 1318 | + |
| 1319 | + // removeMention finds no mention at the cursor, so the input is untouched. |
| 1320 | + expect(defaultProps.setInputValue).not.toHaveBeenCalled() |
| 1321 | + }) |
| 1322 | + |
| 1323 | + it("adds a trailing space after a pasted URL", () => { |
| 1324 | + const { container } = render(<ChatTextArea {...defaultProps} inputValue="visit " />) |
| 1325 | + const textarea = getTextarea(container) |
| 1326 | + |
| 1327 | + const pasteEvent = new window.Event("paste", { bubbles: true, cancelable: true }) |
| 1328 | + Object.defineProperty(pasteEvent, "clipboardData", { |
| 1329 | + value: { items: [], getData: () => "https://example.com" }, |
| 1330 | + }) |
| 1331 | + |
| 1332 | + act(() => { |
| 1333 | + textarea.dispatchEvent(pasteEvent) |
| 1334 | + }) |
| 1335 | + |
| 1336 | + // The URL is inserted at cursor position 0, followed by a space. |
| 1337 | + expect(defaultProps.setInputValue).toHaveBeenCalledWith("https://example.com visit ") |
| 1338 | + }) |
| 1339 | + }) |
1255 | 1340 | }) |
0 commit comments