Files
faunosteo/packages/pdf/tests/raster.test.ts
T

123 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* T10 (STEP6 §7): a real rasterizer (resvg) renders the demo report's main
* and head views: 1200 px wide, height from the view box, highlight colour
* present, sane non-white ratio. A failing rasterizer degrades to a page
* without the image and one warning.
*/
import { beforeAll, describe, expect, it } from 'vitest'
import * as UPNG from '@pdf-lib/upng'
import { PDFDocument } from 'pdf-lib'
import { buildSheetSnapshot } from '@faunosteo/domain'
import {
defaultPdfLabels,
exportReportArchive,
type ExportInput,
type Rasterizer,
} from '../src'
import {
FIXED_EXPORTED_AT,
nologosBranding,
recordDrawing,
} from './helpers/goldens'
import { loadGoldenReports, type GoldenReports } from './helpers/reports'
import { resvgRasterizer, type Raster } from './helpers/resvg'
const HIGHLIGHT = { r: 0xb8, g: 0x5a, b: 0x3f }
const TOLERANCE = 24
interface PixelFacts {
width: number
height: number
highlightPixels: number
nonWhiteRatio: number
}
function pixelFacts(png: Uint8Array): PixelFacts {
const buffer = new ArrayBuffer(png.byteLength)
new Uint8Array(buffer).set(png)
const image = UPNG.decode(buffer)
const rgba = new Uint8Array(UPNG.toRGBA8(image)[0]!)
let highlightPixels = 0
let nonWhite = 0
for (let i = 0; i < rgba.length; i += 4) {
const [r, g, b] = [rgba[i]!, rgba[i + 1]!, rgba[i + 2]!]
if (r < 250 || g < 250 || b < 250) nonWhite += 1
if (
Math.abs(r - HIGHLIGHT.r) <= TOLERANCE &&
Math.abs(g - HIGHLIGHT.g) <= TOLERANCE &&
Math.abs(b - HIGHLIGHT.b) <= TOLERANCE
) {
highlightPixels += 1
}
}
const total = rgba.length / 4
return {
width: image.width,
height: image.height,
highlightPixels,
nonWhiteRatio: nonWhite / total,
}
}
let golden: GoldenReports
beforeAll(() => {
golden = loadGoldenReports()
})
function inputFor(
observations: GoldenReports['reports']['demo']['observations'],
rasterizer: Rasterizer,
): ExportInput {
const report = { ...golden.reports.demo, observations }
return {
report,
sheet: buildSheetSnapshot(golden.draft, report),
branding: nologosBranding(),
labels: defaultPdfLabels,
locale: 'fr',
rasterizer,
exportedAt: FIXED_EXPORTED_AT,
}
}
describe('resvg rasterisation of the demo views', () => {
it('main (haloed) and head views: 1200 px wide, highlight present, sane coverage', async () => {
const rasters: Raster[] = []
const [main, , , head] = golden.reports.demo.observations
const { pdf, warnings } = await exportReportArchive(
inputFor([main!, head!], resvgRasterizer(rasters)),
)
expect(warnings).toEqual([])
expect(rasters).toHaveLength(2)
for (const { requested, png } of rasters) {
const facts = pixelFacts(png)
expect(facts.width).toBe(1200)
expect(Math.abs(facts.height - requested.heightPx)).toBeLessThanOrEqual(1)
expect(facts.highlightPixels).toBeGreaterThan(0)
expect(facts.nonWhiteRatio).toBeGreaterThanOrEqual(0.01)
expect(facts.nonWhiteRatio).toBeLessThanOrEqual(0.9)
}
// main: 2073.4 × 1629.4 → 943 px; head: 500 × 380 → 912 px
expect(rasters.map((r) => r.requested.heightPx)).toEqual([943, 912])
expect((await PDFDocument.load(pdf)).getPageCount()).toBe(3)
}, 60_000)
it('a rasterizer that throws: page without image, one view_raster_failed warning', async () => {
const [main] = golden.reports.demo.observations
const failing: Rasterizer = async () => {
throw new Error('no renderer here')
}
const recording = recordDrawing()
try {
const { warnings } = await exportReportArchive(inputFor([main!], failing))
expect(warnings).toEqual([
{ kind: 'view_raster_failed', observationId: main!.id, viewId: 'main' },
])
expect(recording.images).toEqual([])
expect(recording.texts.some((t) => t.text === 'VUE GÉNÉRALE')).toBe(false)
} finally {
recording.stop()
}
})
})