{"size": 2296, "ext": "js", "max_stars_repo_path": "test/basic.js", "max_stars_repo_name": "gschaden/moar-profiler", "max_stars_repo_stars_event_min_datetime": "2017-11-10T00:34:34.000Z", "max_stars_repo_stars_event_max_datetime": "2021-01-05T09:43:24.000Z", "max_issues_repo_path": "test/basic.js", "max_issues_repo_name": "gschaden/moar-profiler", "max_issues_repo_issues_event_min_datetime": "2017-09-09T05:47:15.000Z", "max_issues_repo_issues_event_max_datetime": "2017-09-09T19:17:20.000Z", "max_forks_repo_path": "test/basic.js", "max_forks_repo_name": "moar-things/moar-profiler", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 23.9166666667, "max_line_length": 108, "alphanum_fraction": 0.6424216028}
'use strict'

const moarProfiler = require('..')
const utils = require('./lib/utils')

const profileTestCode = require('./profile-test-code')

const runTest = utils.createTestRunner(__filename)

runTest(function basicRemote (t) {
  const inspectorPort = process.env.INSPECTOR_PORT

  if (inspectorPort == null) {
    t.fail('Expecting the env var INSPECTOR_PORT to be set to the port of an active inspector on localhost')
    t.end()
    return
  }

  const url = `http://localhost:${inspectorPort}`

  moarProfiler.profile(url, { duration: 1 }, (err, profile) => {
    if (err) {
      t.error(err, `error running profile: ${err}`)
      t.end()
      return
    }

    validateProfile(t, profile)
    t.end()

    profile.timeDeltas = []
    profile.samples = []
  })
})

runTest(function runTestInProcess (t) {
  profileTestCode.run(2000)

  moarProfiler.profile(null, { duration: 1 }, (err, profile) => {
    if (err) {
      t.error(err, `error running profile: ${err}`)
      t.end()
      return
    }

    validateProfile(t, profile)
    t.end()
  })
})

function validateProfile (t, profile) {
  t.pass('starting validation')

  if (!Array.isArray(profile.nodes)) t.fail('profile has a nodes object')
  if (!Array.isArray(profile.scripts)) t.fail('profile has a scripts object')
  if (!Array.isArray(profile.pkgs)) t.fail('profile has a pkgs object')

  const nodes = new Map()
  const scripts = new Map()
  const pkgs = new Map()

  for (let node of profile.nodes) { nodes.set(node.id, node) }
  for (let script of profile.scripts) { scripts.set(script.id, script) }
  for (let pkg of profile.pkgs) { pkgs.set(pkg.path, pkg) }

  let testsRun = 0
  let testsPassed = 0

  for (let node of profile.nodes) {
    if (node.children == null) continue

    for (let child of node.children) {
      testOk(t, nodes.get(child), `child node ${child} found`)
    }

    testOk(t, scripts.get(node.callFrame.scriptId), `script ${node.callFrame.scriptId} found`)
  }

  for (let script of profile.scripts) {
    const pkg = script.pkg
    testOk(t, pkgs.get(pkg), `pkg ${pkg} found`)
  }

  t.equal(testsPassed, testsRun, `all ${testsRun} tests should pass`)

  function testOk (t, value, message) {
    testsRun++
    if (value) {
      testsPassed++
      return
    }

    t.fail(message)
  }
}
