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

72 lines
1.9 KiB
JavaScript
Raw Normal View History

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