LibreChat/e2e/specs/mock/scenarios/ask-keyboard.spec.ts
Marco Beretta 2bf6ca847a
❓ refactor: Move the Ask-User Question Cards Into @librechat/chat (#18161)
- refactor: Move Ask User Question Cards Into the Chat Package
- fix: Preserve Ask Card Styling Without Suppression Growth
- fix: Provide Live Ask Pauses As Scoped Context Data
- test: Cover the Settled Ask Record Collapse Cascade
- fix: Give the Ask Call Spec Host the Tool Call Chrome
2026-11-14 19:11:57 +01:00

294 lines
11 KiB
TypeScript

import { expect, test } from '@playwright/test';
import type { Page, Request } from '@playwright/test';
import type { AgentDetail } from '../agents.helpers';
import { cleanupAgent, openAgentBuilder, uniqueAgentName } from '../agents.helpers';
import {
MOCK_ENDPOINTS,
NEW_CHAT_PATH,
escapeRegExp,
getAccessToken,
messagesView,
requestJson,
sendMessage,
mockReply,
} from '../helpers';
/**
* Keyboard control of a live `ask_user_question` batch. The fake model
* (e2e/setup/fake-model.js) pauses a real agent run with three questions for
* `E2E_ASK_USER_QUESTIONS:`, each with two options; popover, card, form state
* and resume request are the production ones.
*/
test.use({ viewport: { width: 1280, height: 800 }, isMobile: false, hasTouch: false });
type AskResumeBody = { actionId?: string; answers?: Record<string, string> };
const ASK_TOOL_ID = 'ask_user_question';
const MOVE_TO_CHAT = 'Answer later in the chat';
const MOVE_TO_COMPOSER = 'Answer from the message box';
const DECLINED = 'The user chose not to answer this question.';
const isResumeRequest = (request: Request) =>
request.method() === 'POST' && new URL(request.url()).pathname === '/api/agents/chat/resume';
const questionsFor = (label: string) => ({
environment: `Which environment for ${label}?`,
region: `Which region for ${label}?`,
notes: `Anything else for ${label}?`,
});
async function createAgent(page: Page): Promise<AgentDetail> {
await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
const token = await getAccessToken(page);
const agent = await requestJson<AgentDetail>(page, {
path: '/api/agents',
token,
method: 'POST',
body: {
name: uniqueAgentName('E2E Ask Keyboard Agent'),
description: 'Pauses at ask_user_question for the keyboard scenarios.',
instructions: 'Ask the user the question the test requests.',
provider: MOCK_ENDPOINTS[0].label,
model: MOCK_ENDPOINTS[0].model,
tools: [ASK_TOOL_ID],
},
});
expect(agent.tools).toEqual(expect.arrayContaining([ASK_TOOL_ID]));
return agent;
}
async function openBatch(page: Page, agent: AgentDetail, label: string) {
const form = await openAgentBuilder(page);
await form.getByRole('combobox', { name: 'Agent', exact: true }).click();
await page.getByRole('option', { name: agent.name }).click();
await expect(form.getByLabel('Agent name')).toHaveValue(agent.name ?? '');
await form.getByRole('button', { name: 'Select Agent' }).click();
const response = await sendMessage(page, `E2E_ASK_USER_QUESTIONS:${label}`);
expect(response.ok()).toBeTruthy();
await expect(page).toHaveURL(/\/c\/(?!new)/, { timeout: 15000 });
/* The batch arrives in the chat, never over the composer. */
const chat = messagesView(page);
await expect(chat.getByRole('button', { name: MOVE_TO_COMPOSER })).toBeVisible({
timeout: 30000,
});
await expect(popoverOf(page)).toHaveCount(0);
return chat;
}
const popoverOf = (page: Page) =>
page.locator('.popover').filter({ has: page.getByRole('button', { name: MOVE_TO_CHAT }) });
/** Moves the live batch from its chat card into the composer popover. */
async function openPopover(page: Page) {
await messagesView(page).getByRole('button', { name: MOVE_TO_COMPOSER }).click();
const popover = popoverOf(page);
await expect(popover).toBeVisible();
return popover;
}
type Scope = Page | ReturnType<Page['locator']>;
const answerField = (scope: Scope, question: string) =>
scope.getByRole('textbox', { name: new RegExp(`^${escapeRegExp(question)}`) });
const optionRow = (scope: Scope, name: string) =>
scope.getByRole('button', { name: new RegExp(`${escapeRegExp(name)}$`) });
const uniqueLabel = () => `${Date.now()}-${Math.floor(Math.random() * 1e4)}`;
test.describe('ask question keyboard control', () => {
test('a batch is answered and submitted from the keyboard alone @scenario:ask-batch-completes-from-keyboard', async ({
page,
}) => {
test.setTimeout(120000);
const label = uniqueLabel();
const questions = questionsFor(label);
let agentId: string | undefined;
try {
const agent = await createAgent(page);
agentId = agent.id;
const card = await openBatch(page, agent, label);
/* The batch arrives while focus sits in the empty composer that sent it. */
await expect(optionRow(card, 'Staging')).toBeFocused();
await page.keyboard.press('Enter');
/* The next question has no pick yet, so focus lands in its answer field,
never on an unpicked option a held Enter could choose. */
await expect(card.getByText(questions.region, { exact: true })).toBeVisible();
await expect(answerField(card, questions.region)).toBeFocused();
await page.keyboard.type('typed-region');
await page.keyboard.press('Enter');
await expect(card.getByText(questions.notes, { exact: true })).toBeVisible();
await expect(answerField(card, questions.notes)).toBeFocused();
await page.keyboard.press('Shift+Tab');
await expect(optionRow(card, 'Call me first')).toBeFocused();
const resumes: Request[] = [];
page.on('request', (request) => {
if (isResumeRequest(request)) {
resumes.push(request);
}
});
/* On the last question the first Enter only picks. */
await page.keyboard.press('Enter');
await expect(optionRow(card, 'Call me first')).toHaveAttribute('aria-pressed', 'true');
expect(resumes).toHaveLength(0);
const [resume] = await Promise.all([
page.waitForRequest(isResumeRequest),
page.keyboard.press('Enter'),
]);
expect((resume.postDataJSON() as AskResumeBody).answers).toEqual({
environment: 'staging',
region: 'typed-region',
notes: 'call',
});
await expect(card.getByRole('button', { name: MOVE_TO_COMPOSER })).toBeHidden({
timeout: 30000,
});
} finally {
await cleanupAgent(page, agentId);
}
});
test('Escape in the composer popover moves the batch to the chat and returns to the composer @scenario:ask-popover-escape-moves-to-chat', async ({
page,
}) => {
test.setTimeout(120000);
const label = uniqueLabel();
const questions = questionsFor(label);
let agentId: string | undefined;
try {
const agent = await createAgent(page);
agentId = agent.id;
await openBatch(page, agent, label);
const popover = await openPopover(page);
const resumes: string[] = [];
page.on('request', (request) => {
if (isResumeRequest(request)) {
resumes.push(request.url());
}
});
/* Moving the batch in from the card hands focus to its first option. */
await expect(optionRow(popover, 'Staging')).toBeFocused();
await page.keyboard.press('Escape');
await expect(popover).toBeHidden();
const chat = messagesView(page);
await expect(chat.getByText(questions.environment, { exact: true })).toBeVisible();
await expect(chat.getByRole('button', { name: MOVE_TO_COMPOSER })).toBeVisible();
const composer = page.getByRole('textbox', { name: 'Message input' });
await expect(composer).toBeEnabled();
await expect(composer).toBeFocused();
expect(resumes).toEqual([]);
} finally {
await cleanupAgent(page, agentId);
}
});
test('Escape in the chat card skips the batch only while nothing is answered @scenario:ask-card-escape-skips-only-when-empty', async ({
page,
}) => {
test.setTimeout(120000);
const label = uniqueLabel();
const questions = questionsFor(label);
let agentId: string | undefined;
try {
const agent = await createAgent(page);
agentId = agent.id;
const chat = await openBatch(page, agent, label);
const field = answerField(chat, questions.environment);
await expect(field).toBeVisible();
const resumes: Request[] = [];
page.on('request', (request) => {
if (isResumeRequest(request)) {
resumes.push(request);
}
});
/* A typed answer is kept: Escape neither clears it nor resumes the run. */
await field.fill('half-typed');
await field.press('Escape');
await expect(field).toHaveValue('half-typed');
await expect(chat.getByRole('button', { name: 'Skip', exact: true })).toBeEnabled();
await field.fill('');
const [resume] = await Promise.all([
page.waitForRequest(isResumeRequest),
field.press('Escape'),
]);
expect((resume.postDataJSON() as AskResumeBody).answers).toEqual({
environment: DECLINED,
region: DECLINED,
notes: DECLINED,
});
expect(resumes).toHaveLength(1);
} finally {
await cleanupAgent(page, agentId);
}
});
test('a settled batch is one record line whose answers open on demand @scenario:ask-settled-record-opens-on-demand', async ({
page,
}) => {
test.setTimeout(120000);
const label = uniqueLabel();
let agentId: string | undefined;
try {
const agent = await createAgent(page);
agentId = agent.id;
const chat = await openBatch(page, agent, label);
const field = answerField(chat, questionsFor(label).environment);
await expect(field).toBeVisible();
await Promise.all([page.waitForRequest(isResumeRequest), field.press('Escape')]);
/* The resumed turn regroups the message's parts as it streams, so a click before it ends
can land on a card that is then replaced. Open the record once the turn is over. */
await expect(mockReply(page)).toBeVisible({ timeout: 30000 });
await expect(page.getByTestId('stop-generation-button')).toBeHidden({ timeout: 30000 });
const record = chat.getByTestId('ask-user-question-call');
const toggle = record.getByRole('button');
await expect(toggle).toBeVisible();
/* The collapse rule lives in the shared stylesheet, which the app's own sheet follows in
the bundle. Read what the cascade resolved rather than which rule was authored. */
const panel = chat.locator('.disclosure-collapse');
const resolved = () =>
panel.evaluate((node) => {
const style = getComputedStyle(node);
return { display: style.display, opacity: style.opacity, rows: style.gridTemplateRows };
});
await expect(toggle).toHaveAttribute('aria-expanded', 'false');
await expect.poll(resolved).toEqual({ display: 'grid', opacity: '0', rows: '0px' });
await toggle.click();
await expect(toggle).toHaveAttribute('aria-expanded', 'true');
await expect
.poll(async () => {
const { display, opacity, rows } = await resolved();
return { display, opacity, open: rows !== '0px' && rows !== 'none' };
})
.toEqual({ display: 'grid', opacity: '1', open: true });
await toggle.click();
await expect(toggle).toHaveAttribute('aria-expanded', 'false');
await expect.poll(resolved).toEqual({ display: 'grid', opacity: '0', rows: '0px' });
} finally {
await cleanupAgent(page, agentId);
}
});
});