|
| 1 | +// Composer native undo/redo regression: the controlled `.value` binding must |
| 2 | +// not re-apply the textarea value after native input, which clobbers the |
| 3 | +// browser's undo/redo bookkeeping (#131708). |
| 4 | +import { chromium, type Browser, type Page } from "playwright"; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + canRunPlaywrightChromium, |
| 8 | + installMockGateway, |
| 9 | + resolvePlaywrightChromiumExecutablePath, |
| 10 | + startControlUiE2eServer, |
| 11 | + type ControlUiE2eServer, |
| 12 | +} from "../../test-helpers/control-ui-e2e.ts"; |
| 13 | + |
| 14 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 15 | +const describeComposerUndoRedo = canRunPlaywrightChromium(chromiumExecutablePath) |
| 16 | + ? describe |
| 17 | + : describe.skip; |
| 18 | + |
| 19 | +const COMPOSER_TEXTAREA = ".agent-chat__composer-combobox > textarea"; |
| 20 | +const TYPED_TEXT = "hello world test"; |
| 21 | + |
| 22 | +let browser: Browser | null = null; |
| 23 | +let page: Page | null = null; |
| 24 | +let server: ControlUiE2eServer | null = null; |
| 25 | + |
| 26 | +describeComposerUndoRedo("chat composer native undo/redo", () => { |
| 27 | + beforeAll(async () => { |
| 28 | + browser = await chromium.launch({ |
| 29 | + executablePath: chromiumExecutablePath, |
| 30 | + headless: true, |
| 31 | + }); |
| 32 | + server = await startControlUiE2eServer(); |
| 33 | + page = await browser.newPage(); |
| 34 | + await installMockGateway(page); |
| 35 | + // Count every programmatic textarea value write so the binding contract |
| 36 | + // (no re-apply after native input) is asserted directly. |
| 37 | + await page.addInitScript(() => { |
| 38 | + const descriptor = Object.getOwnPropertyDescriptor( |
| 39 | + HTMLTextAreaElement.prototype, |
| 40 | + "value", |
| 41 | + ) as PropertyDescriptor & { get(): string; set(next: string): void }; |
| 42 | + Object.defineProperty(HTMLTextAreaElement.prototype, "value", { |
| 43 | + get: descriptor.get, |
| 44 | + set(next: string) { |
| 45 | + if (this.isConnected) { |
| 46 | + (window as { composerValueWrites?: number }).composerValueWrites = |
| 47 | + ((window as { composerValueWrites?: number }).composerValueWrites ?? 0) + 1; |
| 48 | + } |
| 49 | + descriptor.set.call(this, next); |
| 50 | + }, |
| 51 | + configurable: true, |
| 52 | + }); |
| 53 | + }); |
| 54 | + await page.goto(`${server.baseUrl}chat/main`, { |
| 55 | + waitUntil: "domcontentloaded", |
| 56 | + timeout: 30_000, |
| 57 | + }); |
| 58 | + await page.locator(COMPOSER_TEXTAREA).waitFor({ timeout: 30_000 }); |
| 59 | + }); |
| 60 | + |
| 61 | + afterAll(async () => { |
| 62 | + await page?.close(); |
| 63 | + await server?.close(); |
| 64 | + await browser?.close(); |
| 65 | + }); |
| 66 | + |
| 67 | + async function valueWriteCount(): Promise<number> { |
| 68 | + return page!.evaluate( |
| 69 | + () => (window as { composerValueWrites?: number }).composerValueWrites ?? 0, |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + it("keeps native redo working after undo in the composer textarea", async () => { |
| 74 | + const textarea = page!.locator(COMPOSER_TEXTAREA); |
| 75 | + await textarea.click(); |
| 76 | + await textarea.type(TYPED_TEXT); |
| 77 | + expect(await textarea.inputValue()).toBe(TYPED_TEXT); |
| 78 | + |
| 79 | + await page!.keyboard.press("ControlOrMeta+a"); |
| 80 | + await page!.keyboard.press("Backspace"); |
| 81 | + expect(await textarea.inputValue()).toBe(""); |
| 82 | + |
| 83 | + await page!.keyboard.press("ControlOrMeta+z"); |
| 84 | + expect(await textarea.inputValue()).toBe(TYPED_TEXT); |
| 85 | + |
| 86 | + // Redo must re-apply the deletion, exactly like a native textarea. |
| 87 | + await page!.keyboard.press("ControlOrMeta+Shift+z"); |
| 88 | + expect(await textarea.inputValue()).toBe(""); |
| 89 | + }); |
| 90 | + |
| 91 | + it("does not re-apply the textarea value after native input", async () => { |
| 92 | + const textarea = page!.locator(COMPOSER_TEXTAREA); |
| 93 | + await textarea.click(); |
| 94 | + await page!.keyboard.press("ControlOrMeta+a"); |
| 95 | + await page!.keyboard.press("Backspace"); |
| 96 | + await page!.waitForTimeout(100); |
| 97 | + |
| 98 | + const beforeTyping = await valueWriteCount(); |
| 99 | + await textarea.type(TYPED_TEXT); |
| 100 | + await page!.waitForTimeout(100); |
| 101 | + const afterTyping = await valueWriteCount(); |
| 102 | + |
| 103 | + // Typing reaches the draft owner and requests renders, but none of those |
| 104 | + // renders may write the textarea value back: the DOM already holds it, and |
| 105 | + // a programmatic re-apply resets the browser's undo/redo bookkeeping. |
| 106 | + expect(afterTyping - beforeTyping).toBe(0); |
| 107 | + |
| 108 | + // Native undo/redo must likewise complete without any value write-back. |
| 109 | + const beforeUndo = await valueWriteCount(); |
| 110 | + await page!.keyboard.press("ControlOrMeta+a"); |
| 111 | + await page!.keyboard.press("Backspace"); |
| 112 | + await page!.keyboard.press("ControlOrMeta+z"); |
| 113 | + await page!.keyboard.press("ControlOrMeta+Shift+z"); |
| 114 | + await page!.waitForTimeout(100); |
| 115 | + expect(await textarea.inputValue()).toBe(""); |
| 116 | + expect((await valueWriteCount()) - beforeUndo).toBe(0); |
| 117 | + }); |
| 118 | +}); |
0 commit comments