diff --git a/src/tui/components/ImportPreviewDialog.tsx b/src/tui/components/ImportPreviewDialog.tsx index 33f8f8a0c..a1fdc70aa 100644 --- a/src/tui/components/ImportPreviewDialog.tsx +++ b/src/tui/components/ImportPreviewDialog.tsx @@ -1,6 +1,7 @@ import { Box, - Text + Text, + useInput } from 'ink'; import React, { useState } from 'react'; @@ -176,6 +177,18 @@ export function ImportPreviewDialog({ { label: 'Cancel', value: 'cancel' } ]; + // Escape steps back one screen: from the confirmation to the preview, then out + useInput((_, key) => { + if (!key.escape) { + return; + } + if (pendingMode) { + setPendingMode(null); + return; + } + onCancel(); + }); + function handleSelect(value: ImportMode | 'back'): void { if (value === 'cancel' || value === 'back') { onCancel(); @@ -228,6 +241,10 @@ export function ImportPreviewDialog({ } } + // The confirmation above unmounts the List, so it comes back on the mode + // the preview was showing rather than on the first row + const previewModeIndex = previewMode === 'merge' ? 1 : 0; + const diffRows: React.JSX.Element[] = []; for (const key of topLevelKeys) { @@ -315,6 +332,7 @@ export function ImportPreviewDialog({ items={items} onSelect={handleSelect} onSelectionChange={handleSelectionChange} + initialSelection={previewModeIndex} /> ); diff --git a/src/tui/components/InstallMenu.tsx b/src/tui/components/InstallMenu.tsx index 419bf5e52..75c63cce0 100644 --- a/src/tui/components/InstallMenu.tsx +++ b/src/tui/components/InstallMenu.tsx @@ -193,7 +193,8 @@ export const InstallMenu: React.FC = ({ setUpdateStyle(value); setStep('manager'); }} - initialSelection={0} + // Going back from the package manager step mounts this List again + initialSelection={updateStyle === 'auto-update' ? 1 : 0} showBackButton={true} /> diff --git a/src/tui/components/RefreshIntervalMenu.tsx b/src/tui/components/RefreshIntervalMenu.tsx index 48d221093..f1e497f27 100644 --- a/src/tui/components/RefreshIntervalMenu.tsx +++ b/src/tui/components/RefreshIntervalMenu.tsx @@ -173,6 +173,8 @@ export const RefreshIntervalMenu: React.FC = ({ const [refreshInput, setRefreshInput] = useState(() => getRefreshInputValue(currentInterval)); const [ttlInput, setTtlInput] = useState(() => String(gitCacheTtlSeconds)); const [validationError, setValidationError] = useState(null); + // A prompt unmounts the List, so it comes back on the row that opened it + const [selectedItem, setSelectedItem] = useState(0); const ttlFields: Record = { gitCacheTtl: { @@ -346,6 +348,10 @@ export const RefreshIntervalMenu: React.FC = ({ setTtlInput(String(ttlFields[value].currentValue)); setEditingTtlField(value); }} + onSelectionChange={(_, index) => { + setSelectedItem(index); + }} + initialSelection={selectedItem} showBackButton={true} /> )} diff --git a/src/tui/components/__tests__/ImportPreviewDialog.test.ts b/src/tui/components/__tests__/ImportPreviewDialog.test.ts index ba8888ca6..84cdeadb2 100644 --- a/src/tui/components/__tests__/ImportPreviewDialog.test.ts +++ b/src/tui/components/__tests__/ImportPreviewDialog.test.ts @@ -67,6 +67,25 @@ function createMockStdout(): CapturedWriteStream { }); } +// An import that adds a shell command and leaves globalBold out, so Replace All +// resets globalBold and Merge keeps it +const COMMAND_IMPORT_CURRENT: Settings = { ...DEFAULT_SETTINGS, globalBold: true }; +const COMMAND_IMPORT_DATA: Settings = { + ...DEFAULT_SETTINGS, + colorLevel: 3, + lines: [[{ id: 'c', type: 'custom-command', commandPath: 'echo hi' }], [], []] +}; +const COMMAND_IMPORT_VALIDATION = { + status: 'valid' as const, + data: COMMAND_IMPORT_DATA, + presentKeys: ['version', 'colorLevel', 'lines'] as (keyof Settings)[] +}; + +// The globalBold row of the last frame drawn +function lastGlobalBoldRow(output: string): string | undefined { + return output.slice(output.lastIndexOf('globalBold:')).split('\n')[0]; +} + describe('ImportPreviewDialog helpers', () => { it('includes optional settings that exist only in the imported config', () => { const current: Settings = { ...DEFAULT_SETTINGS }; @@ -276,4 +295,169 @@ describe('ImportPreviewDialog helpers', () => { stderr.destroy(); } }); + + it('cancels the import when Escape is pressed on the preview', async () => { + const stdin = createMockStdin(); + const stdout = createMockStdout(); + const stderr = createMockStdout(); + const onApply = vi.fn(); + const onCancel = vi.fn(); + const instance = render(React.createElement(ImportPreviewDialog, { + validation: { + status: 'valid', + data: { ...DEFAULT_SETTINGS, globalBold: true }, + presentKeys: ['version', 'globalBold'] + }, + currentSettings: DEFAULT_SETTINGS, + onApply, + onCancel + }), { + stdin, + stdout, + stderr, + debug: true, + exitOnCtrlC: false, + patchConsole: false + }); + + try { + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Replace All'); + }); + stdin.write('\u001B'); + await waitFor(() => { + expect(onCancel).toHaveBeenCalledTimes(1); + }); + expect(onApply).not.toHaveBeenCalled(); + } finally { + instance.unmount(); + instance.cleanup(); + stdin.destroy(); + stdout.destroy(); + stderr.destroy(); + } + }); + + it('goes back from the shell-command confirmation to the preview on Escape, still on Merge', async () => { + const stdin = createMockStdin(); + const stdout = createMockStdout(); + const stderr = createMockStdout(); + const onApply = vi.fn(); + const onCancel = vi.fn(); + const instance = render(React.createElement(ImportPreviewDialog, { + validation: COMMAND_IMPORT_VALIDATION, + currentSettings: COMMAND_IMPORT_CURRENT, + onApply, + onCancel + }), { + stdin, + stdout, + stderr, + debug: true, + exitOnCtrlC: false, + patchConsole: false + }); + + try { + await waitFor(() => { + expect(lastGlobalBoldRow(stdout.getOutput())).toBe('globalBold: true → false'); + }); + stdin.write('\u001B[B'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Merge'); + expect(lastGlobalBoldRow(stdout.getOutput())).toBe('globalBold: true'); + }); + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Cancel'); + expect(stdout.getOutput()).toContain('Apply and run these commands'); + }); + + stdout.clearOutput(); + stdin.write('\u001B'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('Changes that will be applied:'); + expect(stdout.getOutput()).toContain('▶ Merge'); + expect(lastGlobalBoldRow(stdout.getOutput())).toBe('globalBold: true'); + }); + expect(stdout.getOutput()).not.toContain('▶ Replace All'); + expect(onCancel).not.toHaveBeenCalled(); + + stdin.write('\u001B'); + await waitFor(() => { + expect(onCancel).toHaveBeenCalledTimes(1); + }); + expect(onApply).not.toHaveBeenCalled(); + } finally { + instance.unmount(); + instance.cleanup(); + stdin.destroy(); + stdout.destroy(); + stderr.destroy(); + } + }); + + it('keeps Merge highlighted after Cancel on the shell-command confirmation', async () => { + const stdin = createMockStdin(); + const stdout = createMockStdout(); + const stderr = createMockStdout(); + const onApply = vi.fn(); + const instance = render(React.createElement(ImportPreviewDialog, { + validation: COMMAND_IMPORT_VALIDATION, + currentSettings: COMMAND_IMPORT_CURRENT, + onApply, + onCancel: () => undefined + }), { + stdin, + stdout, + stderr, + debug: true, + exitOnCtrlC: false, + patchConsole: false + }); + + try { + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Replace All'); + }); + stdin.write('\u001B[B'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Merge'); + }); + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Cancel'); + expect(stdout.getOutput()).toContain('Apply and run these commands'); + }); + + stdout.clearOutput(); + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Merge'); + expect(lastGlobalBoldRow(stdout.getOutput())).toBe('globalBold: true'); + }); + expect(stdout.getOutput()).not.toContain('▶ Replace All'); + + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Cancel'); + expect(stdout.getOutput()).toContain('Apply and run these commands'); + }); + stdin.write('\u001B[B'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Apply and run these commands'); + }); + stdin.write('\r'); + await waitFor(() => { + expect(onApply).toHaveBeenCalledWith('merge'); + }); + expect(onApply).toHaveBeenCalledTimes(1); + } finally { + instance.unmount(); + instance.cleanup(); + stdin.destroy(); + stdout.destroy(); + stderr.destroy(); + } + }); }); diff --git a/src/tui/components/__tests__/InstallMenu.test.tsx b/src/tui/components/__tests__/InstallMenu.test.tsx index 339e76f71..272e6253b 100644 --- a/src/tui/components/__tests__/InstallMenu.test.tsx +++ b/src/tui/components/__tests__/InstallMenu.test.tsx @@ -280,4 +280,75 @@ describe('InstallMenu', () => { stderr.destroy(); } }); + + it('keeps Auto-update highlighted when going back from package manager selection', async () => { + const stdin = createMockStdin(); + const stdout = createMockStdout(); + const stderr = createMockStdout(); + const onCancel = vi.fn(); + const instance = render( + React.createElement(InstallMenu, { + commandAvailability: ALL_AVAILABLE, + currentVersion: '2.2.13', + existingStatusLine: null, + onSelect: vi.fn(), + onCancel + }), + { + stdin, + stdout, + stderr, + debug: true, + exitOnCtrlC: false, + patchConsole: false + } + ); + + try { + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Pinned global install'); + }); + stdin.write('\u001B[B'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ Auto-update'); + }); + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ npx -y ccstatusline@latest'); + }); + + // Escape + stdout.clearOutput(); + stdin.write('\u001B'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('Select update style'); + expect(stdout.getOutput()).toContain('▶ Auto-update'); + }); + expect(stdout.getOutput()).not.toContain('▶ Pinned global install'); + + // The '← Back' row + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ npx -y ccstatusline@latest'); + }); + stdin.write('\u001B[A'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('▶ ← Back'); + }); + stdout.clearOutput(); + stdin.write('\r'); + await waitFor(() => { + expect(stdout.getOutput()).toContain('Select update style'); + expect(stdout.getOutput()).toContain('▶ Auto-update'); + }); + expect(stdout.getOutput()).not.toContain('▶ Pinned global install'); + expect(onCancel).not.toHaveBeenCalled(); + } finally { + instance.unmount(); + instance.cleanup(); + stdin.destroy(); + stdout.destroy(); + stderr.destroy(); + } + }); }); diff --git a/src/tui/components/__tests__/RefreshIntervalMenu.test.ts b/src/tui/components/__tests__/RefreshIntervalMenu.test.ts index ce24c708c..a3f8e4303 100644 --- a/src/tui/components/__tests__/RefreshIntervalMenu.test.ts +++ b/src/tui/components/__tests__/RefreshIntervalMenu.test.ts @@ -434,4 +434,58 @@ describe('RefreshIntervalMenu', () => { stderr.destroy(); } }); + + it('keeps the cancelled setting highlighted after Escape closes its prompt', async () => { + const stdin = createMockStdin(); + const stdout = createMockStdout(); + const stderr = createMockStdout(); + const onUpdate = vi.fn(); + const onCustomCommandCacheTtlUpdate = vi.fn(); + const onBack = vi.fn(); + const instance = render( + React.createElement(RefreshIntervalMenu, { + currentInterval: 10, + supportsRefreshInterval: true, + gitCacheTtlSeconds: 5, + customCommandCacheTtlSeconds: 7, + terminalWidthCacheTtlSeconds: 9, + onUpdate, + onGitCacheTtlUpdate: vi.fn(), + onCustomCommandCacheTtlUpdate, + onTerminalWidthCacheTtlUpdate: vi.fn(), + onBack + }), + { + stdin, + stdout, + stderr, + debug: true, + exitOnCtrlC: false, + patchConsole: false + } + ); + + try { + await waitFor(() => { + expect(stdout.getOutput()).toMatch(selected('Refresh Interval')); + }); + await press(stdin, stdout, '\u001B[B', selected('Git Cache TTL')); + await press(stdin, stdout, '\u001B[B', selected('Custom Command Cache TTL')); + await press(stdin, stdout, '\r', 'Enter custom command cache TTL in seconds (0-60):'); + + await press(stdin, stdout, '\u001B', selected('Custom Command Cache TTL')); + expect(stdout.getOutput()).not.toMatch(selected('Refresh Interval')); + + await press(stdin, stdout, '\r', 'Enter custom command cache TTL in seconds (0-60):'); + expect(onUpdate).not.toHaveBeenCalled(); + expect(onCustomCommandCacheTtlUpdate).not.toHaveBeenCalled(); + expect(onBack).not.toHaveBeenCalled(); + } finally { + instance.unmount(); + instance.cleanup(); + stdin.destroy(); + stdout.destroy(); + stderr.destroy(); + } + }); });