11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { NodeInstance } = require('../common/inspector-helper.js');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ciconst { pathToFileURL } = require('url');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// This needs to be an ES module file to ensure that internal modules are
101cb0ef41Sopenharmony_ci// loaded before pausing. See
111cb0ef41Sopenharmony_ci// https://bugs.chromium.org/p/chromium/issues/detail?id=1246905
121cb0ef41Sopenharmony_ciconst script = fixtures.path('inspector-global-function.mjs');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciasync function setupDebugger(session) {
151cb0ef41Sopenharmony_ci  console.log('[test]', 'Setting up a debugger');
161cb0ef41Sopenharmony_ci  const commands = [
171cb0ef41Sopenharmony_ci    { 'method': 'Runtime.enable' },
181cb0ef41Sopenharmony_ci    { 'method': 'Debugger.enable' },
191cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setAsyncCallStackDepth',
201cb0ef41Sopenharmony_ci      'params': { 'maxDepth': 0 } },
211cb0ef41Sopenharmony_ci    { 'method': 'Runtime.runIfWaitingForDebugger' },
221cb0ef41Sopenharmony_ci  ];
231cb0ef41Sopenharmony_ci  session.send(commands);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  await session.waitForNotification('Debugger.paused', 'Initial pause');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  // NOTE(mmarchini): We wait for the second console.log to ensure we loaded
281cb0ef41Sopenharmony_ci  // every internal module before pausing. See
291cb0ef41Sopenharmony_ci  // https://bugs.chromium.org/p/chromium/issues/detail?id=1246905
301cb0ef41Sopenharmony_ci  const waitForReady = session.waitForConsoleOutput('log', 'Ready!');
311cb0ef41Sopenharmony_ci  session.send({ 'method': 'Debugger.resume' });
321cb0ef41Sopenharmony_ci  await waitForReady;
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciasync function breakOnLine(session) {
361cb0ef41Sopenharmony_ci  console.log('[test]', 'Breaking in the code');
371cb0ef41Sopenharmony_ci  const commands = [
381cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setBreakpointByUrl',
391cb0ef41Sopenharmony_ci      'params': { 'lineNumber': 9,
401cb0ef41Sopenharmony_ci                  'url': pathToFileURL(script).toString(),
411cb0ef41Sopenharmony_ci                  'columnNumber': 0,
421cb0ef41Sopenharmony_ci                  'condition': '' } },
431cb0ef41Sopenharmony_ci    { 'method': 'Runtime.evaluate',
441cb0ef41Sopenharmony_ci      'params': { 'expression': 'sum()',
451cb0ef41Sopenharmony_ci                  'objectGroup': 'console',
461cb0ef41Sopenharmony_ci                  'includeCommandLineAPI': true,
471cb0ef41Sopenharmony_ci                  'silent': false,
481cb0ef41Sopenharmony_ci                  'contextId': 1,
491cb0ef41Sopenharmony_ci                  'returnByValue': false,
501cb0ef41Sopenharmony_ci                  'generatePreview': true,
511cb0ef41Sopenharmony_ci                  'userGesture': true,
521cb0ef41Sopenharmony_ci                  'awaitPromise': false } },
531cb0ef41Sopenharmony_ci  ];
541cb0ef41Sopenharmony_ci  session.send(commands);
551cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(9, pathToFileURL(script).toString());
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciasync function stepOverConsoleStatement(session) {
591cb0ef41Sopenharmony_ci  console.log('[test]', 'Step over console statement and test output');
601cb0ef41Sopenharmony_ci  session.send({ 'method': 'Debugger.stepOver' });
611cb0ef41Sopenharmony_ci  await session.waitForConsoleOutput('log', [0, 3]);
621cb0ef41Sopenharmony_ci  await session.waitForNotification('Debugger.paused');
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciasync function runTests() {
661cb0ef41Sopenharmony_ci  // NOTE(mmarchini): Use --inspect-brk to improve avoid undeterministic
671cb0ef41Sopenharmony_ci  // behavior.
681cb0ef41Sopenharmony_ci  const child = new NodeInstance(['--inspect-brk=0'], undefined, script);
691cb0ef41Sopenharmony_ci  const session = await child.connectInspectorSession();
701cb0ef41Sopenharmony_ci  await setupDebugger(session);
711cb0ef41Sopenharmony_ci  await breakOnLine(session);
721cb0ef41Sopenharmony_ci  await stepOverConsoleStatement(session);
731cb0ef41Sopenharmony_ci  await session.runToCompletion();
741cb0ef41Sopenharmony_ci  assert.strictEqual((await child.expectShutdown()).exitCode, 0);
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cirunTests().then(common.mustCall());
78