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
20 changes: 19 additions & 1 deletion src/tui/components/ImportPreviewDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Box,
Text
Text,
useInput
} from 'ink';
import React, { useState } from 'react';

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -315,6 +332,7 @@ export function ImportPreviewDialog({
items={items}
onSelect={handleSelect}
onSelectionChange={handleSelectionChange}
initialSelection={previewModeIndex}
/>
</Box>
);
Expand Down
3 changes: 2 additions & 1 deletion src/tui/components/InstallMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ export const InstallMenu: React.FC<InstallMenuProps> = ({
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}
/>
</>
Expand Down
6 changes: 6 additions & 0 deletions src/tui/components/RefreshIntervalMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ export const RefreshIntervalMenu: React.FC<RefreshIntervalMenuProps> = ({
const [refreshInput, setRefreshInput] = useState(() => getRefreshInputValue(currentInterval));
const [ttlInput, setTtlInput] = useState(() => String(gitCacheTtlSeconds));
const [validationError, setValidationError] = useState<string | null>(null);
// A prompt unmounts the List, so it comes back on the row that opened it
const [selectedItem, setSelectedItem] = useState(0);

const ttlFields: Record<TtlField, TtlFieldConfig> = {
gitCacheTtl: {
Expand Down Expand Up @@ -346,6 +348,10 @@ export const RefreshIntervalMenu: React.FC<RefreshIntervalMenuProps> = ({
setTtlInput(String(ttlFields[value].currentValue));
setEditingTtlField(value);
}}
onSelectionChange={(_, index) => {
setSelectedItem(index);
}}
initialSelection={selectedItem}
showBackButton={true}
/>
)}
Expand Down
184 changes: 184 additions & 0 deletions src/tui/components/__tests__/ImportPreviewDialog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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();
}
});
});
71 changes: 71 additions & 0 deletions src/tui/components/__tests__/InstallMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
});
Loading
Loading