11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// General hook test setup
71cb0ef41Sopenharmony_ciconst tick = require('../common/tick');
81cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
91cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst hooks = initHooks();
121cb0ef41Sopenharmony_cihooks.enable();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciif (!process.stdin.isTTY)
151cb0ef41Sopenharmony_ci  return common.skip('no valid readable TTY available');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// test specific setup
181cb0ef41Sopenharmony_ciconst checkInitOpts = { init: 1 };
191cb0ef41Sopenharmony_ciconst checkEndedOpts = { init: 1, before: 1, after: 1, destroy: 1 };
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// test code
221cb0ef41Sopenharmony_ci//
231cb0ef41Sopenharmony_ci// listen to stdin except on Windows
241cb0ef41Sopenharmony_ciconst activities = hooks.activitiesOfTypes('TTYWRAP');
251cb0ef41Sopenharmony_ciassert.strictEqual(activities.length, 1);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst tty = activities[0];
281cb0ef41Sopenharmony_ciassert.strictEqual(tty.type, 'TTYWRAP');
291cb0ef41Sopenharmony_ciassert.strictEqual(typeof tty.uid, 'number');
301cb0ef41Sopenharmony_ciassert.strictEqual(typeof tty.triggerAsyncId, 'number');
311cb0ef41Sopenharmony_cicheckInvocations(tty, checkInitOpts, 'when tty created');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst delayedOnCloseHandler = common.mustCall(() => {
341cb0ef41Sopenharmony_ci  checkInvocations(tty, checkEndedOpts, 'when tty ended');
351cb0ef41Sopenharmony_ci});
361cb0ef41Sopenharmony_ciprocess.stdin.on('error', (err) => assert.fail(err));
371cb0ef41Sopenharmony_ciprocess.stdin.on('close', common.mustCall(() =>
381cb0ef41Sopenharmony_ci  tick(2, delayedOnCloseHandler),
391cb0ef41Sopenharmony_ci));
401cb0ef41Sopenharmony_ciprocess.stdin.destroy();
411cb0ef41Sopenharmony_cicheckInvocations(tty, checkInitOpts, 'when tty.end() was invoked');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciprocess.on('exit', () => {
441cb0ef41Sopenharmony_ci  hooks.disable();
451cb0ef41Sopenharmony_ci  hooks.sanityCheck('TTYWRAP');
461cb0ef41Sopenharmony_ci  checkInvocations(tty, checkEndedOpts, 'when process exits');
471cb0ef41Sopenharmony_ci});
48