-
Notifications
You must be signed in to change notification settings - Fork 51
fix: stop web-search TUI early exit when no gateway #1575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+108
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/cli/tui/screens/web-search/__tests__/AddWebSearchScreen.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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
exitEnableddirectly 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.There was a problem hiding this comment.
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