11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { NodeInstance } = require('../common/inspector-helper.js');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciasync function runTests() {
101cb0ef41Sopenharmony_ci  const child = new NodeInstance(['-e', `(${main.toString()})()`], '', '');
111cb0ef41Sopenharmony_ci  const session = await child.connectInspectorSession();
121cb0ef41Sopenharmony_ci  await session.send({ method: 'Runtime.enable' });
131cb0ef41Sopenharmony_ci  // Check that there is only one console message received.
141cb0ef41Sopenharmony_ci  await session.waitForConsoleOutput('log', 'before wait for debugger');
151cb0ef41Sopenharmony_ci  assert.ok(!session.unprocessedNotifications()
161cb0ef41Sopenharmony_ci                    .some((n) => n.method === 'Runtime.consoleAPICalled'));
171cb0ef41Sopenharmony_ci  // Check that inspector.url() is available between inspector.open() and
181cb0ef41Sopenharmony_ci  // inspector.waitForDebugger()
191cb0ef41Sopenharmony_ci  const { result: { value } } = await session.send({
201cb0ef41Sopenharmony_ci    method: 'Runtime.evaluate',
211cb0ef41Sopenharmony_ci    params: {
221cb0ef41Sopenharmony_ci      expression: 'process._ws',
231cb0ef41Sopenharmony_ci      includeCommandLineAPI: true
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci  assert.ok(value.startsWith('ws://'));
271cb0ef41Sopenharmony_ci  session.send({ method: 'Runtime.runIfWaitingForDebugger' });
281cb0ef41Sopenharmony_ci  // Check that messages after first and before second waitForDebugger are
291cb0ef41Sopenharmony_ci  // received
301cb0ef41Sopenharmony_ci  await session.waitForConsoleOutput('log', 'after wait for debugger');
311cb0ef41Sopenharmony_ci  await session.waitForConsoleOutput('log', 'before second wait for debugger');
321cb0ef41Sopenharmony_ci  assert.ok(!session.unprocessedNotifications()
331cb0ef41Sopenharmony_ci                    .some((n) => n.method === 'Runtime.consoleAPICalled'));
341cb0ef41Sopenharmony_ci  const secondSession = await child.connectInspectorSession();
351cb0ef41Sopenharmony_ci  // Check that inspector.waitForDebugger can be resumed from another session
361cb0ef41Sopenharmony_ci  secondSession.send({ method: 'Runtime.runIfWaitingForDebugger' });
371cb0ef41Sopenharmony_ci  await session.waitForConsoleOutput('log', 'after second wait for debugger');
381cb0ef41Sopenharmony_ci  assert.ok(!session.unprocessedNotifications()
391cb0ef41Sopenharmony_ci                    .some((n) => n.method === 'Runtime.consoleAPICalled'));
401cb0ef41Sopenharmony_ci  secondSession.disconnect();
411cb0ef41Sopenharmony_ci  session.disconnect();
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  function main(prefix) {
441cb0ef41Sopenharmony_ci    const inspector = require('inspector');
451cb0ef41Sopenharmony_ci    inspector.open(0, undefined, false);
461cb0ef41Sopenharmony_ci    process._ws = inspector.url();
471cb0ef41Sopenharmony_ci    console.log('before wait for debugger');
481cb0ef41Sopenharmony_ci    inspector.waitForDebugger();
491cb0ef41Sopenharmony_ci    console.log('after wait for debugger');
501cb0ef41Sopenharmony_ci    console.log('before second wait for debugger');
511cb0ef41Sopenharmony_ci    inspector.waitForDebugger();
521cb0ef41Sopenharmony_ci    console.log('after second wait for debugger');
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  // Check that inspector.waitForDebugger throws if there is no active
561cb0ef41Sopenharmony_ci  // inspector
571cb0ef41Sopenharmony_ci  const re = /^Error \[ERR_INSPECTOR_NOT_ACTIVE\]: Inspector is not active$/;
581cb0ef41Sopenharmony_ci  assert.throws(() => require('inspector').waitForDebugger(), re);
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cirunTests().then(common.mustCall());
62