<commit_before>{ exec, spawn } = require('child_process')
assert = require 'assert'
tmp = require 'tmp'
path = require 'path'
fs = require 'fs'

versal = path.resolve __dirname, '../bin/versal'

describe 'versal cli', ->
  cwd = null

  before (done) ->
    tmp.dir (err, dir) ->
      cwd = dir
      done err

  it 'version', (done) ->
    exec "#{versal} -v", (err, stdout, stderr) ->
      assert.equal stdout.trim(), process.env.npm_package_version
      assert !stderr
      done(err)

  it 'usage', (done) ->
    exec "#{versal}", (err, stdout, stderr) ->
      assert stdout.length
      assert !stderr
      done(err)

  it 'create foo', (done) ->
    exec "#{versal} create foo", { cwd }, (err, stdout, stderr) ->
      fs.exists path.join(cwd, 'foo/manifest.json'), (exists) ->
        assert exists
        done()

  describe 'preview foo', ->
    preview = null

    after ->
      if preview && !preview.killed then preview.kill()

    it 'on port 3073', (done) ->
      preview = spawn versal, ['preview', 'foo', '--port', '3073'], { cwd }

      preview.stdout.on 'data', (data) ->
        if data.toString().indexOf('localhost:3073') > 0
          preview.kill 'SIGINT'

      preview.stderr.on 'data', (err) ->
        done new Error err.toString()

      preview.on 'close', (code,signal) ->
        assert.equal code, 130
        done()

  describe 'signin', ->
    signin = null

    after ->
      if signin && !signin.killed then signin.kill()

    it 'with credentials', (done) ->
      args = ['signin',
        '--authUrl', 'versal-api',
        '--email', 'foo',
        '--password', '<PASSWORD>']
      signin = spawn versal, args, { cwd }

      signin.stdout.on 'data', (data) ->
        if data.toString().indexOf('Signing in to versal-api') == 0
          signin.kill 'SIGINT'
          assert true
          done()

      signin.stderr.on 'data', (err) ->
        done new Error err.toString()

  # We terminate the process after manifest is found and
  # apiUrl is established
  describe 'upload', ->
    upload = null

    after ->
      if upload && !upload.killed then upload.kill()

    it 'with credentials', (done) ->
      upload = spawn versal, ['upload', 'foo', '--apiUrl', 'versal-api', '--sid', 'foo'], { cwd }

      upload.stdout.on 'data', (data) ->
        if data.toString().indexOf('foo@0.0.1 to versal-api') > 0
          upload.kill 'SIGINT'
          assert true
          done()

      upload.stderr.on 'data', (err) ->
        done new Error err.toString()<commit_msg>make travis happy<commit_after>{ exec, spawn } = require('child_process')
assert = require 'assert'
tmp = require 'tmp'
path = require 'path'
fs = require 'fs'

versal = path.resolve __dirname, '../bin/versal'

describe 'versal cli', ->
  cwd = null

  before (done) ->
    tmp.dir (err, dir) ->
      cwd = dir
      done err

  it 'version', (done) ->
    exec "#{versal} -v", (err, stdout, stderr) ->
      assert.equal stdout.trim(), process.env.npm_package_version
      assert !stderr
      done(err)

  it 'usage', (done) ->
    exec "#{versal}", (err, stdout, stderr) ->
      assert stdout.length
      assert !stderr
      done(err)

  it 'create foo', (done) ->
    exec "#{versal} create foo", { cwd }, (err, stdout, stderr) ->
      fs.exists path.join(cwd, 'foo/manifest.json'), (exists) ->
        assert exists
        done()

  describe 'preview foo', ->
    preview = null

    after ->
      if preview && !preview.killed then preview.kill()

    it 'on port 3073', (done) ->
      preview = spawn versal, ['preview', 'foo', '--port', '3073'], { cwd }

      preview.stdout.on 'data', (data) ->
        if data.toString().indexOf('localhost:3073') > 0
          preview.kill 'SIGINT'

      preview.stderr.on 'data', (err) ->
        done new Error err.toString()

      preview.on 'close', (code,signal) ->
        assert.equal code, 130
        done()

  describe 'signin', ->
    signin = null

    after ->
      if signin && !signin.killed then signin.kill()

    it 'with credentials', (done) ->
      args = ['signin',
        '--authUrl', 'http://versal-api',
        '--email', 'foo',
        '--password', '<PASSWORD>']
      signin = spawn versal, args, { cwd }

      signin.stdout.on 'data', (data) ->
        if data.toString().indexOf('Signing in to versal-api') == 0
          signin.kill 'SIGINT'
          assert true
          done()

      signin.stderr.on 'data', (err) ->
        done new Error err.toString()

  # We terminate the process after manifest is found and
  # apiUrl is established
  describe 'upload', ->
    upload = null

    after ->
      if upload && !upload.killed then upload.kill()

    it 'with credentials', (done) ->
      upload = spawn versal, ['upload', 'foo', '--apiUrl', 'http://versal-api', '--sid', 'foo'], { cwd }

      upload.stdout.on 'data', (data) ->
        if data.toString().indexOf('foo@0.0.1 to versal-api') > 0
          upload.kill 'SIGINT'
          assert true
          done()

      upload.stderr.on 'data', (err) ->
        done new Error err.toString()
{'fraction_non_alphanumeric': 0.16574585635359115, 'fraction_numerical': 0.008287292817679558, 'mean_word_length': 2.2102596580114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3178295', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1583, 'n_words': 463}