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:
bchanot committed 2026-10-19 01:26:13 +02:00
1 parent 9e688b6801
commit d492bf7112
29 files changed
+507 -131

No files matched your search

@@ -27,7 +27,9 @@ import { loadTestSheet, minimalReport } from '../helpers/sheet'
vi.mock('@/pdf/export', () => ({ exportReportToPdf: vi.fn() }))
const exportMock = vi.mocked(exportReportToPdf)
const PDF_BYTES = new Uint8Array([37, 80, 68, 70])
const PDF_BYTES = new Blob([new Uint8Array([37, 80, 68, 70])], {
type: 'application/pdf',
})
let sheet: SheetIndex
const downloads: string[] = []
@@ -69,25 +71,32 @@ afterEach(() => {
URL.revokeObjectURL = realRevokeObjectURL
})
async function mountAndExport(label: string): Promise<void> {
render(
async function mountAndExport(label: string): Promise<() => void> {
const { unmount } = render(
<MemoryRouter initialEntries={['/editor']}>
<EditorRoute />
</MemoryRouter>,
)
const button = await screen.findByRole('button', { name: label })
await userEvent.setup().click(button)
return unmount
}
/** The options the route passes: the locale and the signal it aborts on unmount. */
const options = (locale: 'fr' | 'en') => ({
locale,
signal: expect.any(AbortSignal) as AbortSignal,
})
describe('EditorRoute export', () => {
it('calls exportReportToPdf with the store report, the loaded index and the FR locale', async () => {
exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] })
await mountAndExport(fr['editor.export']!)
await waitFor(() => expect(exportMock).toHaveBeenCalledTimes(1))
const [report, index, options] = exportMock.mock.calls[0]!
const [report, index, passed] = exportMock.mock.calls[0]!
expect(report).toBe(useReportStore.getState().report)
expect(index).toBe(sheet)
expect(options).toEqual({ locale: 'fr' })
expect(passed).toEqual(options('fr'))
})
it('passes the active locale (EN)', async () => {
@@ -95,7 +104,7 @@ describe('EditorRoute export', () => {
exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] })
await mountAndExport(en['editor.export']!)
await waitFor(() => expect(exportMock).toHaveBeenCalledTimes(1))
expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' })
expect(exportMock.mock.calls[0]![2]).toEqual(options('en'))
})
it('downloads bilan-<animal>-<date>.pdf through a blob URL it revokes after a delay', async () => {
@@ -131,6 +140,28 @@ describe('EditorRoute export', () => {
)
})
it('aborts the export when the editor unmounts mid-way: no download, no alert', async () => {
let settle: (() => void) | null = null
exportMock.mockImplementation(
(_report, _index, opts) =>
new Promise((_, reject) => {
settle = () =>
reject(new DOMException('export aborted', 'AbortError'))
opts.signal?.addEventListener('abort', () => settle?.())
}),
)
const unmount = await mountAndExport(fr['editor.export']!)
await waitFor(() => expect(exportMock).toHaveBeenCalledTimes(1))
const signal = exportMock.mock.calls[0]![2].signal!
expect(signal.aborted).toBe(false)
unmount()
expect(signal.aborted).toBe(true)
await Promise.resolve()
await Promise.resolve()
expect(downloads).toHaveLength(0)
expect(window.alert).not.toHaveBeenCalled()
})
it('alerts editor.export_failed when the export rejects', async () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
exportMock.mockRejectedValue(new Error('render exploded'))