Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/cli/src/ui/opentui/commands-dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,31 @@ describe('guards (ink handleSlashCommand parity)', () => {
sentToModel: false,
});
});

it('hides only the bare /output-style invocation', async () => {
const commands = [
stub({
name: 'output-style',
action: (_context, args) =>
args
? { type: 'message', messageType: 'info', content: args }
: { type: 'dialog', dialog: 'output-style' },
}),
];

const { host: bareHost } = await dispatch('/output-style', commands);
expect(bareHost.items.some((item) => item.type === 'user')).toBe(false);

const { host: namedHost } = await dispatch(
'/output-style Concise',
commands,
);
expect(namedHost.items[0]).toMatchObject({
type: 'user',
text: '/output-style Concise',
sentToModel: false,
});
});
});

describe('shouldHideSlashCommandInvocation (slashCommandProcessor parity)', () => {
Expand Down Expand Up @@ -331,6 +356,7 @@ describe('shouldHideSlashCommandInvocation (slashCommandProcessor parity)', () =

it.each([
['effort', ''],
['output-style', ''],
['statusline', ''],
['model', ''],
['model', '--fast'],
Expand All @@ -345,6 +371,7 @@ describe('shouldHideSlashCommandInvocation (slashCommandProcessor parity)', () =
['model', 'qwen-max'],
['model', '--fast qwen3-coder-flash'],
['effort', 'high'],
['output-style', 'Concise'],
['statusline', 'show'],
])('keeps /%s %j (work-performing)', (root, args) => {
expect(shouldHideSlashCommandInvocation(cmd(root), [root], args)).toBe(
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/ui/opentui/commands-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const SLASH_COMMAND_ROOTS_HIDE_INVOCATION = new Set([
const BARE_SLASH_COMMANDS_HIDE_INVOCATION = new Set([
'effort',
'model',
'output-style',
'statusline',
]);

Expand Down
299 changes: 299 additions & 0 deletions packages/cli/src/ui/opentui/dialogs-modes.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
/** @jsxImportSource @opentui/react */
/**
* @license
* Copyright 2026 Qwen
* SPDX-License-Identifier: Apache-2.0
*/

import { act, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
BUILT_IN_OUTPUT_STYLES,
type Config,
type OutputStyleDefinition,
} from '@qwen-code/qwen-code-core';
import { SettingScope, type LoadedSettings } from '../../config/settings.js';

const mocks = vi.hoisted(() => {
const state = {
inputHandlers: [] as Array<(sequence: string) => boolean>,
keyboardHandlers: [] as Array<(key: unknown) => void>,
};
const renderer = {
addInputHandler(handler: (sequence: string) => boolean) {
state.inputHandlers.push(handler);
},
removeInputHandler(handler: (sequence: string) => boolean) {
const index = state.inputHandlers.indexOf(handler);
if (index >= 0) state.inputHandlers.splice(index, 1);
},
};
async function buildJsxRuntime() {
const React = await import('react');
const jsx = (
type: unknown,
props: { children?: unknown; key?: React.Key } | null,
key?: React.Key,
) => {
const config = key === undefined ? props : { ...props, key };
const children = (config?.children ?? null) as React.ReactNode;
if (type === 'box' || type === 'text') {
return React.createElement(
type === 'box' ? 'div' : 'span',
key === undefined ? null : { key },
children,
);
}
return React.createElement(
type as React.ElementType,
config as Record<string, unknown>,
children,
);
};
return { jsx, jsxs: jsx, jsxDEV: jsx, Fragment: React.Fragment };
}
return { state, renderer, buildJsxRuntime };
});

vi.mock('@opentui/react', () => ({
useKeyboard: (handler: (key: unknown) => void) => {
mocks.state.keyboardHandlers.push(handler);
},
useRenderer: () => mocks.renderer,
}));
vi.mock('@opentui/react/jsx-runtime', () => mocks.buildJsxRuntime());
vi.mock('@opentui/react/jsx-dev-runtime', () => mocks.buildJsxRuntime());
vi.mock('./key-map.js', () => ({
toOriginalKey: (key: { name?: string }) => ({ name: key.name ?? '' }),
}));
vi.mock('./theme.js', () => ({
C: new Proxy({}, { get: () => '#ffffff' }),
}));

import { OpenTuiOutputStyleDialog } from './dialogs-modes.js';

const CONCISE = BUILT_IN_OUTPUT_STYLES.find(
(style) => style.name === 'Concise',
);
if (!CONCISE) throw new Error('missing Concise output style');

function createHarness(
options: {
current?: OutputStyleDefinition;
systemPrompt?: string;
setValue?: ReturnType<typeof vi.fn>;
} = {},
) {
let current = options.current;
const setOutputStyle = vi.fn((style: OutputStyleDefinition | undefined) => {
current = style;
});
const refreshSystemInstruction = vi.fn().mockResolvedValue(undefined);
const setValue = options.setValue ?? vi.fn();
const config = {
getOutputStyle: () => current,
getSystemPrompt: () => options.systemPrompt,
getExperimentalZedIntegration: () => false,
getInputFormat: () => undefined,
isInteractive: () => true,
getBareMode: () => false,
isSafeMode: () => false,
setOutputStyle,
getLlmClient: () => ({ refreshSystemInstruction }),
} as unknown as Config;
const settings = {
isTrusted: true,
workspace: { settings: { general: {} } },
setValue,
} as unknown as LoadedSettings;
return {
config,
settings,
setOutputStyle,
refreshSystemInstruction,
setValue,
};
}

function press(name: string) {
const handler = mocks.state.keyboardHandlers.at(-1);
if (!handler) throw new Error('no keyboard handler registered');
act(() => handler({ name }));
}

async function pressEsc(): Promise<boolean> {
const handler = mocks.state.inputHandlers.at(-1);
if (!handler) throw new Error('no raw input handler registered');
let consumed = false;
await act(async () => {
consumed = handler('\x1b');
});
return consumed;
}

describe('OpenTuiOutputStyleDialog', () => {
beforeEach(() => {
mocks.state.inputHandlers.length = 0;
mocks.state.keyboardHandlers.length = 0;
});

afterEach(() => {
vi.unstubAllEnvs();
});

it('keeps the configured style selected while a system prompt override is active', async () => {
const harness = createHarness({
current: CONCISE,
systemPrompt: 'Replace the base prompt.',
});
const onClose = vi.fn();
const notify = vi.fn();
render(
<OpenTuiOutputStyleDialog
config={harness.config}
settings={harness.settings}
onClose={onClose}
notify={notify}
/>,
);

expect(screen.getByText('Concise').parentElement?.textContent).toContain(
'● Concise',
);
press('return');

await waitFor(() =>
expect(harness.setOutputStyle).toHaveBeenCalledWith(CONCISE),
);
expect(harness.refreshSystemInstruction).toHaveBeenCalledTimes(1);
expect(harness.setValue).toHaveBeenCalledWith(
SettingScope.User,
'general.outputStyle',
'Concise',
undefined,
{ throwOnWriteFailure: true },
);
expect(onClose).toHaveBeenCalledTimes(1);
expect(notify).toHaveBeenCalledWith(
expect.stringContaining('Output style set to Concise'),
);
});

it('moves from default to Concise and applies it on Enter', async () => {
const harness = createHarness();
render(
<OpenTuiOutputStyleDialog
config={harness.config}
settings={harness.settings}
onClose={vi.fn()}
notify={vi.fn()}
/>,
);

press('down');
press('return');

await waitFor(() =>
expect(harness.setOutputStyle).toHaveBeenCalledWith(CONCISE),
);
});

it('keeps and applies the configured style while QWEN_SYSTEM_MD is active', async () => {
vi.stubEnv('QWEN_SYSTEM_MD', '/tmp/replacement-system.md');
const harness = createHarness({ current: CONCISE });
const notify = vi.fn();
render(
<OpenTuiOutputStyleDialog
config={harness.config}
settings={harness.settings}
onClose={vi.fn()}
notify={notify}
/>,
);

expect(screen.getByText('Concise').parentElement?.textContent).toContain(
'● Concise',
);
press('return');

await waitFor(() =>
expect(harness.setOutputStyle).toHaveBeenCalledWith(CONCISE),
);
expect(harness.setValue).toHaveBeenCalledWith(
SettingScope.User,
'general.outputStyle',
'Concise',
undefined,
{ throwOnWriteFailure: true },
);
expect(notify).toHaveBeenCalledWith(
expect.stringContaining('saved but has no effect in this session'),
);
});

it('clears the configured style only after default is selected', async () => {
const harness = createHarness({ current: CONCISE });
render(
<OpenTuiOutputStyleDialog
config={harness.config}
settings={harness.settings}
onClose={vi.fn()}
notify={vi.fn()}
/>,
);

press('up');
press('return');

await waitFor(() =>
expect(harness.setOutputStyle).toHaveBeenCalledWith(undefined),
);
expect(harness.setValue).toHaveBeenCalledWith(
SettingScope.User,
'general.outputStyle',
'default',
undefined,
{ throwOnWriteFailure: true },
);
});

it('closes on Esc without changing or persisting the style', async () => {
const harness = createHarness({ current: CONCISE });
const onClose = vi.fn();
render(
<OpenTuiOutputStyleDialog
config={harness.config}
settings={harness.settings}
onClose={onClose}
notify={vi.fn()}
/>,
);

expect(await pressEsc()).toBe(true);
expect(onClose).toHaveBeenCalledTimes(1);
expect(harness.setOutputStyle).not.toHaveBeenCalled();
expect(harness.setValue).not.toHaveBeenCalled();
});

it('notifies when persistence fails', async () => {
const setValue = vi.fn(() => {
throw new Error('disk full');
});
const harness = createHarness({ current: CONCISE, setValue });
const notify = vi.fn();
render(
<OpenTuiOutputStyleDialog
config={harness.config}
settings={harness.settings}
onClose={vi.fn()}
notify={notify}
/>,
);

press('return');

await waitFor(() => expect(notify).toHaveBeenCalledWith('disk full'));
expect(harness.setOutputStyle).not.toHaveBeenCalled();
expect(harness.refreshSystemInstruction).not.toHaveBeenCalled();
});
});
3 changes: 1 addition & 2 deletions packages/cli/src/ui/opentui/dialogs-modes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
APPROVAL_MODES,
BUILT_IN_OUTPUT_STYLES,
REASONING_EFFORT_TIERS,
resolveMainSessionOutputStyle,
type ApprovalMode,
type OutputStyleDefinition,
type ReasoningEffort,
Expand Down Expand Up @@ -256,7 +255,7 @@ export function OpenTuiOutputStyleDialog(props: {
// Unlike /effort, "no style configured" genuinely is the first entry
// (default), so pre-selecting index 0 in that case tells the truth (ink
// OutputStyleDialog parity).
const current = resolveMainSessionOutputStyle(config)?.name;
const current = config.getOutputStyle()?.name;
const [sel, setSel] = useState(
Math.max(
0,
Expand Down
Loading