// TODO: rename this file to module_api_spec

const path = require('path')
const _ = require('lodash')
const { fs } = require('@packages/server/lib/util/fs')
const { default: systemTests, STDOUT_DURATION_IN_TABLES_RE } = require('../lib/system-tests')
const Fixtures = require('../lib/fixtures')
const { expectCorrectModuleApiResult } = require('../lib/resultsUtils')
const e2ePath = Fixtures.projectPath('e2e')
const { it } = systemTests

const outputPath = path.join(e2ePath, 'output.json')

const specs = [
  'simple_passing_spec.js',
  'simple_hooks_spec.js',
  'simple_failing_spec.js',
  'simple_failing_h*_spec.js', // simple failing hook spec
].join(',')

describe('e2e spec_isolation', () => {
  systemTests.setup()

  it('fails', {
    spec: specs,
    outputPath,
    snapshot: false,
    expectedExitCode: 5,
    config: {
      video: false,
    },
    async onRun (execFn) {
      const { stdout } = await execFn()

      _.each(STDOUT_DURATION_IN_TABLES_RE.exec(stdout), (str) => {
        expect(str.trim(), 'spec durations in tables should not be 0ms').not.eq('0ms')
      })

      // now what we want to do is read in the outputPath
      // and snapshot it so its what we expect after normalizing it
      let json = await fs.readJsonAsync(outputPath)

      json.runs = systemTests.normalizeRuns(json.runs)

      // also mutates into normalized obj ready for snapshot
      expectCorrectModuleApiResult(json, {
        e2ePath, runs: 4, video: false,
      })

      systemTests.snapshot(json, { allowSharedSnapshot: true })
    },
  })

  it('failing with retries enabled', {
    spec: 'simple_failing_hook_spec.js,simple_retrying_spec.js',
    outputPath,
    snapshot: true,
    expectedExitCode: 4,
    config: {
      retries: 1,
      video: false,
    },
    async onRun (execFn) {
      await execFn()
      let json = await fs.readJsonAsync(outputPath)

      json.runs = systemTests.normalizeRuns(json.runs)

      // also mutates into normalized obj ready for snapshot
      expectCorrectModuleApiResult(json, {
        e2ePath, runs: 2, video: false,
      })

      systemTests.snapshot(json)
    },
  })
})
