ConversionTests.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Client from './helpers/Client.js'
  2. import ClsiApp from './helpers/ClsiApp.js'
  3. import Path from 'node:path'
  4. import fs from 'node:fs'
  5. import { pipeline } from 'node:stream/promises'
  6. import yauzl from 'yauzl'
  7. import { expect } from 'chai'
  8. describe('Conversions', function () {
  9. describe('docx conversion', function () {
  10. before(async function () {
  11. await ClsiApp.ensureRunning()
  12. try {
  13. this.body = await Client.compile(this.project_id, this.request)
  14. } catch (error) {
  15. this.error = error
  16. }
  17. })
  18. it('should convert file to docx', async function () {
  19. const sourcePath = Path.join(
  20. import.meta.dirname,
  21. '../fixtures/conversion-source.docx'
  22. )
  23. const outputStream = fs.createWriteStream(
  24. '/tmp/clsi_acceptance_tests_' + crypto.randomUUID() + '.zip'
  25. )
  26. const stream = await Client.convertDocx(sourcePath)
  27. await pipeline(stream, outputStream)
  28. await new Promise((resolve, reject) => {
  29. yauzl.open(outputStream.path, { lazyEntries: true }, (err, zipfile) => {
  30. if (err) {
  31. return reject(err)
  32. }
  33. zipfile.on('error', reject)
  34. zipfile.on('end', resolve)
  35. zipfile.readEntry()
  36. zipfile.on('entry', entry => {
  37. if (entry.fileName === 'main.tex') {
  38. zipfile.openReadStream(entry, (err, readStream) => {
  39. if (err) {
  40. return reject(err)
  41. }
  42. let data = ''
  43. readStream.on('data', chunk => {
  44. data += chunk.toString()
  45. })
  46. readStream.on('end', () => {
  47. try {
  48. expect(data).to.include('\\begin{document}')
  49. expect(data).to.include(
  50. '\\[x = \\frac{- b \\pm \\sqrt{b^{2} - 4ac}}{2a}\\]'
  51. )
  52. zipfile.readEntry()
  53. } catch (err) {
  54. reject(err)
  55. }
  56. })
  57. })
  58. } else if (entry.fileName === 'media/') {
  59. // Skip the media directory entry
  60. zipfile.readEntry()
  61. } else if (entry.fileName.startsWith('media/')) {
  62. expect(entry.fileName).to.equal('media/image1.png')
  63. zipfile.readEntry()
  64. } else {
  65. reject(new Error('Unexpected file in zip: ' + entry.fileName))
  66. }
  67. })
  68. })
  69. })
  70. })
  71. it('should fail when file is not a docx', async function () {
  72. const sourcePath = Path.join(
  73. import.meta.dirname,
  74. '../fixtures/minimal.pdf'
  75. )
  76. await expect(Client.convertDocx(sourcePath)).to.eventually.be.rejected
  77. })
  78. })
  79. })