<file_sep>// tslint:disable-next-line:no-var-requires
const path = require('path')
process.env.TS_NODE_PROJECT = path.resolve('test/tsconfig.json')
<file_sep>import {expect, test} from '@oclif/test'
import * as chai from 'chai'
import {describe} from 'mocha'
import * as sinon from 'sinon'
import Snapshot from '../../src/commands/snapshot'
import { DEFAULT_CONFIGURATION } from '../../src/configuration/configuration'
import {AgentService} from '../../src/services/agent-service'
import StaticSnapshotService from '../../src/services/static-snapshot-service'
import {captureStdOut} from '../helpers/stdout'

describe('snapshot', () => {
  describe('#run', () => {
    const sandbox = sinon.createSandbox()

    afterEach(() => {
      sandbox.restore()
    })

    function AgentServiceStub(): AgentService {
      const agentService = AgentService.prototype as AgentService
      sandbox.stub(agentService, 'start')

      const start = new Snapshot([], '') as Snapshot
      sandbox.stub(start, 'agentService').returns(agentService)

      return agentService
    }

    function StaticSnapshotServiceStub(): StaticSnapshotService {
      const staticSnapshotService = StaticSnapshotService.prototype as StaticSnapshotService
      sandbox.stub(staticSnapshotService, 'snapshotAll')
      sandbox.stub(staticSnapshotService, 'start')

      return staticSnapshotService
    }

    it('starts the static snapshot service', async () => {
      const agentServiceStub = AgentServiceStub()
      const staticSnapshotServiceStub = StaticSnapshotServiceStub()

      const stdout = await captureStdOut(async () => {
        await Snapshot.run(['.'])
      })

      chai.expect(agentServiceStub.start).to.be.calledWith(DEFAULT_CONFIGURATION)
      chai.expect(staticSnapshotServiceStub.start).to.have.callCount(1)
      chai.expect(staticSnapshotServiceStub.snapshotAll).to.have.callCount(1)
      chai.expect(stdout).to.match(/\[percy\] percy has started./)
    })
  })

  describe('snapshot command', () => {
    test
      .stub(process, 'env', {PERCY_TOKEN: ''})
      .stderr()
      .command(['snapshot', './test_dir'])
      .exit(0)
      .do((output) => expect(output.stderr).to.contain(
        'Warning: Skipping visual tests. PERCY_TOKEN was not provided.',
      ))
      .it('warns about PERCY_TOKEN not being set and exits gracefully')

    test
      .env({PERCY_TOKEN: 'abc'})
      .command(['snapshot'])
      .exit(2)
      .it('exits when the asset directory arg is missing')
  })
})
<file_sep>import * as std from 'stdout-stderr'

export async function captureStdOut(callback: any): Promise<string> {
  std.stdout.start()
  await callback()
  std.stdout.stop()

  return std.stdout.output
}

export async function captureStdErr(callback: any): Promise<string> {
  std.stderr.start()
  await callback()
  std.stderr.stop()

  return std.stderr.output
}
<file_sep>import * as chai from 'chai'
import {describe} from 'mocha'
import * as nock from 'nock'
import * as sinon from 'sinon'
import Exec from '../../src/commands/exec'
import {AgentService} from '../../src/services/agent-service'
import {DEFAULT_PORT} from '../../src/services/agent-service-constants'
import {captureStdOut} from '../helpers/stdout'

const expect = chai.expect

describe('Exec', () => {
  xdescribe('#run', () => {
    const sandbox = sinon.createSandbox()

    function AgentServiceStub(): AgentService {
      const agentService = AgentService.prototype as AgentService
      sandbox.stub(agentService, 'start')

      const start = new Exec([], '') as Exec
      sandbox.stub(start, 'agentService').returns(agentService)

      return agentService
    }

    beforeEach(() => {
      nock(/localhost/).get('/percy/healthcheck').reply(200)
    })

    afterEach(() => {
      nock.cleanAll()
      sandbox.restore()
    })

    it('starts and stops percy', async () => {
      const agentServiceStub = AgentServiceStub()

      const stdout = await captureStdOut(async () => {
        await Exec.run(['--', 'echo', 'hello'])
      })

      expect(agentServiceStub.start).to.calledWithMatch(DEFAULT_PORT)
      expect(stdout).to.match(/\[percy\] percy has started on port \d+./)
    })

    xit('starts percy on a specific port')
    xit('warns when percy is already running')
  })
})
