feat(app): export and import through @faunosteo/pdf — snapshot, archived branding and labels, browser rasterizer, imported archive kept in session, explicit rebase with messages; legacy PDF modules removed

This commit is contained in:
bchanot committed 2026-10-11 13:32:14 +02:00
1 parent 20dafc0b00
commit 9bd5173a9c
43 files changed
+1492 -1989

No files matched your search

+54 -28
View File
@@ -12,18 +12,32 @@ import {
PDFPage,
PDFString,
} from 'pdf-lib'
import type { Observation, Report } from '@faunosteo/domain'
import type { SheetIndex } from '@/services/sheetIndex'
import { highlightedViewSvg } from '@/pdf/render/observationPage'
import type { Observation, Report, SheetSnapshot } from '@faunosteo/domain'
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
/**
* Step 6 Phase 0 freeze: what the current PDF pipeline draws, recorded as
* JSON goldens (docs/development/STEP6_PDF_ARCHIVE_ANALYSIS.md §17). The
* recorder spies on pdf-lib, the link reader parses the saved bytes, the
* view snapshot parses the highlighted SVG. Numbers are rounded to 2
* decimals; nothing ordered by drawing order is sorted.
* Step 6 Phase 0 freeze: what the PDF pipeline drew before the engine,
* recorded as JSON goldens (docs/development/STEP6_PDF_ARCHIVE_ANALYSIS.md
* §17) that the app export path must still reproduce. The recorder spies on
* pdf-lib, the link reader parses the saved bytes, the view snapshot parses
* the engine's highlighted SVG with the browser DOM. Numbers are rounded to
* 2 decimals; nothing ordered by drawing order is sorted.
*/
/** 1×1 PNG (nologos: the legacy stub) and 2×1 PNG (logos: deterministic partner logo width). */
export const ONE_PX_PNG = Uint8Array.from(
Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
'base64',
),
)
export const TWO_BY_ONE_PNG = Uint8Array.from(
Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC',
'base64',
),
)
const HERE = dirname(fileURLToPath(import.meta.url))
export const FIXTURES_DIR = resolve(HERE, '../fixtures')
export const GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/step6')
@@ -174,8 +188,7 @@ const LOGO_FILES: Readonly<Record<string, string>> = {
'/Paws_in_harmony.svg': 'Paws_in_harmony.svg',
}
function serveLogo(input: RequestInfo | URL): Response {
const path = new URL(String(input), 'http://localhost').pathname
function serveLogo(path: string): Response {
const relative = LOGO_FILES[path]
const file = relative ? resolve(PUBLIC_DIR, relative) : null
if (!file || !existsSync(file))
@@ -183,18 +196,27 @@ function serveLogo(input: RequestInfo | URL): Response {
return new Response(new Uint8Array(readFileSync(file)), { status: 200 })
}
/** Stubs fetch for one logo mode; returns the restore function. */
export function installLogoFetch(mode: LogoMode): () => void {
/** Stubs fetch with a path → Response handler; returns the restore function. */
export function installFetch(respond: (path: string) => Response): () => void {
const real = globalThis.fetch
globalThis.fetch = (async (input: RequestInfo | URL) =>
mode === 'logos'
? serveLogo(input)
: new Response('not found', { status: 404 })) as typeof fetch
respond(
new URL(String(input), 'http://localhost').pathname,
)) as typeof fetch
return () => {
globalThis.fetch = real
}
}
/** Stubs fetch for one logo mode; returns the restore function. */
export function installLogoFetch(mode: LogoMode): () => void {
return installFetch((path) =>
mode === 'logos'
? serveLogo(path)
: new Response('not found', { status: 404 }),
)
}
const HIGHLIGHT_FILL = '#b85a3f'
function highlightedOf(el: Element): HighlightedElement {
@@ -236,11 +258,11 @@ function countDataAttributes(root: Element): number {
}
export function snapshotView(
sheet: SheetIndex,
sheet: SheetSnapshot,
observation: Observation,
viewId: string,
): ViewSnapshot {
const svg = highlightedViewSvg(sheet, viewId, observation)
const svg = composeHighlightedViewSvg(sheet, viewId, observation)
const root = new DOMParser().parseFromString(
svg,
'image/svg+xml',
@@ -261,19 +283,18 @@ export function snapshotView(
/** The views an observation draws, in the pipeline's order: main first, then first appearance. */
export function viewIdsOf(
sheet: SheetIndex,
sheet: SheetSnapshot,
observation: Observation,
): string[] {
const views = new Set(sheet.document.views.map((v) => v.id))
const main = sheet.document.views.find((v) => v.kind === 'main')!.id
return [...new Set(observation.zones.map((z) => z.viewId))]
.filter((id) => sheet.view(id))
.sort(
(a, b) =>
Number(b === sheet.mainView.id) - Number(a === sheet.mainView.id),
)
.filter((id) => views.has(id))
.sort((a, b) => Number(b === main) - Number(a === main))
}
export function snapshotViews(
sheet: SheetIndex,
sheet: SheetSnapshot,
report: Report,
): ViewSnapshot[] {
return report.observations.flatMap((observation) =>
@@ -283,14 +304,19 @@ export function snapshotViews(
)
}
export function readGolden<T = unknown>(name: string): T {
return JSON.parse(readFileSync(resolve(GOLDEN_DIR, name), 'utf8')) as T
}
/** Writes the golden under MAKE_GOLDEN, asserts deep equality otherwise. */
export function checkGolden(name: string, actual: unknown): void {
const path = resolve(GOLDEN_DIR, name)
if (MAKE_GOLDEN) {
mkdirSync(GOLDEN_DIR, { recursive: true })
writeFileSync(path, `${JSON.stringify(actual, null, 2)}\n`)
writeFileSync(
resolve(GOLDEN_DIR, name),
`${JSON.stringify(actual, null, 2)}\n`,
)
return
}
const golden: unknown = JSON.parse(readFileSync(path, 'utf8'))
expect(actual, name).toEqual(golden)
expect(actual, name).toEqual(readGolden(name))
}
+42
View File
@@ -1,8 +1,14 @@
import {
buildSheetSnapshot,
ENVELOPE_FORMAT,
ENVELOPE_VERSION,
REPORT_VERSION,
type Branding,
type Observation,
type Report,
type ReportArchiveEnvelope,
} from '@faunosteo/domain'
import { defaultPdfLabels } from '@faunosteo/pdf'
import { loadSheetIndex } from '@/sheets/useSheet'
import type { SheetIndex } from '@/services/sheetIndex'
@@ -37,3 +43,39 @@ export function minimalReport(
...patch,
}
}
/** The branding the app loads when every logo fetch fails (credits keep their text). */
export function nologosBranding(): Branding {
return {
siteUrl: 'https://faunosteo.com',
headerLogo: null,
footerLogo: null,
poweredBy: {
logo: null,
url: 'https://zenquality.fr',
fallbackText: 'zenquality.fr',
},
partner: {
logo: null,
url: 'https://www.instagram.com/paws.iin.harmony',
fallbackText: '@paws.iin.harmony',
},
}
}
/** A valid Step 6 envelope for `report`, snapshotted on `sheet` (no logos). */
export function archiveEnvelope(
sheet: SheetIndex,
report: Report,
): ReportArchiveEnvelope {
return {
format: ENVELOPE_FORMAT,
envelopeVersion: ENVELOPE_VERSION,
exportedAt: '2026-10-11T12:00:00.000Z',
locale: 'fr',
report,
sheetSnapshot: buildSheetSnapshot(sheet.sheet, report),
branding: nologosBranding(),
labels: defaultPdfLabels,
}
}