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_ci{
151cb0ef41Sopenharmony_ci  // The list of modules supported in the snapshot is unstable, so just check
161cb0ef41Sopenharmony_ci  // a few that are known to work.
171cb0ef41Sopenharmony_ci  const code = `
181cb0ef41Sopenharmony_ci    require("node:v8");
191cb0ef41Sopenharmony_ci    require("node:fs");
201cb0ef41Sopenharmony_ci    require("node:fs/promises");
211cb0ef41Sopenharmony_ci  `;
221cb0ef41Sopenharmony_ci  fs.writeFileSync(
231cb0ef41Sopenharmony_ci    path.join(tmpdir.path, 'entry.js'),
241cb0ef41Sopenharmony_ci    code,
251cb0ef41Sopenharmony_ci    'utf8'
261cb0ef41Sopenharmony_ci  );
271cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
281cb0ef41Sopenharmony_ci    '--snapshot-blob',
291cb0ef41Sopenharmony_ci    blobPath,
301cb0ef41Sopenharmony_ci    '--build-snapshot',
311cb0ef41Sopenharmony_ci    'entry.js',
321cb0ef41Sopenharmony_ci  ], {
331cb0ef41Sopenharmony_ci    cwd: tmpdir.path
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci  if (child.status !== 0) {
361cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
371cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
381cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
411cb0ef41Sopenharmony_ci  assert(stats.isFile());
421cb0ef41Sopenharmony_ci}
43