eigent/test/unit/components/ChatBox/BottomBox/InputBox.test.tsx
Douglas Lai 07c602aabf
Some checks failed
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Pre-commit / pre-commit (push) Has been cancelled
Test / Run Web + Local Brain Smoke (push) Has been cancelled
Test / Run Frontend Guardrails (push) Has been cancelled
Test / Run Browser Preview Electron Test (push) Has been cancelled
Test / Run Python Tests (push) Has been cancelled
feat: redesign the composer's context menu, agents, onboarding and alignment (#2197)
2026-11-13 19:04:50 +00:00

199 lines
6.2 KiB
TypeScript

// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. =========
import { Inputbox } from '@/components/ChatBox/BottomBox/InputBox';
import { fireEvent, render } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
vi.mock('@/components/ChatBox/BottomBox/RichChatInput', async () => {
const { forwardRef } = await import('react');
return {
RichChatInput: forwardRef<
HTMLDivElement,
{
value?: string;
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
}
>(({ value, onKeyDown }, ref) => (
<div ref={ref} data-testid="rich-chat-input" onKeyDown={onKeyDown}>
{value}
</div>
)),
};
});
function primaryAction(container: HTMLElement) {
return container.querySelector<HTMLButtonElement>(
'[data-composer-primary-action]'
)!;
}
describe('Inputbox primary action', () => {
it('distinguishes adding a follow-up to the queue from sending', () => {
const { container, rerender } = render(
<Inputbox value="Follow up" queuesFollowUp />
);
expect(primaryAction(container)).toHaveAttribute(
'aria-label',
'Add to queue'
);
rerender(<Inputbox value="First request" />);
expect(primaryAction(container)).toHaveAttribute('aria-label', 'Send');
});
it('rotates the default arrow upward when text becomes sendable', () => {
const onSend = vi.fn();
const { container, rerender } = render(
<Inputbox value="" onSend={onSend} />
);
expect(primaryAction(container)).toHaveAttribute(
'data-composer-primary-action',
'idle'
);
expect(primaryAction(container)).toBeDisabled();
expect(
container.querySelector('[data-composer-primary-icon="arrow"]')
).toHaveClass('rotate-0', 'motion-reduce:transition-none');
rerender(<Inputbox value="Queue this next" onSend={onSend} />);
expect(primaryAction(container)).toHaveAttribute(
'data-composer-primary-action',
'send'
);
expect(primaryAction(container)).not.toBeDisabled();
expect(
container.querySelector('[data-composer-primary-icon="arrow"]')
).toHaveClass('-rotate-90');
fireEvent.click(primaryAction(container));
expect(onSend).toHaveBeenCalledOnce();
});
it('uses pause and resume while an empty composer controls a live task', () => {
const onPauseTask = vi.fn();
const onResumeTask = vi.fn();
const { container, rerender } = render(
<Inputbox
taskControlState="running"
onPauseTask={onPauseTask}
onResumeTask={onResumeTask}
/>
);
expect(primaryAction(container)).toHaveAttribute(
'data-composer-primary-action',
'pause'
);
fireEvent.click(primaryAction(container));
expect(onPauseTask).toHaveBeenCalledOnce();
rerender(
<Inputbox
taskControlState="paused"
onPauseTask={onPauseTask}
onResumeTask={onResumeTask}
/>
);
expect(primaryAction(container)).toHaveAttribute(
'data-composer-primary-action',
'resume'
);
expect(primaryAction(container)).toHaveAttribute('aria-label', 'Continue');
fireEvent.click(primaryAction(container));
expect(onResumeTask).toHaveBeenCalledOnce();
});
it('prioritizes sending text or files over pause and resume', () => {
const onSend = vi.fn();
const onPauseTask = vi.fn();
const { container, rerender } = render(
<Inputbox
value="A follow-up"
taskControlState="running"
onPauseTask={onPauseTask}
onSend={onSend}
/>
);
expect(primaryAction(container)).toHaveAttribute(
'data-composer-primary-action',
'send'
);
fireEvent.click(primaryAction(container));
expect(onSend).toHaveBeenCalledOnce();
expect(onPauseTask).not.toHaveBeenCalled();
rerender(
<Inputbox
files={[{ fileName: 'brief.pdf', filePath: '/tmp/brief.pdf' }]}
taskControlState="running"
onPauseTask={onPauseTask}
onSend={onSend}
/>
);
expect(primaryAction(container)).toHaveAttribute(
'data-composer-primary-action',
'send'
);
fireEvent.click(primaryAction(container));
expect(onSend).toHaveBeenCalledTimes(2);
expect(onPauseTask).not.toHaveBeenCalled();
});
it('sends a running-task follow-up with Return but not Shift+Return', () => {
const onSend = vi.fn();
const { getByTestId } = render(
<Inputbox
value="A follow-up"
taskControlState="running"
onSend={onSend}
/>
);
fireEvent.keyDown(getByTestId('rich-chat-input'), {
key: 'Enter',
shiftKey: true,
});
expect(onSend).not.toHaveBeenCalled();
fireEvent.keyDown(getByTestId('rich-chat-input'), { key: 'Enter' });
expect(onSend).toHaveBeenCalledOnce();
});
});
describe('Inputbox edge state', () => {
it('keeps a disabled composer neutral while preserving and restoring its draft', () => {
const { container, rerender } = render(
<Inputbox value="A saved follow-up" />
);
const surface = () =>
container.querySelector('[data-bottom-box-input-surface]');
expect(surface()).toHaveClass('ring-ds-border-information-default-default');
rerender(<Inputbox value="A saved follow-up" disabled />);
expect(surface()).toHaveClass('ring-ds-hairline-subtle-default');
expect(surface()).not.toHaveClass(
'ring-ds-border-information-default-default'
);
expect(
container.querySelector('[data-testid="rich-chat-input"]')
).toHaveTextContent('A saved follow-up');
rerender(<Inputbox value="A saved follow-up" />);
expect(surface()).toHaveClass('ring-ds-border-information-default-default');
});
});