ImageOptimiserTests.js 1.8 KB

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