Files
faunosteo/.claude/audits/2026-10-30-v1-2-2-three-medium-tools/zz-real-pdf.test.ts

60 lines
3.2 KiB
TypeScript

// THROWAWAY (never committed): a real PDF export, every view rasterised by Chromium as
// apps/app/src/pdf/browserRasterizer.ts does, written to REAL_PDF_OUT.
import { createRequire } from 'node:module'
import { readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { openExportSession } from '../src/index'
import { nodeCoreRuntime } from '../src/node'
import { readAttachedDocument } from './n-1/scanner/attachment/read'
const ROOT = fileURLToPath(new URL('../../../', import.meta.url))
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
const OUT = process.env.REAL_PDF_OUT
function unwrap<T>(o: { ok: true; value: T } | { ok: false; kind: string; detail?: string }, what: string): T {
if (!o.ok) throw new Error(`${what}: ${o.kind} ${o.detail ?? ''}`)
return o.value
}
it.runIf(OUT)('real pdf', async () => {
const require = createRequire(join(ROOT, 'apps/app/package.json'))
const { chromium } = require('@playwright/test')
const browser = await chromium.launch()
const page = await browser.newPage()
await page.setContent('<html><body></body></html>')
const runtime = await nodeCoreRuntime('pkg')
for (const name of ['demo.fr.logos', 'v2full.fr.logos', 'v2unresolved.fr.nologos']) {
const env = (await readAttachedDocument(new Uint8Array(readFileSync(join(FIXTURES, `pdf/r3/${name}.pdf`))))) as Record<string, unknown>
const input = JSON.stringify({ report: env.report, sheetSnapshot: env.sheetSnapshot, branding: env.branding, labels: env.labels, locale: env.locale, exportedAt: env.exportedAt })
const session = unwrap(openExportSession(runtime, input), name)
const svgs: string[] = []
try {
for (;;) {
const step = unwrap(session.next(), 'next')
if (step.step === 'done') break
for (const r of step.requests) {
svgs.push(r.svg)
const b64: string = await page.evaluate(async ([svg, w, h]: [string, number, number]) => {
const root = new DOMParser().parseFromString(svg, 'image/svg+xml').documentElement
if (root.namespaceURI !== 'http://www.w3.org/2000/svg') root.setAttribute('xmlns', 'http://www.w3.org/2000/svg')
root.setAttribute('width', String(w)); root.setAttribute('height', String(h))
const url = URL.createObjectURL(new Blob([new XMLSerializer().serializeToString(root)], { type: 'image/svg+xml;charset=utf-8' }))
const img = new Image(); img.decoding = 'sync'; img.src = url; await img.decode()
const c = document.createElement('canvas'); c.width = w; c.height = h
const ctx = c.getContext('2d')!; ctx.fillStyle = '#ffffff'; ctx.fillRect(0, 0, w, h); ctx.drawImage(img, 0, 0, w, h)
return c.toDataURL('image/png').split(',')[1]
}, [r.svg, r.widthPx, r.heightPx])
unwrap(session.addRaster(r.key, new Uint8Array(Buffer.from(b64, 'base64'))), r.key)
}
}
const parts = unwrap(session.finish(), 'finish')
writeFileSync(join(OUT!, `${name}.pdf`), Buffer.concat(parts))
writeFileSync(join(OUT!, `${name}.views.json`), JSON.stringify(svgs.length))
} finally {
session.free()
}
}
await browser.close()
}, 600_000)