const path = require('path')
const fs = require('fs')
const should = require('should')
const { init, clean, cwd, exec } = require('./utils')

describe('cli', () => {
  before(init)
  beforeEach(clean)

  it('should fail when passing unknown option', () => {
    should(() => {
      exec('--unknown-arg value')
    }).throw(/Unknown argument/)
  })

  it('--version should return version', () => {
    exec('--version').should.match(/cli version/)
  })

  it('--help should return message', () => {
    exec('--help').should.match(/Usage:\n\njsreport/)
  })

  it('should fail when command receives unknown option', () => {
    should(() => {
      exec('help --unknown-arg value')
    }).throw(/Unknown argument/)
  })

  it('"help config" should return message', () => {
    const result = exec('help config')

    result.should.match(/Configuration format description/)
    result.should.match(/"extensions": <object> {/)
  })

  it('render should write to output file', () => {
    fs.writeFileSync(path.join(cwd, 'test.html'), 'foo')
    exec('render --template.content=test.html --template.engine=none --template.recipe=html --out=out.html')
    fs.readFileSync(path.join(cwd, 'out.html')).toString().should.be.eql('foo')
  })

  it('render failing instance should provide init error', () => {
    fs.writeFileSync(path.join(cwd, 'jsreport.config.json'), 'intention')
    fs.writeFileSync(path.join(cwd, 'test.html'), 'foo')
    should(() => {
      exec('render --template.content=test.html --template.engine=none --template.recipe=html --out=out.html')
    }).throw(/Error parsing your configuration file/)
  })

  it('render with keepAlive and failing instance should provide init error', () => {
    fs.writeFileSync(path.join(cwd, 'jsreport.config.json'), 'intention')
    fs.writeFileSync(path.join(cwd, 'test.html'), 'foo')
    should(() => {
      exec('render --keepAlive --template.content=test.html --template.engine=none --template.recipe=html --out=out.html')
    }).throw(/Error parsing your configuration file/)
  })
})

@@ -273,32 +273,189 @@
 should(() =%3E %7B%0D%0A
+      exec('--unknown value')%0D%0A    %7D).throw(/Unknown argument/)%0D%0A  %7D)%0D%0A%0D%0A  it('should fail when passing unknown dashed option', () =%3E %7B%0D%0A    should(() =%3E %7B%0D%0A
       exec('--un
@@ -830,20 +830,183 @@
   exec('
-help
+ --unknown value')%0D%0A    %7D).throw(/Unknown argument/)%0D%0A  %7D)%0D%0A%0D%0A  it('should fail when command receives unknown dashed option', () =%3E %7B%0D%0A    should(() =%3E %7B%0D%0A      exec('
  --unkno
@@ -1003,32 +1003,33 @@
 (' --unknown-arg
+s
  value')%0D%0A    %7D)

add tests cases for unknown args