Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
import React from 'react';
import { RecoilRoot } from 'recoil';
import { fireEvent, render, screen } from '@testing-library/react';
import type { Agents } from 'librechat-data-provider';
import { ChatSurfaceProvider } from '~/components/Chat/Subagents/surface';
import { ASK_USER_DECLINED_ANSWER } from '~/utils/approval';
import AskUserQuestions from '../AskUserQuestions';
const mockSubmitAskAnswer = jest.fn();
let mockStatus = 'idle';
jest.mock('~/components/Chat/Messages/Content/ApprovalContext', () => ({
useAskSubmitStatus: () => ({ getAskStatus: () => mockStatus }),
useResumeSubmit: () => ({ submitAskAnswer: mockSubmitAskAnswer }),
}));
jest.mock('~/hooks', () => ({
useLocalize: () => (key: string, values?: Record<number, number>) => {
const labels: Record<string, string> = {
com_ui_question_number: `Question ${values?.[0] ?? ''}`,
com_ui_question_step: `Question ${values?.[0] ?? ''} of ${values?.[1] ?? ''}`,
com_ui_question_step_answered: `Go to question ${values?.[0] ?? ''}, answered`,
com_ui_question_step_unanswered: `Go to question ${values?.[0] ?? ''}, not answered`,
com_ui_question_navigation: 'Question navigation',
com_ui_questions_remaining_one: `${values?.[0] ?? ''} question still needs an answer`,
com_ui_questions_remaining: `${values?.[0] ?? ''} questions still need an answer`,
com_ui_your_answer: 'Your answer',
com_ui_back: 'Back',
com_ui_next: 'Next',
com_ui_skip: 'Skip',
com_ui_submit: 'Submit',
com_ui_submitting: 'Submitting',
};
return labels[key] ?? key;
},
}));
const questions: Agents.AskUserQuestionBatchItem[] = [
{
id: 'environment',
header: 'Environment',
question: 'Where should this run?',
options: [
{ label: 'Staging', value: 'staging' },
{ label: 'Production', value: 'production' },
],
},
{ id: 'window', question: 'Which time window?' },
];
const renderBatch = (
actionId: string,
batch: Agents.AskUserQuestionBatchItem[] = questions,
focusOnArrival = true,
) =>
render(
<RecoilRoot>
<AskUserQuestions actionId={actionId} questions={batch} focusOnArrival={focusOnArrival} />
</RecoilRoot>,
);
const optionBatch: Agents.AskUserQuestionBatchItem[] = [
{ id: 'window', question: 'Which time window?' },
{
id: 'environment',
question: 'Where should this run?',
options: [
{ label: 'Staging', value: 'staging' },
{ label: 'Production', value: 'production' },
],
},
{
id: 'region',
question: 'Which region?',
options: [
{ label: 'us-east', value: 'us-east' },
{ label: 'eu-west', value: 'eu-west' },
],
},
];
/** Only the active step renders. */
const isShown = (text: string) => screen.queryByText(text) != null;
describe('AskUserQuestions', () => {
beforeEach(() => {
mockStatus = 'idle';
mockSubmitAskAnswer.mockClear();
});
test('shows one question at a time and walks the batch with Next/Back', () => {
renderBatch('ask-steps');
expect(isShown('Where should this run?')).toBe(true);
expect(isShown('Which time window?')).toBe(false);
expect(screen.getByText('Question 1 of 2')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Submit' })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect(isShown('Which time window?')).toBe(true);
expect(isShown('Where should this run?')).toBe(false);
expect(screen.getByText('Question 2 of 2')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Next' })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Back' }));
expect(isShown('Where should this run?')).toBe(true);
expect(screen.getByRole('button', { name: 'Back' })).toBeDisabled();
});
test('advances automatically when a single-select choice is picked', () => {
renderBatch('ask-advance');
fireEvent.click(screen.getByRole('button', { name: /Staging/ }));
expect(isShown('Which time window?')).toBe(true);
expect(screen.getByText('Question 2 of 2')).toBeInTheDocument();
});
test('does not auto-advance a multi-select question', () => {
renderBatch('ask-multi', [
{
id: 'regions',
question: 'Which regions?',
multiSelect: true,
options: [
{ label: 'us-east', value: 'us-east' },
{ label: 'eu-west', value: 'eu-west' },
],
},
{ id: 'window', question: 'Which time window?' },
]);
fireEvent.click(screen.getByRole('checkbox', { name: /us-east/ }));
expect(isShown('Which regions?')).toBe(true);
expect(screen.getByText('Question 1 of 2')).toBeInTheDocument();
});
test('marks the chosen single-select answer with a check', () => {
renderBatch('ask-check', [
{
id: 'tests',
question: 'Should I add tests?',
options: [
{ label: 'Yes', value: 'yes' },
{ label: 'No', value: 'no' },
],
},
]);
const yes = screen.getByRole('button', { name: /Yes/ });
expect(yes).toHaveTextContent('1');
fireEvent.click(yes);
expect(yes).toHaveAttribute('aria-pressed', 'true');
expect(yes).not.toHaveTextContent('1');
expect(yes.querySelector('svg')).not.toBeNull();
});
test('clears and locks the choices once an answer is typed', () => {
renderBatch('ask-typed', [
{
id: 'regions',
question: 'Which regions?',
multiSelect: true,
options: [
{ label: 'us-east', value: 'us-east' },
{ label: 'eu-west', value: 'eu-west' },
],
},
]);
const usEast = screen.getByRole('checkbox', { name: /us-east/ });
fireEvent.click(usEast);
expect(usEast).toHaveAttribute('aria-checked', 'true');
const field = screen.getByRole('textbox', { name: /Which regions/ });
fireEvent.change(field, { target: { value: 'ap-south' } });
expect(usEast).toHaveAttribute('aria-checked', 'false');
expect(usEast).toBeDisabled();
fireEvent.change(field, { target: { value: '' } });
expect(usEast).toBeEnabled();
});
test('submits one answer map after every question is complete', () => {
renderBatch('ask-batch');
fireEvent.click(screen.getByRole('button', { name: /Staging/ }));
fireEvent.change(screen.getByRole('textbox', { name: /Which time window/ }), {
target: { value: 'Last seven days' },
});
const submit = screen.getByRole('button', { name: 'Submit' });
expect(submit).toBeEnabled();
fireEvent.click(submit);
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-batch',
{
environment: 'staging',
window: 'Last seven days',
},
expect.objectContaining({ onSuccess: expect.any(Function) }),
);
});
test('keeps Submit gated on the last step until every question is answered', () => {
renderBatch('ask-gated');
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect(screen.getByRole('button', { name: 'Submit' })).toBeDisabled();
expect(
screen.getByRole('button', { name: '2 questions still need an answer' }),
).toBeInTheDocument();
fireEvent.change(screen.getByRole('textbox', { name: /Which time window/ }), {
target: { value: 'Today' },
});
expect(screen.getByRole('button', { name: 'Submit' })).toBeDisabled();
fireEvent.click(screen.getByRole('button', { name: '1 question still needs an answer' }));
expect(isShown('Where should this run?')).toBe(true);
fireEvent.click(screen.getByRole('button', { name: /Staging/ }));
expect(screen.getByRole('button', { name: 'Submit' })).toBeEnabled();
});
test('jumps to any question from the step dots and marks answered ones', () => {
renderBatch('ask-dots');
fireEvent.click(screen.getByRole('button', { name: 'Go to question 2, not answered' }));
expect(isShown('Which time window?')).toBe(true);
fireEvent.change(screen.getByRole('textbox', { name: /Which time window/ }), {
target: { value: 'Today' },
});
expect(screen.getByRole('button', { name: 'Go to question 2, answered' })).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Go to question 1, not answered' }));
expect(isShown('Where should this run?')).toBe(true);
});
test('Enter in an answer field confirms the answer instead of submitting a surrounding form', () => {
const onFormSubmit = jest.fn((event: React.FormEvent) => event.preventDefault());
render(
<RecoilRoot>
<form onSubmit={onFormSubmit}>
<AskUserQuestions actionId="ask-enter" questions={questions} />
<button type="submit" aria-label="Send composer" />
</form>
</RecoilRoot>,
);
const first = screen.getByRole('textbox', { name: /Where should this run/ });
fireEvent.change(first, { target: { value: 'Locally' } });
fireEvent.keyDown(first, { key: 'Enter' });
expect(isShown('Which time window?')).toBe(true);
const last = screen.getByRole('textbox', { name: /Which time window/ });
fireEvent.change(last, { target: { value: 'Today' } });
fireEvent.keyDown(last, { key: 'Enter' });
expect(onFormSubmit).not.toHaveBeenCalled();
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-enter',
{ environment: 'Locally', window: 'Today' },
expect.anything(),
);
});
test('Enter that confirms an IME composition neither advances nor submits', () => {
renderBatch('ask-ime');
const first = screen.getByRole('textbox', { name: /Where should this run/ });
fireEvent.change(first, { target: { value: '本番' } });
fireEvent.keyDown(first, { key: 'Enter', keyCode: 229 });
expect(isShown('Where should this run?')).toBe(true);
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
});
test('Enter moves focus into the next question, ready to answer', () => {
renderBatch('ask-enter-focus');
const first = screen.getByRole('textbox', { name: /Where should this run/ });
fireEvent.change(first, { target: { value: 'Locally' } });
fireEvent.keyDown(first, { key: 'Enter' });
expect(isShown('Which time window?')).toBe(true);
expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveFocus();
});
test('Ctrl/Cmd+Enter confirm like Enter; Shift+Enter never does', () => {
renderBatch('ask-modifiers');
const first = screen.getByRole('textbox', { name: /Where should this run/ });
fireEvent.change(first, { target: { value: 'Locally' } });
fireEvent.keyDown(first, { key: 'Enter', shiftKey: true });
expect(isShown('Where should this run?')).toBe(true);
fireEvent.keyDown(first, { key: 'Enter', ctrlKey: true });
expect(isShown('Which time window?')).toBe(true);
const last = screen.getByRole('textbox', { name: /Which time window/ });
fireEvent.change(last, { target: { value: 'Today' } });
fireEvent.keyDown(last, { key: 'Enter', metaKey: true });
expect(mockSubmitAskAnswer).toHaveBeenCalledTimes(1);
});
test('modified or repeated Enter in an answer field keeps the enclosing form from submitting', () => {
const onSubmit = jest.fn((event: React.FormEvent) => event.preventDefault());
render(
<RecoilRoot>
<form onSubmit={onSubmit}>
<AskUserQuestions actionId="ask-implicit-submit" questions={questions} />
</form>
</RecoilRoot>,
);
const first = screen.getByRole('textbox', { name: /Where should this run/ });
fireEvent.change(first, { target: { value: 'Locally' } });
for (const init of [{ shiftKey: true }, { altKey: true }, { repeat: true }]) {
/* fireEvent returns false when the default (implicit submit) was prevented. */
expect(fireEvent.keyDown(first, { key: 'Enter', ...init })).toBe(false);
}
expect(isShown('Where should this run?')).toBe(true);
expect(onSubmit).not.toHaveBeenCalled();
});
test('a submit shortcut rebound off Enter confirms the answer like Enter', () => {
render(
<RecoilRoot>
<ChatSurfaceProvider
value={{
enterToSend: false,
maximizeChatSpace: false,
showScrollButton: false,
composerBindings: {
shortcutsEnabled: true,
submitOverride: { meta: false, ctrl: true, alt: false, shift: false, key: 'J' },
yieldedChords: new Set(),
},
claimForeground: jest.fn(),
handOffComposerText: jest.fn(),
}}
>
<AskUserQuestions actionId="ask-rebound-submit" questions={questions} />
</ChatSurfaceProvider>
</RecoilRoot>,
);
const first = screen.getByRole('textbox', { name: /Where should this run/ });
fireEvent.change(first, { target: { value: 'Locally' } });
fireEvent.keyDown(first, { key: 'j', ctrlKey: true });
expect(isShown('Which time window?')).toBe(true);
const last = screen.getByRole('textbox', { name: /Which time window/ });
fireEvent.change(last, { target: { value: 'Today' } });
fireEvent.keyDown(last, { key: 'j', ctrlKey: true });
expect(mockSubmitAskAnswer).toHaveBeenCalledTimes(1);
});
describe('focus on arrival', () => {
afterEach(() => {
document.getElementById('prompt-textarea')?.remove();
});
test('lands on the first option when the question offers choices', () => {
renderBatch('ask-focus-option');
expect(screen.getByRole('button', { name: /Staging/ })).toHaveFocus();
});
test('lands in the answer field when the question has no choices', () => {
renderBatch('ask-focus-field', [{ id: 'window', question: 'Which time window?' }]);
expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveFocus();
});
test('takes focus from the empty composer that sent the turn', () => {
const composer = document.createElement('textarea');
composer.id = 'prompt-textarea';
document.body.appendChild(composer);
composer.focus();
renderBatch('ask-focus-composer');
expect(screen.getByRole('button', { name: /Staging/ })).toHaveFocus();
});
test('stays put on a surface that is not the live pause', () => {
renderBatch('ask-focus-not-live', questions, false);
expect(document.body).toHaveFocus();
});
test('happens once per batch, not again when it moves between surfaces', () => {
const view = renderBatch('ask-focus-once');
expect(screen.getByRole('button', { name: /Staging/ })).toHaveFocus();
view.unmount();
renderBatch('ask-focus-once');
expect(document.body).toHaveFocus();
});
test('leaves focus alone while the user is drafting in the composer', () => {
const composer = document.createElement('textarea');
composer.id = 'prompt-textarea';
composer.value = 'half a thought';
document.body.appendChild(composer);
composer.focus();
renderBatch('ask-focus-draft');
expect(composer).toHaveFocus();
});
});
describe('option keyboard', () => {
/** A keyboard Enter on a button is its click; jsdom does not synthesize
* that, so the click fires only when the handler leaves Enter alone. */
const pressEnter = (option: HTMLElement) => {
if (fireEvent.keyDown(option, { key: 'Enter' })) {
fireEvent.click(option);
}
};
test('Enter on an option mid-batch picks it and moves to the next question', () => {
renderBatch('ask-option-enter');
pressEnter(screen.getByRole('button', { name: /Production/ }));
expect(isShown('Which time window?')).toBe(true);
expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveFocus();
});
test('on the last question, Enter picks an option and a second Enter submits', () => {
renderBatch('ask-option-submit', [
{ id: 'window', question: 'Which time window?' },
{
id: 'environment',
question: 'Where should this run?',
options: [
{ label: 'Staging', value: 'staging' },
{ label: 'Production', value: 'production' },
],
},
]);
fireEvent.change(screen.getByRole('textbox', { name: /Which time window/ }), {
target: { value: 'Today' },
});
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
const staging = screen.getByRole('button', { name: /Staging/ });
pressEnter(staging);
expect(staging).toHaveAttribute('aria-pressed', 'true');
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
pressEnter(staging);
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-option-submit',
{ window: 'Today', environment: 'staging' },
expect.objectContaining({ onSuccess: expect.any(Function) }),
);
});
test('a held Enter that picks an option on the last question does not also submit', () => {
renderBatch('ask-option-held-last', [
{
id: 'environment',
question: 'Where should this run?',
options: [
{ label: 'Staging', value: 'staging' },
{ label: 'Production', value: 'production' },
],
},
]);
const staging = screen.getByRole('button', { name: /Staging/ });
pressEnter(staging);
expect(staging).toHaveAttribute('aria-pressed', 'true');
fireEvent.keyDown(staging, { key: 'Enter', repeat: true });
fireEvent.keyDown(staging, { key: 'Enter', repeat: true });
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
pressEnter(staging);
expect(mockSubmitAskAnswer).toHaveBeenCalledTimes(1);
});
test('a held or double-tapped Enter from the answer field never picks the next options', () => {
renderBatch('ask-option-held', optionBatch);
const first = screen.getByRole('textbox', { name: /Which time window/ });
fireEvent.change(first, { target: { value: 'Today' } });
fireEvent.keyDown(first, { key: 'Enter' });
expect(isShown('Where should this run?')).toBe(true);
/* Focus waits in the answer field, not on an option, so the repeats
reach a field: they can page on, but never pick an option for the user. */
expect(screen.getByRole('textbox', { name: /Where should this run/ })).toHaveFocus();
for (let i = 0; i < 3; i++) {
pressEnter(document.activeElement as HTMLElement);
}
expect(screen.queryByRole('button', { pressed: true })).not.toBeInTheDocument();
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
});
test('Enter on a multi-select option toggles it and never submits', () => {
renderBatch('ask-option-multi', [
{
id: 'regions',
question: 'Which regions?',
multiSelect: true,
options: [
{ label: 'us-east', value: 'us-east' },
{ label: 'eu-west', value: 'eu-west' },
],
},
]);
const usEast = screen.getByRole('checkbox', { name: /us-east/ });
fireEvent.click(usEast);
pressEnter(usEast);
pressEnter(screen.getByRole('checkbox', { name: /eu-west/ }));
expect(usEast).toHaveAttribute('aria-checked', 'false');
expect(screen.getByRole('checkbox', { name: /eu-west/ })).toHaveAttribute(
'aria-checked',
'true',
);
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
});
});
test('retains partial answers and the current step across surface remounts', () => {
const view = renderBatch('ask-remount');
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
fireEvent.change(screen.getByRole('textbox', { name: /Which time window/ }), {
target: { value: 'Today' },
});
view.rerender(
<RecoilRoot>
<AskUserQuestions actionId="ask-remount" questions={questions} />
</RecoilRoot>,
);
expect(screen.getByText('Question 2 of 2')).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /Which time window/ })).toHaveValue('Today');
});
test('leaves a single-question batch free of stepper chrome', () => {
renderBatch('ask-single', [
{ id: 'confirmation', question: 'Continue?', options: [{ label: 'Yes', value: 'yes' }] },
]);
expect(screen.queryByRole('button', { name: 'Next' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Back' })).not.toBeInTheDocument();
expect(screen.queryByRole('group', { name: 'Question navigation' })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: /Yes/ }));
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-single',
{ confirmation: 'yes' },
expect.objectContaining({ onSuccess: expect.any(Function) }),
);
});
test('supports question ids inherited by ordinary objects', () => {
renderBatch('ask-prototype-id', [
{
id: 'constructor',
question: 'Continue?',
options: [{ label: 'Yes', value: 'yes' }],
},
]);
fireEvent.click(screen.getByRole('button', { name: /Yes/ }));
fireEvent.click(screen.getByRole('button', { name: 'Submit' }));
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-prototype-id',
{ constructor: 'yes' },
expect.objectContaining({ onSuccess: expect.any(Function) }),
);
});
test('keeps an expired batch readable but freezes steps mid-submit', () => {
mockStatus = 'expired';
const view = renderBatch('ask-expired');
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
expect(isShown('Which time window?')).toBe(true);
mockStatus = 'submitting';
view.rerender(
<RecoilRoot>
<AskUserQuestions actionId="ask-expired" questions={questions} />
</RecoilRoot>,
);
expect(screen.getByRole('button', { name: 'Back' })).toBeDisabled();
});
test('skips the whole batch from any step', () => {
renderBatch('ask-skip');
fireEvent.click(screen.getByRole('button', { name: 'Next' }));
fireEvent.click(screen.getByRole('button', { name: 'Skip' }));
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-skip',
{ environment: ASK_USER_DECLINED_ANSWER, window: ASK_USER_DECLINED_ANSWER },
expect.objectContaining({ onSuccess: expect.any(Function) }),
);
});
describe('Escape', () => {
test('skips the batch when nothing is answered yet', () => {
renderBatch('ask-escape-skip');
fireEvent.keyDown(screen.getByRole('button', { name: /Staging/ }), { key: 'Escape' });
expect(mockSubmitAskAnswer).toHaveBeenCalledWith(
'ask-escape-skip',
{ environment: ASK_USER_DECLINED_ANSWER, window: ASK_USER_DECLINED_ANSWER },
expect.objectContaining({ onSuccess: expect.any(Function) }),
);
});
test('keeps partial answers instead of skipping', () => {
renderBatch('ask-escape-partial');
fireEvent.click(screen.getByRole('button', { name: /Staging/ }));
fireEvent.keyDown(screen.getByRole('textbox', { name: /Your answer/ }), { key: 'Escape' });
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
});
test('ignores Escape that cancels an IME composition or carries a modifier', () => {
renderBatch('ask-escape-ime', [{ id: 'window', question: 'Which time window?' }]);
const field = screen.getByRole('textbox', { name: /Your answer/ });
fireEvent.keyDown(field, { key: 'Escape', isComposing: true });
fireEvent.keyDown(field, { key: 'Escape', shiftKey: true });
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
});
test('hands Escape to the surface instead of skipping when it supplies one', () => {
const onEscape = jest.fn();
render(
<RecoilRoot>
<AskUserQuestions
actionId="ask-escape-surface"
questions={questions}
onEscape={onEscape}
/>
</RecoilRoot>,
);
fireEvent.keyDown(screen.getByRole('button', { name: /Staging/ }), { key: 'Escape' });
expect(onEscape).toHaveBeenCalledTimes(1);
expect(mockSubmitAskAnswer).not.toHaveBeenCalled();
});
});
});