ExampleDocumentTests.js 8.3 KB

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