Skip to content
317 changes: 316 additions & 1 deletion packages/cli/src/ui/components/InputPrompt.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { renderWithProviders } from '../../test-utils/render.js';
import { waitFor, act } from '@testing-library/react';
import type { InputPromptProps } from './InputPrompt.js';
import { InputPrompt } from './InputPrompt.js';
import type { TextBuffer } from './shared/text-buffer.js';
import { useTextBuffer, type TextBuffer } from './shared/text-buffer.js';
import type { Config } from '@qwen-code/qwen-code-core';
import { ApprovalMode } from '@qwen-code/qwen-code-core';
import * as path from 'node:path';
Expand Down Expand Up @@ -742,6 +742,321 @@ describe('InputPrompt', () => {
unmount();
});

it('should submit a perfect match on Enter when suggestions were not navigated', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'html', value: 'html' },
{ label: 'md', value: 'md' },
{ label: 'json', value: 'json' },
{ label: 'jsonl', value: 'jsonl' },
],
activeSuggestionIndex: 0,
isPerfectMatch: true,
});
props.buffer.setText('/export');

const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();

stdin.write('\r');
await wait();

expect(props.onSubmit).toHaveBeenCalledWith('/export');
expect(mockCommandCompletion.handleAutocomplete).not.toHaveBeenCalled();
unmount();
});

it('should fill and submit an export format selected with arrow navigation', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'html', value: 'html' },
{ label: 'md', value: 'md' },
{ label: 'json', value: 'json' },
{ label: 'jsonl', value: 'jsonl' },
],
activeSuggestionIndex: 0,
isPerfectMatch: true,
});
props.buffer.setText('/export');

const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();

stdin.write('\u001B[B');
await wait();

expect(props.buffer.setText).toHaveBeenLastCalledWith('/export md');
expect(mockCommandCompletion.handleAutocomplete).not.toHaveBeenCalled();
expect(props.onSubmit).not.toHaveBeenCalled();

stdin.write('\r');
await wait();

expect(props.onSubmit).toHaveBeenCalledWith('/export md');
unmount();
});

it('should keep cycling export formats after arrow navigation fills input', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'html', value: 'html' },
{ label: 'md', value: 'md' },
{ label: 'json', value: 'json' },
{ label: 'jsonl', value: 'jsonl' },
],
activeSuggestionIndex: 0,
isPerfectMatch: true,
});
props.buffer.setText('/export');

const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();

stdin.write('\u001B[B');
await wait();
stdin.write('\u001B[B');
await wait();

expect(props.buffer.setText).toHaveBeenNthCalledWith(2, '/export md');
expect(props.buffer.setText).toHaveBeenNthCalledWith(3, '/export json');
expect(mockInputHistory.navigateDown).not.toHaveBeenCalled();
unmount();
});

it('should keep export format suggestions visible after arrow navigation fills input', async () => {
const exportSuggestions = [
{ label: 'html', value: 'html' },
{ label: 'md', value: 'md' },
{ label: 'json', value: 'json' },
{ label: 'jsonl', value: 'jsonl' },
];
mockedUseCommandCompletion.mockImplementation((buffer) => {
const isExportRoot = buffer.text.trim() === '/export';
return {
...mockCommandCompletion,
showSuggestions: isExportRoot,
suggestions: isExportRoot ? exportSuggestions : [],
activeSuggestionIndex: 0,
isPerfectMatch: isExportRoot,
};
});
props.slashCommands = [
...mockSlashCommands,
{
name: 'export',
kind: CommandKind.BUILT_IN,
description: 'Export session',
action: vi.fn(),
subCommands: [
{
name: 'html',
kind: CommandKind.BUILT_IN,
description: 'Export HTML',
action: vi.fn(),
},
{
name: 'md',
kind: CommandKind.BUILT_IN,
description: 'Export Markdown',
action: vi.fn(),
},
{
name: 'json',
kind: CommandKind.BUILT_IN,
description: 'Export JSON',
action: vi.fn(),
},
{
name: 'jsonl',
kind: CommandKind.BUILT_IN,
description: 'Export JSONL',
action: vi.fn(),
},
],
},
];
const TestHarness = () => {
const buffer = useTextBuffer({
initialText: '/export',
viewport: { width: 80, height: 20 },
isValidPath: () => false,
onChange: () => {},
});
return <InputPrompt {...props} buffer={buffer} />;
};

const { stdin, lastFrame, unmount } = renderWithProviders(<TestHarness />);
await wait();

stdin.write('\u001B[B');
await wait();

const output = stripAnsi(lastFrame() ?? '');
expect(output).toContain('/export md');
expect(output).toContain('html');
expect(output).toContain('md');
expect(output).toContain('json');
expect(output).toContain('jsonl');
expect(output).toContain('Export Markdown');
unmount();
});

