11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.isMainThread)
61cb0ef41Sopenharmony_ci  common.skip('process.chdir is not available in Workers');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst { writeHeapSnapshot, getHeapSnapshot } = require('v8');
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst fs = require('fs');
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ciprocess.chdir(tmpdir.path);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  writeHeapSnapshot('my.heapdump');
181cb0ef41Sopenharmony_ci  fs.accessSync('my.heapdump');
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci{
221cb0ef41Sopenharmony_ci  const heapdump = writeHeapSnapshot();
231cb0ef41Sopenharmony_ci  assert.strictEqual(typeof heapdump, 'string');
241cb0ef41Sopenharmony_ci  fs.accessSync(heapdump);
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci  const directory = 'directory';
291cb0ef41Sopenharmony_ci  fs.mkdirSync(directory);
301cb0ef41Sopenharmony_ci  assert.throws(() => {
311cb0ef41Sopenharmony_ci    writeHeapSnapshot(directory);
321cb0ef41Sopenharmony_ci  }, (e) => {
331cb0ef41Sopenharmony_ci    assert.ok(e, 'writeHeapSnapshot should error');
341cb0ef41Sopenharmony_ci    assert.strictEqual(e.code, 'EISDIR');
351cb0ef41Sopenharmony_ci    assert.strictEqual(e.syscall, 'open');
361cb0ef41Sopenharmony_ci    return true;
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci[1, true, {}, [], null, Infinity, NaN].forEach((i) => {
411cb0ef41Sopenharmony_ci  assert.throws(() => writeHeapSnapshot(i), {
421cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
431cb0ef41Sopenharmony_ci    name: 'TypeError',
441cb0ef41Sopenharmony_ci    message: 'The "path" argument must be of type string or an instance of ' +
451cb0ef41Sopenharmony_ci             'Buffer or URL.' +
461cb0ef41Sopenharmony_ci             common.invalidArgTypeHelper(i)
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci});
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  let data = '';
521cb0ef41Sopenharmony_ci  const snapshot = getHeapSnapshot();
531cb0ef41Sopenharmony_ci  snapshot.setEncoding('utf-8');
541cb0ef41Sopenharmony_ci  snapshot.on('data', common.mustCallAtLeast((chunk) => {
551cb0ef41Sopenharmony_ci    data += chunk.toString();
561cb0ef41Sopenharmony_ci  }));
571cb0ef41Sopenharmony_ci  snapshot.on('end', common.mustCall(() => {
581cb0ef41Sopenharmony_ci    JSON.parse(data);
591cb0ef41Sopenharmony_ci  }));
601cb0ef41Sopenharmony_ci}
61