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 fixtures = require('../common/fixtures');
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst fs = require('fs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst v8 = require('v8');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// By default it should be false. We'll test that it's true in snapshot
161cb0ef41Sopenharmony_ci// building mode in the fixture.
171cb0ef41Sopenharmony_ciassert(!v8.startupSnapshot.isBuildingSnapshot());
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_citmpdir.refresh();
201cb0ef41Sopenharmony_ciconst blobPath = path.join(tmpdir.path, 'snapshot.blob');
211cb0ef41Sopenharmony_ciconst entry = fixtures.path('snapshot', 'v8-startup-snapshot-api.js');
221cb0ef41Sopenharmony_ci{
231cb0ef41Sopenharmony_ci  for (const book of [
241cb0ef41Sopenharmony_ci    'book1.en_US.txt',
251cb0ef41Sopenharmony_ci    'book1.es_ES.txt',
261cb0ef41Sopenharmony_ci    'book2.zh_CN.txt',
271cb0ef41Sopenharmony_ci  ]) {
281cb0ef41Sopenharmony_ci    const content = `This is ${book}`;
291cb0ef41Sopenharmony_ci    fs.writeFileSync(path.join(tmpdir.path, book), content, 'utf8');
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci  fs.copyFileSync(entry, path.join(tmpdir.path, 'entry.js'));
321cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
331cb0ef41Sopenharmony_ci    '--snapshot-blob',
341cb0ef41Sopenharmony_ci    blobPath,
351cb0ef41Sopenharmony_ci    '--build-snapshot',
361cb0ef41Sopenharmony_ci    'entry.js',
371cb0ef41Sopenharmony_ci  ], {
381cb0ef41Sopenharmony_ci    cwd: tmpdir.path
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci  if (child.status !== 0) {
411cb0ef41Sopenharmony_ci    console.log(child.stderr.toString());
421cb0ef41Sopenharmony_ci    console.log(child.stdout.toString());
431cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci  const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
461cb0ef41Sopenharmony_ci  assert(stats.isFile());
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci{
501cb0ef41Sopenharmony_ci  const child = spawnSync(process.execPath, [
511cb0ef41Sopenharmony_ci    '--snapshot-blob',
521cb0ef41Sopenharmony_ci    blobPath,
531cb0ef41Sopenharmony_ci    'book1',
541cb0ef41Sopenharmony_ci  ], {
551cb0ef41Sopenharmony_ci    cwd: tmpdir.path,
561cb0ef41Sopenharmony_ci    env: {
571cb0ef41Sopenharmony_ci      ...process.env,
581cb0ef41Sopenharmony_ci      BOOK_LANG: 'en_US',
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci  });
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  const stdout = child.stdout.toString().trim();
631cb0ef41Sopenharmony_ci  const stderr = child.stderr.toString().trim();
641cb0ef41Sopenharmony_ci  assert.strictEqual(stderr, 'Reading book1.en_US.txt');
651cb0ef41Sopenharmony_ci  assert.strictEqual(stdout, 'This is book1.en_US.txt');
661cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
671cb0ef41Sopenharmony_ci}
68