Open History to see the last commit
1import * as NodeModule from "node:module";
2
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5const requireCjs = NodeModule.createRequire(import.meta.url);
6const {
7 WindowsPtyAgent,
8}: {
9 WindowsPtyAgent: {
10 prototype: {
11 _flushDataAndCleanUp(this: FakeAgent): void;
12 _cleanUpProcess(this: FakeAgent): void;
13 };
14 };
15} = requireCjs("node-pty/lib/windowsPtyAgent.js");
16
17interface FakeAgent {
18 _useConptyDll: boolean;
19 _closeTimeout?: unknown;
20 _inSocket: { readable: boolean };
21 _outSocket: { readable: boolean; destroy: () => void };
22 _cleanUpProcess: () => void;
23}
24
25// The sockets are stand-ins; the timer logic and _cleanUpProcess are node-pty's own (as patched by T3).
26const makeAgent = (): FakeAgent => {
27 const agent: FakeAgent = {
28 _useConptyDll: false,
29 _inSocket: { readable: true },
30 _outSocket: { readable: true, destroy: vi.fn() },
31 _cleanUpProcess: () => WindowsPtyAgent.prototype._cleanUpProcess.call(agent),
32 };
33 return agent;
34};
35
36const flush = (agent: FakeAgent) => WindowsPtyAgent.prototype._flushDataAndCleanUp.call(agent);
37
38// A minimal event loop that mirrors Node's phases: expired timers run first, then immediates (the check phase).
39// node-pty calls the global timer functions at call time, so replacing them lets the test place a "poll phase"
40// (delivery of already-buffered pipe data) exactly between the two.
41interface Timer {
42 readonly id: number;
43 readonly dueAt: number;
44 readonly run: () => void;
45}
46let now = 0;
47let nextId = 0;
48let timers: Timer[] = [];
49let immediates: Array<() => void> = [];
50
51const runExpiredTimers = () => {
52 for (const timer of timers.filter((t) => t.dueAt <= now).toSorted((a, b) => a.dueAt - b.dueAt)) {
53 timers = timers.filter((t) => t.id !== timer.id);
54 timer.run();
55 }
56};
57const runImmediates = () => {
58 const batch = immediates;
59 immediates = [];
60 for (const run of batch) run();
61};
62/** One loop iteration after `ms` of wall time; `poll` runs between the timers and check phases. */
63const loopIteration = (ms: number, poll?: () => void) => {
64 now += ms;
65 runExpiredTimers();
66 poll?.();
67 runImmediates();
68};
69
70// ConPTY never closes the output pipe after the shell exits, so node-pty ends the session on a timer.
71// T3 shortens it (patches/node-pty@1.2.0-beta.15.patch) so the terminal reports "exited" about 0.9 s sooner.
72describe("node-pty Windows exit flush", () => {
73 beforeEach(() => {
74 now = 0;
75 nextId = 0;
76 timers = [];
77 immediates = [];
78 vi.stubGlobal("setTimeout", (run: () => void, ms: number) => {
79 const id = ++nextId;
80 timers.push({ id, dueAt: now + ms, run });
81 return id;
82 });
83 vi.stubGlobal("clearTimeout", (id: number) => {
84 timers = timers.filter((t) => t.id !== id);
85 });
86 vi.stubGlobal("setImmediate", (run: () => void) => {
87 immediates.push(run);
88 return ++nextId;
89 });
90 });
91 afterEach(() => {
92 vi.unstubAllGlobals();
93 });
94
95 it("closes the output socket 100 ms after the exit, not a second later", () => {
96 const agent = makeAgent();
97 flush(agent);
98
99 loopIteration(99);
100 expect(agent._outSocket.destroy).not.toHaveBeenCalled();
101
102 loopIteration(1);
103 expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1);
104 expect(agent._outSocket.readable).toBe(false);
105 expect(agent._inSocket.readable).toBe(false);
106 });
107
108 // Timers run before the poll phase. After a main-thread stall longer than the window, output that is already
109 // buffered in the pipe is read only after the timer has expired, so the cleanup has to wait one loop turn for it.
110 it("keeps the socket open when buffered output is read right after a stalled timer expires", () => {
111 const agent = makeAgent();
112 flush(agent);
113
114 loopIteration(150, () => flush(agent));
115 expect(agent._outSocket.destroy).not.toHaveBeenCalled();
116
117 loopIteration(99);
118 expect(agent._outSocket.destroy).not.toHaveBeenCalled();
119
120 loopIteration(1);
121 expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1);
122 });
123
124 it("closes after a stall when no output was buffered", () => {
125 const agent = makeAgent();
126 flush(agent);
127
128 loopIteration(150);
129 expect(agent._outSocket.destroy).toHaveBeenCalledTimes(1);
130 });
131});