MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, summary } from './helpers.js';
2import nodeProcess, { cwd as nodeCwd, execPath as nodeExecPath, hrtime as nodeHrtime, stdout as nodeStdout } from 'node:process';
3
4console.log('Process Tests\n');
5
6test('process exists', typeof process, 'object');
7test('node:process default is global process', nodeProcess, process);
8test('node:process named cwd is function', typeof nodeCwd, 'function');
9test('node:process named execPath is string', typeof nodeExecPath, 'string');
10test('node:process named hrtime is function', typeof nodeHrtime, 'function');
11test('node:process named hrtime.bigint is function', typeof nodeHrtime.bigint, 'function');
12test('node:process named hrtime matches process.hrtime', nodeHrtime, process.hrtime);
13test('node:process named stdout is global stdout', nodeStdout, process.stdout);
14test('process toStringTag', Object.prototype.toString.call(process), '[object process]');
15
16test('pid is number', typeof process.pid, 'number');
17test('pid is positive', process.pid > 0, true);
18
19test('platform is string', typeof process.platform, 'string');
20test('platform valid', ['darwin', 'linux', 'win32', 'freebsd'].includes(process.platform), true);
21
22test('arch is string', typeof process.arch, 'string');
23test('arch valid', ['x64', 'ia32', 'arm64', 'arm'].includes(process.arch), true);
24
25test('argv is array', Array.isArray(process.argv), true);
26test('argv has entries', process.argv.length >= 1, true);
27test('execArgv is array', Array.isArray(process.execArgv), true);
28test('argv0 is string', typeof process.argv0, 'string');
29test('execPath is string', typeof process.execPath, 'string');
30test('ppid is number', typeof process.ppid, 'number');
31test('ppid is positive', process.ppid > 0, true);
32
33test('cwd is function', typeof process.cwd, 'function');
34const cwd = process.cwd();
35test('cwd returns string', typeof cwd, 'string');
36test('cwd not empty', cwd.length > 0, true);
37
38test('env is object', typeof process.env, 'object');
39test('env has PATH or Path', process.env.PATH !== undefined || process.env.Path !== undefined, true);
40
41test('exit is function', typeof process.exit, 'function');
42
43test('on is function', typeof process.on, 'function');
44test('once is function', typeof process.once, 'function');
45test('off is function', typeof process.off, 'function');
46test('addListener is function', typeof process.addListener, 'function');
47test('removeListener is function', typeof process.removeListener, 'function');
48test('removeAllListeners is function', typeof process.removeAllListeners, 'function');
49test('emit is function', typeof process.emit, 'function');
50test('listenerCount is function', typeof process.listenerCount, 'function');
51test('listeners is function', typeof process.listeners, 'function');
52test('rawListeners is function', typeof process.rawListeners, 'function');
53test('eventNames is function', typeof process.eventNames, 'function');
54test('prependListener is function', typeof process.prependListener, 'function');
55test('prependOnceListener is function', typeof process.prependOnceListener, 'function');
56test('setMaxListeners is function', typeof process.setMaxListeners, 'function');
57test('getMaxListeners is function', typeof process.getMaxListeners, 'function');
58
59let onCalled = false;
60process.on('testOn', () => {
61 onCalled = true;
62});
63process.emit('testOn');
64test('on/emit works', onCalled, true);
65process.removeAllListeners('testOn');
66
67let onceCount = 0;
68process.once('testOnce', () => {
69 onceCount++;
70});
71process.emit('testOnce');
72process.emit('testOnce');
73test('once fires once', onceCount, 1);
74
75let offCount = 0;
76const offHandler = () => {
77 offCount++;
78};
79process.on('testOff', offHandler);
80process.emit('testOff');
81process.off('testOff', offHandler);
82process.emit('testOff');
83test('off removes listener', offCount, 1);
84
85let addListenerCalled = false;
86process.addListener('testAddListener', () => {
87 addListenerCalled = true;
88});
89process.emit('testAddListener');
90test('addListener works', addListenerCalled, true);
91process.removeAllListeners('testAddListener');
92
93let removeListenerCount = 0;
94const rlHandler = () => {
95 removeListenerCount++;
96};
97process.on('testRL', rlHandler);
98process.emit('testRL');
99process.removeListener('testRL', rlHandler);
100process.emit('testRL');
101test('removeListener works', removeListenerCount, 1);
102
103process.on('testCount', () => {});
104process.on('testCount', () => {});
105test('listenerCount returns count', process.listenerCount('testCount'), 2);
106process.removeAllListeners('testCount');
107test('removeAllListeners clears', process.listenerCount('testCount'), 0);
108
109const listenersSeen = [];
110const listenersFirst = () => {
111 listenersSeen.push('first');
112};
113const listenersSecond = () => {
114 listenersSeen.push('second');
115};
116process.on('testListeners', listenersFirst);
117process.prependListener('testListeners', listenersSecond);
118const processListeners = process.listeners('testListeners');
119test('listeners returns array', Array.isArray(processListeners), true);
120test('listeners includes prepended handler first', processListeners[0], listenersSecond);
121test('listeners includes appended handler second', processListeners[1], listenersFirst);
122const processRawListeners = process.rawListeners('testListeners');
123test('rawListeners returns array', Array.isArray(processRawListeners), true);
124test('rawListeners includes prepended handler first', processRawListeners[0], listenersSecond);
125test('rawListeners includes appended handler second', processRawListeners[1], listenersFirst);
126test('eventNames includes active process event', process.eventNames().includes('testListeners'), true);
127process.emit('testListeners');
128test('prependListener affects emit order', listenersSeen.join(','), 'second,first');
129process.removeAllListeners('testListeners');
130test('eventNames excludes removed process event', process.eventNames().includes('testListeners'), false);
131
132let prependOnceCount = 0;
133const prependOnceMarker = [];
134process.on('testPrependOnce', () => {
135 prependOnceMarker.push('on');
136});
137process.prependOnceListener('testPrependOnce', () => {
138 prependOnceCount++;
139 prependOnceMarker.push('once');
140});
141process.emit('testPrependOnce');
142process.emit('testPrependOnce');
143test('prependOnceListener fires once', prependOnceCount, 1);
144test('prependOnceListener runs before on listener', prependOnceMarker[0], 'once');
145process.removeAllListeners('testPrependOnce');
146
147let receivedArg = null;
148process.on('testArgs', arg => {
149 receivedArg = arg;
150});
151process.emit('testArgs', 'hello');
152test('emit passes arguments', receivedArg, 'hello');
153process.removeAllListeners('testArgs');
154
155const chainResult = process.on('testChain', () => {});
156test('on returns process', chainResult, process);
157process.removeAllListeners('testChain');
158
159test('getMaxListeners default', process.getMaxListeners(), 10);
160
161process.setMaxListeners(20);
162test('setMaxListeners updates', process.getMaxListeners(), 20);
163process.setMaxListeners(10);
164
165process.setMaxListeners(0);
166for (let i = 0; i < 15; i++) {
167 process.on('manyListeners', () => {});
168}
169test('supports many listeners', process.listenerCount('manyListeners'), 15);
170process.removeAllListeners('manyListeners');
171process.setMaxListeners(10);
172
173test('stdin is object', typeof process.stdin, 'object');
174test('stdin.isTTY is boolean', typeof process.stdin.isTTY, 'boolean');
175test('stdin.setRawMode is function', typeof process.stdin.setRawMode, 'function');
176test('stdin.resume is function', typeof process.stdin.resume, 'function');
177test('stdin.pause is function', typeof process.stdin.pause, 'function');
178test('stdin.on is function', typeof process.stdin.on, 'function');
179test('stdin.removeAllListeners is function', typeof process.stdin.removeAllListeners, 'function');
180
181test('stdout is object', typeof process.stdout, 'object');
182test('stdout.isTTY is boolean', typeof process.stdout.isTTY, 'boolean');
183test('stdout.rows shape', process.stdout.isTTY ? typeof process.stdout.rows === 'number' : process.stdout.rows === undefined, true);
184test('stdout.columns shape', process.stdout.isTTY ? typeof process.stdout.columns === 'number' : process.stdout.columns === undefined, true);
185test('stdout.write is function', typeof process.stdout.write, 'function');
186test('stdout.on is function', typeof process.stdout.on, 'function');
187test('stdout.once is function', typeof process.stdout.once, 'function');
188test('stdout.removeAllListeners is function', typeof process.stdout.removeAllListeners, 'function');
189test('stdout.getWindowSize is function', typeof process.stdout.getWindowSize, 'function');
190
191const windowSize = process.stdout.getWindowSize();
192test('getWindowSize returns array', Array.isArray(windowSize), true);
193test('getWindowSize has 2 elements', windowSize.length, 2);
194test('getWindowSize cols is number', typeof windowSize[0], 'number');
195test('getWindowSize rows is number', typeof windowSize[1], 'number');
196
197test('stderr is object', typeof process.stderr, 'object');
198test('stderr.isTTY is boolean', typeof process.stderr.isTTY, 'boolean');
199test('stderr.write is function', typeof process.stderr.write, 'function');
200test('stderr.on is function', typeof process.stderr.on, 'function');
201test('stderr.once is function', typeof process.stderr.once, 'function');
202test('stderr.removeAllListeners is function', typeof process.stderr.removeAllListeners, 'function');
203
204test('version is string', typeof process.version, 'string');
205test('version starts with v', process.version.startsWith('v'), true);
206
207test('versions is object', typeof process.versions, 'object');
208test('versions.ant exists', typeof process.versions.ant, 'string');
209test('versions.uv exists', typeof process.versions.uv, 'string');
210
211test('features is object', typeof process.features, 'object');
212test('features.uv is boolean', typeof process.features.uv, 'boolean');
213test('features.typescript exists', typeof process.features.typescript, 'string');
214
215test('release is object', typeof process.release, 'object');
216test('release.name is string', typeof process.release.name, 'string');
217
218test('uptime is function', typeof process.uptime, 'function');
219const uptime = process.uptime();
220test('uptime returns number', typeof uptime, 'number');
221test('uptime is non-negative', uptime >= 0, true);
222
223test('memoryUsage is function', typeof process.memoryUsage, 'function');
224const mem = process.memoryUsage();
225test('memoryUsage returns object', typeof mem, 'object');
226test('memoryUsage.rss is number', typeof mem.rss, 'number');
227test('memoryUsage.heapTotal is number', typeof mem.heapTotal, 'number');
228test('memoryUsage.heapUsed is number', typeof mem.heapUsed, 'number');
229test('memoryUsage.rss method exists', typeof process.memoryUsage.rss, 'function');
230test('memoryUsage.rss returns number', typeof process.memoryUsage.rss(), 'number');
231
232test('cpuUsage is function', typeof process.cpuUsage, 'function');
233const cpu = process.cpuUsage();
234test('cpuUsage returns object', typeof cpu, 'object');
235test('cpuUsage.user is number', typeof cpu.user, 'number');
236test('cpuUsage.system is number', typeof cpu.system, 'number');
237
238test('hrtime is function', typeof process.hrtime, 'function');
239const hr = process.hrtime();
240test('hrtime returns array', Array.isArray(hr), true);
241test('hrtime has 2 elements', hr.length, 2);
242test('hrtime[0] is number', typeof hr[0], 'number');
243test('hrtime[1] is number', typeof hr[1], 'number');
244
245test('hrtime.bigint is function', typeof process.hrtime.bigint, 'function');
246const hrBigint = process.hrtime.bigint();
247test('hrtime.bigint returns bigint', typeof hrBigint, 'bigint');
248
249test('kill is function', typeof process.kill, 'function');
250test('abort is function', typeof process.abort, 'function');
251test('chdir is function', typeof process.chdir, 'function');
252test('umask is function', typeof process.umask, 'function');
253
254const oldMask = process.umask();
255test('umask returns number', typeof oldMask, 'number');
256
257if (process.platform !== 'win32') {
258 test('getuid is function', typeof process.getuid, 'function');
259 test('geteuid is function', typeof process.geteuid, 'function');
260 test('getgid is function', typeof process.getgid, 'function');
261 test('getegid is function', typeof process.getegid, 'function');
262 test('getgroups is function', typeof process.getgroups, 'function');
263 test('setuid is function', typeof process.setuid, 'function');
264 test('setgid is function', typeof process.setgid, 'function');
265 test('seteuid is function', typeof process.seteuid, 'function');
266 test('setegid is function', typeof process.setegid, 'function');
267 test('setgroups is function', typeof process.setgroups, 'function');
268 test('initgroups is function', typeof process.initgroups, 'function');
269
270 test('getuid returns number', typeof process.getuid(), 'number');
271 test('getgid returns number', typeof process.getgid(), 'number');
272 test('getgroups returns array', Array.isArray(process.getgroups()), true);
273}
274
275summary();