ExampleDocumentTests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const Client = require('./helpers/Client')
  16. const fetch = require('node-fetch')
  17. const { pipeline } = require('stream')
  18. const fs = require('fs')
  19. const fsExtra = require('fs-extra')
  20. const ChildProcess = require('child_process')
  21. const ClsiApp = require('./helpers/ClsiApp')
  22. const logger = require('@overleaf/logger')
  23. const Path = require('path')
  24. const fixturePath = path => {
  25. if (path.slice(0, 3) === 'tmp') {
  26. return '/tmp/clsi_acceptance_tests' + path.slice(3)
  27. }
  28. return Path.join(__dirname, '../fixtures/', path)
  29. }
  30. const process = require('process')
  31. console.log(
  32. process.pid,
  33. process.ppid,
  34. process.getuid(),
  35. process.getgroups(),
  36. 'PID'
  37. )
  38. const MOCHA_LATEX_TIMEOUT = 60 * 1000
  39. const convertToPng = function (pdfPath, pngPath, callback) {
  40. if (callback == null) {
  41. callback = function () {}
  42. }
  43. const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`
  44. console.log('COMMAND')
  45. console.log(command)
  46. const convert = ChildProcess.exec(command)
  47. const stdout = ''
  48. convert.stdout.on('data', chunk => console.log('STDOUT', chunk.toString()))
  49. convert.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
  50. return convert.on('exit', () => callback())
  51. }
  52. const compare = function (originalPath, generatedPath, callback) {
  53. if (callback == null) {
  54. callback = function () {}
  55. }
  56. const diffFile = `${fixturePath(generatedPath)}-diff.png`
  57. const proc = ChildProcess.exec(
  58. `compare -metric mae ${fixturePath(originalPath)} ${fixturePath(
  59. generatedPath
  60. )} ${diffFile}`
  61. )
  62. let stderr = ''
  63. proc.stderr.on('data', chunk => (stderr += chunk))
  64. return proc.on('exit', () => {
  65. if (stderr.trim() === '0 (0)') {
  66. // remove output diff if test matches expected image
  67. fs.unlink(diffFile, err => {
  68. if (err) {
  69. throw err
  70. }
  71. })
  72. return callback(null, true)
  73. } else {
  74. console.log('compare result', stderr)
  75. return callback(null, false)
  76. }
  77. })
  78. }
  79. const checkPdfInfo = function (pdfPath, callback) {
  80. if (callback == null) {
  81. callback = function () {}
  82. }
  83. const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`)
  84. let stdout = ''
  85. proc.stdout.on('data', chunk => (stdout += chunk))
  86. proc.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
  87. return proc.on('exit', () => {
  88. if (stdout.match(/Optimized:\s+yes/)) {
  89. return callback(null, true)
  90. } else {
  91. return callback(null, false)
  92. }
  93. })
  94. }
  95. const compareMultiplePages = function (projectId, callback) {
  96. if (callback == null) {
  97. callback = function () {}
  98. }
  99. function compareNext(pageNo, callback) {
  100. const path = `tmp/${projectId}-source-${pageNo}.png`
  101. return fs.stat(fixturePath(path), (error, stat) => {
  102. if (error != null) {
  103. return callback()
  104. } else {
  105. return compare(
  106. `tmp/${projectId}-source-${pageNo}.png`,
  107. `tmp/${projectId}-generated-${pageNo}.png`,
  108. (error, same) => {
  109. if (error != null) {
  110. throw error
  111. }
  112. same.should.equal(true)
  113. return compareNext(pageNo + 1, callback)
  114. }
  115. )
  116. }
  117. })
  118. }
  119. return compareNext(0, callback)
  120. }
  121. const comparePdf = function (projectId, exampleDir, callback) {
  122. if (callback == null) {
  123. callback = function () {}
  124. }
  125. console.log('CONVERT')
  126. console.log(`tmp/${projectId}.pdf`, `tmp/${projectId}-generated.png`)
  127. return convertToPng(
  128. `tmp/${projectId}.pdf`,
  129. `tmp/${projectId}-generated.png`,
  130. error => {
  131. if (error != null) {
  132. throw error
  133. }
  134. return convertToPng(
  135. `examples/${exampleDir}/output.pdf`,
  136. `tmp/${projectId}-source.png`,
  137. error => {
  138. if (error != null) {
  139. throw error
  140. }
  141. return fs.stat(
  142. fixturePath(`tmp/${projectId}-source-0.png`),
  143. (error, stat) => {
  144. if (error != null) {
  145. return compare(
  146. `tmp/${projectId}-source.png`,
  147. `tmp/${projectId}-generated.png`,
  148. (error, same) => {
  149. if (error != null) {
  150. throw error
  151. }
  152. same.should.equal(true)
  153. return callback()
  154. }
  155. )
  156. } else {
  157. return compareMultiplePages(projectId, error => {
  158. if (error != null) {
  159. throw error
  160. }
  161. return callback()
  162. })
  163. }
  164. }
  165. )
  166. }
  167. )
  168. }
  169. )
  170. }
  171. const downloadAndComparePdf = function (projectId, exampleDir, url, callback) {
  172. fetch(url)
  173. .then(res => {
  174. if (!res.ok) {
  175. return callback(new Error('non success response: ' + res.statusText))
  176. }
  177. const dest = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`))
  178. pipeline(res.body, dest, err => {
  179. if (err) return callback(err)
  180. checkPdfInfo(`tmp/${projectId}.pdf`, (err, optimised) => {
  181. if (err) return callback(err)
  182. optimised.should.equal(true)
  183. comparePdf(projectId, exampleDir, callback)
  184. })
  185. })
  186. })
  187. .catch(callback)
  188. }
  189. describe('Example Documents', function () {
  190. Client.runFakeFilestoreService(fixturePath('examples'))
  191. before(function (done) {
  192. ClsiApp.ensureRunning(done)
  193. })
  194. before(function (done) {
  195. fsExtra.remove(fixturePath('tmp'), done)
  196. })
  197. before(function (done) {
  198. fs.mkdir(fixturePath('tmp'), done)
  199. })
  200. after(function (done) {
  201. fsExtra.remove(fixturePath('tmp'), done)
  202. })
  203. return Array.from(fs.readdirSync(fixturePath('examples'))).map(exampleDir =>
  204. (exampleDir =>
  205. describe(exampleDir, function () {
  206. before(function () {
  207. return (this.project_id = Client.randomId() + '_' + exampleDir)
  208. })
  209. it('should generate the correct pdf', function (done) {
  210. this.timeout(MOCHA_LATEX_TIMEOUT)
  211. return Client.compileDirectory(
  212. this.project_id,
  213. fixturePath('examples'),
  214. exampleDir,
  215. (error, res, body) => {
  216. if (
  217. error ||
  218. __guard__(
  219. body != null ? body.compile : undefined,
  220. x => x.status
  221. ) === 'failure'
  222. ) {
  223. console.log('DEBUG: error', error, 'body', JSON.stringify(body))
  224. return done(new Error('Compile failed'))
  225. }
  226. const pdf = Client.getOutputFile(body, 'pdf')
  227. return downloadAndComparePdf(
  228. this.project_id,
  229. exampleDir,
  230. pdf.url,
  231. done
  232. )
  233. }
  234. )
  235. })
  236. return it('should generate the correct pdf on the second run as well', function (done) {
  237. this.timeout(MOCHA_LATEX_TIMEOUT)
  238. return Client.compileDirectory(
  239. this.project_id,
  240. fixturePath('examples'),
  241. exampleDir,
  242. (error, res, body) => {
  243. if (
  244. error ||
  245. __guard__(
  246. body != null ? body.compile : undefined,
  247. x => x.status
  248. ) === 'failure'
  249. ) {
  250. console.log('DEBUG: error', error, 'body', JSON.stringify(body))
  251. return done(new Error('Compile failed'))
  252. }
  253. const pdf = Client.getOutputFile(body, 'pdf')
  254. return downloadAndComparePdf(
  255. this.project_id,
  256. exampleDir,
  257. pdf.url,
  258. done
  259. )
  260. }
  261. )
  262. })
  263. }))(exampleDir)
  264. )
  265. })
  266. function __guard__(value, transform) {
  267. return typeof value !== 'undefined' && value !== null
  268. ? transform(value)
  269. : undefined
  270. }