FileConverterTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const SandboxedModule = require('sandboxed-module')
  5. const { Errors } = require('@overleaf/object-persistor')
  6. const modulePath = '../../../app/js/FileConverter.js'
  7. describe('FileConverter', function () {
  8. let SafeExec, FileConverter
  9. const sourcePath = '/data/wombat.eps'
  10. const destPath = '/tmp/dest.png'
  11. const format = 'png'
  12. const errorMessage = 'guru meditation error'
  13. const Settings = {
  14. commands: {
  15. convertCommandPrefix: [],
  16. },
  17. }
  18. beforeEach(function () {
  19. SafeExec = {
  20. promises: sinon.stub().resolves(destPath),
  21. }
  22. const ObjectPersistor = { Errors }
  23. FileConverter = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './SafeExec': SafeExec,
  26. '@overleaf/metrics': {
  27. inc: sinon.stub(),
  28. Timer: sinon.stub().returns({ done: sinon.stub() }),
  29. },
  30. '@overleaf/settings': Settings,
  31. '@overleaf/object-persistor': ObjectPersistor,
  32. },
  33. })
  34. })
  35. describe('convert', function () {
  36. it('should convert the source to the requested format', async function () {
  37. await FileConverter.promises.convert(sourcePath, format)
  38. const args = SafeExec.promises.args[0][0]
  39. expect(args).to.include(`${sourcePath}[0]`)
  40. expect(args).to.include(`${sourcePath}.${format}`)
  41. })
  42. it('should return the dest path', async function () {
  43. const destPath = await FileConverter.promises.convert(sourcePath, format)
  44. destPath.should.equal(`${sourcePath}.${format}`)
  45. })
  46. it('should wrap the error from convert', async function () {
  47. SafeExec.promises.rejects(errorMessage)
  48. try {
  49. await FileConverter.promises.convert(sourcePath, format)
  50. expect('error should have been thrown').not.to.exist
  51. } catch (err) {
  52. expect(err.name).to.equal('ConversionError')
  53. expect(err.cause.toString()).to.equal(errorMessage)
  54. }
  55. })
  56. it('should not accept an non approved format', async function () {
  57. try {
  58. await FileConverter.promises.convert(sourcePath, 'potato')
  59. expect('error should have been thrown').not.to.exist
  60. } catch (err) {
  61. expect(err.name).to.equal('ConversionError')
  62. }
  63. })
  64. it('should prefix the command with Settings.commands.convertCommandPrefix', async function () {
  65. Settings.commands.convertCommandPrefix = ['nice']
  66. await FileConverter.promises.convert(sourcePath, format)
  67. })
  68. it('should convert the file when called as a callback', function (done) {
  69. FileConverter.convert(sourcePath, format, (err, destPath) => {
  70. expect(err).not.to.exist
  71. destPath.should.equal(`${sourcePath}.${format}`)
  72. const args = SafeExec.promises.args[0][0]
  73. expect(args).to.include(`${sourcePath}[0]`)
  74. expect(args).to.include(`${sourcePath}.${format}`)
  75. done()
  76. })
  77. })
  78. })
  79. describe('thumbnail', function () {
  80. it('should call converter resize with args', async function () {
  81. await FileConverter.promises.thumbnail(sourcePath)
  82. const args = SafeExec.promises.args[0][0]
  83. expect(args).to.include(`${sourcePath}[0]`)
  84. })
  85. })
  86. describe('preview', function () {
  87. it('should call converter resize with args', async function () {
  88. await FileConverter.promises.preview(sourcePath)
  89. const args = SafeExec.promises.args[0][0]
  90. expect(args).to.include(`${sourcePath}[0]`)
  91. })
  92. })
  93. })