Simplify auth in interactive tests. (#10921)

This commit is contained in:
Tommaso Sciortino
2025-10-10 14:50:14 -07:00
committed by GitHub
parent a6e00d9183
commit a64bb433b0
4 changed files with 48 additions and 148 deletions
+30 -56
View File
@@ -7,33 +7,36 @@
import { describe, it, expect } from 'vitest';
import * as os from 'node:os';
import { TestRig } from './test-helper.js';
import * as pty from '@lydell/node-pty';
function waitForExit(ptyProcess: pty.IPty): Promise<number> {
return new Promise((resolve, reject) => {
const timer = setTimeout(
() =>
reject(
new Error(`Test timed out: process did not exit within a minute.`),
),
60000,
);
ptyProcess.onExit(({ exitCode }) => {
clearTimeout(timer);
resolve(exitCode);
});
});
}
describe('Ctrl+C exit', () => {
it('should exit gracefully on second Ctrl+C', async () => {
const rig = new TestRig();
await rig.setup('should exit gracefully on second Ctrl+C');
const { ptyProcess, promise } = rig.runInteractive();
let output = '';
ptyProcess.onData((data) => {
output += data;
});
// Wait for the app to be ready by looking for the initial prompt indicator
await rig.poll(() => output.includes('▶'), 5000, 100);
const ptyProcess = await rig.runInteractive();
// Send first Ctrl+C
ptyProcess.write('\x03');
// Wait for the exit prompt
await rig.poll(
() => output.includes('Press Ctrl+C again to exit'),
1500,
50,
);
await rig.waitForText('Press Ctrl+C again to exit', 5000);
// Send second Ctrl+C
if (os.platform() === 'win32') {
// This is a workaround for node-pty/winpty on Windows.
// Reliably sending a second Ctrl+C signal to a process that is already
@@ -44,50 +47,21 @@ describe('Ctrl+C exit', () => {
// simulating a successful exit. We accept that we cannot test the
// graceful shutdown message on Windows in this automated context.
ptyProcess.kill();
} else {
// On Unix-like systems, send the second Ctrl+C to trigger the graceful exit.
ptyProcess.write('\x03');
}
const timeout = new Promise((_, reject) =>
setTimeout(
() =>
reject(
new Error(
`Test timed out: process did not exit within a minute. Output: ${output}`,
),
),
60000,
),
);
const result = await Promise.race([promise, timeout]);
// On Windows, killing the process may result in a non-zero exit code. On
// other platforms, a graceful exit is code 0.
if (os.platform() === 'win32') {
const exitCode = await waitForExit(ptyProcess);
// On Windows, the exit code after ptyProcess.kill() can be unpredictable
// (often 1), so we accept any non-null exit code as a pass condition,
// focusing on the fact that the process did terminate.
expect(
result.exitCode,
`Process exited with code ${result.exitCode}. Output: ${result.output}`,
).not.toBeNull();
} else {
// Expect a graceful exit (code 0) on non-Windows platforms
expect(
result.exitCode,
`Process exited with code ${result.exitCode}. Output: ${result.output}`,
).toBe(0);
// Only check for the quitting message on non-Windows platforms due to the
// forceful kill workaround.
const quittingMessage = 'Agent powering down. Goodbye!';
// The regex below is intentionally matching the ESC control character (\x1b)
// to strip ANSI color codes from the terminal output.
// eslint-disable-next-line no-control-regex
const cleanOutput = output.replace(/\x1b\[[0-9;]*m/g, '');
expect(cleanOutput).toContain(quittingMessage);
expect(exitCode, `Process exited with code ${exitCode}.`).not.toBeNull();
return;
}
// Send second Ctrl+C
ptyProcess.write('\x03');
const exitCode = await waitForExit(ptyProcess);
expect(exitCode, `Process exited with code ${exitCode}.`).toBe(0);
await rig.waitForText('Agent powering down. Goodbye!', 5000);
});
});