ImageOptimiser.test.js 2.0 KB

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