1'use strict'; 2const common = require('../common'); 3 4common.skipIfInspectorDisabled(); 5 6const assert = require('assert'); 7const { resolve: UrlResolve } = require('url'); 8const fixtures = require('../common/fixtures'); 9const { NodeInstance } = require('../common/inspector-helper.js'); 10 11function assertScopeValues({ result }, expected) { 12 const unmatched = new Set(Object.keys(expected)); 13 for (const actual of result) { 14 const value = expected[actual.name]; 15 assert.strictEqual(actual.value.value, value); 16 unmatched.delete(actual.name); 17 } 18 assert.deepStrictEqual(Array.from(unmatched.values()), []); 19} 20 21async function testBreakpointOnStart(session) { 22 console.log('[test]', 23 'Verifying debugger stops on start (--inspect-brk option)'); 24 const commands = [ 25 { 'method': 'Runtime.enable' }, 26 { 'method': 'Debugger.enable' }, 27 { 'method': 'Debugger.setPauseOnExceptions', 28 'params': { 'state': 'none' } }, 29 { 'method': 'Debugger.setAsyncCallStackDepth', 30 'params': { 'maxDepth': 0 } }, 31 { 'method': 'Profiler.enable' }, 32 { 'method': 'Profiler.setSamplingInterval', 33 'params': { 'interval': 100 } }, 34 { 'method': 'Debugger.setBlackboxPatterns', 35 'params': { 'patterns': [] } }, 36 { 'method': 'Runtime.runIfWaitingForDebugger' } 37 ]; 38 39 await session.send(commands); 40 await session.waitForBreakOnLine( 41 0, UrlResolve(session.scriptURL().toString(), 'message.mjs')); 42} 43 44async function testBreakpoint(session) { 45 console.log('[test]', 'Setting a breakpoint and verifying it is hit'); 46 const commands = [ 47 { 'method': 'Debugger.setBreakpointByUrl', 48 'params': { 'lineNumber': 7, 49 'url': session.scriptURL(), 50 'columnNumber': 0, 51 'condition': '' 52 } 53 }, 54 { 'method': 'Debugger.resume' }, 55 ]; 56 await session.send(commands); 57 const { scriptSource } = await session.send({ 58 'method': 'Debugger.getScriptSource', 59 'params': { 'scriptId': session.mainScriptId } }); 60 assert(scriptSource && (scriptSource.includes(session.script())), 61 `Script source is wrong: ${scriptSource}`); 62 63 await session.waitForConsoleOutput('log', ['A message', 5]); 64 const paused = await session.waitForBreakOnLine(7, session.scriptURL()); 65 const scopeId = paused.params.callFrames[0].scopeChain[0].object.objectId; 66 67 console.log('[test]', 'Verify we can read current application state'); 68 const response = await session.send({ 69 'method': 'Runtime.getProperties', 70 'params': { 71 'objectId': scopeId, 72 'ownProperties': false, 73 'accessorPropertiesOnly': false, 74 'generatePreview': true 75 } 76 }); 77 assertScopeValues(response, { t: 1001, k: 1, message: 'A message' }); 78 79 let { result } = await session.send({ 80 'method': 'Debugger.evaluateOnCallFrame', 'params': { 81 'callFrameId': session.pausedDetails().callFrames[0].callFrameId, 82 'expression': 'k + t', 83 'objectGroup': 'console', 84 'includeCommandLineAPI': true, 85 'silent': false, 86 'returnByValue': false, 87 'generatePreview': true 88 } 89 }); 90 91 assert.strictEqual(result.value, 1002); 92 93 result = (await session.send({ 94 'method': 'Runtime.evaluate', 'params': { 95 'expression': '5 * 5' 96 } 97 })).result; 98 assert.strictEqual(result.value, 25); 99} 100 101async function runTest() { 102 const child = new NodeInstance(['--inspect-brk=0'], '', 103 fixtures.path('es-modules/loop.mjs')); 104 105 const session = await child.connectInspectorSession(); 106 await testBreakpointOnStart(session); 107 await testBreakpoint(session); 108 await session.runToCompletion(); 109 assert.strictEqual((await child.expectShutdown()).exitCode, 55); 110} 111 112runTest().then(common.mustCall()); 113