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
@@ -1,4 +1,4 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { expect, vi } from 'vitest'
|
||||
@@ -12,8 +12,14 @@ import {
|
||||
PDFPage,
|
||||
PDFString,
|
||||
} from 'pdf-lib'
|
||||
import type { Observation, Report, SheetSnapshot } from '@faunosteo/domain'
|
||||
import type {
|
||||
Branding,
|
||||
Observation,
|
||||
Report,
|
||||
SheetSnapshot,
|
||||
} from '@faunosteo/domain'
|
||||
import { composeHighlightedViewSvg } from '@faunosteo/pdf'
|
||||
import { nologosBranding } from './sheet'
|
||||
|
||||
/**
|
||||
* Step 6 Phase 0 freeze: what the PDF pipeline drew before the engine,
|
||||
@@ -41,7 +47,6 @@ export const TWO_BY_ONE_PNG = Uint8Array.from(
|
||||
const HERE = dirname(fileURLToPath(import.meta.url))
|
||||
export const FIXTURES_DIR = resolve(HERE, '../fixtures')
|
||||
export const GOLDEN_DIR = resolve(FIXTURES_DIR, 'goldens/step6')
|
||||
const PUBLIC_DIR = resolve(HERE, '../../public')
|
||||
|
||||
/** `MAKE_GOLDEN=1` writes the goldens instead of asserting them. */
|
||||
export const MAKE_GOLDEN = process.env.MAKE_GOLDEN === '1'
|
||||
@@ -179,42 +184,16 @@ export async function readLinks(bytes: Uint8Array): Promise<LinkEntry[]> {
|
||||
return out
|
||||
}
|
||||
|
||||
const LOGO_FILES: Readonly<Record<string, string>> = {
|
||||
'/assets/faunosteo_logo_horizontal-512.png':
|
||||
'assets/faunosteo_logo_horizontal-512.png',
|
||||
'/assets/faunosteo-horizontal-com-512.png':
|
||||
'assets/faunosteo-horizontal-com-512.png',
|
||||
'/brand/zenquality-logo-fr.png': 'brand/zenquality-logo-fr.png',
|
||||
'/Paws_in_harmony.svg': 'Paws_in_harmony.svg',
|
||||
}
|
||||
|
||||
function serveLogo(path: string): Response {
|
||||
const relative = LOGO_FILES[path]
|
||||
const file = relative ? resolve(PUBLIC_DIR, relative) : null
|
||||
if (!file || !existsSync(file))
|
||||
return new Response('not found', { status: 404 })
|
||||
return new Response(new Uint8Array(readFileSync(file)), { status: 200 })
|
||||
}
|
||||
|
||||
/** 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) =>
|
||||
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 }),
|
||||
)
|
||||
/**
|
||||
* The branding of one logo mode, for a test that mocks `@/pdf/branding`:
|
||||
* `logos` is the real module (the bundled files), `nologos` every slot
|
||||
* empty with the credits' fallback texts (the Phase 0 fetch 404).
|
||||
*/
|
||||
export async function brandingOf(mode: LogoMode): Promise<Branding> {
|
||||
if (mode === 'nologos') return nologosBranding()
|
||||
const real =
|
||||
await vi.importActual<typeof import('@/pdf/branding')>('@/pdf/branding')
|
||||
return real.loadBranding()
|
||||
}
|
||||
|
||||
const HIGHLIGHT_FILL = '#b85a3f'
|
||||
|
||||
Reference in new issue
Block a user