fix(pdf): bundle the branding assets into the app (no fetch at export), export returns its warnings and the editor reports missing elements

This commit is contained in:
bchanot committed 2026-10-11 16:09:39 +02:00
1 parent 80e95ca61d
commit 293a6e0b36
22 files changed
+199 -185

No files matched your search

+30 -24
View File
@@ -19,6 +19,7 @@ import {
buildSheetIndex,
buildSheetSnapshot,
ReportSchema,
type Branding,
type FindingCategory,
type FindingNode,
type Report,
@@ -33,8 +34,6 @@ import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
import {
FIXTURES_DIR,
installFetch,
installLogoFetch,
ONE_PX_PNG,
readLinks,
recordDrawing,
@@ -50,10 +49,17 @@ vi.mock('@/pdf/browserRasterizer', async () => {
const { TWO_BY_ONE_PNG } = await import('../helpers/goldens')
return { browserRasterizer: vi.fn(async () => TWO_BY_ONE_PNG) }
})
// The bundled branding, unless a scenario swaps the deployment's logos.
const branding = vi.hoisted(() => ({ current: null as Branding | null }))
vi.mock('@/pdf/branding', async (importOriginal) => {
const real = await importOriginal<typeof import('@/pdf/branding')>()
return {
loadBranding: vi.fn(async () => branding.current ?? real.loadBranding()),
}
})
let sheet: SheetIndex
let demo: Report
let restoreFetch: () => void = () => undefined
beforeAll(async () => {
sheet = await loadTestSheet()
@@ -65,8 +71,8 @@ beforeAll(async () => {
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
})
afterEach(() => {
restoreFetch()
restoreFetch = () => undefined
branding.current = null
vi.unstubAllGlobals()
})
afterAll(() => vi.restoreAllMocks())
@@ -96,18 +102,9 @@ const facts = ({ texts, images, links }: Facts): Facts => ({
links,
})
/** The app export path, logos served from public/, under the recorder. */
async function appExport(
index: SheetIndex,
): Promise<Facts & { pdf: Uint8Array }> {
const restore = installLogoFetch('logos')
try {
return await recorded(async () => ({
pdf: await exportReportToPdf(demo, index, { locale: 'fr' }),
}))
} finally {
restore()
}
/** The app export path, bundled logos, under the recorder. */
function appExport(index: SheetIndex): Promise<Facts & { pdf: Uint8Array }> {
return recorded(() => exportReportToPdf(demo, index, { locale: 'fr' }))
}
async function archiveOf(pdf: Uint8Array): Promise<ReportArchiveEnvelope> {
@@ -181,18 +178,27 @@ describe('the archive survives its resources', () => {
const archive = await archiveOf(first.pdf)
// The deployment now ships other logos: a 1×1 PNG everywhere, another partner SVG.
const onePx = {
kind: 'png' as const,
base64: Buffer.from(ONE_PX_PNG).toString('base64'),
}
const otherSvg =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><rect width="10" height="10"/></svg>'
const fetchSpy = vi.fn((path: string) =>
path.endsWith('.svg')
? new Response(otherSvg, { status: 200 })
: new Response(ONE_PX_PNG, { status: 200 }),
)
restoreFetch = installFetch(fetchSpy)
branding.current = {
...archive.branding,
headerLogo: onePx,
footerLogo: onePx,
poweredBy: { ...archive.branding.poweredBy!, logo: onePx },
partner: {
...archive.branding.partner!,
logo: { kind: 'svg', svg: otherSvg },
},
}
const current = await loadBranding()
expect(current.headerLogo).not.toBeNull()
expect(current).not.toEqual(archive.branding)
fetchSpy.mockClear()
const fetchSpy = vi.fn()
vi.stubGlobal('fetch', fetchSpy)
const again = await recorded(() => exportFromEnvelope(archive, REEXPORT))
expect(fetchSpy).not.toHaveBeenCalled()