import {test} from 'node:test'; import assert from 'node:assert/strict'; import {readPdfPages} from '../src/services/pdfDocumentReader.js'; const page=(text)=>({getTextContent:async()=>({items:[{str:text,hasEOL:true}]}),cleanup(){}}); test('PDF reader preserves the final requirement beyond former 40 page limit',async()=>{ const doc={numPages:45,getPage:async n=>page(n===45 ? 'FINAL MANDATORY REQUIREMENT' : `Page ${n}`)}; const result=await readPdfPages(doc); assert.equal(result.usedPages,45);assert.equal(result.complete,true); assert.ok(result.text.endsWith('FINAL MANDATORY REQUIREMENT')); const partial=await readPdfPages(doc,{maxPages:2}); assert.equal(partial.complete,false);assert.equal(partial.pageCount,45); }); test('mixed text and scanned pages use OCR only where needed and preserve order',async()=>{ const pages=[page('Official requirement '.repeat(10)),page(''),page('Final clause '.repeat(15))]; let calls=0; const result=await readPdfPages({numPages:3,getPage:async n=>pages[n-1]},{ocrPage:async()=>{calls++;return 'SCANNED REQUIREMENT';}}); assert.equal(calls,1);assert.deepEqual(result.pages.map(p=>p.method),['text','ocr','text']); assert.match(result.text,/Official requirement[\s\S]*SCANNED REQUIREMENT[\s\S]*Final clause/); }); test('OCR failure never returns apparently complete extracted text',async()=>{ await assert.rejects(readPdfPages({numPages:1,getPage:async()=>page('')},{ocrPage:async()=>{throw Error('worker failed');}}),/1. sayfada OCR tamamlanamadı/); }); test('actual 45-page PDF retains final-page text through pdfjs extraction',async()=>{ const {jsPDF}=await import('jspdf'); const {getDocument}=await import('pdfjs-dist/legacy/build/pdf.mjs'); const pdf=new jsPDF(); for(let n=1;n<=45;n++) { if(n>1) pdf.addPage(); pdf.text(n===45 ? 'FINAL OFFICIAL SOURCE REQUIREMENT' : `Synthetic QA page ${n}`,15,20); } const task=getDocument({data:new Uint8Array(pdf.output('arraybuffer')),useSystemFonts:true}); const doc=await task.promise; try { const result=await readPdfPages(doc); assert.equal(result.pageCount,45);assert.equal(result.usedPages,45); assert.ok(result.text.endsWith('FINAL OFFICIAL SOURCE REQUIREMENT')); } finally { await task.destroy(); } });