• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5
6// General hook test setup
7const tick = require('../common/tick');
8const initHooks = require('./init-hooks');
9const { checkInvocations } = require('./hook-checks');
10
11const hooks = initHooks();
12hooks.enable();
13
14if (!process.stdin.isTTY)
15  return common.skip('no valid readable TTY available');
16
17// test specific setup
18const checkInitOpts = { init: 1 };
19const checkEndedOpts = { init: 1, before: 1, after: 1, destroy: 1 };
20
21// test code
22//
23// listen to stdin except on Windows
24const activities = hooks.activitiesOfTypes('TTYWRAP');
25assert.strictEqual(activities.length, 1);
26
27const tty = activities[0];
28assert.strictEqual(tty.type, 'TTYWRAP');
29assert.strictEqual(typeof tty.uid, 'number');
30assert.strictEqual(typeof tty.triggerAsyncId, 'number');
31checkInvocations(tty, checkInitOpts, 'when tty created');
32
33const delayedOnCloseHandler = common.mustCall(() => {
34  checkInvocations(tty, checkEndedOpts, 'when tty ended');
35});
36process.stdin.on('error', (err) => assert.fail(err));
37process.stdin.on('close', common.mustCall(() =>
38  tick(2, delayedOnCloseHandler)
39));
40process.stdin.destroy();
41checkInvocations(tty, checkInitOpts, 'when tty.end() was invoked');
42
43process.on('exit', () => {
44  hooks.disable();
45  hooks.sanityCheck('TTYWRAP');
46  checkInvocations(tty, checkEndedOpts, 'when process exits');
47});
48