11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
61cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst p = new Promise(common.mustCall(function executor(resolve) {
91cb0ef41Sopenharmony_ci  resolve(5);
101cb0ef41Sopenharmony_ci}));
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Init hooks after promise was created
131cb0ef41Sopenharmony_ciconst hooks = initHooks({ allowNoInit: true });
141cb0ef41Sopenharmony_cihooks.enable();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cip.then(function afterResolution(val) {
171cb0ef41Sopenharmony_ci  assert.strictEqual(val, 5);
181cb0ef41Sopenharmony_ci  const as = hooks.activitiesOfTypes('PROMISE');
191cb0ef41Sopenharmony_ci  assert.strictEqual(as.length, 1);
201cb0ef41Sopenharmony_ci  checkInvocations(as[0], { init: 1, before: 1 },
211cb0ef41Sopenharmony_ci                   'after resolution child promise');
221cb0ef41Sopenharmony_ci  return val;
231cb0ef41Sopenharmony_ci});
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciprocess.on('exit', function onexit() {
261cb0ef41Sopenharmony_ci  hooks.disable();
271cb0ef41Sopenharmony_ci  hooks.sanityCheck('PROMISE');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  const as = hooks.activitiesOfTypes('PROMISE');
301cb0ef41Sopenharmony_ci  assert.strictEqual(as.length, 1);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  const a0 = as[0];
331cb0ef41Sopenharmony_ci  assert.strictEqual(a0.type, 'PROMISE');
341cb0ef41Sopenharmony_ci  assert.strictEqual(typeof a0.uid, 'number');
351cb0ef41Sopenharmony_ci  // We can't get the asyncId from the parent dynamically, since init was
361cb0ef41Sopenharmony_ci  // never called. However, it is known that the parent promise was created
371cb0ef41Sopenharmony_ci  // immediately before the child promise, thus there should only be one
381cb0ef41Sopenharmony_ci  // difference in id.
391cb0ef41Sopenharmony_ci  assert.strictEqual(a0.triggerAsyncId, a0.uid - 1);
401cb0ef41Sopenharmony_ci  // We expect a destroy hook as well but we cannot guarantee predictable gc.
411cb0ef41Sopenharmony_ci  checkInvocations(a0, { init: 1, before: 1, after: 1 }, 'when process exits');
421cb0ef41Sopenharmony_ci});
43