Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(chat): narrow the caret defect, and catch a dead Edit affordance

M3gA-Mindf7f13a0ecea0
diff --git a/app/test/playwright/specs/chat-composer-edit-and-clipboard.spec.ts b/app/test/playwright/specs/chat-composer-edit-and-clipboard.spec.ts
new file mode 100644
index 0000000000..d388ce4292
--- /dev/null
+++ b/app/test/playwright/specs/chat-composer-edit-and-clipboard.spec.ts
@@ -0,0 +1,267 @@
+/**
+ * The caret defect, isolated to Lexical — plus paste and undo/redo.
+ *
+ * # The natural experiment this file exists for
+ *
+ * `chat-composer-caret.spec.ts` proves the main composer drops the caret to the
+ * end on every insertion. This file asks *which* input implementation is at
+ * fault, without editing any product code, because the app already renders two
+ * different ones:
+ *
+ * main composer -> `LexicalComposerInput` from `@assistant-ui/react-lexical`
+ * (`app/src/components/assistant-ui/thread.tsx:429`), chosen
+ * deliberately over the plain primitive so `/` commands can
+ * anchor a popover to the caret (`:421-427`). It is a
+ * contenteditable, and `:346` bolts the
+ * `chat-message-input` testid onto it precisely because it
+ * "deliberately is not a native textarea".
+ *
+ * edit composer -> the plain `ComposerPrimitive.Input`
+ * (`app/src/components/assistant-ui/thread.tsx:859`) —
+ * unreachable, and for a reason that is itself a bug: the
+ * Edit button renders but the adapter cannot honour it.
+ * See the first test.
+ *
+ * That corrects my own earlier attribution: I had written that the culprit was
+ * `ComposerPrimitive.Input`'s re-render. It is not — the main composer does not
+ * use that component at all, it uses Lexical.
+ *
+ * With the edit composer closed off, the isolation is done by BEHAVIOUR
+ * instead, and it lands somewhere more useful: deletion keeps the caret
+ * (`chat-composer-caret.spec.ts`), paste keeps the caret (below), and only a
+ * typed keystroke loses it. So the fault is not "any insertion" and not "any
+ * re-render" — it is Lexical's insert-text path specifically.
+ */
+import { expect, type Locator, type Page, test } from '@playwright/test';
+
+import {
+ bootAuthenticatedPage,
+ dismissWalkthroughIfPresent,
+ waitForAppReady,
+} from '../helpers/core-rpc';
+
+const MOCK_ADMIN_BASE = `http://127.0.0.1:${process.env.E2E_MOCK_PORT || '18473'}`;
+const USER_ID = 'pw-composer-edit-clipboard';
+const REPLY = 'canary-reply-3m8k1v';
+
+async function resetMock(): Promise<void> {
+ await fetch(`${MOCK_ADMIN_BASE}/__admin/reset`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({}),
+ });
+}
+
+async function setMockBehavior(key: string, value: string): Promise<void> {
+ await fetch(`${MOCK_ADMIN_BASE}/__admin/behavior`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ key, value }),
+ });
+}
+
+async function openChat(page: Page): Promise<Locator> {
+ await bootAuthenticatedPage(page, USER_ID, '/chat');
+ await page.goto('/#/chat');
+ await waitForAppReady(page);
+ await dismissWalkthroughIfPresent(page);
+ const input = page.getByTestId('chat-message-input');
+ await expect(input).toBeVisible();
+ return input;
+}
+
+async function waitForSocketConnected(page: Page): Promise<void> {
+ await expect
+ .poll(
+ async () =>
+ page.evaluate(() => {
+ const store = (
+ window as unknown as {
+ __OPENHUMAN_STORE__?: {
+ getState?: () => { socket?: { byUser?: Record<string, { status?: string }> } };
+ };
+ }
+ ).__OPENHUMAN_STORE__;
+ const byUser = store?.getState?.().socket?.byUser ?? {};
+ return Object.values(byUser).some(entry => entry?.status === 'connected');
+ }),
+ { timeout: 30_000 }
+ )
+ .toBe(true);
+}
+
+function textOf(el: Locator): Promise<string> {
+ return el.evaluate(node => {
+ const ta = node as HTMLTextAreaElement;
+ // The edit composer is a real textarea; the main one is a contenteditable.
+ return typeof ta.value === 'string' ? ta.value : (node.textContent ?? '');
+ });
+}
+
+/** Caret offset, handling both a textarea and a contenteditable. */
+function caretOf(el: Locator): Promise<number> {
+ return el.evaluate(node => {
+ const ta = node as HTMLTextAreaElement;
+ if (typeof ta.selectionStart === 'number') return ta.selectionStart;
+ const sel = window.getSelection();
+ if (!sel || sel.rangeCount === 0) return -1;
+ const range = sel.getRangeAt(0).cloneRange();
+ const pre = document.createRange();
+ pre.selectNodeContents(node);
+ pre.setEnd(range.startContainer, range.startOffset);
+ return pre.toString().length;
+ });
+}
+
+async function clearAndType(page: Page, el: Locator, text: string): Promise<void> {
+ await el.click();
+ await page.keyboard.press('ControlOrMeta+a');
+ await page.keyboard.press('Delete');
+ await expect.poll(() => textOf(el), { timeout: 15_000 }).toBe('');
+ await page.keyboard.type(text);
+ await expect.poll(() => textOf(el), { timeout: 15_000 }).toBe(text);
+}
+
+async function placeCaret(page: Page, el: Locator, index: number): Promise<void> {
+ await el.click();
+ await page.keyboard.press('Home');
+ for (let i = 0; i < index; i += 1) await page.keyboard.press('ArrowRight');
+ await page.waitForTimeout(100);
+}
+
+test.describe('Composer — clipboard, history, and the edit composer', () => {
+ test.beforeEach(async () => {
+ await resetMock();
+ });
+
+ /**
+ * A DEAD AFFORDANCE, found while trying to reach the edit composer.
+ *
+ * The plan was to compare the main composer (Lexical) against the edit
+ * composer (`ComposerPrimitive.Input`, `thread.tsx:859`). It cannot be done,
+ * and the reason is a bug rather than a design choice.
+ *
+ * The runtime adapter implements neither `onEdit` nor `setMessages`, so
+ * assistant-ui reports `edit: false`
+ * (`features/conversations/components/aui/auiThreadState.ts:56-67`). The
+ * jsdom test at `auiThreadState.test.tsx:42` asserts that flag and its
+ * header states the consequence: "The transcript renders no edit composer …
+ * this test is what would fail the day someone wires an affordance to a
+ * capability the adapter cannot honour."
+ *
+ * That day has arrived and no test caught it, because the flag was asserted
+ * and the DOM never was. **The Edit button IS rendered** (measured: count 1
+ * on hover) and clicking it opens nothing — `.aui-edit-composer-input` stays
+ * absent. The user gets a button that silently does nothing.
+ *
+ * Asserted as the current behaviour so the defect is recorded. When it is
+ * fixed — by hiding the button, or by implementing `onEdit` — this test goes
+ * red and must be rewritten to whichever was chosen.
+ */
+ test('CHARACTERISES: the Edit button renders but does nothing (adapter has edit: false)', async ({
+ page,
+ }) => {
+ await setMockBehavior('llmForcedResponses', JSON.stringify([{ content: REPLY }]));
+ const input = await openChat(page);
+ await waitForSocketConnected(page);
+
+ await clearAndType(page, input, 'hello world');
+ await page.getByTestId('send-message-button').click();
+ await expect(page.getByText(REPLY).last()).toBeVisible({ timeout: 45_000 });
+
+ const userMessage = page.locator('[data-slot="aui_user-message-root"]').last();
+ await expect(userMessage).toBeVisible();
+ await userMessage.hover();
+ await page.waitForTimeout(300);
+
+ // The affordance is offered to the user...
+ const editButton = page.locator('.aui-user-action-edit');
+ await expect(editButton).toHaveCount(1);
+ await expect(editButton.last()).toBeVisible();
+
+ // ...and does nothing when taken.
+ await editButton.last().click();
+ await page.waitForTimeout(1000);
+ await expect(
+ page.locator('.aui-edit-composer-input'),
+ 'if an edit composer opens, edit now works and this test must be rewritten'
+ ).toHaveCount(0);
+
+ // The message is unchanged and the main composer still has focus available.
+ await expect(userMessage).toContainText('hello world');
+ });
+
+ /**
+ * CONTRAST: paste is an insertion and it does NOT lose the caret.
+ *
+ * Measured 8 — exactly after the pasted "ABC" at offset 5 — after I had
+ * predicted 14 (the end) on the assumption that every insertion took the same
+ * path. It does not.
+ *
+ * Together with the backspace contrast in `chat-composer-caret.spec.ts`, this
+ * narrows the defect a long way: deletion is fine, paste is fine, and only a
+ * **typed keystroke** loses the caret. Whatever preserves the selection on
+ * Lexical's paste and delete commands is not running on its insert-text
+ * command. That is the seam to fix, and this test guards paste against
+ * regressing while it is fixed.
+ */
+ test('pasting mid-string keeps the caret after the pasted text (contrast case)', async ({
+ page,
+ }) => {
+ const input = await openChat(page);
+
+ // Put "ABC" on the clipboard by typing then cutting — no clipboard
+ // permissions needed, and it exercises the surface a user would.
+ await clearAndType(page, input, 'ABC');
+ await page.keyboard.press('ControlOrMeta+a');
+ await page.keyboard.press('ControlOrMeta+x');
+ await expect.poll(() => textOf(input), { timeout: 15_000 }).toBe('');
+
+ await page.keyboard.type('hello world');
+ await expect.poll(() => textOf(input)).toBe('hello world');
+
+ await placeCaret(page, input, 5);
+ expect(await caretOf(input)).toBe(5);
+
+ await page.keyboard.press('ControlOrMeta+v');
+ await page.waitForTimeout(500);
+
+ expect(await textOf(input)).toBe('helloABC world');
+ expect(
+ await caretOf(input),
+ 'paste keeps the caret; if this ever reads 14 the defect has spread to the paste path'
+ ).toBe(8);
+ });
+
+ test('undo after a mid-string insertion restores the previous text', async ({ page }) => {
+ const input = await openChat(page);
+ await clearAndType(page, input, 'hello world');
+
+ await placeCaret(page, input, 5);
+ await page.keyboard.type('X');
+ await expect.poll(() => textOf(input)).toBe('helloX world');
+
+ await page.keyboard.press('ControlOrMeta+z');
+ await page.waitForTimeout(400);
+
+ // Undo must remove the inserted character, not clear the composer.
+ expect(await textOf(input)).toBe('hello world');
+ });
+
+ test('redo after undo re-applies the insertion', async ({ page }) => {
+ const input = await openChat(page);
+ await clearAndType(page, input, 'hello world');
+
+ await placeCaret(page, input, 5);
+ await page.keyboard.type('X');
+ await expect.poll(() => textOf(input)).toBe('helloX world');
+
+ await page.keyboard.press('ControlOrMeta+z');
+ await expect.poll(() => textOf(input), { timeout: 10_000 }).toBe('hello world');
+
+ await page.keyboard.press('ControlOrMeta+Shift+z');
+ await page.waitForTimeout(400);
+
+ expect(await textOf(input)).toBe('helloX world');
+ });
+});