ImageOptimiserTests.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const modulePath = '../../../app/js/ImageOptimiser.js'
  5. const { FailedCommandError } = require('../../../app/js/Errors')
  6. const SandboxedModule = require('sandboxed-module')
  7. describe('ImageOptimiser', function () {
  8. let ImageOptimiser, SafeExec, logger
  9. const sourcePath = '/wombat/potato.eps'
  10. beforeEach(function () {
  11. SafeExec = {
  12. promises: sinon.stub().resolves(),
  13. }
  14. logger = {
  15. warn: sinon.stub(),
  16. }
  17. ImageOptimiser = SandboxedModule.require(modulePath, {
  18. requires: {
  19. './SafeExec': SafeExec,
  20. '@overleaf/logger': logger,
  21. '@overleaf/metrics': {
  22. Timer: sinon.stub().returns({ done: sinon.stub() }),
  23. },
  24. },
  25. })
  26. })
  27. describe('compressPng', function () {
  28. it('should convert the file', function (done) {
  29. ImageOptimiser.compressPng(sourcePath, err => {
  30. expect(err).not.to.exist
  31. expect(SafeExec.promises).to.have.been.calledWith([
  32. 'optipng',
  33. sourcePath,
  34. ])
  35. done()
  36. })
  37. })
  38. it('should return the error', function (done) {
  39. SafeExec.promises.rejects('wombat herding failure')
  40. ImageOptimiser.compressPng(sourcePath, err => {
  41. expect(err.toString()).to.equal('wombat herding failure')
  42. done()
  43. })
  44. })
  45. })
  46. describe('when optimiser is sigkilled', function () {
  47. const expectedError = new FailedCommandError('', 'SIGKILL', '', '')
  48. let error
  49. beforeEach(function (done) {
  50. SafeExec.promises.rejects(expectedError)
  51. ImageOptimiser.compressPng(sourcePath, err => {
  52. error = err
  53. done()
  54. })
  55. })
  56. it('should not produce an error', function () {
  57. expect(error).not.to.exist
  58. })
  59. it('should log a warning', function () {
  60. expect(logger.warn).to.have.been.calledOnce
  61. })
  62. })
  63. })