Files
open-design/apps/web/tests/components/QuestionForm.test.tsx
T
61e2b7dca3 feat(web): move discovery questions into a right-side Questions tab (#3355)
* feat(web): move discovery questions into a right-side Questions tab

Surface the agent's <question-form> in a dedicated right-hand Questions
tab (Claude-Design style) instead of inline in chat. A chat banner
focuses the tab; the form streams in and reveals one question at a time;
a 120s auto-skip countdown continues generation if the user steps away.

The reveal is keyed per form occurrence so the streaming->persisted
message swap (which unmounts and re-focuses the tab) no longer replays
the animation. Adds a tolerant streaming parser, a QuestionForm backfill
effect for progressively-arriving questions, panel/banner styles, the
questions.* i18n keys, and a deterministic reveal test.

* fix(web): keep required-field validation on QuestionForm submit

Restore the required-field gate in `ready`/`handleSubmit` so the standard
submit button and the Questions-tab Continue CTA both block on unanswered
required questions; only `skipAll()` intentionally bypasses it. Prevents
main-path forms (discovery taskType/output, ElevenLabs voice picker) from
accepting an empty submit. Adds disabled-state regression coverage.

* fix(web): close Questions tab once a discovery form is answered

The Questions tab derived its visibility from the mere presence of a
form, so answering a question left a locked duplicate mounted in the
panel even though the answered copy had already moved back into chat.
Gate both showQuestionsTab and the auto-focus hasQuestions signal on
questionFormSubmittedAnswers === undefined so the tab closes on answer.

Generated-By: looper 0.8.1 (runner=fixer, agent=claude-code)

* fix(web): add questions.* i18n keys to remaining locale files

The six questions.* keys were declared in the Dict type but only
implemented in en.ts and zh-CN.ts, breaking the web typecheck once
dependencies are installed. Add them with English fallback copy to the
other 17 locales so every Dict implementation is complete.

Generated-By: looper 0.8.1 (runner=fixer, agent=claude-code)

* fix(web): scope Questions reveal key per occurrence

Fold the hosting assistant message id into questionFormKey so a second
discovery form later in the same conversation (which reuses the
`discovery` template id) gets a distinct reveal key and animates again,
instead of mounting fully-revealed. The message id is stable across the
streaming→persisted swap, so a single occurrence still survives its
remount without replaying. Adds a regression test and refreshes the
stale canSubmit comment now that required-field validation is gated by
`ready`/`canContinue`.

Generated-By: looper 0.8.1 (runner=fixer, agent=claude-code)

* fix(web): restore inline required marker on QuestionForm fields

The Questions-tab refactor dropped the qf-required asterisk next to each
required question label. With the panel footer hidden, a mixed
required/optional form gave no per-field cue for which fields gate the
disabled Continue button. Restore the inline marker and add a regression
test covering the hidden-footer (panel) path.

Generated-By: looper 0.8.1 (runner=fixer, agent=claude-code)

---------

Co-authored-by: qiongyu1999 <2694684348@qq.com>
2026-06-03 13:35:46 +00:00

276 lines
9.1 KiB
TypeScript

// @vitest-environment jsdom
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { QuestionFormView, parseSubmittedAnswers } from '../../src/components/QuestionForm';
import type { QuestionForm } from '../../src/artifacts/question-form';
const form: QuestionForm = {
id: 'discovery',
title: 'Quick brief',
questions: [
{
id: 'tone',
label: 'Visual tone (pick up to two)',
type: 'checkbox',
options: [
{ label: 'Editorial / magazine', value: 'Editorial / magazine' },
{ label: 'Modern minimal', value: 'Modern minimal' },
{ label: 'Soft gradients', value: 'Soft gradients' },
],
maxSelections: 2,
required: true,
},
],
};
const voiceForm: QuestionForm = {
id: 'elevenlabs-voice',
title: 'Choose an ElevenLabs voice',
description:
'Pick a voice by description. The selected answer will be the exact voice_id passed to the renderer.',
questions: [
{
id: 'voice',
label: 'Voice',
type: 'select',
required: true,
placeholder: 'Choose a voice',
help: 'Select a voice description; the answer submits the matching Voice ID.',
options: [
{ label: 'Rachel — american · female', value: '21m00Tcm4TlvDq8ikWAM' },
{ label: 'Adam — american · male', value: 'pNInz6obpgDQGcFmaJgB' },
],
},
],
submitLabel: 'Use voice',
};
const richForm = {
id: 'discovery',
title: 'Quick brief',
questions: [
{
id: 'platform',
label: 'Primary surface',
type: 'radio',
required: true,
options: [
{ label: 'Responsive', value: 'Responsive' },
{
label: 'Mobile (iOS/Android)',
description: 'Phone-first app prototype',
value: 'mobile',
},
{
label: 'Desktop web',
description: 'Browser-first prototype',
value: 'Desktop web',
},
],
},
],
} as QuestionForm;
const checkboxObjectForm = {
id: 'discovery',
title: 'Quick brief',
questions: [
{
id: 'tone',
label: 'Visual tone',
type: 'checkbox',
required: true,
options: [
{ label: 'Editorial / magazine', value: 'editorial' },
{ label: 'Soft gradients', value: 'soft-gradients' },
{ label: 'Modern minimal', value: 'modern-minimal' },
],
},
],
} as QuestionForm;
const selectObjectForm = {
id: 'discovery',
title: 'Quick brief',
questions: [
{
id: 'platform',
label: 'Primary surface',
type: 'select',
required: true,
options: [
{ label: 'Mobile (iOS/Android)', value: 'mobile' },
{ label: 'Desktop web', value: 'desktop-web' },
],
},
],
} as QuestionForm;
describe('QuestionFormView', () => {
afterEach(() => cleanup());
it('updates locked answers when submitted history arrives after the initial render', () => {
const onSubmit = vi.fn();
const { container, rerender } = render(
<QuestionFormView form={form} interactive submittedAnswers={undefined} onSubmit={onSubmit} />,
);
expect(container.querySelectorAll('input[type="checkbox"]:checked')).toHaveLength(0);
rerender(
<QuestionFormView
form={form}
interactive={false}
submittedAnswers={{ tone: ['Editorial / magazine', 'Modern minimal'] }}
onSubmit={onSubmit}
/>,
);
expect(screen.getByText('answered')).toBeTruthy();
expect(container.querySelectorAll('input[type="checkbox"]:checked')).toHaveLength(2);
});
it('renders select options with labels and submits the selected voice id', () => {
const onSubmit = vi.fn();
const { container, rerender } = render(
<QuestionFormView form={voiceForm} interactive submittedAnswers={undefined} onSubmit={onSubmit} />,
);
const select = screen.getByRole('combobox') as HTMLSelectElement;
expect(container.querySelector('option[value="21m00Tcm4TlvDq8ikWAM"]')?.textContent).toBe(
'Rachel — american · female',
);
fireEvent.change(select, { target: { value: '21m00Tcm4TlvDq8ikWAM' } });
fireEvent.click(screen.getByRole('button', { name: 'Use voice' }));
expect(onSubmit).toHaveBeenCalledWith(
'[form answers — elevenlabs-voice]\n- Voice: Rachel — american · female [value: 21m00Tcm4TlvDq8ikWAM]',
{ voice: '21m00Tcm4TlvDq8ikWAM' },
);
rerender(
<QuestionFormView
form={voiceForm}
interactive={false}
submittedAnswers={{ voice: 'Rachel — american · female' }}
onSubmit={onSubmit}
/>,
);
expect((screen.getByRole('combobox') as HTMLSelectElement).value).toBe(
'21m00Tcm4TlvDq8ikWAM',
);
});
it('parses submitted object-option values from readable answer text', () => {
expect(
parseSubmittedAnswers(
richForm,
[
'[form answers - discovery]',
'- Primary surface: Mobile (iOS/Android) [value: mobile]',
].join('\n'),
),
).toEqual({ platform: 'mobile' });
});
it('renders radio object options and submits the readable label with stable value', () => {
const onSubmit = vi.fn();
render(<QuestionFormView form={richForm} interactive onSubmit={onSubmit} />);
expect(screen.getByText('Responsive')).toBeTruthy();
expect(screen.getByText('Mobile (iOS/Android)')).toBeTruthy();
expect(screen.getByText('Phone-first app prototype')).toBeTruthy();
expect(screen.getByText('Desktop web')).toBeTruthy();
fireEvent.click(screen.getByLabelText('Mobile (iOS/Android)'));
fireEvent.click(screen.getByRole('button', { name: 'Send answers' }));
expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit.mock.calls[0]?.[0]).toContain(
'- Primary surface: Mobile (iOS/Android) [value: mobile]',
);
expect(onSubmit.mock.calls[0]?.[1]).toEqual({ platform: 'mobile' });
});
it('submits required checkbox object options with stable values', () => {
const onSubmit = vi.fn();
const { container } = render(
<QuestionFormView form={checkboxObjectForm} interactive onSubmit={onSubmit} />,
);
const submit = screen.getByRole('button', { name: 'Send answers' });
// Required field unanswered → submit stays disabled (regression guard:
// the Questions-tab refactor must not make required fields optional on the
// standard submit path).
expect((submit as HTMLButtonElement).disabled).toBe(true);
fireEvent.click(screen.getByLabelText('Editorial / magazine'));
fireEvent.click(screen.getByLabelText('Soft gradients'));
expect(container.querySelectorAll('input[type="checkbox"]:checked')).toHaveLength(2);
expect((submit as HTMLButtonElement).disabled).toBe(false);
fireEvent.click(submit);
expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit.mock.calls[0]?.[0]).toContain('Editorial / magazine [value: editorial]');
expect(onSubmit.mock.calls[0]?.[0]).toContain('Soft gradients [value: soft-gradients]');
expect(onSubmit.mock.calls[0]?.[1]).toEqual({
tone: ['editorial', 'soft-gradients'],
});
});
it('marks required fields with the inline indicator even when the footer is hidden', () => {
// Panel path (Questions tab): hideInternalSubmit hides the form footer, so
// the inline "*" next to a required label is the only per-field cue that a
// field is mandatory. A mixed required/optional form must still advertise
// which fields block the disabled Continue button.
const mixedForm = {
id: 'discovery',
title: 'Quick brief',
questions: [
{ id: 'taskType', label: 'Task type', type: 'text', required: true },
{ id: 'notes', label: 'Notes', type: 'text' },
],
} as QuestionForm;
const onSubmit = vi.fn();
const { container } = render(
<QuestionFormView form={mixedForm} interactive hideInternalSubmit onSubmit={onSubmit} />,
);
const fields = container.querySelectorAll('.qf-field');
const requiredField = fields[0]!;
const optionalField = fields[1]!;
expect(requiredField.querySelector('.qf-required')?.textContent).toBe('*');
expect(optionalField.querySelector('.qf-required')).toBeNull();
});
it('submits required select object options with stable values', () => {
const onSubmit = vi.fn();
const { container } = render(
<QuestionFormView form={selectObjectForm} interactive onSubmit={onSubmit} />,
);
const submit = screen.getByRole('button', { name: 'Send answers' });
// Required select unanswered → submit stays disabled (regression guard).
expect((submit as HTMLButtonElement).disabled).toBe(true);
const select = container.querySelector('select');
if (!select) throw new Error('expected select control');
fireEvent.change(select, { target: { value: 'mobile' } });
expect((submit as HTMLButtonElement).disabled).toBe(false);
fireEvent.click(submit);
expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit.mock.calls[0]?.[0]).toContain(
'- Primary surface: Mobile (iOS/Android) [value: mobile]',
);
expect(onSubmit.mock.calls[0]?.[1]).toEqual({ platform: 'mobile' });
});
});