fs = require('fs')
_ = require('lodash')
chai = require('chai')
expect = chai.expect
sinon = require('sinon')
chai.use(require('sinon-chai'))
fileslice = require('../lib/fileslice')

describe 'Fileslice:', ->

	describe '.copy()', ->

		it 'should throw if file is not a string', ->
			expect ->
				fileslice.copy(123, 'output', { start: 0, end: 45 }, _.noop)
			.to.throw('File should be a string')

		it 'should throw if output is not a string', ->
			expect ->
				fileslice.copy('file', 123, { start: 0, end: 45 }, _.noop)
			.to.throw('Output should be a string')

		it 'should throw if options.start is not a number', ->
			expect ->
				fileslice.copy('file', 'output', { start: 'hello', end: 45 }, _.noop)
			.to.throw('Start option should be a number')

		it 'should throw if options.end is not a number', ->
			expect ->
				fileslice.copy('file', 'output', { start: 0, end: 'hello' }, _.noop)
			.to.throw('End option should be a number')

		it 'should throw if callback is not a function', ->
			expect ->
				fileslice.copy('file', 'output', { start: 0, end: 512 }, 123)
			.to.throw('Callback should be a function')

		it 'should throw if end <= start', ->
			expect ->
				fileslice.copy('file', 'output', { start: 6, end: 2 }, _.noop)
			.to.throw('Invalid ranges: start should be less than end')

			expect ->
				fileslice.copy('file', 'output', { start: 2, end: 2 }, _.noop)
			.to.throw('Invalid ranges: start should be less than end')

		describe 'given input does not exist', ->

			beforeEach ->
				@fsExistsStub = sinon.stub(fs, 'exists')
				@fsExistsStub.yields(false)

			afterEach ->
				@fsExistsStub.restore()

			it 'should return an error', (done) ->
				fileslice.copy 'file', 'output',
					start: 0
					end: 512
				, (error) ->
					expect(error).to.be.an.instanceof(Error)
					expect(error.message).to.equal('File does not exist: file')
					done()
