overleaf/services/filestore/test/unit/js/FileConverterTests.js

108 lines
3.4 KiB
JavaScript
Raw Normal View History

const sinon = require('sinon')
const chai = require('chai')
const { expect } = chai
const SandboxedModule = require('sandboxed-module')
const { Errors } = require('@overleaf/object-persistor')
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
const modulePath = '../../../app/js/FileConverter.js'
2020-08-10 12:01:12 -04:00
describe('FileConverter', function () {
2019-12-19 05:41:41 -05:00
let SafeExec, FileConverter
const sourcePath = '/data/wombat.eps'
const destPath = '/tmp/dest.png'
const format = 'png'
const errorMessage = 'guru meditation error'
const Settings = {
commands: {
2021-07-13 07:04:46 -04:00
convertCommandPrefix: [],
},
2019-12-19 05:41:41 -05:00
}
2020-08-10 12:01:12 -04:00
beforeEach(function () {
2019-12-19 05:41:41 -05:00
SafeExec = {
2021-07-13 07:04:46 -04:00
promises: sinon.stub().resolves(destPath),
2019-12-19 05:41:41 -05:00
}
const ObjectPersistor = { Errors }
2019-12-19 05:41:41 -05:00
FileConverter = SandboxedModule.require(modulePath, {
requires: {
2019-12-19 05:41:41 -05:00
'./SafeExec': SafeExec,
'@overleaf/metrics': {
inc: sinon.stub(),
2021-07-13 07:04:46 -04:00
Timer: sinon.stub().returns({ done: sinon.stub() }),
},
'@overleaf/settings': Settings,
2021-07-13 07:04:46 -04:00
'@overleaf/object-persistor': ObjectPersistor,
},
})
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
describe('convert', function () {
it('should convert the source to the requested format', async function () {
2019-12-19 05:41:41 -05:00
await FileConverter.promises.convert(sourcePath, format)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
expect(args).to.include(`${sourcePath}.${format}`)
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
it('should return the dest path', async function () {
2019-12-19 05:41:41 -05:00
const destPath = await FileConverter.promises.convert(sourcePath, format)
destPath.should.equal(`${sourcePath}.${format}`)
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
it('should wrap the error from convert', async function () {
2019-12-19 05:41:41 -05:00
SafeExec.promises.rejects(errorMessage)
try {
await FileConverter.promises.convert(sourcePath, format)
expect('error should have been thrown').not.to.exist
} catch (err) {
expect(err.name).to.equal('ConversionError')
expect(err.cause.toString()).to.equal(errorMessage)
}
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
it('should not accept an non approved format', async function () {
2019-12-19 05:41:41 -05:00
try {
await FileConverter.promises.convert(sourcePath, 'potato')
expect('error should have been thrown').not.to.exist
} catch (err) {
expect(err.name).to.equal('ConversionError')
}
})
2020-08-10 12:01:12 -04:00
it('should prefix the command with Settings.commands.convertCommandPrefix', async function () {
2019-12-19 05:41:41 -05:00
Settings.commands.convertCommandPrefix = ['nice']
await FileConverter.promises.convert(sourcePath, format)
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
it('should convert the file when called as a callback', function (done) {
2019-12-19 05:41:41 -05:00
FileConverter.convert(sourcePath, format, (err, destPath) => {
expect(err).not.to.exist
destPath.should.equal(`${sourcePath}.${format}`)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
expect(args).to.include(`${sourcePath}.${format}`)
done()
})
})
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
describe('thumbnail', function () {
it('should call converter resize with args', async function () {
2019-12-19 05:41:41 -05:00
await FileConverter.promises.thumbnail(sourcePath)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
})
})
2014-02-14 11:39:05 -05:00
2020-08-10 12:01:12 -04:00
describe('preview', function () {
it('should call converter resize with args', async function () {
2019-12-19 05:41:41 -05:00
await FileConverter.promises.preview(sourcePath)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
})
})
})