File size: 1,371 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const fs = require('fs');
const os = require('os');
const path = require('path');
const cp = require('child_process');

(async () => {
  const converter = path.join(process.cwd(), 'convert.js');
  const samples = path.join(process.cwd(), 'samples');
  if (!fs.existsSync(converter) || !fs.existsSync(samples)) throw Error('convert.js and samples are required');
  const output = fs.mkdtempSync(path.join(os.tmpdir(), 'ux009-api-'));
  try {
    const mod = require(converter);
    if (typeof mod.convertBatch !== 'function') throw Error('convert.js must export convertBatch');
    await mod.convertBatch(samples, output);
    const pdf = path.join(output, 'math.pdf');
    for (const [command, args] of [['pdfinfo', [pdf]], ['pdftotext', [pdf, '-']]]) {
      const result = cp.spawnSync(command, args, {encoding: 'utf8', timeout: 15000});
      if (result.status !== 0) throw Error(`${command} rejected PDF: ${result.stderr}`);
      if (command === 'pdftotext') {
        if (!/(√2|sqrt\(?2\)?)/i.test(result.stdout.replace(/\s/g, ''))) throw Error('rendered sqrt(2) is missing');
        if (/\\sqrt|\\\(|\\\)/.test(result.stdout)) throw Error('raw LaTeX is visible');
      }
    }
    console.log('Parsed batch PDF export passed');
  } finally { fs.rmSync(output, {recursive: true, force: true}); }
})().catch(error => { console.error(error); process.exit(1); });