import fs from 'node:fs/promises'; import path from 'node:path'; import {execFileSync} from 'node:child_process'; import {chromium} from '@playwright/test'; import {boxes} from '../demo/trial-demo-data.mjs'; // Local, fictional fixtures only. Never authenticates or writes customer orders. const phase=process.argv[2]||'after'; const root=path.resolve('..','caterium-pdf-20260918','tmp','pdfs',phase); await fs.mkdir(root,{recursive:true}); const browser=await chromium.launch(); const page=await browser.newPage(); await page.route(/https:\/\/(?!127\.0\.0\.1)/,r=>r.abort()); await page.goto('http://127.0.0.1:4173/index.html',{waitUntil:'domcontentloaded'}); await page.waitForFunction(()=>Boolean(window.sunClientOfferDebugPdfPages)); const image=async file=>'data:image/'+(file.endsWith('.png')?'png':'jpeg')+';base64,'+(await fs.readFile(path.resolve('public',file))).toString('base64'); const items=await Promise.all(boxes.map(async(b,i)=>({...b,categoryId:0,categoryName:'Боксы',qty:i===0?2:1,unitPrice:b.price,sum:b.price*(i===0?2:1),photoData:await image(b.photo)}))); const base=items.reduce((n,i)=>n+i.sum,0); const snapshot={brandName:'Солнце Кейтеринг',brandCity:'Санкт-Петербург',brandContacts:'Ваш менеджер · +7 (900) 000-00-00',logo:await image('sun-logo.png'),client:'Анна и Михаил',event:'Вечер в кругу друзей',date:'2026-10-24',time:'18:00',guests:20,items,pricing:{base,manual:1500,promo:0,discount:1500,itemsTotal:base-1500,delivery:1500,total:base},foodGrams:11000,foodPieces:192,controlLines:['Согласуем время и адрес доставки','Подготовим заказ к вашему мероприятию'],extraServices:['Сервировка стола','Обслуживание мероприятия'],finalGallery:[await image('offer-gallery/001.jpg'),await image('offer-gallery/002.jpg')]}; if(phase==='thumbs'){snapshot.brandName='Caterium';snapshot.brandCity='';snapshot.logo='';snapshot.client='Анна и Михаил'} await page.evaluate(s=>window.__pdfFixture=s,snapshot); const ids=await page.evaluate(()=>[...SunClassicOfferPDFV1767.CLASSIC_IDS,...SunClassicOfferPDFV1767.ARCHIVE_IDS,...SunSignatureOfferPDFV18.SIGNATURE_IDS]); if(phase==='early'){ const source=execFileSync('git',['show','a3616b7:public/core/classic-offer-pdf-v1767.js'],{encoding:'utf8'}); await page.evaluate(()=>{delete window.SunClassicOfferPDFV1767}); await page.addScriptTag({content:source}); } const report=[]; for(const id of phase==='early'?ids.slice(0,10):ids){ const result=await page.evaluate(async({id,phase})=>{ const s={...window.__pdfFixture,offerTemplateId:id}; const pages=phase==='early'?[await SunClassicOfferPDFV1767.renderCover(s,id)]:await sunClientOfferDebugPdfPages(s); const jpg=pages.map(c=>({width:c.width,height:c.height,bytes:Uint8Array.from(atob(c.toDataURL('image/jpeg',.94).split(',')[1]),c=>c.charCodeAt(0))})); const blob=SunPdfEngine.fromJpegs(jpg); const pdf=await new Promise(resolve=>{const r=new FileReader();r.onload=()=>resolve(r.result.split(',')[1]);r.readAsDataURL(blob)}); const thumb=document.createElement('canvas');thumb.width=320;thumb.height=452;thumb.getContext('2d').drawImage(pages[0],0,0,320,452); const thumbnail=thumb.toDataURL('image/jpeg',.88).split(',')[1]; const info={id,pages:pages.length,width:pages[0].width,cover:pages[0].dataset.sunProposalTemplate||pages[0].dataset.sunClassicTemplate||pages[0].dataset.sunSignatureTemplate||null,layout:pages.map(p=>p.__proposalLayout||null)}; pages.forEach(c=>{c.width=0;c.height=0}); return {pdf,info,thumbnail}; },{id,phase}); if(phase==='thumbs')await fs.writeFile(path.resolve('public','offer-templates','quality-'+id+'.jpg'),Buffer.from(result.thumbnail,'base64')); else await fs.writeFile(path.join(root,id+'.pdf'),Buffer.from(result.pdf,'base64')); report.push(result.info);console.log(id,result.info.pages,result.info.cover); } await fs.writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2)); await browser.close();