Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
77 changes: 77 additions & 0 deletions integration-tests/test-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ function isProcessAlive(pid: number): boolean {
}
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

/** Emitted by the stand-in CLI below; nothing else in the run writes it. */
const FORWARD_CANARY = 'PTY_FORWARD_CANARY_11002';

describe('TestRig', () => {
const originalKeepOutput = process.env['KEEP_OUTPUT'];

Expand Down Expand Up @@ -89,6 +94,78 @@ describe('TestRig', () => {
.toBe(false);
});

it("detaches a session's output forwarding during cleanup", async () => {
// KEEP_OUTPUT is what the OpenTUI leg sets, and it is what makes the rig
// forward every PTY byte into this worker's stdout.
process.env['KEEP_OUTPUT'] = 'true';
const rig = new TestRig();
await rig.setup('cleanup detaches interactive output');
// Stands in for the CLI bundle, which traps the SIGHUP that node-pty's
// signal-less kill() sends and keeps rendering while its exit cleanup
// drains. The child therefore outlives cleanup() on purpose and keeps
// producing bytes: what is under test is whether the harness still
// forwards them into a stdout pipe vitest is about to destroy (#11002).
rig.bundlePath = rig.createFile(
'slow-exit-cli.js',
[
"process.on('SIGHUP', () => {});",
`setInterval(() => process.stdout.write('${FORWARD_CANARY}\\n'), 20);`,
'setTimeout(() => process.exit(0), 30000);',
'',
].join('\n'),
);

const { ptyProcess } = rig.runInteractive();
try {
await expect
.poll(() => rig._interactiveOutput.includes(FORWARD_CANARY), {
message: 'the stand-in CLI never produced output',
timeout: 20_000,
})
.toBe(true);

const forwarded: string[] = [];
vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => {
forwarded.push(String(chunk));
return true;
});

// Positive control, through the same KEEP_OUTPUT gate the harness uses:
// a forwarding path that never ran satisfies the empty window below
// exactly as well as one cleanup() has detached.
await expect
.poll(() => forwarded.some((chunk) => chunk.includes(FORWARD_CANARY)), {
message: 'stdout forwarding never went live before cleanup',
timeout: 20_000,
})
.toBe(true);

await rig.cleanup();
// Still alive: it swallowed SIGHUP. That is the window the real CLI's
// graceful shutdown opens between cleanup() and the child's own exit.
expect(isProcessAlive(ptyProcess.pid)).toBe(true);

forwarded.length = 0;
await sleep(500);
// Alive at the END of the window too: a producer that died inside it
// emits no bytes, so the assertion below could no longer tell
// "detached" apart from "dead".
expect(isProcessAlive(ptyProcess.pid)).toBe(true);

expect(
forwarded.filter((chunk) => chunk.includes(FORWARD_CANARY)),
'cleanup() left the session forwarding PTY bytes into stdout',
).toEqual([]);
} finally {
vi.restoreAllMocks();
try {
process.kill(ptyProcess.pid, 'SIGKILL');
} catch {
// Already gone
}
}
});

it.each([
[
'telemetry events',
Expand Down
23 changes: 17 additions & 6 deletions integration-tests/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ export class TestRig {
testName?: string;
_lastRunStdout?: string;
_interactiveOutput = '';
private readonly interactiveProcesses: pty.IPty[] = [];
// Each interactive PTY paired with the listener forwarding its bytes to
// this worker's stdout, so cleanup() can detach that forwarding.
private readonly interactiveSessions: Array<{
ptyProcess: pty.IPty;
forwardOutput: pty.IDisposable;
}> = [];

constructor() {
this.bundlePath = join(__dirname, '..', 'dist/cli.js');
Expand Down Expand Up @@ -497,9 +502,16 @@ export class TestRig {
// A session a test never closed keeps its CLI child forwarding PTY bytes
// into this worker's stdout; after vitest tears the worker down those
// writes EPIPE and fail an otherwise all-green run (#10969).
for (const ptyProcess of this.interactiveProcesses.splice(0)) {
//
// Killing is not enough: node-pty's signal-less kill() sends SIGHUP, which
// the CLI traps into an asynchronous graceful shutdown, so the child still
// renders after cleanup() returns and one more forwarded byte can reach a
// stdout pipe vitest has since destroyed. Detaching the listener closes
// that window however long the child takes to die (#11002).
for (const session of this.interactiveSessions.splice(0)) {
session.forwardOutput.dispose();
try {
ptyProcess.kill();
session.ptyProcess.kill();
} catch {
// Process may have already exited
}
Expand Down Expand Up @@ -944,14 +956,13 @@ export class TestRig {
...e2eRendererEnv(renderer),
} as { [key: string]: string },
});
this.interactiveProcesses.push(ptyProcess);

ptyProcess.onData((data) => {
const forwardOutput = ptyProcess.onData((data) => {
this._interactiveOutput += data;
if (env.KEEP_OUTPUT === 'true' || env.VERBOSE === 'true') {
process.stdout.write(data);
}
});
this.interactiveSessions.push({ ptyProcess, forwardOutput });

const promise = new Promise<{
exitCode: number;
Expand Down
Loading