 touch = require 'touch'
 date = require 'date-utils'
 mkdirp = require 'mkdirp'
+moment = require 'moment'
 log = require('printit')
     prefix: "Tests"
 

operationQueue = require '../../backend/operation_queue'
{syncPath} = helpers.options

describe "Operation Queue Tests", ->
    @timeout 4000

    before cliHelpers.resetDatabase
    before cliHelpers.initConfiguration
    before fileHelpers.deleteAll
    after cliHelpers.resetDatabase

    describe "createFileLocally", ->

                        docType: 'Binary'
                        checksum: sum
                        path: 'chat-mignon.jpg'

                    pouch.db.put binaryDoc, (err) ->
                        operationQueue.createFileLocally doc, (err) ->
                            should.not.exist err
                            done()

            it 'creates parent Folder', ->
                fs.existsSync(folderPath).should.be.ok

            it 'copy the local file', (done) ->
                fs.existsSync(filePath).should.be.ok
                filesystem.checksum fixturePath, (err, baseSum) =>
                    filesystem.checksum filePath, (err, sum) ->
                        sum.should.equal baseSum
                        done()

            it 'update last modification date', ->
                stat = fs.statSync filePath
                mtime = moment stat.mtime
                expected = lastModification.millisecond(0)
                mtime.format().should.equal lastModification.format()

        describe 'File exists remotely', ->
            fileName = 'cool-pillow.jpg'
            folderName = 'dossier-2'
            fileRelativePath = path.join '..', 'fixtures', fileName
            fixturePath = path.resolve __dirname, fileRelativePath
            folderPath = path.join syncPath, folderName
            filePath = path.join syncPath, folderName, fileName
            lastModification = null

            before (done) ->
                binaryDoc =
                    _id: '124'
                    docType: 'Binary'
                    path: 'dossier-2/cool-pillow.jpg'

                folderHelpers.createFolder folderName, ->
                    fileHelpers.uploadFile fileName, fixturePath, folderName, (err, doc) ->
                        binaryDoc =
                            _id: doc.binary.file.id
                            docType: 'Binary'
                            path: 'dossier-2/cool-pillow.jpg'
                        lastModification = moment doc.lastModification
                        pouch.db.put binaryDoc, (err) ->

                            operationQueue.createFileLocally doc, (err) ->
                                should.not.exist err
                                done()

            it 'creates parent Folder', ->
                fs.existsSync(folderPath).should.be.ok

            it 'copy the local file', (done) ->
                fs.existsSync(filePath).should.be.ok
                filesystem.checksum fixturePath, (err, baseSum) =>
                    filesystem.checksum filePath, (err, sum) ->
                        sum.should.equal baseSum
                        done()

            it 'update last modification date', ->
                stat = fs.statSync filePath
                mtime = moment stat.mtime
                expected = lastModification.millisecond(0)
                mtime.format().should.equal lastModification.format()

