11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst helper = require('../../common/report.js');
71cb0ef41Sopenharmony_ciconst tmpdir = require('../../common/tmpdir');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
101cb0ef41Sopenharmony_ciconst addon = require(binding);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cifunction myAddonMain(method, { hasContext, hasEnv }) {
131cb0ef41Sopenharmony_ci  tmpdir.refresh();
141cb0ef41Sopenharmony_ci  process.report.directory = tmpdir.path;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  addon[method]();
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const reports = helper.findReports(process.pid, tmpdir.path);
191cb0ef41Sopenharmony_ci  assert.strictEqual(reports.length, 1);
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  const report = reports[0];
221cb0ef41Sopenharmony_ci  helper.validate(report, [
231cb0ef41Sopenharmony_ci    ['header.event', 'FooMessage'],
241cb0ef41Sopenharmony_ci    ['header.trigger', 'BarTrigger'],
251cb0ef41Sopenharmony_ci  ]);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  const content = require(report);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  // Check that the javascript stack is present.
301cb0ef41Sopenharmony_ci  if (hasContext) {
311cb0ef41Sopenharmony_ci    assert.strictEqual(content.javascriptStack.stack.findIndex((frame) => frame.match('myAddonMain')), 0);
321cb0ef41Sopenharmony_ci  } else {
331cb0ef41Sopenharmony_ci    assert.strictEqual(content.javascriptStack.message, 'No stack.');
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  if (hasEnv) {
371cb0ef41Sopenharmony_ci    assert.strictEqual(content.header.threadId, 0);
381cb0ef41Sopenharmony_ci  } else {
391cb0ef41Sopenharmony_ci    assert.strictEqual(content.header.threadId, null);
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciconst methods = [
441cb0ef41Sopenharmony_ci  ['triggerReport', true, true],
451cb0ef41Sopenharmony_ci  ['triggerReportNoIsolate', false, false],
461cb0ef41Sopenharmony_ci  ['triggerReportEnv', true, true],
471cb0ef41Sopenharmony_ci  ['triggerReportNoEnv', false, false],
481cb0ef41Sopenharmony_ci  ['triggerReportNoContext', false, false],
491cb0ef41Sopenharmony_ci  ['triggerReportNewContext', true, false],
501cb0ef41Sopenharmony_ci];
511cb0ef41Sopenharmony_cifor (const [method, hasContext, hasEnv] of methods) {
521cb0ef41Sopenharmony_ci  myAddonMain(method, { hasContext, hasEnv });
531cb0ef41Sopenharmony_ci}
54