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
2 changes: 1 addition & 1 deletion src/cli/tui/screens/web-search/AddWebSearchScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function AddWebSearchScreen({
onExit={onExit}
helpText={helpText}
headerContent={headerContent}
exitEnabled={isNameStep}
exitEnabled={isNameStep || (isGatewayStep && noGatewaysAvailable)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pasting my own understanding of why this fixes it, since I don't think its obvious.

So before, exitEnabled was false on that page, meaning this hook was disabled. Therefore, when there were no gateways, that screen in the wizard wasn't listening for any user inputs so nothing was keeping the node event loop alive.

Relevant docs:

An Ink app is a Node.js process, so it stays alive only while there is active work in the event loop (timers, pending promises, useInput listening on stdin, etc.). If your component tree has no async work, the app will render once and exit immediately.
https://github.com/vadimdemedes/ink/blob/master/readme.md#app-lifecycle

As a long term fix (OOS here), I feel like we need to build a common Wizard abstraction that handles this once for us. Exposing exitEnabled directly here will always carry the risk that someone disables exits, and forgets to wire up another hook in its place, causing the process to exit early.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! And I agree with the suggestion as a follow up

>
<Panel>
{isNameStep && (
Expand Down
107 changes: 107 additions & 0 deletions src/cli/tui/screens/web-search/__tests__/AddWebSearchScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { AddWebSearchScreen } from '../AddWebSearchScreen';
import type { AddWebSearchConfig } from '../types';
import { render } from 'ink-testing-library';
import React from 'react';
import { afterEach, describe, expect, it, vi } from 'vitest';

const ENTER = '\r';
const ESCAPE = '\x1B';
const BACKSPACE = '\x7f';
const delay = (ms = 50) => new Promise(resolve => setTimeout(resolve, ms));

function makeProps(overrides: Partial<React.ComponentProps<typeof AddWebSearchScreen>> = {}) {
return {
onComplete: vi.fn<(config: AddWebSearchConfig) => void>(),
onExit: vi.fn(),
existingGatewayNames: [],
existingToolNames: [],
...overrides,
};
}

// Walk past the name step by accepting the default and pressing Enter.
async function submitName(stdin: ReturnType<typeof render>['stdin']) {
stdin.write(ENTER);
await delay();
}

afterEach(() => vi.restoreAllMocks());

describe('AddWebSearchScreen — Escape navigation', () => {
it('Escape on the no-gateways view calls onExit (the only step where Screen owns Esc)', async () => {
const props = makeProps({ existingGatewayNames: [] });
const { lastFrame, stdin } = render(<AddWebSearchScreen {...props} />);

await submitName(stdin);
expect(lastFrame() ?? '').toContain('No gateways found');

stdin.write(ESCAPE);
await delay();

expect(props.onExit).toHaveBeenCalledTimes(1);
});

it('Escape on the gateway step (with gateways) goes back to name, does NOT call onExit', async () => {
const props = makeProps({ existingGatewayNames: ['gw1'] });
const { lastFrame, stdin } = render(<AddWebSearchScreen {...props} />);

await submitName(stdin);
expect(lastFrame() ?? '').toContain('Attach to which gateway?');

stdin.write(ESCAPE);
await delay();

expect(props.onExit).not.toHaveBeenCalled();
// Back at the name step: the name input is mounted again.
expect(lastFrame() ?? '').toContain('Web search target name');
});

it('Escape on the exclude-domains step goes back to gateway, does NOT call onExit', async () => {
const props = makeProps({ existingGatewayNames: ['gw1'] });
const { lastFrame, stdin } = render(<AddWebSearchScreen {...props} />);

await submitName(stdin);
// Pick gw1 (single item, already selected), advance to exclude-domains.
stdin.write(ENTER);
await delay();
expect(lastFrame() ?? '').toContain('Exclude domains');

stdin.write(ESCAPE);
await delay();

expect(props.onExit).not.toHaveBeenCalled();
expect(lastFrame() ?? '').toContain('Attach to which gateway?');
});

it('Escape on the confirm step goes back to exclude-domains, does NOT call onExit', async () => {
const props = makeProps({ existingGatewayNames: ['gw1'] });
const { lastFrame, stdin } = render(<AddWebSearchScreen {...props} />);

await submitName(stdin);
stdin.write(ENTER); // pick gw1
await delay();
stdin.write(ENTER); // submit empty exclude-domains, advance to confirm
await delay();
expect(lastFrame() ?? '').toContain('Confirm');

stdin.write(ESCAPE);
await delay();

expect(props.onExit).not.toHaveBeenCalled();
expect(lastFrame() ?? '').toContain('Exclude domains');
});

it('Escape on the name step calls onExit', async () => {
const props = makeProps({ existingGatewayNames: ['gw1'] });
const { stdin } = render(<AddWebSearchScreen {...props} />);

// Clear the default value first so TextInput's onCancel fires onExit
// rather than just clearing input.
for (let i = 0; i < 30; i++) stdin.write(BACKSPACE);
await delay();
stdin.write(ESCAPE);
await delay();

expect(props.onExit).toHaveBeenCalled();
});
});
Loading