2019-12-16 11:20:29 +00:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const modulePath = '../../../app/js/ImageOptimiser.js'
|
2019-12-19 16:56:03 +00:00
|
|
|
const { FailedCommandError } = require('../../../app/js/Errors')
|
2019-12-16 11:20:29 +00:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2014-02-14 16:39:05 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
describe('ImageOptimiser', function () {
|
2020-01-07 21:19:26 +00:00
|
|
|
let ImageOptimiser, SafeExec, logger
|
2019-12-19 16:56:03 +00:00
|
|
|
const sourcePath = '/wombat/potato.eps'
|
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
beforeEach(function () {
|
2019-12-19 16:56:03 +00:00
|
|
|
SafeExec = {
|
2021-07-13 11:04:46 +00:00
|
|
|
promises: sinon.stub().resolves(),
|
2019-12-19 16:56:03 +00:00
|
|
|
}
|
2020-01-07 21:19:26 +00:00
|
|
|
logger = {
|
2021-07-13 11:04:46 +00:00
|
|
|
warn: sinon.stub(),
|
2020-01-07 21:19:26 +00:00
|
|
|
}
|
2019-12-19 16:56:03 +00:00
|
|
|
ImageOptimiser = SandboxedModule.require(modulePath, {
|
2019-12-16 11:20:29 +00:00
|
|
|
requires: {
|
2019-12-19 16:56:03 +00:00
|
|
|
'./SafeExec': SafeExec,
|
2021-12-14 13:00:35 +00:00
|
|
|
'@overleaf/logger': logger,
|
2020-11-25 11:57:23 +00:00
|
|
|
'@overleaf/metrics': {
|
2021-07-13 11:04:46 +00:00
|
|
|
Timer: sinon.stub().returns({ done: sinon.stub() }),
|
|
|
|
},
|
|
|
|
},
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|
|
|
|
})
|
2014-02-14 16:39:05 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
describe('compressPng', function () {
|
|
|
|
it('should convert the file', function (done) {
|
2021-07-13 11:04:46 +00:00
|
|
|
ImageOptimiser.compressPng(sourcePath, err => {
|
2019-12-19 16:56:03 +00:00
|
|
|
expect(err).not.to.exist
|
|
|
|
expect(SafeExec.promises).to.have.been.calledWith([
|
|
|
|
'optipng',
|
2021-07-13 11:04:46 +00:00
|
|
|
sourcePath,
|
2019-12-19 16:56:03 +00:00
|
|
|
])
|
|
|
|
done()
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|
|
|
|
})
|
2014-02-14 16:39:05 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
it('should return the error', function (done) {
|
2019-12-19 16:56:03 +00:00
|
|
|
SafeExec.promises.rejects('wombat herding failure')
|
2021-07-13 11:04:46 +00:00
|
|
|
ImageOptimiser.compressPng(sourcePath, err => {
|
2019-12-19 16:56:03 +00:00
|
|
|
expect(err.toString()).to.equal('wombat herding failure')
|
|
|
|
done()
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-02-14 16:39:05 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
describe('when optimiser is sigkilled', function () {
|
2020-01-07 21:19:26 +00:00
|
|
|
const expectedError = new FailedCommandError('', 'SIGKILL', '', '')
|
|
|
|
let error
|
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
beforeEach(function (done) {
|
2020-01-07 21:19:26 +00:00
|
|
|
SafeExec.promises.rejects(expectedError)
|
2021-07-13 11:04:46 +00:00
|
|
|
ImageOptimiser.compressPng(sourcePath, err => {
|
2020-01-07 21:19:26 +00:00
|
|
|
error = err
|
2019-12-19 16:56:03 +00:00
|
|
|
done()
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|
2019-12-19 16:56:03 +00:00
|
|
|
})
|
2020-01-07 21:19:26 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
it('should not produce an error', function () {
|
2020-01-07 21:19:26 +00:00
|
|
|
expect(error).not.to.exist
|
|
|
|
})
|
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
it('should log a warning', function () {
|
2020-01-07 21:19:26 +00:00
|
|
|
expect(logger.warn).to.have.been.calledOnce
|
|
|
|
})
|
2019-12-19 16:56:03 +00:00
|
|
|
})
|
2019-12-16 11:20:29 +00:00
|
|
|
})
|