feat(pdf): R4.6 app export on the core's session: export.ts drives ExportSession (rasters one by one, warnings in the engine's order, abort on unmount), labels moved to the app, EditorRoute downloads the Blob of the parts; suites read the file's content streams (pdfRead helper), app-path bytes = R4 digests, hex strings upper-case as pdf-lib
This commit is contained in:
1 parent
9e688b6801
commit
d492bf7112
29 files changed
+507
-131
No files matched your search
@@ -1,30 +1,25 @@
|
||||
/**
|
||||
* Replaces char.pdfFull / pdf.roundtrip / pdf.zoneLabels: the application
|
||||
* export path on the engine with the bundled sheet (drawn strings from the
|
||||
* document, envelope attachment, roundtrip FR/EN) and the import of the
|
||||
* frozen v2 archive PDFs, migrated without silent loss.
|
||||
* export path on the core's session with the bundled sheet (drawn strings
|
||||
* from the document, read back from the file's content streams since R4.6,
|
||||
* envelope attachment, roundtrip FR/EN) and the import of the frozen v2
|
||||
* archive PDFs, migrated without silent loss.
|
||||
*/
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeAll,
|
||||
beforeEach,
|
||||
afterAll,
|
||||
} from 'vitest'
|
||||
import { AFRelationship, PDFDocument, PDFPage } from 'pdf-lib'
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import { AFRelationship, PDFDocument } from 'pdf-lib'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import type { Observation, Report } from '@faunosteo/core-wasm/types'
|
||||
import { composeHighlightedViewSvg, defaultPdfLabels } from '@faunosteo/pdf'
|
||||
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
|
||||
import { exportReportToPdf } from '@/pdf/export'
|
||||
import { defaultPdfLabels } from '@/pdf/labels'
|
||||
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, sheetSnapshot } from '../helpers/sheet'
|
||||
import { bytesOf, readDrawing } from '../helpers/pdfRead'
|
||||
|
||||
// The factory runs when the engine first loads the adapter: fixed 1×1 PNG.
|
||||
vi.mock('@/pdf/browserRasterizer', async () => {
|
||||
@@ -40,12 +35,19 @@ async function readBack(bytes: Uint8Array) {
|
||||
return (await importReportFromPdf(bytes, sheet)).read
|
||||
}
|
||||
|
||||
/** The static PDF strings now belong to the engine's label set. */
|
||||
/** The static PDF strings belong to the app's label set. */
|
||||
const { fr, en } = defaultPdfLabels
|
||||
|
||||
/** The bytes of an app export (its warnings are not under test here). */
|
||||
/**
|
||||
* The bytes of an app export (its warnings are not under test here); `drawn` holds the
|
||||
* texts of its content streams, in draw order.
|
||||
*/
|
||||
async function exportPdf(report: Report, locale: Locale): Promise<Uint8Array> {
|
||||
return (await exportReportToPdf(report, sheet, { locale })).pdf
|
||||
const bytes = await bytesOf(
|
||||
(await exportReportToPdf(report, sheet, { locale })).pdf,
|
||||
)
|
||||
drawn = (await readDrawing(bytes)).texts.map((t) => t.text)
|
||||
return bytes
|
||||
}
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
@@ -60,15 +62,6 @@ beforeAll(async () => {
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||||
})
|
||||
afterAll(() => vi.restoreAllMocks())
|
||||
beforeEach(() => {
|
||||
drawn = []
|
||||
vi.spyOn(PDFPage.prototype, 'drawText').mockImplementation(function (
|
||||
this: PDFPage,
|
||||
text: string,
|
||||
) {
|
||||
drawn.push(text)
|
||||
} as never)
|
||||
})
|
||||
|
||||
/** A full v3 observation: plain zone, bilateral both sides, spine with vertebrae, head zone, findings. */
|
||||
function fullObservation(): Observation {
|
||||
@@ -208,7 +201,7 @@ function fullReport() {
|
||||
|
||||
describe('PDF content on a two-observation report', () => {
|
||||
it('prints the empty marker for a zoneless observation and keeps its findings', async () => {
|
||||
await exportReportToPdf(fullReport(), sheet, { locale: 'fr' })
|
||||
await exportPdf(fullReport(), 'fr')
|
||||
expect(drawn).toContain(fr['pdf.empty'])
|
||||
const prefix = fr['pdf.section.findings_prefix']!
|
||||
const occurrences = drawn.filter(
|
||||
@@ -231,14 +224,14 @@ describe('PDF content on a two-observation report', () => {
|
||||
],
|
||||
}
|
||||
const report = minimalReport(sheet, { observations: [stale] })
|
||||
await exportReportToPdf(report, sheet, { locale: 'fr' })
|
||||
await exportPdf(report, 'fr')
|
||||
expect(drawn).toContain('point-gone')
|
||||
expect(drawn).toContain(`${fr['pdf.section.findings_prefix']}ZONE-GONE`)
|
||||
expect(drawn).toContain('Snapshot FR')
|
||||
})
|
||||
|
||||
it('draws the EN dictionary, annotation names and finding snapshots', async () => {
|
||||
await exportReportToPdf(fullReport(), sheet, { locale: 'en' })
|
||||
await exportPdf(fullReport(), 'en')
|
||||
expect(drawn).toContain(en['pdf.section.zones'])
|
||||
expect(drawn.some((l) => l.includes('Hip joint'))).toBe(true)
|
||||
expect(drawn).toContain('Junction fixation')
|
||||
@@ -282,7 +275,7 @@ describe('PDF content on a two-observation report', () => {
|
||||
})
|
||||
expect(fetchSpy).not.toHaveBeenCalled()
|
||||
expect(warnings).toEqual([])
|
||||
const out = await readBack(pdf)
|
||||
const out = await readBack(await bytesOf(pdf))
|
||||
const branding = out.archive!.branding
|
||||
expect(branding.headerLogo?.kind).toBe('png')
|
||||
expect(branding.footerLogo?.kind).toBe('png')
|
||||
|
||||
Reference in new issue
Block a user