it('should not clobber manually edited buffer when arrow is pressed after export fill', async () => {
// Regression for PR #3701 review: exportCompletionSelectionIndexRef
// leaked across buffer edits, so arrow keys would overwrite user-typed
// text after the user moved away from an "/export <fmt>" input.
mockedUseCommandCompletion.mockImplementation((buffer) => {
const isExportRoot = buffer.text.trim() === '/export';
return {
...mockCommandCompletion,
showSuggestions: isExportRoot,
suggestions: isExportRoot
? [
{ label: 'html', value: 'html' },
{ label: 'md', value: 'md' },
{ label: 'json', value: 'json' },
{ label: 'jsonl', value: 'jsonl' },
]
: [],
activeSuggestionIndex: 0,
isPerfectMatch: isExportRoot,
};
});
props.slashCommands = [
...mockSlashCommands,
{
name: 'export',
kind: CommandKind.BUILT_IN,
description: 'Export session',
action: vi.fn(),
subCommands: [
{
name: 'html',
kind: CommandKind.BUILT_IN,
description: 'Export HTML',
action: vi.fn(),
},
{
name: 'md',
kind: CommandKind.BUILT_IN,
description: 'Export Markdown',
action: vi.fn(),
},
{
name: 'json',
kind: CommandKind.BUILT_IN,
description: 'Export JSON',
action: vi.fn(),
},
{
name: 'jsonl',
kind: CommandKind.BUILT_IN,
description: 'Export JSONL',
action: vi.fn(),
},
],
},
];

const TestHarness = () => {
const buffer = useTextBuffer({
initialText: '/export',
viewport: { width: 80, height: 20 },
isValidPath: () => false,
onChange: () => {},
});
return <InputPrompt {...props} buffer={buffer} />;
};

const { stdin, lastFrame, unmount } = renderWithProviders(<TestHarness />);
await wait();

// Phase 1 + 2: Down fills "/export md".
stdin.write('\u001B[B');
await wait();
expect(stripAnsi(lastFrame() ?? '')).toContain('/export md');

// User clears buffer and types a different command manually.
stdin.write('\u0015'); // Ctrl+U: clear line
await wait();
stdin.write('/help');
await wait();
const afterEditFrame = stripAnsi(lastFrame() ?? '');
expect(afterEditFrame).toContain('/help');

// Pressing Down now must NOT overwrite "/help" with an export format.
stdin.write('\u001B[B');
await wait();
const afterArrowFrame = stripAnsi(lastFrame() ?? '');
expect(afterArrowFrame).toContain('/help');
expect(afterArrowFrame).not.toMatch(/\/export\s+(html|md|json|jsonl)/);
unmount();
});

it('should autocomplete on Enter when user arrow-navigated a perfect-match suggestion list', async () => {
// Regression for PR #3701 review: the isPerfectMatch + navigated + Enter
// branch was not covered by tests.
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'show', value: 'show' },
{ label: 'add', value: 'add' },
{ label: 'refresh', value: 'refresh' },
],
activeSuggestionIndex: 0,
isPerfectMatch: true,
});
props.buffer.setText('/memory');

const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();

// Arrow-navigate so completionSelectionWasNavigatedRef flips to true.
stdin.write('\u001B[B');
await wait();
expect(mockCommandCompletion.navigateDown).toHaveBeenCalled();

// Enter should autocomplete the active suggestion, NOT submit the raw buffer.
stdin.write('\r');
await wait();

expect(mockCommandCompletion.handleAutocomplete).toHaveBeenCalledWith(0);
expect(props.onSubmit).not.toHaveBeenCalled();
unmount();
});

it('should submit directly on Enter for a perfect match without prior arrow navigation', async () => {
// Control: with no arrow navigation, Enter on a perfect match must submit.
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
showSuggestions: true,
suggestions: [
{ label: 'show', value: 'show' },
{ label: 'add', value: 'add' },
],
activeSuggestionIndex: 0,
isPerfectMatch: true,
});
props.buffer.setText('/memory');

const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();

stdin.write('\r');
await wait();

expect(props.onSubmit).toHaveBeenCalledWith('/memory');
expect(mockCommandCompletion.handleAutocomplete).not.toHaveBeenCalled();
unmount();
});

it('should reset history navigation after submitting on Enter', async () => {
mockedUseCommandCompletion.mockReturnValue({
...mockCommandCompletion,
Expand Down
Loading
Loading