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,12 +1,15 @@
|
||||
/**
|
||||
* Step 6 Phase 0 freeze (STEP6_PDF_ARCHIVE_ANALYSIS.md §17) replayed on the
|
||||
* application export path: `exportReportToPdf` now runs the engine with the
|
||||
* bundled sheet snapshotted, the bundled branding (mocked to every slot empty
|
||||
* for the `nologos` mode, the real module for `logos`) and the browser
|
||||
* rasterizer (mocked to fixed PNGs, as the Phase 0 stub was). Text,
|
||||
* images and links goldens are asserted as frozen; `MAKE_GOLDEN=1` rewrites
|
||||
* them, never without a user decision. Never regenerate `report-v3.bare.pdf`:
|
||||
* it is the Step 5 bare export, an import fixture only.
|
||||
* application export path: `exportReportToPdf` runs the core's session (R4.6)
|
||||
* with the bundled sheet snapshotted, the bundled branding (mocked to every
|
||||
* slot empty for the `nologos` mode, the real module for `logos`) and the
|
||||
* browser rasterizer (mocked to fixed PNGs, as the Phase 0 stub was). Text,
|
||||
* images and links goldens are read back from the file's content streams and
|
||||
* asserted as frozen; `MAKE_GOLDEN=1` rewrites them, never without a user
|
||||
* decision. At the fixed export time the file's bytes are the R4 digests the
|
||||
* cargo suite froze (`goldens/r4/pdf.*.sha256`): the app path writes what the
|
||||
* native core writes. Never regenerate `report-v3.bare.pdf`: it is the Step 5
|
||||
* bare export, an import fixture only.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
@@ -29,7 +32,6 @@ import {
|
||||
ONE_PX_PNG,
|
||||
readGolden,
|
||||
readLinks,
|
||||
recordDrawing,
|
||||
snapshotViews,
|
||||
TWO_BY_ONE_PNG,
|
||||
type ImageEntry,
|
||||
@@ -38,6 +40,8 @@ import {
|
||||
type TextEntry,
|
||||
type ViewSnapshot,
|
||||
} from '../helpers/goldens'
|
||||
import { bytesOf, readDrawing } from '../helpers/pdfRead'
|
||||
import { FIXED_EXPORTED_AT, R4_GOLDEN_DIR, sha256 } from '../helpers/r4-draw'
|
||||
|
||||
const raster = vi.hoisted(() => ({ png: new Uint8Array() }))
|
||||
vi.mock('@/pdf/browserRasterizer', () => ({
|
||||
@@ -61,8 +65,13 @@ beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
;({ reports, unresolvedOfV2Unresolved } = buildGoldenReports(sheet))
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||||
vi.useFakeTimers({ toFake: ['Date'] })
|
||||
vi.setSystemTime(FIXED_EXPORTED_AT)
|
||||
})
|
||||
afterAll(() => {
|
||||
vi.useRealTimers()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
afterAll(() => vi.restoreAllMocks())
|
||||
|
||||
interface Exported {
|
||||
bytes: Uint8Array
|
||||
@@ -78,14 +87,29 @@ async function exportWith(
|
||||
): Promise<Exported> {
|
||||
raster.png = mode === 'logos' ? TWO_BY_ONE_PNG : ONE_PX_PNG
|
||||
logoMode.current = mode
|
||||
const recording = recordDrawing()
|
||||
try {
|
||||
const { pdf: bytes } = await exportReportToPdf(report, sheet, { locale })
|
||||
const { texts, images } = recording
|
||||
return { bytes, texts, images, links: await readLinks(bytes) }
|
||||
} finally {
|
||||
recording.stop()
|
||||
}
|
||||
const { pdf, warnings } = await exportReportToPdf(report, sheet, { locale })
|
||||
expect(warnings).toEqual([])
|
||||
const bytes = await bytesOf(pdf)
|
||||
const { texts, images } = await readDrawing(bytes)
|
||||
return { bytes, texts, images, links: await readLinks(bytes) }
|
||||
}
|
||||
|
||||
/**
|
||||
* The goldens were recorded in call order: every page's content, then the footers of
|
||||
* every page (the engine drew them last). Read back from the file, the facts come page
|
||||
* by page, each page's footer after its content: the golden in page order, stably, is
|
||||
* the same sequence.
|
||||
*/
|
||||
function inPageOrder<T extends { page: number }>(name: string): T[] {
|
||||
return [...readGolden<T[]>(name)].sort((a, b) => a.page - b.page)
|
||||
}
|
||||
|
||||
/** The R4 digest of this run, frozen by the cargo suite from the native writer. */
|
||||
function r4Digest(name: string, locale: Locale, mode: LogoMode): string {
|
||||
return readFileSync(
|
||||
resolve(R4_GOLDEN_DIR, `pdf.${name}.${locale}.${mode}.sha256`),
|
||||
'utf8',
|
||||
).trim()
|
||||
}
|
||||
|
||||
const pagesOf = (entries: readonly { page: number }[]) =>
|
||||
@@ -133,16 +157,22 @@ describe.each(REPORTS)('goldens of %s', (name) => {
|
||||
// Images and links are recorded per locale too: the footer centres the
|
||||
// "powered by" block on the label width, so the ZenQuality link (and its
|
||||
// logo) shift between FR and EN. Only the pagination is locale-free.
|
||||
it('text, images and links per locale; same pagination across locales', async () => {
|
||||
it('text, images and links per locale; same pagination across locales; app-path bytes = native digest', async () => {
|
||||
const fr = await exportWith(reports[name], mode, 'fr')
|
||||
const en = await exportWith(reports[name], mode, 'en')
|
||||
for (const [locale, out] of [
|
||||
['fr', fr],
|
||||
['en', en],
|
||||
] as const) {
|
||||
checkGolden(`text.${name}.${locale}.${mode}.json`, out.texts)
|
||||
checkGolden(`images.${name}.${locale}.${mode}.json`, out.images)
|
||||
checkGolden(`links.${name}.${locale}.${mode}.json`, out.links)
|
||||
const id = `${name}.${locale}.${mode}`
|
||||
expect(out.texts, `text.${id}`).toEqual(inPageOrder(`text.${id}.json`))
|
||||
expect(out.images, `images.${id}`).toEqual(
|
||||
inPageOrder(`images.${id}.json`),
|
||||
)
|
||||
checkGolden(`links.${id}.json`, out.links)
|
||||
expect(sha256(out.bytes), id).toBe(
|
||||
r4Digest(name, locale, mode),
|
||||
)
|
||||
}
|
||||
expect(pagesOf(en.images)).toEqual(pagesOf(fr.images))
|
||||
expect(pagesOf(en.links)).toEqual(pagesOf(fr.links))
|
||||
|
||||
Reference in new issue
Block a user