11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cicommon.skipIfInspectorDisabled();
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { resolve: UrlResolve } = require('url');
81cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
91cb0ef41Sopenharmony_ciconst { NodeInstance } = require('../common/inspector-helper.js');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction assertScopeValues({ result }, expected) {
121cb0ef41Sopenharmony_ci  const unmatched = new Set(Object.keys(expected));
131cb0ef41Sopenharmony_ci  for (const actual of result) {
141cb0ef41Sopenharmony_ci    const value = expected[actual.name];
151cb0ef41Sopenharmony_ci    assert.strictEqual(actual.value.value, value);
161cb0ef41Sopenharmony_ci    unmatched.delete(actual.name);
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Array.from(unmatched.values()), []);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciasync function testBreakpointOnStart(session) {
221cb0ef41Sopenharmony_ci  console.log('[test]',
231cb0ef41Sopenharmony_ci              'Verifying debugger stops on start (--inspect-brk option)');
241cb0ef41Sopenharmony_ci  const commands = [
251cb0ef41Sopenharmony_ci    { 'method': 'Runtime.enable' },
261cb0ef41Sopenharmony_ci    { 'method': 'Debugger.enable' },
271cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setPauseOnExceptions',
281cb0ef41Sopenharmony_ci      'params': { 'state': 'none' } },
291cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setAsyncCallStackDepth',
301cb0ef41Sopenharmony_ci      'params': { 'maxDepth': 0 } },
311cb0ef41Sopenharmony_ci    { 'method': 'Profiler.enable' },
321cb0ef41Sopenharmony_ci    { 'method': 'Profiler.setSamplingInterval',
331cb0ef41Sopenharmony_ci      'params': { 'interval': 100 } },
341cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setBlackboxPatterns',
351cb0ef41Sopenharmony_ci      'params': { 'patterns': [] } },
361cb0ef41Sopenharmony_ci    { 'method': 'Runtime.runIfWaitingForDebugger' },
371cb0ef41Sopenharmony_ci  ];
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  await session.send(commands);
401cb0ef41Sopenharmony_ci  await session.waitForBreakOnLine(
411cb0ef41Sopenharmony_ci    0, UrlResolve(session.scriptURL().toString(), 'message.mjs'));
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciasync function testBreakpoint(session) {
451cb0ef41Sopenharmony_ci  console.log('[test]', 'Setting a breakpoint and verifying it is hit');
461cb0ef41Sopenharmony_ci  const commands = [
471cb0ef41Sopenharmony_ci    { 'method': 'Debugger.setBreakpointByUrl',
481cb0ef41Sopenharmony_ci      'params': { 'lineNumber': 7,
491cb0ef41Sopenharmony_ci                  'url': session.scriptURL(),
501cb0ef41Sopenharmony_ci                  'columnNumber': 0,
511cb0ef41Sopenharmony_ci                  'condition': '' } },
521cb0ef41Sopenharmony_ci    { 'method': 'Debugger.resume' },
531cb0ef41Sopenharmony_ci  ];
541cb0ef41Sopenharmony_ci  await session.send(commands);
551cb0ef41Sopenharmony_ci  const { scriptSource } = await session.send({
561cb0ef41Sopenharmony_ci    'method': 'Debugger.getScriptSource',
571cb0ef41Sopenharmony_ci    'params': { 'scriptId': session.mainScriptId },
581cb0ef41Sopenharmony_ci  });
591cb0ef41Sopenharmony_ci  assert(scriptSource && (scriptSource.includes(session.script())),
601cb0ef41Sopenharmony_ci         `Script source is wrong: ${scriptSource}`);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  await session.waitForConsoleOutput('log', ['A message', 5]);
631cb0ef41Sopenharmony_ci  const paused = await session.waitForBreakOnLine(7, session.scriptURL());
641cb0ef41Sopenharmony_ci  const scopeId = paused.params.callFrames[0].scopeChain[0].object.objectId;
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  console.log('[test]', 'Verify we can read current application state');
671cb0ef41Sopenharmony_ci  const response = await session.send({
681cb0ef41Sopenharmony_ci    'method': 'Runtime.getProperties',
691cb0ef41Sopenharmony_ci    'params': {
701cb0ef41Sopenharmony_ci      'objectId': scopeId,
711cb0ef41Sopenharmony_ci      'ownProperties': false,
721cb0ef41Sopenharmony_ci      'accessorPropertiesOnly': false,
731cb0ef41Sopenharmony_ci      'generatePreview': true
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci  });
761cb0ef41Sopenharmony_ci  assertScopeValues(response, { t: 1001, k: 1, message: 'A message' });
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  let { result } = await session.send({
791cb0ef41Sopenharmony_ci    'method': 'Debugger.evaluateOnCallFrame', 'params': {
801cb0ef41Sopenharmony_ci      'callFrameId': session.pausedDetails().callFrames[0].callFrameId,
811cb0ef41Sopenharmony_ci      'expression': 'k + t',
821cb0ef41Sopenharmony_ci      'objectGroup': 'console',
831cb0ef41Sopenharmony_ci      'includeCommandLineAPI': true,
841cb0ef41Sopenharmony_ci      'silent': false,
851cb0ef41Sopenharmony_ci      'returnByValue': false,
861cb0ef41Sopenharmony_ci      'generatePreview': true
871cb0ef41Sopenharmony_ci    }
881cb0ef41Sopenharmony_ci  });
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  assert.strictEqual(result.value, 1002);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  result = (await session.send({
931cb0ef41Sopenharmony_ci    'method': 'Runtime.evaluate', 'params': {
941cb0ef41Sopenharmony_ci      'expression': '5 * 5'
951cb0ef41Sopenharmony_ci    }
961cb0ef41Sopenharmony_ci  })).result;
971cb0ef41Sopenharmony_ci  assert.strictEqual(result.value, 25);
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ciasync function runTest() {
1011cb0ef41Sopenharmony_ci  const child = new NodeInstance(['--inspect-brk=0'], '',
1021cb0ef41Sopenharmony_ci                                 fixtures.path('es-modules/loop.mjs'));
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  const session = await child.connectInspectorSession();
1051cb0ef41Sopenharmony_ci  await testBreakpointOnStart(session);
1061cb0ef41Sopenharmony_ci  await testBreakpoint(session);
1071cb0ef41Sopenharmony_ci  await session.runToCompletion();
1081cb0ef41Sopenharmony_ci  assert.strictEqual((await child.expectShutdown()).exitCode, 55);
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_cirunTest().then(common.mustCall());
112