OutputFileOptimiserTests.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. node/no-deprecated-api,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. require('chai').should()
  16. const modulePath = require('path').join(
  17. __dirname,
  18. '../../../app/js/OutputFileOptimiser'
  19. )
  20. const path = require('path')
  21. const { expect } = require('chai')
  22. const { EventEmitter } = require('events')
  23. describe('OutputFileOptimiser', function () {
  24. beforeEach(function () {
  25. this.OutputFileOptimiser = SandboxedModule.require(modulePath, {
  26. requires: {
  27. fs: (this.fs = {}),
  28. path: (this.Path = {}),
  29. child_process: { spawn: (this.spawn = sinon.stub()) },
  30. 'logger-sharelatex': { log: sinon.stub(), warn: sinon.stub() },
  31. './Metrics': {}
  32. },
  33. globals: { Math } // used by lodash
  34. })
  35. this.directory = '/test/dir'
  36. return (this.callback = sinon.stub())
  37. })
  38. describe('optimiseFile', function () {
  39. beforeEach(function () {
  40. this.src = './output.pdf'
  41. return (this.dst = './output.pdf')
  42. })
  43. describe('when the file is not a pdf file', function () {
  44. beforeEach(function (done) {
  45. this.src = './output.log'
  46. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  47. .stub()
  48. .callsArgWith(1, null, false)
  49. this.OutputFileOptimiser.optimisePDF = sinon
  50. .stub()
  51. .callsArgWith(2, null)
  52. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  53. })
  54. it('should not check if the file is optimised', function () {
  55. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  56. .calledWith(this.src)
  57. .should.equal(false)
  58. })
  59. return it('should not optimise the file', function () {
  60. return this.OutputFileOptimiser.optimisePDF
  61. .calledWith(this.src, this.dst)
  62. .should.equal(false)
  63. })
  64. })
  65. describe('when the pdf file is not optimised', function () {
  66. beforeEach(function (done) {
  67. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  68. .stub()
  69. .callsArgWith(1, null, false)
  70. this.OutputFileOptimiser.optimisePDF = sinon
  71. .stub()
  72. .callsArgWith(2, null)
  73. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  74. })
  75. it('should check if the pdf is optimised', function () {
  76. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  77. .calledWith(this.src)
  78. .should.equal(true)
  79. })
  80. return it('should optimise the pdf', function () {
  81. return this.OutputFileOptimiser.optimisePDF
  82. .calledWith(this.src, this.dst)
  83. .should.equal(true)
  84. })
  85. })
  86. return describe('when the pdf file is optimised', function () {
  87. beforeEach(function (done) {
  88. this.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  89. .stub()
  90. .callsArgWith(1, null, true)
  91. this.OutputFileOptimiser.optimisePDF = sinon
  92. .stub()
  93. .callsArgWith(2, null)
  94. return this.OutputFileOptimiser.optimiseFile(this.src, this.dst, done)
  95. })
  96. it('should check if the pdf is optimised', function () {
  97. return this.OutputFileOptimiser.checkIfPDFIsOptimised
  98. .calledWith(this.src)
  99. .should.equal(true)
  100. })
  101. return it('should not optimise the pdf', function () {
  102. return this.OutputFileOptimiser.optimisePDF
  103. .calledWith(this.src, this.dst)
  104. .should.equal(false)
  105. })
  106. })
  107. })
  108. return describe('checkIfPDFISOptimised', function () {
  109. beforeEach(function () {
  110. this.callback = sinon.stub()
  111. this.fd = 1234
  112. this.fs.open = sinon.stub().yields(null, this.fd)
  113. this.fs.read = sinon
  114. .stub()
  115. .withArgs(this.fd)
  116. .yields(null, 100, Buffer.from('hello /Linearized 1'))
  117. this.fs.close = sinon.stub().withArgs(this.fd).yields(null)
  118. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  119. this.src,
  120. this.callback
  121. )
  122. })
  123. describe('for a linearised file', function () {
  124. beforeEach(function () {
  125. this.fs.read = sinon
  126. .stub()
  127. .withArgs(this.fd)
  128. .yields(null, 100, Buffer.from('hello /Linearized 1'))
  129. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  130. this.src,
  131. this.callback
  132. )
  133. })
  134. it('should open the file', function () {
  135. return this.fs.open.calledWith(this.src, 'r').should.equal(true)
  136. })
  137. it('should read the header', function () {
  138. return this.fs.read.calledWith(this.fd).should.equal(true)
  139. })
  140. it('should close the file', function () {
  141. return this.fs.close.calledWith(this.fd).should.equal(true)
  142. })
  143. return it('should call the callback with a true result', function () {
  144. return this.callback.calledWith(null, true).should.equal(true)
  145. })
  146. })
  147. return describe('for an unlinearised file', function () {
  148. beforeEach(function () {
  149. this.fs.read = sinon
  150. .stub()
  151. .withArgs(this.fd)
  152. .yields(null, 100, Buffer.from('hello not linearized 1'))
  153. return this.OutputFileOptimiser.checkIfPDFIsOptimised(
  154. this.src,
  155. this.callback
  156. )
  157. })
  158. it('should open the file', function () {
  159. return this.fs.open.calledWith(this.src, 'r').should.equal(true)
  160. })
  161. it('should read the header', function () {
  162. return this.fs.read.calledWith(this.fd).should.equal(true)
  163. })
  164. it('should close the file', function () {
  165. return this.fs.close.calledWith(this.fd).should.equal(true)
  166. })
  167. return it('should call the callback with a false result', function () {
  168. return this.callback.calledWith(null, false).should.equal(true)
  169. })
  170. })
  171. })
  172. })