11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
41cb0ef41Sopenharmony_cicommon.skipIf32Bits();
51cb0ef41Sopenharmony_ciconst { NodeInstance } = require('../common/inspector-helper.js');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci// Even with --inspect, the default async call stack depth is 0. We need a
91cb0ef41Sopenharmony_ci// chance to call Debugger.setAsyncCallStackDepth *before* activating the timer
101cb0ef41Sopenharmony_ci// for async stack traces to work.
111cb0ef41Sopenharmony_ciconst script = `
121cb0ef41Sopenharmony_ciprocess._rawDebug('Waiting until the inspector is activated...');
131cb0ef41Sopenharmony_ciconst waiting = setInterval(() => { debugger; }, 50);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// This function is called by the inspector client (session)
161cb0ef41Sopenharmony_cifunction setupTimeoutWithBreak() {
171cb0ef41Sopenharmony_ci  clearInterval(waiting);
181cb0ef41Sopenharmony_ci  process._rawDebug('Debugger ready, setting up timeout with a break');
191cb0ef41Sopenharmony_ci  setTimeout(() => { debugger; }, 50);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci`;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciasync function waitForInitialSetup(session) {
241cb0ef41Sopenharmony_ci  console.error('[test]', 'Waiting for initial setup');
251cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(2, '[eval]');
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciasync function setupTimeoutForStackTrace(session) {
291cb0ef41Sopenharmony_ci  console.error('[test]', 'Setting up timeout for async stack trace');
301cb0ef41Sopenharmony_ci  await session.send([
311cb0ef41Sopenharmony_ci    { 'method': 'Runtime.evaluate',
321cb0ef41Sopenharmony_ci      'params': { expression: 'setupTimeoutWithBreak()' } },
331cb0ef41Sopenharmony_ci    { 'method': 'Debugger.resume' },
341cb0ef41Sopenharmony_ci  ]);
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciasync function checkAsyncStackTrace(session) {
381cb0ef41Sopenharmony_ci  console.error('[test]', 'Verify basic properties of asyncStackTrace');
391cb0ef41Sopenharmony_ci  const paused = await session.waitForBreakOnLine(8, '[eval]');
401cb0ef41Sopenharmony_ci  assert(paused.params.asyncStackTrace,
411cb0ef41Sopenharmony_ci         `${Object.keys(paused.params)} contains "asyncStackTrace" property`);
421cb0ef41Sopenharmony_ci  assert(paused.params.asyncStackTrace.description, 'Timeout');
431cb0ef41Sopenharmony_ci  assert(paused.params.asyncStackTrace.callFrames
441cb0ef41Sopenharmony_ci           .some((frame) => frame.functionName === 'setupTimeoutWithBreak'));
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciasync function runTests() {
481cb0ef41Sopenharmony_ci  const instance = new NodeInstance(['--inspect=0'], script);
491cb0ef41Sopenharmony_ci  const session = await instance.connectInspectorSession();
501cb0ef41Sopenharmony_ci  await session.send([
511cb0ef41Sopenharmony_ci    { 'method': 'Runtime.enable' },
521cb0ef41Sopenharmony_ci    { 'method': 'Debugger.enable' },
531cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setAsyncCallStackDepth',
541cb0ef41Sopenharmony_ci      'params': { 'maxDepth': 10 } },
551cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setBlackboxPatterns',
561cb0ef41Sopenharmony_ci      'params': { 'patterns': [] } },
571cb0ef41Sopenharmony_ci    { 'method': 'Runtime.runIfWaitingForDebugger' },
581cb0ef41Sopenharmony_ci  ]);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  await waitForInitialSetup(session);
611cb0ef41Sopenharmony_ci  await setupTimeoutForStackTrace(session);
621cb0ef41Sopenharmony_ci  await checkAsyncStackTrace(session);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  console.error('[test]', 'Stopping child instance');
651cb0ef41Sopenharmony_ci  session.disconnect();
661cb0ef41Sopenharmony_ci  instance.kill();
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cirunTests();
70