11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciif (!process.features.inspector) return;
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_cirequire('../common');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst fs = require('fs');
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ciconst intervals = 40;
151cb0ef41Sopenharmony_ci// Outputs coverage when v8.takeCoverage() is invoked.
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
181cb0ef41Sopenharmony_ci    '-r',
191cb0ef41Sopenharmony_ci    fixtures.path('v8-coverage', 'take-coverage'),
201cb0ef41Sopenharmony_ci    fixtures.path('v8-coverage', 'interval'),
211cb0ef41Sopenharmony_ci  ], {
221cb0ef41Sopenharmony_ci    env: {
231cb0ef41Sopenharmony_ci      ...process.env,
241cb0ef41Sopenharmony_ci      NODE_V8_COVERAGE: tmpdir.path,
251cb0ef41Sopenharmony_ci      NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER',
261cb0ef41Sopenharmony_ci      TEST_INTERVALS: intervals
271cb0ef41Sopenharmony_ci    },
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  console.log(output.stderr.toString());
301cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
311cb0ef41Sopenharmony_ci  const coverageFiles = fs.readdirSync(tmpdir.path);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  let coverages = [];
341cb0ef41Sopenharmony_ci  for (const coverageFile of coverageFiles) {
351cb0ef41Sopenharmony_ci    const coverage = require(path.join(tmpdir.path, coverageFile));
361cb0ef41Sopenharmony_ci    for (const result of coverage.result) {
371cb0ef41Sopenharmony_ci      if (result.url.includes('/interval')) {
381cb0ef41Sopenharmony_ci        coverages.push({
391cb0ef41Sopenharmony_ci          file: coverageFile,
401cb0ef41Sopenharmony_ci          func: result.functions.find((f) => f.functionName === 'interval'),
411cb0ef41Sopenharmony_ci          timestamp: coverage.timestamp
421cb0ef41Sopenharmony_ci        });
431cb0ef41Sopenharmony_ci      }
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  coverages = coverages.sort((a, b) => { return a.timestamp - b.timestamp; });
481cb0ef41Sopenharmony_ci  // There should be two coverages taken, one triggered by v8.takeCoverage(),
491cb0ef41Sopenharmony_ci  // the other by process exit.
501cb0ef41Sopenharmony_ci  console.log('Coverages:', coverages);
511cb0ef41Sopenharmony_ci  assert.strictEqual(coverages.length, 3);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  let blockHitsTotal = 0;
541cb0ef41Sopenharmony_ci  for (let i = 0; i < coverages.length; ++i) {
551cb0ef41Sopenharmony_ci    const { ranges } = coverages[i].func;
561cb0ef41Sopenharmony_ci    console.log('coverage', i, ranges);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    if (i !== coverages.length - 1) {
591cb0ef41Sopenharmony_ci      // When the first two coverages are taken:
601cb0ef41Sopenharmony_ci      assert.strictEqual(ranges.length, 2);
611cb0ef41Sopenharmony_ci      const blockHits = ranges[0].count;
621cb0ef41Sopenharmony_ci      // The block inside interval() should be hit at least once.
631cb0ef41Sopenharmony_ci      assert.notStrictEqual(blockHits, 0);
641cb0ef41Sopenharmony_ci      blockHitsTotal += blockHits;
651cb0ef41Sopenharmony_ci      // The else branch should not be hit.
661cb0ef41Sopenharmony_ci      const elseBranchHits = ranges[1].count;
671cb0ef41Sopenharmony_ci      assert.strictEqual(elseBranchHits, 0);
681cb0ef41Sopenharmony_ci    } else {
691cb0ef41Sopenharmony_ci      // At process exit:
701cb0ef41Sopenharmony_ci      assert.strictEqual(ranges.length, 3);
711cb0ef41Sopenharmony_ci      const blockHits = ranges[0].count;
721cb0ef41Sopenharmony_ci      // The block inside interval() should be hit at least once more.
731cb0ef41Sopenharmony_ci      assert.notStrictEqual(blockHits, 0);
741cb0ef41Sopenharmony_ci      blockHitsTotal += blockHits;
751cb0ef41Sopenharmony_ci      // The else branch should be hit exactly once.
761cb0ef41Sopenharmony_ci      const elseBranchHits = ranges[2].count;
771cb0ef41Sopenharmony_ci      assert.strictEqual(elseBranchHits, 1);
781cb0ef41Sopenharmony_ci      const ifBranchHits = ranges[1].count;
791cb0ef41Sopenharmony_ci      assert.strictEqual(ifBranchHits, blockHits - elseBranchHits);
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  // The block should be hit `intervals` times in total.
841cb0ef41Sopenharmony_ci  assert.strictEqual(blockHitsTotal, intervals);
851cb0ef41Sopenharmony_ci}
86