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:
1 parent
80e95ca61d
commit
293a6e0b36
22 files changed
+199
-185
No files matched your search
@@ -17,11 +17,16 @@ import { AFRelationship, PDFDocument, PDFPage } from 'pdf-lib'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { buildSheetSnapshot, type Observation } from '@faunosteo/domain'
|
||||
import {
|
||||
buildSheetSnapshot,
|
||||
type Observation,
|
||||
type Report,
|
||||
} from '@faunosteo/domain'
|
||||
import { composeHighlightedViewSvg, defaultPdfLabels } from '@faunosteo/pdf'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { importReportFromPdf, ImportError } from '@/pdf/import'
|
||||
import { formatDate } from '@/lib/dates'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||
|
||||
@@ -34,29 +39,25 @@ vi.mock('@/pdf/browserRasterizer', async () => {
|
||||
/** The static PDF strings now belong to the engine's label set. */
|
||||
const { fr, en } = defaultPdfLabels
|
||||
|
||||
/** The bytes of an app export (its warnings are not under test here). */
|
||||
async function exportPdf(report: Report, locale: Locale): Promise<Uint8Array> {
|
||||
return (await exportReportToPdf(report, sheet, { locale })).pdf
|
||||
}
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
const fixture = (name: string) =>
|
||||
new Uint8Array(readFileSync(resolve(HERE, '../fixtures', name)))
|
||||
|
||||
let sheet: SheetIndex
|
||||
let drawn: string[] = []
|
||||
const fetched: string[] = []
|
||||
const realFetch = globalThis.fetch
|
||||
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
||||
fetched.push(String(input))
|
||||
return new Response('not found', { status: 404 })
|
||||
}) as typeof fetch
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||||
})
|
||||
afterAll(() => {
|
||||
globalThis.fetch = realFetch
|
||||
})
|
||||
afterAll(() => vi.restoreAllMocks())
|
||||
beforeEach(() => {
|
||||
drawn = []
|
||||
fetched.length = 0
|
||||
vi.spyOn(PDFPage.prototype, 'drawText').mockImplementation(function (
|
||||
this: PDFPage,
|
||||
text: string,
|
||||
@@ -118,7 +119,7 @@ describe('export on a full v3 report (FR)', () => {
|
||||
observations: [fullObservation()],
|
||||
conclusion: 'ok',
|
||||
})
|
||||
const bytes = await exportReportToPdf(report, sheet, { locale: 'fr' })
|
||||
const bytes = await exportPdf(report, 'fr')
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
expect(doc.getPageCount()).toBe(2)
|
||||
const zonesIdx = drawn.indexOf(fr['pdf.section.zones']!)
|
||||
@@ -145,7 +146,7 @@ describe('export on a full v3 report (FR)', () => {
|
||||
it('attaches the envelope and roundtrips in both locales', async () => {
|
||||
const report = minimalReport(sheet, { observations: [fullObservation()] })
|
||||
for (const locale of ['fr', 'en'] as const) {
|
||||
const bytes = await exportReportToPdf(report, sheet, { locale })
|
||||
const bytes = await exportPdf(report, locale)
|
||||
const out = await importReportFromPdf(bytes, sheet.sheet.migrationMap)
|
||||
expect(out.migrated).toBe(false)
|
||||
expect(out.origin).toBe('envelope_v1')
|
||||
@@ -242,7 +243,7 @@ describe('PDF content on a two-observation report', () => {
|
||||
|
||||
it('draws free findings, comments, the conclusion page and the header on every page', async () => {
|
||||
const report = fullReport()
|
||||
const bytes = await exportReportToPdf(report, sheet, { locale: 'fr' })
|
||||
const bytes = await exportPdf(report, 'fr')
|
||||
const pages = (await PDFDocument.load(bytes)).getPageCount()
|
||||
expect(drawn).toContain(fr['pdf.section.free_findings'])
|
||||
expect(joined()).toContain('Sensibilité au garrot')
|
||||
@@ -260,7 +261,7 @@ describe('PDF content on a two-observation report', () => {
|
||||
})
|
||||
|
||||
it('sets the PDF metadata and the archive keyword', async () => {
|
||||
const bytes = await exportReportToPdf(fullReport(), sheet, { locale: 'fr' })
|
||||
const bytes = await exportPdf(fullReport(), 'fr')
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
expect(doc.getTitle()).toBe(`${fr['pdf.title.suffix.bilan']}Mistral`)
|
||||
expect(doc.getAuthor()).toBe('Dr. Dupont')
|
||||
@@ -268,17 +269,28 @@ describe('PDF content on a two-observation report', () => {
|
||||
expect(doc.getKeywords()).toContain('faunosteo-archive/1')
|
||||
})
|
||||
|
||||
it('fetches the logos at export time', async () => {
|
||||
await exportReportToPdf(fullReport(), sheet, { locale: 'fr' })
|
||||
expect(
|
||||
fetched.some(
|
||||
(u) => u.endsWith('.png') || u.endsWith('Paws_in_harmony.svg'),
|
||||
),
|
||||
).toBe(true)
|
||||
it('embeds the four bundled logos without any network request', async () => {
|
||||
const fetchSpy = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchSpy)
|
||||
try {
|
||||
const { pdf, warnings } = await exportReportToPdf(fullReport(), sheet, {
|
||||
locale: 'fr',
|
||||
})
|
||||
expect(fetchSpy).not.toHaveBeenCalled()
|
||||
expect(warnings).toEqual([])
|
||||
const out = await importReportFromPdf(pdf, sheet.sheet.migrationMap)
|
||||
const branding = out.archive!.branding
|
||||
expect(branding.headerLogo?.kind).toBe('png')
|
||||
expect(branding.footerLogo?.kind).toBe('png')
|
||||
expect(branding.poweredBy?.logo?.kind).toBe('png')
|
||||
expect(branding.partner?.logo?.kind).toBe('svg')
|
||||
} finally {
|
||||
vi.unstubAllGlobals()
|
||||
}
|
||||
})
|
||||
|
||||
it('attaches osteo-report.json as a Flate EmbeddedFile, AFRelationship Data, "Faunosteo report archive v1"', async () => {
|
||||
const bytes = await exportReportToPdf(fullReport(), sheet, { locale: 'fr' })
|
||||
const bytes = await exportPdf(fullReport(), 'fr')
|
||||
const doc = await PDFDocument.load(bytes)
|
||||
const objects = [...doc.context.enumerateIndirectObjects()].map(([, o]) =>
|
||||
String(o),
|
||||
@@ -368,7 +380,7 @@ describe('import of the frozen v2 archive PDFs', () => {
|
||||
)
|
||||
// Re-export and re-import: stable.
|
||||
const again = await importReportFromPdf(
|
||||
await exportReportToPdf(report, sheet, { locale: 'fr' }),
|
||||
await exportPdf(report, 'fr'),
|
||||
sheet.sheet.migrationMap,
|
||||
)
|
||||
expect(again.report).toEqual(report)
|
||||
@@ -440,7 +452,7 @@ describe('import of the frozen v2 archive PDFs', () => {
|
||||
expect(out.unresolvedZones).toBe(1)
|
||||
// The migrated document is a valid v3 document: exporting it again keeps the flags.
|
||||
const again = await importReportFromPdf(
|
||||
await exportReportToPdf(out.report, sheet, { locale: 'en' }),
|
||||
await exportPdf(out.report, 'en'),
|
||||
sheet.sheet.migrationMap,
|
||||
)
|
||||
expect(again.report.observations[0]!.findings[0]!.unresolved).toBe(true)
|
||||
|
||||
Reference in new issue
Block a user