11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This tests snapshot JS API using the example in the docs.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cirequire('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_ciconst path = require('path');
101cb0ef41Sopenharmony_ciconst fs = require('fs');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_citmpdir.refresh();
131cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'snapshot.blob');
141cb0ef41Sopenharmony_ciconst code = `
151cb0ef41Sopenharmony_cirequire('v8').startupSnapshot.setDeserializeMainFunction(() => {
161cb0ef41Sopenharmony_ci  console.log(JSON.stringify(process.argv));
171cb0ef41Sopenharmony_ci});
181cb0ef41Sopenharmony_ci`;
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  fs.writeFileSync(path.join(tmpdir.path, 'entry.js'), code, 'utf8');
211cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
221cb0ef41Sopenharmony_ci    '--snapshot-blob',
231cb0ef41Sopenharmony_ci    blobPath,
241cb0ef41Sopenharmony_ci    '--build-snapshot',
251cb0ef41Sopenharmony_ci    'entry.js',
261cb0ef41Sopenharmony_ci  ], {
271cb0ef41Sopenharmony_ci    cwd: tmpdir.path
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  if (child.status !== 0) {
301cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
311cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
321cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
351cb0ef41Sopenharmony_ci  assert(stats.isFile());
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
401cb0ef41Sopenharmony_ci    '--snapshot-blob',
411cb0ef41Sopenharmony_ci    blobPath,
421cb0ef41Sopenharmony_ci    'argv1',
431cb0ef41Sopenharmony_ci    'argv2',
441cb0ef41Sopenharmony_ci  ], {
451cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
461cb0ef41Sopenharmony_ci    env: {
471cb0ef41Sopenharmony_ci      ...process.env,
481cb0ef41Sopenharmony_ci    }
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  const stdout = JSON.parse(child.stdout.toString().trim());
521cb0ef41Sopenharmony_ci  assert.deepStrictEqual(stdout, [
531cb0ef41Sopenharmony_ci    process.execPath,
541cb0ef41Sopenharmony_ci    'argv1',
551cb0ef41Sopenharmony_ci    'argv2',
561cb0ef41Sopenharmony_ci  ]);
571cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
581cb0ef41Sopenharmony_ci}
59