File size: 2,938 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
const fs=require('fs'),os=require('os'),path=require('path'),cp=require('child_process');function assert(x,m){if(!x)throw new Error(m)}const converter=path.join(process.cwd(),'convert.js');function run(files){const root=fs.mkdtempSync(path.join(os.tmpdir(),'task009-enhanced-')),input=path.join(root,'samples'),out=path.join(root,'out');fs.mkdirSync(input);for(const [n,t] of Object.entries(files))fs.writeFileSync(path.join(input,n),t);const r=cp.spawnSync(process.execPath,[converter,input,out],{cwd:process.cwd(),timeout:15000,env:{...process.env,NO_PROXY:'*',HTTP_PROXY:'',HTTPS_PROXY:''},encoding:'utf8'});assert(r.status===0,`conversion failed: ${r.stderr||r.stdout}`);return{root,out,pdfs:fs.readdirSync(out).filter(n=>/\.pdf$/i.test(n)).map(n=>path.join(out,n))}}function valid(p){const info=cp.spawnSync('pdfinfo',[p],{encoding:'utf8'});assert(info.status===0,`pdfinfo rejected ${p}: ${info.stderr}`);const text=cp.spawnSync('pdftotext',[p,'-'],{encoding:'utf8'});assert(text.status===0,`pdftotext rejected ${p}: ${text.stderr}`);return text.stdout}
try{
 // ENHANCED_CASE_1: two different Markdown inputs are converted as a batch into two different PDFs.
 {const x=run({'alpha.md':'# Alpha\nfirst document','beta.md':'# Beta\nsecond document'});assert(x.pdfs.length===2,'batch conversion must emit one PDF for each Markdown input');const texts=x.pdfs.map(valid);assert(texts.some(t=>/Alpha/.test(t))&&texts.some(t=>/Beta/.test(t)),'different inputs must produce PDFs containing their own text');fs.rmSync(x.root,{recursive:true,force:true})}
 // ENHANCED_CASE_2: every PDF from a three-file batch has the required viewer-readable structure.
 {const x=run({'one.md':'one','two.md':'two','three.md':'three'});assert(x.pdfs.length===3,'three Markdown inputs must produce three PDFs');x.pdfs.forEach(valid);fs.rmSync(x.root,{recursive:true,force:true})}
 // ENHANCED_CASE_3: the specified sqrt(2) fixture embedded in new surrounding text renders without raw LaTeX.
 {const x=run({'math.md':'Unique prefix 731: \\(\\sqrt{2}\\) unique suffix'});assert(x.pdfs.length===1,'math input must produce a PDF');const text=valid(x.pdfs[0]),compact=text.replace(/\s/g,'');assert(/731/.test(text)&&/(√2|sqrt\(?2\)?)/i.test(compact),'parsed PDF should preserve surrounding text and rendered sqrt(2)');assert(!/\\sqrt|\\\(|\\\)/.test(text),'raw LaTeX must not remain visible');fs.rmSync(x.root,{recursive:true,force:true})}
 // ENHANCED_CASE_4: common Markdown syntax must become rendered text, not leak literal markup.
 {const x=run({'format.md':'# Overview\nThis is **bold** and `code`.'});const t=valid(x.pdfs[0]);assert(/Overview/.test(t)&&/bold/.test(t)&&/code/.test(t),'Markdown content missing');assert(!/# Overview|\*\*bold\*\*|`code`/.test(t),'raw Markdown markers leaked into PDF text');fs.rmSync(x.root,{recursive:true,force:true})}
 console.log('enhanced cases passed for task 9')}catch(e){console.error(e.stack||e);process.exit(1)}