ExampleDocumentTests.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const Client = require('./helpers/Client')
  2. const fetch = require('node-fetch')
  3. const Stream = require('node:stream')
  4. const fs = require('node:fs')
  5. const fsPromises = require('node:fs/promises')
  6. const ChildProcess = require('node:child_process')
  7. const { promisify } = require('node:util')
  8. const ClsiApp = require('./helpers/ClsiApp')
  9. const Path = require('node:path')
  10. const fixturePath = path => {
  11. if (path.slice(0, 3) === 'tmp') {
  12. return '/tmp/clsi_acceptance_tests' + path.slice(3)
  13. }
  14. return Path.join(__dirname, '../fixtures/', path)
  15. }
  16. const process = require('node:process')
  17. const pipeline = promisify(Stream.pipeline)
  18. console.log(
  19. process.pid,
  20. process.ppid,
  21. process.getuid(),
  22. process.getgroups(),
  23. 'PID'
  24. )
  25. const MOCHA_LATEX_TIMEOUT = 60 * 1000
  26. const convertToPng = function (pdfPath, pngPath) {
  27. return new Promise((resolve, reject) => {
  28. const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`
  29. console.log('COMMAND')
  30. console.log(command)
  31. const convert = ChildProcess.exec(command)
  32. convert.stdout.on('data', chunk => console.log('STDOUT', chunk.toString()))
  33. convert.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
  34. convert.on('exit', () => resolve())
  35. convert.on('error', error => reject(error))
  36. })
  37. }
  38. const compare = function (originalPath, generatedPath) {
  39. return new Promise((resolve, reject) => {
  40. const diffFile = `${fixturePath(generatedPath)}-diff.png`
  41. const proc = ChildProcess.exec(
  42. `compare -metric mae ${fixturePath(originalPath)} ${fixturePath(
  43. generatedPath
  44. )} ${diffFile}`
  45. )
  46. let stderr = ''
  47. proc.stderr.on('data', chunk => (stderr += chunk))
  48. proc.on('exit', () => {
  49. if (stderr.trim() === '0 (0)') {
  50. // remove output diff if test matches expected image
  51. fs.unlink(diffFile, err => {
  52. if (err) {
  53. reject(err)
  54. }
  55. })
  56. resolve(true)
  57. } else {
  58. console.log('compare result', stderr)
  59. resolve(false)
  60. }
  61. })
  62. })
  63. }
  64. const checkPdfInfo = function (pdfPath) {
  65. return new Promise((resolve, reject) => {
  66. const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`)
  67. let stdout = ''
  68. proc.stdout.on('data', chunk => (stdout += chunk))
  69. proc.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
  70. proc.on('exit', () => {
  71. if (stdout.match(/Optimized:\s+yes/)) {
  72. resolve(true)
  73. } else {
  74. resolve(false)
  75. }
  76. })
  77. proc.on('error', error => reject(error))
  78. })
  79. }
  80. const compareMultiplePages = async function (projectId) {
  81. async function compareNext(pageNo) {
  82. const path = `tmp/${projectId}-source-${pageNo}.png`
  83. try {
  84. await fsPromises.stat(fixturePath(path))
  85. } catch (error) {
  86. return
  87. }
  88. const same = await compare(
  89. `tmp/${projectId}-source-${pageNo}.png`,
  90. `tmp/${projectId}-generated-${pageNo}.png`
  91. )
  92. same.should.equal(true)
  93. await compareNext(pageNo + 1)
  94. }
  95. await compareNext(0)
  96. }
  97. const comparePdf = async function (projectId, exampleDir) {
  98. console.log('CONVERT')
  99. console.log(`tmp/${projectId}.pdf`, `tmp/${projectId}-generated.png`)
  100. await convertToPng(`tmp/${projectId}.pdf`, `tmp/${projectId}-generated.png`)
  101. await convertToPng(
  102. `examples/${exampleDir}/output.pdf`,
  103. `tmp/${projectId}-source.png`
  104. )
  105. try {
  106. await fsPromises.stat(fixturePath(`tmp/${projectId}-source-0.png`))
  107. await compareMultiplePages(projectId)
  108. } catch (error) {
  109. const same = await compare(
  110. `tmp/${projectId}-source.png`,
  111. `tmp/${projectId}-generated.png`
  112. )
  113. same.should.equal(true)
  114. }
  115. }
  116. const downloadAndComparePdf = async function (projectId, exampleDir, url) {
  117. const res = await fetch(url)
  118. if (!res.ok) {
  119. throw new Error('non success response: ' + res.statusText)
  120. }
  121. const dest = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`))
  122. await pipeline(res.body, dest)
  123. const optimised = await checkPdfInfo(`tmp/${projectId}.pdf`)
  124. optimised.should.equal(true)
  125. await comparePdf(projectId, exampleDir)
  126. }
  127. describe('Example Documents', function () {
  128. Client.runFakeFilestoreService(fixturePath('examples'))
  129. before(async function () {
  130. await ClsiApp.ensureRunning()
  131. })
  132. before(async function () {
  133. await fsPromises.rm(fixturePath('tmp'), { force: true, recursive: true })
  134. })
  135. before(async function () {
  136. await fsPromises.mkdir(fixturePath('tmp'))
  137. })
  138. after(async function () {
  139. await fsPromises.rm(fixturePath('tmp'), { force: true, recursive: true })
  140. })
  141. return fs.readdirSync(fixturePath('examples')).map(exampleDir =>
  142. (exampleDir =>
  143. describe(exampleDir, function () {
  144. before(function () {
  145. this.project_id = Client.randomId() + '_' + exampleDir
  146. })
  147. it('should generate the correct pdf', async function () {
  148. this.timeout(MOCHA_LATEX_TIMEOUT)
  149. const body = await Client.compileDirectory(
  150. this.project_id,
  151. fixturePath('examples'),
  152. exampleDir
  153. )
  154. if (body?.compile?.status === 'failure') {
  155. throw new Error('Compile failed')
  156. }
  157. const pdf = Client.getOutputFile(body, 'pdf')
  158. await downloadAndComparePdf(this.project_id, exampleDir, pdf.url)
  159. })
  160. it('should generate the correct pdf on the second run as well', async function () {
  161. this.timeout(MOCHA_LATEX_TIMEOUT)
  162. const body = await Client.compileDirectory(
  163. this.project_id,
  164. fixturePath('examples'),
  165. exampleDir
  166. )
  167. if (body?.compile?.status === 'failure') {
  168. throw new Error('Compile failed')
  169. }
  170. const pdf = Client.getOutputFile(body, 'pdf')
  171. await downloadAndComparePdf(this.project_id, exampleDir, pdf.url)
  172. })
  173. }))(exampleDir)
  174. )
  175. })