fix: Exit app on pressing esc on trust dialog at launch (#10668)

This commit is contained in:
shrutip90
2025-10-14 07:27:40 -07:00
committed by GitHub
parent 249a193c00
commit b2ba67f337
2 changed files with 25 additions and 20 deletions
@@ -7,7 +7,7 @@
import { renderWithProviders } from '../../test-utils/render.js'; import { renderWithProviders } from '../../test-utils/render.js';
import { waitFor } from '@testing-library/react'; import { waitFor } from '@testing-library/react';
import { vi } from 'vitest'; import { vi } from 'vitest';
import { FolderTrustDialog, FolderTrustChoice } from './FolderTrustDialog.js'; import { FolderTrustDialog } from './FolderTrustDialog.js';
import * as processUtils from '../../utils/processUtils.js'; import * as processUtils from '../../utils/processUtils.js';
vi.mock('../../utils/processUtils.js', () => ({ vi.mock('../../utils/processUtils.js', () => ({
@@ -44,30 +44,23 @@ describe('FolderTrustDialog', () => {
); );
}); });
it('should call onSelect with DO_NOT_TRUST when escape is pressed and not restarting', async () => { it('should display exit message and call process.exit and not call onSelect when escape is pressed', async () => {
const onSelect = vi.fn(); const onSelect = vi.fn();
const { stdin } = renderWithProviders( const { lastFrame, stdin } = renderWithProviders(
<FolderTrustDialog onSelect={onSelect} isRestarting={false} />, <FolderTrustDialog onSelect={onSelect} isRestarting={false} />,
); );
stdin.write('\x1b'); // escape key stdin.write('\x1b'); // escape key
await waitFor(() => { await waitFor(() => {
expect(onSelect).toHaveBeenCalledWith(FolderTrustChoice.DO_NOT_TRUST); expect(lastFrame()).toContain(
'A folder trust level must be selected to continue. Exiting since escape was pressed.',
);
}); });
});
it('should not call onSelect when escape is pressed and is restarting', async () => {
const onSelect = vi.fn();
const { stdin } = renderWithProviders(
<FolderTrustDialog onSelect={onSelect} isRestarting={true} />,
);
stdin.write('\x1b'); // escape key
await waitFor(() => { await waitFor(() => {
expect(onSelect).not.toHaveBeenCalled(); expect(mockedExit).toHaveBeenCalledWith(1);
}); });
expect(onSelect).not.toHaveBeenCalled();
}); });
it('should display restart message when isRestarting is true', () => { it('should display restart message when isRestarting is true', () => {
@@ -84,7 +77,7 @@ describe('FolderTrustDialog', () => {
renderWithProviders( renderWithProviders(
<FolderTrustDialog onSelect={vi.fn()} isRestarting={true} />, <FolderTrustDialog onSelect={vi.fn()} isRestarting={true} />,
); );
await vi.advanceTimersByTimeAsync(1000); await vi.advanceTimersByTimeAsync(250);
expect(relaunchApp).toHaveBeenCalled(); expect(relaunchApp).toHaveBeenCalled();
vi.useRealTimers(); vi.useRealTimers();
}); });
@@ -6,7 +6,7 @@
import { Box, Text } from 'ink'; import { Box, Text } from 'ink';
import type React from 'react'; import type React from 'react';
import { useEffect } from 'react'; import { useEffect, useState } from 'react';
import { theme } from '../semantic-colors.js'; import { theme } from '../semantic-colors.js';
import type { RadioSelectItem } from './shared/RadioButtonSelect.js'; import type { RadioSelectItem } from './shared/RadioButtonSelect.js';
import { RadioButtonSelect } from './shared/RadioButtonSelect.js'; import { RadioButtonSelect } from './shared/RadioButtonSelect.js';
@@ -30,6 +30,7 @@ export const FolderTrustDialog: React.FC<FolderTrustDialogProps> = ({
onSelect, onSelect,
isRestarting, isRestarting,
}) => { }) => {
const [exiting, setExiting] = useState(false);
useEffect(() => { useEffect(() => {
const doRelaunch = async () => { const doRelaunch = async () => {
if (isRestarting) { if (isRestarting) {
@@ -44,7 +45,10 @@ export const FolderTrustDialog: React.FC<FolderTrustDialogProps> = ({
useKeypress( useKeypress(
(key) => { (key) => {
if (key.name === 'escape') { if (key.name === 'escape') {
onSelect(FolderTrustChoice.DO_NOT_TRUST); setExiting(true);
setTimeout(() => {
process.exit(1);
}, 100);
} }
}, },
{ isActive: !isRestarting }, { isActive: !isRestarting },
@@ -65,9 +69,9 @@ export const FolderTrustDialog: React.FC<FolderTrustDialogProps> = ({
key: `Trust parent folder (${parentFolder})`, key: `Trust parent folder (${parentFolder})`,
}, },
{ {
label: "Don't trust (esc)", label: "Don't trust",
value: FolderTrustChoice.DO_NOT_TRUST, value: FolderTrustChoice.DO_NOT_TRUST,
key: "Don't trust (esc)", key: "Don't trust",
}, },
]; ];
@@ -105,6 +109,14 @@ export const FolderTrustDialog: React.FC<FolderTrustDialogProps> = ({
</Text> </Text>
</Box> </Box>
)} )}
{exiting && (
<Box marginLeft={1} marginTop={1}>
<Text color={theme.status.warning}>
A folder trust level must be selected to continue. Exiting since
escape was pressed.
</Text>
</Box>
)}
</Box> </Box>
); );
}; };