// The # task picker by keyboard: arrows move the selection and wrap, Enter // attaches the selected task, Escape closes the picker and leaves the // conversation open, and an IME confirmation Enter never picks. const assert = require('node:assert/strict'); const h = require('./helpers/chat-dock.cjs'); const tasks = [ { id: 1861, title: 'Task picker', status: 'running' }, { id: 1842, title: 'Retry stalled task runs', status: 'done' }, { id: 1830, title: 'Filter forge text', status: 'queued' }, ]; module.exports = async ({ page, go, shot }) => { await page.route('**/api/tasks?*', (route) => route.fulfill({ contentType: 'application/json', body: JSON.stringify(tasks) })); await h.load(page, go); await h.expand(page); const composer = h.composerOf(page); const palette = page.getByRole('listbox', { name: 'Tasks' }); const options = palette.getByRole('option'); const selected = () => options.evaluateAll((els) => els.findIndex((el) => el.getAttribute('aria-selected') === 'true')); const tags = page.getByRole('list', { name: 'Attached tasks' }); await composer.fill('Look at #'); await options.first().waitFor(); assert.equal(await options.count(), 3); assert.equal(await selected(), 0); await shot('picker'); await composer.press('ArrowDown'); assert.equal(await selected(), 1); assert.equal(await composer.getAttribute('aria-activedescendant'), 'slash-palette-option-1'); await composer.press('ArrowUp'); await composer.press('ArrowUp'); assert.equal(await selected(), 2, 'ArrowUp from the first row did not wrap to the last'); await composer.press('ArrowDown'); assert.equal(await selected(), 0, 'ArrowDown from the last row did not wrap to the first'); await composer.press('ArrowDown'); for (const event of [{ isComposing: true }, { keyCode: 229 }]) { await composer.dispatchEvent('keydown', { key: 'Enter', bubbles: true, cancelable: true, ...event }); assert.equal(await options.count(), 3, 'an IME confirmation closed the picker'); assert.equal(await tags.count(), 0, 'an IME confirmation attached a task'); } await composer.press('Enter'); await palette.waitFor({ state: 'hidden' }); await tags.getByText('#1842').waitFor(); assert.equal(await tags.getByRole('listitem').count(), 1); assert.equal(await composer.inputValue(), 'Look at ', 'the # token stayed in the text'); assert.ok(await composer.evaluate((el) => document.activeElement === el), 'focus left the composer'); await composer.fill('Look at #'); await options.first().waitFor(); await composer.press('Escape'); await palette.waitFor({ state: 'hidden' }); assert.equal(await h.hideOf(page).isVisible(), true, 'Escape on the picker also closed the conversation'); await composer.press('Escape'); await h.hideOf(page).waitFor({ state: 'hidden' }); };