2020-02-19 06:16:07 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:16:00 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:16:14 -05:00
|
|
|
const Client = require('./helpers/Client')
|
2023-04-26 09:40:14 -04:00
|
|
|
const fetch = require('node-fetch')
|
2024-11-08 05:21:56 -05:00
|
|
|
const { pipeline } = require('node:stream')
|
|
|
|
const fs = require('node:fs')
|
|
|
|
const ChildProcess = require('node:child_process')
|
2020-02-19 06:16:14 -05:00
|
|
|
const ClsiApp = require('./helpers/ClsiApp')
|
2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2024-11-08 05:21:56 -05:00
|
|
|
const Path = require('node:path')
|
2021-07-13 07:04:48 -04:00
|
|
|
const fixturePath = path => {
|
2020-03-27 06:10:27 -04:00
|
|
|
if (path.slice(0, 3) === 'tmp') {
|
|
|
|
return '/tmp/clsi_acceptance_tests' + path.slice(3)
|
|
|
|
}
|
2021-10-27 05:49:53 -04:00
|
|
|
return Path.join(__dirname, '../fixtures/', path)
|
2020-03-27 06:10:27 -04:00
|
|
|
}
|
2024-11-08 05:21:56 -05:00
|
|
|
const process = require('node:process')
|
2020-02-19 06:16:14 -05:00
|
|
|
console.log(
|
|
|
|
process.pid,
|
|
|
|
process.ppid,
|
|
|
|
process.getuid(),
|
|
|
|
process.getgroups(),
|
|
|
|
'PID'
|
|
|
|
)
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-02-19 06:16:14 -05:00
|
|
|
const MOCHA_LATEX_TIMEOUT = 60 * 1000
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
const convertToPng = function (pdfPath, pngPath, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`
|
|
|
|
console.log('COMMAND')
|
|
|
|
console.log(command)
|
|
|
|
const convert = ChildProcess.exec(command)
|
|
|
|
const stdout = ''
|
2021-07-13 07:04:48 -04:00
|
|
|
convert.stdout.on('data', chunk => console.log('STDOUT', chunk.toString()))
|
|
|
|
convert.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
|
2020-02-19 06:16:14 -05:00
|
|
|
return convert.on('exit', () => callback())
|
|
|
|
}
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
const compare = function (originalPath, generatedPath, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2023-03-22 07:52:39 -04:00
|
|
|
const diffFile = `${fixturePath(generatedPath)}-diff.png`
|
2020-02-19 06:16:14 -05:00
|
|
|
const proc = ChildProcess.exec(
|
|
|
|
`compare -metric mae ${fixturePath(originalPath)} ${fixturePath(
|
|
|
|
generatedPath
|
2023-03-22 07:52:39 -04:00
|
|
|
)} ${diffFile}`
|
2020-02-19 06:16:14 -05:00
|
|
|
)
|
|
|
|
let stderr = ''
|
2021-07-13 07:04:48 -04:00
|
|
|
proc.stderr.on('data', chunk => (stderr += chunk))
|
2020-02-19 06:16:14 -05:00
|
|
|
return proc.on('exit', () => {
|
|
|
|
if (stderr.trim() === '0 (0)') {
|
|
|
|
// remove output diff if test matches expected image
|
2023-03-22 07:52:39 -04:00
|
|
|
fs.unlink(diffFile, err => {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return callback(null, true)
|
|
|
|
} else {
|
|
|
|
console.log('compare result', stderr)
|
|
|
|
return callback(null, false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
const checkPdfInfo = function (pdfPath, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`)
|
|
|
|
let stdout = ''
|
2021-07-13 07:04:48 -04:00
|
|
|
proc.stdout.on('data', chunk => (stdout += chunk))
|
|
|
|
proc.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
|
2020-02-19 06:16:14 -05:00
|
|
|
return proc.on('exit', () => {
|
|
|
|
if (stdout.match(/Optimized:\s+yes/)) {
|
|
|
|
return callback(null, true)
|
|
|
|
} else {
|
|
|
|
return callback(null, false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2023-03-22 07:52:39 -04:00
|
|
|
const compareMultiplePages = function (projectId, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2023-03-22 07:52:39 -04:00
|
|
|
function compareNext(pageNo, callback) {
|
|
|
|
const path = `tmp/${projectId}-source-${pageNo}.png`
|
2020-02-19 06:16:14 -05:00
|
|
|
return fs.stat(fixturePath(path), (error, stat) => {
|
|
|
|
if (error != null) {
|
|
|
|
return callback()
|
|
|
|
} else {
|
|
|
|
return compare(
|
2023-03-22 07:52:39 -04:00
|
|
|
`tmp/${projectId}-source-${pageNo}.png`,
|
|
|
|
`tmp/${projectId}-generated-${pageNo}.png`,
|
2020-02-19 06:16:14 -05:00
|
|
|
(error, same) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
same.should.equal(true)
|
2023-03-22 07:52:39 -04:00
|
|
|
return compareNext(pageNo + 1, callback)
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return compareNext(0, callback)
|
|
|
|
}
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2023-03-22 07:52:39 -04:00
|
|
|
const comparePdf = function (projectId, exampleDir, callback) {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
|
|
|
console.log('CONVERT')
|
2023-03-22 07:52:39 -04:00
|
|
|
console.log(`tmp/${projectId}.pdf`, `tmp/${projectId}-generated.png`)
|
2020-02-19 06:16:14 -05:00
|
|
|
return convertToPng(
|
2023-03-22 07:52:39 -04:00
|
|
|
`tmp/${projectId}.pdf`,
|
|
|
|
`tmp/${projectId}-generated.png`,
|
2021-07-13 07:04:48 -04:00
|
|
|
error => {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return convertToPng(
|
2023-03-22 07:52:39 -04:00
|
|
|
`examples/${exampleDir}/output.pdf`,
|
|
|
|
`tmp/${projectId}-source.png`,
|
2021-07-13 07:04:48 -04:00
|
|
|
error => {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return fs.stat(
|
2023-03-22 07:52:39 -04:00
|
|
|
fixturePath(`tmp/${projectId}-source-0.png`),
|
2020-02-19 06:16:14 -05:00
|
|
|
(error, stat) => {
|
|
|
|
if (error != null) {
|
|
|
|
return compare(
|
2023-03-22 07:52:39 -04:00
|
|
|
`tmp/${projectId}-source.png`,
|
|
|
|
`tmp/${projectId}-generated.png`,
|
2020-02-19 06:16:14 -05:00
|
|
|
(error, same) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
same.should.equal(true)
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
2023-03-22 07:52:39 -04:00
|
|
|
return compareMultiplePages(projectId, error => {
|
2020-02-19 06:16:14 -05:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2023-03-22 07:52:39 -04:00
|
|
|
const downloadAndComparePdf = function (projectId, exampleDir, url, callback) {
|
2023-04-26 09:40:14 -04:00
|
|
|
fetch(url)
|
|
|
|
.then(res => {
|
|
|
|
if (!res.ok) {
|
|
|
|
return callback(new Error('non success response: ' + res.statusText))
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2023-04-26 09:40:14 -04:00
|
|
|
|
|
|
|
const dest = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`))
|
|
|
|
pipeline(res.body, dest, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
checkPdfInfo(`tmp/${projectId}.pdf`, (err, optimised) => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
optimised.should.equal(true)
|
|
|
|
comparePdf(projectId, exampleDir, callback)
|
|
|
|
})
|
|
|
|
})
|
2020-02-19 06:16:14 -05:00
|
|
|
})
|
2023-04-26 09:40:14 -04:00
|
|
|
.catch(callback)
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('Example Documents', function () {
|
2023-04-26 10:06:02 -04:00
|
|
|
Client.runFakeFilestoreService(fixturePath('examples'))
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
before(function (done) {
|
2020-03-27 06:10:27 -04:00
|
|
|
ClsiApp.ensureRunning(done)
|
|
|
|
})
|
2020-08-10 12:01:11 -04:00
|
|
|
before(function (done) {
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.rm(fixturePath('tmp'), { force: true, recursive: true }, done)
|
2020-03-27 06:10:27 -04:00
|
|
|
})
|
2020-08-10 12:01:11 -04:00
|
|
|
before(function (done) {
|
2020-03-27 06:10:27 -04:00
|
|
|
fs.mkdir(fixturePath('tmp'), done)
|
|
|
|
})
|
2020-08-10 12:01:11 -04:00
|
|
|
after(function (done) {
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.rm(fixturePath('tmp'), { force: true, recursive: true }, done)
|
2020-02-19 06:16:14 -05:00
|
|
|
})
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2023-03-22 07:52:39 -04:00
|
|
|
return Array.from(fs.readdirSync(fixturePath('examples'))).map(exampleDir =>
|
|
|
|
(exampleDir =>
|
|
|
|
describe(exampleDir, function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
before(function () {
|
2023-03-22 07:52:39 -04:00
|
|
|
return (this.project_id = Client.randomId() + '_' + exampleDir)
|
2021-07-13 07:04:48 -04:00
|
|
|
})
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
it('should generate the correct pdf', function (done) {
|
|
|
|
this.timeout(MOCHA_LATEX_TIMEOUT)
|
|
|
|
return Client.compileDirectory(
|
|
|
|
this.project_id,
|
|
|
|
fixturePath('examples'),
|
2023-03-22 07:52:39 -04:00
|
|
|
exampleDir,
|
2021-07-13 07:04:48 -04:00
|
|
|
(error, res, body) => {
|
|
|
|
if (
|
|
|
|
error ||
|
|
|
|
__guard__(
|
|
|
|
body != null ? body.compile : undefined,
|
|
|
|
x => x.status
|
|
|
|
) === 'failure'
|
|
|
|
) {
|
|
|
|
console.log('DEBUG: error', error, 'body', JSON.stringify(body))
|
|
|
|
return done(new Error('Compile failed'))
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
const pdf = Client.getOutputFile(body, 'pdf')
|
|
|
|
return downloadAndComparePdf(
|
|
|
|
this.project_id,
|
2023-03-22 07:52:39 -04:00
|
|
|
exampleDir,
|
2021-07-13 07:04:48 -04:00
|
|
|
pdf.url,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-02-19 06:16:00 -05:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
return it('should generate the correct pdf on the second run as well', function (done) {
|
|
|
|
this.timeout(MOCHA_LATEX_TIMEOUT)
|
|
|
|
return Client.compileDirectory(
|
|
|
|
this.project_id,
|
|
|
|
fixturePath('examples'),
|
2023-03-22 07:52:39 -04:00
|
|
|
exampleDir,
|
2021-07-13 07:04:48 -04:00
|
|
|
(error, res, body) => {
|
|
|
|
if (
|
|
|
|
error ||
|
|
|
|
__guard__(
|
|
|
|
body != null ? body.compile : undefined,
|
|
|
|
x => x.status
|
|
|
|
) === 'failure'
|
|
|
|
) {
|
|
|
|
console.log('DEBUG: error', error, 'body', JSON.stringify(body))
|
|
|
|
return done(new Error('Compile failed'))
|
2020-02-19 06:16:14 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
const pdf = Client.getOutputFile(body, 'pdf')
|
|
|
|
return downloadAndComparePdf(
|
|
|
|
this.project_id,
|
2023-03-22 07:52:39 -04:00
|
|
|
exampleDir,
|
2021-07-13 07:04:48 -04:00
|
|
|
pdf.url,
|
|
|
|
done
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2023-03-22 07:52:39 -04:00
|
|
|
}))(exampleDir)
|
2020-02-19 06:16:14 -05:00
|
|
|
)
|
|
|
|
})
|
2020-02-19 06:16:00 -05:00
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
2020-02-19 06:16:14 -05:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|