FileConverter.test.js 3.5 KB

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