-
Notifications
You must be signed in to change notification settings - Fork 3
fix(test): replace fixed-delay waits with polling in MainConfig walkthrough tests #126
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
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,6 +31,17 @@ const down = '\u001B[B'; | |||||||||
| // const left = '\u001B[D'; | ||||||||||
| const enter = '\r'; | ||||||||||
|
|
||||||||||
| // Ink re-renders asynchronously after stdin writes; polling for the expected | ||||||||||
| // frame avoids the flakiness of a fixed-duration sleep under CI load. | ||||||||||
| const waitForFrame = (lastFrame: () => string | undefined, text: string) => | ||||||||||
| vi.waitFor(() => expect(lastFrame()).toContain(text)); | ||||||||||
|
|
||||||||||
| // ApiKeyStep renders input as a password field, masking typed characters as | ||||||||||
| // `*`. Wait for the masked value before submitting so the keystrokes have | ||||||||||
| // actually reached component state. | ||||||||||
| const waitForMaskedInput = (lastFrame: () => string | undefined, text: string) => | ||||||||||
| vi.waitFor(() => expect(lastFrame()).toContain('*'.repeat(text.length))); | ||||||||||
|
Comment on lines
+42
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly to
Suggested change
|
||||||||||
|
|
||||||||||
| describe('MainConfig', () => { | ||||||||||
| const mockExit = vi.fn(); | ||||||||||
|
|
||||||||||
|
|
@@ -74,35 +85,30 @@ describe('MainConfig', () => { | |||||||||
| const onComplete = vi.fn(); | ||||||||||
| const { lastFrame, stdin } = render(<MainConfig onComplete={onComplete} />); | ||||||||||
|
|
||||||||||
| const simulateKey = async (char: string) => { | ||||||||||
| stdin.write(char); | ||||||||||
| // Wait for re-render | ||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| // 1. ProviderStep - Choose OpenAI (default) | ||||||||||
| expect(lastFrame()).toContain('What model provider would you like to use today?'); | ||||||||||
| await simulateKey(enter); | ||||||||||
| await waitForFrame(lastFrame, 'What model provider would you like to use today?'); | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 2. ApiKeyStep | ||||||||||
| expect(lastFrame()).toContain('Can you provide us with your OpenAI API key?'); | ||||||||||
| await simulateKey('sk-test-key'); | ||||||||||
| await simulateKey(enter); | ||||||||||
| await waitForFrame(lastFrame, 'Can you provide us with your OpenAI API key?'); | ||||||||||
| stdin.write('sk-test-key'); | ||||||||||
| await waitForMaskedInput(lastFrame, 'sk-test-key'); | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 3. ModelSelectionStep - Model | ||||||||||
| expect(lastFrame()).toContain('What model would you like to use?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model would you like to use?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 4. ModelSelectionStep - Compactor | ||||||||||
| expect(lastFrame()).toContain('What model should we use for memory compaction?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model should we use for memory compaction?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 5. EnvironmentSettingsStep | ||||||||||
| expect(lastFrame()).toContain('Additional Settings'); | ||||||||||
| await simulateKey(enter); // Accept defaults | ||||||||||
| await waitForFrame(lastFrame, 'Additional Settings'); | ||||||||||
| stdin.write(enter); // Accept defaults | ||||||||||
|
|
||||||||||
| // Verification | ||||||||||
| expect(onComplete).toHaveBeenCalled(); | ||||||||||
| await vi.waitFor(() => expect(onComplete).toHaveBeenCalled()); | ||||||||||
| expect(vi.mocked(writeFileSync)).toHaveBeenCalled(); | ||||||||||
|
|
||||||||||
| // Check process.env | ||||||||||
|
|
@@ -115,36 +121,31 @@ describe('MainConfig', () => { | |||||||||
| const onComplete = vi.fn(); | ||||||||||
| const { lastFrame, stdin } = render(<MainConfig onComplete={onComplete} />); | ||||||||||
|
|
||||||||||
| const simulateKey = async (char: string) => { | ||||||||||
| stdin.write(char); | ||||||||||
| // Wait for re-render | ||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| // 1. ProviderStep - Choose Anthropic | ||||||||||
| expect(lastFrame()).toContain('What model provider would you like to use today?'); | ||||||||||
| await simulateKey(down); // to Anthropic | ||||||||||
| await simulateKey(enter); | ||||||||||
| await waitForFrame(lastFrame, 'What model provider would you like to use today?'); | ||||||||||
| stdin.write(down); // to Anthropic | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 2. ApiKeyStep | ||||||||||
| expect(lastFrame()).toContain('Can you provide us with your Anthropic API key?'); | ||||||||||
| await simulateKey('sk-ant-test-key'); | ||||||||||
| await simulateKey(enter); | ||||||||||
| await waitForFrame(lastFrame, 'Can you provide us with your Anthropic API key?'); | ||||||||||
| stdin.write('sk-ant-test-key'); | ||||||||||
| await waitForMaskedInput(lastFrame, 'sk-ant-test-key'); | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 3. ModelSelectionStep - Model | ||||||||||
| expect(lastFrame()).toContain('What model would you like to use?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model would you like to use?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 4. ModelSelectionStep - Compactor | ||||||||||
| expect(lastFrame()).toContain('What model should we use for memory compaction?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model should we use for memory compaction?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 5. EnvironmentSettingsStep | ||||||||||
| expect(lastFrame()).toContain('Additional Settings'); | ||||||||||
| await simulateKey(enter); // Accept defaults | ||||||||||
| await waitForFrame(lastFrame, 'Additional Settings'); | ||||||||||
| stdin.write(enter); // Accept defaults | ||||||||||
|
|
||||||||||
| // Verification | ||||||||||
| expect(onComplete).toHaveBeenCalled(); | ||||||||||
| await vi.waitFor(() => expect(onComplete).toHaveBeenCalled()); | ||||||||||
| expect(vi.mocked(writeFileSync)).toHaveBeenCalled(); | ||||||||||
|
|
||||||||||
| // Check process.env | ||||||||||
|
|
@@ -157,37 +158,32 @@ describe('MainConfig', () => { | |||||||||
| const onComplete = vi.fn(); | ||||||||||
| const { lastFrame, stdin } = render(<MainConfig onComplete={onComplete} />); | ||||||||||
|
|
||||||||||
| const simulateKey = async (char: string) => { | ||||||||||
| stdin.write(char); | ||||||||||
| // Wait for re-render | ||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| // 1. ProviderStep - Choose Google | ||||||||||
| expect(lastFrame()).toContain('What model provider would you like to use today?'); | ||||||||||
| await simulateKey(down); // to Anthropic | ||||||||||
| await simulateKey(down); // to Google | ||||||||||
| await simulateKey(enter); | ||||||||||
| await waitForFrame(lastFrame, 'What model provider would you like to use today?'); | ||||||||||
| stdin.write(down); // to Anthropic | ||||||||||
| stdin.write(down); // to Google | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 2. ApiKeyStep | ||||||||||
| expect(lastFrame()).toContain('Can you provide us with your Google API key?'); | ||||||||||
| await simulateKey('google-test-key'); | ||||||||||
| await simulateKey(enter); | ||||||||||
| await waitForFrame(lastFrame, 'Can you provide us with your Google API key?'); | ||||||||||
| stdin.write('google-test-key'); | ||||||||||
| await waitForMaskedInput(lastFrame, 'google-test-key'); | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 3. ModelSelectionStep - Model | ||||||||||
| expect(lastFrame()).toContain('What model would you like to use?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model would you like to use?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 4. ModelSelectionStep - Compactor | ||||||||||
| expect(lastFrame()).toContain('What model should we use for memory compaction?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model should we use for memory compaction?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 5. EnvironmentSettingsStep | ||||||||||
| expect(lastFrame()).toContain('Additional Settings'); | ||||||||||
| await simulateKey(enter); // Accept defaults | ||||||||||
| await waitForFrame(lastFrame, 'Additional Settings'); | ||||||||||
| stdin.write(enter); // Accept defaults | ||||||||||
|
|
||||||||||
| // Verification | ||||||||||
| expect(onComplete).toHaveBeenCalled(); | ||||||||||
| await vi.waitFor(() => expect(onComplete).toHaveBeenCalled()); | ||||||||||
| expect(vi.mocked(writeFileSync)).toHaveBeenCalled(); | ||||||||||
|
|
||||||||||
| // Check process.env | ||||||||||
|
|
@@ -200,38 +196,32 @@ describe('MainConfig', () => { | |||||||||
| const onComplete = vi.fn(); | ||||||||||
| const { lastFrame, stdin } = render(<MainConfig onComplete={onComplete} />); | ||||||||||
|
|
||||||||||
| const simulateKey = async (char: string) => { | ||||||||||
| stdin.write(char); | ||||||||||
| // Wait for re-render | ||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| // 1. ProviderStep - Choose Ollama | ||||||||||
| expect(lastFrame()).toContain('What model provider would you like to use today?'); | ||||||||||
| await waitForFrame(lastFrame, 'What model provider would you like to use today?'); | ||||||||||
|
|
||||||||||
| await simulateKey(down); // to Anthropic | ||||||||||
| await simulateKey(down); // to Google | ||||||||||
| await simulateKey(down); // to Ollama | ||||||||||
| await simulateKey(enter); | ||||||||||
| stdin.write(down); // to Anthropic | ||||||||||
| stdin.write(down); // to Google | ||||||||||
| stdin.write(down); // to Ollama | ||||||||||
| stdin.write(enter); | ||||||||||
|
|
||||||||||
| // 2. ApiUrlStep (since provider is Ollama) | ||||||||||
| expect(lastFrame()).toContain('Where are you hosting Ollama?'); | ||||||||||
| await simulateKey(enter); // Accept default http://localhost:11434/api | ||||||||||
| await waitForFrame(lastFrame, 'Where are you hosting Ollama?'); | ||||||||||
| stdin.write(enter); // Accept default http://localhost:11434/api | ||||||||||
|
|
||||||||||
| // 3. ModelSelectionStep - Model | ||||||||||
| expect(lastFrame()).toContain('What model would you like to use?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model would you like to use?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 4. ModelSelectionStep - Compactor | ||||||||||
| expect(lastFrame()).toContain('What model should we use for memory compaction?'); | ||||||||||
| await simulateKey(enter); // Accept default | ||||||||||
| await waitForFrame(lastFrame, 'What model should we use for memory compaction?'); | ||||||||||
| stdin.write(enter); // Accept default | ||||||||||
|
|
||||||||||
| // 5. EnvironmentSettingsStep | ||||||||||
| expect(lastFrame()).toContain('Additional Settings'); | ||||||||||
| await simulateKey(enter); // Accept defaults | ||||||||||
| await waitForFrame(lastFrame, 'Additional Settings'); | ||||||||||
| stdin.write(enter); // Accept defaults | ||||||||||
|
|
||||||||||
| // Verification | ||||||||||
| expect(onComplete).toHaveBeenCalled(); | ||||||||||
| await vi.waitFor(() => expect(onComplete).toHaveBeenCalled()); | ||||||||||
| expect(vi.mocked(writeFileSync)).toHaveBeenCalled(); | ||||||||||
|
|
||||||||||
| // Check process.env | ||||||||||
|
|
||||||||||
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.
If
lastFrame()returnsundefined(which can happen during initial render or before the first frame is drawn), callingexpect(undefined).toContain(text)can throw aTypeErrorin some test runners becauseundefinedis not iterable/a string. Using the nullish coalescing operator?? ''ensures that we always perform a string assertion, which provides a much cleaner assertion failure message if the test times out, rather than a genericTypeError.