1import { skipIfInspectorDisabled } from '../common/index.mjs';
2
3skipIfInspectorDisabled();
4
5import * as fixtures from '../common/fixtures.mjs';
6import startCLI from '../common/debugger.js';
7
8import assert from 'assert';
9import { spawn } from 'child_process';
10
11// NOTE(oyyd): We might want to import this regexp from "lib/_inspect.js"?
12const kDebuggerMsgReg = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\//;
13
14function launchTarget(...args) {
15  const childProc = spawn(process.execPath, args);
16  return new Promise((resolve, reject) => {
17    const onExit = () => {
18      reject(new Error('Child process exits unexpectedly'));
19    };
20    childProc.on('exit', onExit);
21    childProc.stderr.setEncoding('utf8');
22    let data = '';
23    childProc.stderr.on('data', (chunk) => {
24      data += chunk;
25      const ret = kDebuggerMsgReg.exec(data);
26      childProc.removeListener('exit', onExit);
27      if (ret) {
28        resolve({
29          childProc,
30          host: ret[1],
31          port: ret[2],
32        });
33      }
34    });
35  });
36}
37
38{
39  const script = fixtures.path('debugger/alive.js');
40  let cli = null;
41  let target = null;
42
43  function cleanup(error) {
44    if (cli) {
45      cli.quit();
46      cli = null;
47    }
48    if (target) {
49      target.kill();
50      target = null;
51    }
52    assert.ifError(error);
53  }
54
55  try {
56    const { childProc, host, port } = await launchTarget('--inspect=0', script);
57    target = childProc;
58    cli = startCLI([`${host || '127.0.0.1'}:${port}`]);
59    await cli.waitForPrompt();
60    await cli.command('sb("alive.js", 3)');
61    await cli.waitFor(/break/);
62    await cli.waitForPrompt();
63    assert.match(
64      cli.output,
65      /> 3 {3}\+\+x;/,
66      'marks the 3rd line'
67    );
68  } finally {
69    cleanup();
70  }
71}
72