diff --git a/packages/cli/src/ui/opentui/commands-dispatch.test.ts b/packages/cli/src/ui/opentui/commands-dispatch.test.ts index 0e922c6a1a83..f8a38ef9a082 100644 --- a/packages/cli/src/ui/opentui/commands-dispatch.test.ts +++ b/packages/cli/src/ui/opentui/commands-dispatch.test.ts @@ -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)', () => { @@ -331,6 +356,7 @@ describe('shouldHideSlashCommandInvocation (slashCommandProcessor parity)', () = it.each([ ['effort', ''], + ['output-style', ''], ['statusline', ''], ['model', ''], ['model', '--fast'], @@ -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( diff --git a/packages/cli/src/ui/opentui/commands-dispatch.ts b/packages/cli/src/ui/opentui/commands-dispatch.ts index 8a7757839aa2..c1888143c6ed 100644 --- a/packages/cli/src/ui/opentui/commands-dispatch.ts +++ b/packages/cli/src/ui/opentui/commands-dispatch.ts @@ -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', ]); diff --git a/packages/cli/src/ui/opentui/dialogs-modes.test.tsx b/packages/cli/src/ui/opentui/dialogs-modes.test.tsx new file mode 100644 index 000000000000..b5ef54d9d51c --- /dev/null +++ b/packages/cli/src/ui/opentui/dialogs-modes.test.tsx @@ -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, + 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; + } = {}, +) { + 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 { + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + 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( + , + ); + + press('return'); + + await waitFor(() => expect(notify).toHaveBeenCalledWith('disk full')); + expect(harness.setOutputStyle).not.toHaveBeenCalled(); + expect(harness.refreshSystemInstruction).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/cli/src/ui/opentui/dialogs-modes.tsx b/packages/cli/src/ui/opentui/dialogs-modes.tsx index 57f0abcfc59e..18c984a062d2 100644 --- a/packages/cli/src/ui/opentui/dialogs-modes.tsx +++ b/packages/cli/src/ui/opentui/dialogs-modes.tsx @@ -19,7 +19,6 @@ import { APPROVAL_MODES, BUILT_IN_OUTPUT_STYLES, REASONING_EFFORT_TIERS, - resolveMainSessionOutputStyle, type ApprovalMode, type OutputStyleDefinition, type ReasoningEffort, @@ -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,