// The chat composer: IME confirmation never submits the draft, Shift+Enter // inserts a newline, ordinary Enter submits and clears, and # attaches a task. const assert = require('node:assert/strict'); module.exports = async ({ page, go, shot, tolerate }) => { tolerate(/api\/tasks/); await go('/events'); const composer = page.getByRole('combobox', { name: 'Message Archie', exact: true }); await composer.waitFor(); await composer.fill('Composition in progress'); const sent = []; await page.route('**/api/chat/stream', async route => { sent.push(route.request().postDataJSON().text); await route.fulfill({ contentType: 'text/event-stream', body: 'data: {"type":"done","text":"Test response"}\n\n' }); }); for (const event of [{ isComposing: true }, { keyCode: 229 }]) { await composer.dispatchEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true, ...event }); assert.equal(await composer.inputValue(), 'Composition in progress', 'IME confirmation submitted the draft'); } assert.equal(sent.length, 0); await composer.press('Shift+Enter'); assert.ok((await composer.inputValue()).includes('\n'), 'Shift+Enter no longer inserts a newline'); await composer.fill('Ordinary Enter submits'); const request = page.waitForRequest('**/api/chat/stream'); await composer.press('Enter'); assert.equal((await request).postDataJSON().text, 'Ordinary Enter submits'); assert.equal(await composer.inputValue(), ''); let failTasks = true; await page.route('**/api/tasks?*', async route => { if (failTasks) return route.fulfill({ status: 500, contentType: 'text/plain', body: 'task store down' }); await route.fulfill({ contentType: 'application/json', body: JSON.stringify([ { id: 1861, title: "Chat composer: '#' context dropdown", status: 'running' }, { id: 1842, title: 'Retry stalled task runs', status: 'done' }, ]), }); }); const tasksPalette = page.getByRole('listbox', { name: 'Tasks' }); await composer.fill('Look at #'); await tasksPalette.getByRole('alert').waitFor(); assert.equal(await tasksPalette.getByRole('option').count(), 0); failTasks = false; await tasksPalette.getByRole('button', { name: 'Retry' }).click(); await tasksPalette.getByRole('option').first().waitFor(); assert.equal(await tasksPalette.getByRole('option').count(), 2); await composer.press('Escape'); await tasksPalette.waitFor({ state: 'hidden' }); await composer.fill('Look at #184 now'); await composer.evaluate(el => el.setSelectionRange(12, 12)); await composer.dispatchEvent('input'); await tasksPalette.getByRole('option').first().waitFor(); assert.equal(await tasksPalette.getByRole('option').count(), 1); await composer.press('Tab'); await tasksPalette.waitFor({ state: 'hidden' }); assert.equal(await composer.inputValue(), 'Look at now'); await page.getByRole('list', { name: 'Attached tasks' }).getByText('#1842').waitFor(); const tagged = page.waitForRequest('**/api/chat/stream'); await composer.press('Enter'); const text = (await tagged).postDataJSON().text; assert.ok(text.startsWith('Look at now') && text.includes('#1842 Retry stalled task runs'), text); assert.equal(await page.getByRole('list', { name: 'Attached tasks' }).count(), 0); await shot('chat-composition'); };