OutputFileOptimiser.test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { vi, describe, beforeEach, it } from 'vitest'
  2. import sinon from 'sinon'
  3. import path from 'node:path'
  4. const modulePath = path.join(
  5. import.meta.dirname,
  6. '../../../app/js/OutputFileOptimiser'
  7. )
  8. describe('OutputFileOptimiser', function () {
  9. beforeEach(async function (ctx) {
  10. vi.doMock('fs', () => ({
  11. default: (ctx.fs = {}),
  12. }))
  13. vi.doMock('path', () => ({
  14. default: (ctx.Path = {}),
  15. }))
  16. vi.doMock('child_process', () => ({
  17. default: { spawn: (ctx.spawn = sinon.stub()) },
  18. }))
  19. vi.doMock('@overleaf/metrics', () => ({
  20. default: {},
  21. }))
  22. ctx.OutputFileOptimiser = (await import(modulePath)).default
  23. ctx.directory = '/test/dir'
  24. return (ctx.callback = sinon.stub())
  25. })
  26. describe('optimiseFile', function () {
  27. beforeEach(function (ctx) {
  28. ctx.src = './output.pdf'
  29. return (ctx.dst = './output.pdf')
  30. })
  31. describe('when the file is not a pdf file', function () {
  32. beforeEach(async function (ctx) {
  33. await new Promise((resolve, reject) => {
  34. ctx.src = './output.log'
  35. ctx.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  36. .stub()
  37. .callsArgWith(1, null, false)
  38. ctx.OutputFileOptimiser.optimisePDF = sinon
  39. .stub()
  40. .callsArgWith(2, null)
  41. ctx.OutputFileOptimiser.optimiseFile(ctx.src, ctx.dst, err => {
  42. if (err) reject(err)
  43. resolve()
  44. })
  45. })
  46. })
  47. it('should not check if the file is optimised', function (ctx) {
  48. return ctx.OutputFileOptimiser.checkIfPDFIsOptimised
  49. .calledWith(ctx.src)
  50. .should.equal(false)
  51. })
  52. return it('should not optimise the file', function (ctx) {
  53. return ctx.OutputFileOptimiser.optimisePDF
  54. .calledWith(ctx.src, ctx.dst)
  55. .should.equal(false)
  56. })
  57. })
  58. describe('when the pdf file is not optimised', function () {
  59. beforeEach(async function (ctx) {
  60. await new Promise((resolve, reject) => {
  61. ctx.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  62. .stub()
  63. .callsArgWith(1, null, false)
  64. ctx.OutputFileOptimiser.optimisePDF = sinon
  65. .stub()
  66. .callsArgWith(2, null)
  67. ctx.OutputFileOptimiser.optimiseFile(ctx.src, ctx.dst, err => {
  68. if (err) reject(err)
  69. resolve()
  70. })
  71. })
  72. })
  73. it('should check if the pdf is optimised', function (ctx) {
  74. return ctx.OutputFileOptimiser.checkIfPDFIsOptimised
  75. .calledWith(ctx.src)
  76. .should.equal(true)
  77. })
  78. return it('should optimise the pdf', function (ctx) {
  79. return ctx.OutputFileOptimiser.optimisePDF
  80. .calledWith(ctx.src, ctx.dst)
  81. .should.equal(true)
  82. })
  83. })
  84. return describe('when the pdf file is optimised', function () {
  85. beforeEach(async function (ctx) {
  86. await new Promise((resolve, reject) => {
  87. ctx.OutputFileOptimiser.checkIfPDFIsOptimised = sinon
  88. .stub()
  89. .callsArgWith(1, null, true)
  90. ctx.OutputFileOptimiser.optimisePDF = sinon
  91. .stub()
  92. .callsArgWith(2, null)
  93. ctx.OutputFileOptimiser.optimiseFile(ctx.src, ctx.dst, err => {
  94. if (err) reject(err)
  95. resolve()
  96. })
  97. })
  98. })
  99. it('should check if the pdf is optimised', function (ctx) {
  100. ctx.OutputFileOptimiser.checkIfPDFIsOptimised
  101. .calledWith(ctx.src)
  102. .should.equal(true)
  103. })
  104. it('should not optimise the pdf', function (ctx) {
  105. ctx.OutputFileOptimiser.optimisePDF
  106. .calledWith(ctx.src, ctx.dst)
  107. .should.equal(false)
  108. })
  109. })
  110. })
  111. describe('checkIfPDFISOptimised', function () {
  112. beforeEach(function (ctx) {
  113. ctx.callback = sinon.stub()
  114. ctx.fd = 1234
  115. ctx.fs.open = sinon.stub().yields(null, ctx.fd)
  116. ctx.fs.read = sinon
  117. .stub()
  118. .withArgs(ctx.fd)
  119. .yields(null, 100, Buffer.from('hello /Linearized 1'))
  120. ctx.fs.close = sinon.stub().withArgs(ctx.fd).yields(null)
  121. ctx.OutputFileOptimiser.checkIfPDFIsOptimised(ctx.src, ctx.callback)
  122. })
  123. describe('for a linearised file', function () {
  124. beforeEach(function (ctx) {
  125. ctx.fs.read = sinon
  126. .stub()
  127. .withArgs(ctx.fd)
  128. .yields(null, 100, Buffer.from('hello /Linearized 1'))
  129. ctx.OutputFileOptimiser.checkIfPDFIsOptimised(ctx.src, ctx.callback)
  130. })
  131. it('should open the file', function (ctx) {
  132. ctx.fs.open.calledWith(ctx.src, 'r').should.equal(true)
  133. })
  134. it('should read the header', function (ctx) {
  135. ctx.fs.read.calledWith(ctx.fd).should.equal(true)
  136. })
  137. it('should close the file', function (ctx) {
  138. ctx.fs.close.calledWith(ctx.fd).should.equal(true)
  139. })
  140. it('should call the callback with a true result', function (ctx) {
  141. ctx.callback.calledWith(null, true).should.equal(true)
  142. })
  143. })
  144. describe('for an unlinearised file', function () {
  145. beforeEach(function (ctx) {
  146. ctx.fs.read = sinon
  147. .stub()
  148. .withArgs(ctx.fd)
  149. .yields(null, 100, Buffer.from('hello not linearized 1'))
  150. ctx.OutputFileOptimiser.checkIfPDFIsOptimised(ctx.src, ctx.callback)
  151. })
  152. it('should open the file', function (ctx) {
  153. ctx.fs.open.calledWith(ctx.src, 'r').should.equal(true)
  154. })
  155. it('should read the header', function (ctx) {
  156. ctx.fs.read.calledWith(ctx.fd).should.equal(true)
  157. })
  158. it('should close the file', function (ctx) {
  159. ctx.fs.close.calledWith(ctx.fd).should.equal(true)
  160. })
  161. it('should call the callback with a false result', function (ctx) {
  162. ctx.callback.calledWith(null, false).should.equal(true)
  163. })
  164. })
  165. })
  166. })