chore: refactored test-helper to handle boilerplate for interactive mode (#10322)

Co-authored-by: Taneja Hriday <hridayt@google.com>
This commit is contained in:
hritan
2025-10-08 19:44:09 +05:30
committed by GitHub
parent 8cd2ec7c9b
commit 5d09ab7eb3
2 changed files with 46 additions and 40 deletions

View File

@@ -24,26 +24,7 @@ describe('Interactive Mode', () => {
await rig.setup('interactive-compress-test');
const { ptyProcess } = rig.runInteractive();
let fullOutput = '';
ptyProcess.onData((data) => (fullOutput += data));
const authDialogAppeared = await rig.waitForText(
'How would you like to authenticate',
5000,
);
// select the second option if auth dialog come's up
if (authDialogAppeared) {
ptyProcess.write('2');
}
// Wait for the app to be ready
const isReady = await rig.waitForText('Type your message', 15000);
expect(
isReady,
'CLI did not start up in interactive mode correctly',
).toBe(true);
await rig.ensureReadyForInput(ptyProcess);
const longPrompt =
'Dont do anything except returning a 1000 token long paragragh with the <name of the scientist who discovered theory of relativity> at the end to indicate end of response. This is a moderately long sentence.';
@@ -74,26 +55,7 @@ describe('Interactive Mode', () => {
await rig.setup('interactive-compress-test');
const { ptyProcess } = rig.runInteractive();
let fullOutput = '';
ptyProcess.onData((data) => (fullOutput += data));
const authDialogAppeared = await rig.waitForText(
'How would you like to authenticate',
5000,
);
// select the second option if auth dialog come's up
if (authDialogAppeared) {
ptyProcess.write('2');
}
// Wait for the app to be ready
const isReady = await rig.waitForText('Type your message', 25000);
expect(
isReady,
'CLI did not start up in interactive mode correctly',
).toBe(true);
await rig.ensureReadyForInput(ptyProcess);
await type(ptyProcess, '/compress');
await new Promise((resolve) => setTimeout(resolve, 100));

View File

@@ -855,4 +855,48 @@ export class TestRig {
return { ptyProcess, promise };
}
/**
* Waits for an interactive session to be fully ready for input.
* This is a higher-level utility to be used with `runInteractive`.
*
* It handles the initial setup boilerplate:
* 1. Automatically handles the authentication prompt if it appears.
* 2. Waits for the "Type your message" prompt to ensure the CLI is ready for input.
*
* Throws an error if the session fails to become ready within the timeout.
*
* @param ptyProcess The process returned from `runInteractive`.
*/
async ensureReadyForInput(ptyProcess: pty.IPty): Promise<void> {
const timeout = 25000;
const pollingInterval = 200;
const startTime = Date.now();
let authPromptHandled = false;
while (Date.now() - startTime < timeout) {
const output = stripAnsi(this._interactiveOutput).toLowerCase();
// If the ready prompt appears, we're done.
if (output.includes('type your message')) {
return;
}
// If the auth prompt appears and we haven't handled it yet.
if (
!authPromptHandled &&
output.includes('how would you like to authenticate')
) {
ptyProcess.write('2');
authPromptHandled = true;
}
// Wait for the next poll.
await new Promise((resolve) => setTimeout(resolve, pollingInterval));
}
throw new Error(
`CLI did not start up in interactive mode correctly. Output: ${this._interactiveOutput}`,
);
}
}