210 lines
6.5 KiB
TypeScript
210 lines
6.5 KiB
TypeScript
/**
|
||
* T2 and user scenario 3 (STEP6 §7): the archive the app exports is
|
||
* self-sufficient. Once exported, the catalog may change its labels and
|
||
* geometry and the deployment may ship other logos: a re-export from the
|
||
* envelope alone reproduces the first export, not the current resources.
|
||
*/
|
||
import {
|
||
describe,
|
||
it,
|
||
expect,
|
||
vi,
|
||
beforeAll,
|
||
afterAll,
|
||
afterEach,
|
||
} from 'vitest'
|
||
import { readFileSync } from 'node:fs'
|
||
import { resolve } from 'node:path'
|
||
import {
|
||
buildSheetIndex,
|
||
buildSheetSnapshot,
|
||
ReportSchema,
|
||
type Branding,
|
||
type FindingCategory,
|
||
type FindingNode,
|
||
type Report,
|
||
type ReportArchiveEnvelope,
|
||
} from '@faunosteo/domain'
|
||
import { exportFromEnvelope, readReportArchive } from '@faunosteo/pdf'
|
||
import { exportReportToPdf } from '@/pdf/export'
|
||
import { browserRasterizer } from '@/pdf/browserRasterizer'
|
||
import { loadBranding } from '@/pdf/branding'
|
||
import type { LoadedSheet } from '@/sheets/provider'
|
||
import type { SheetIndex } from '@/services/sheetIndex'
|
||
import { loadTestSheet } from '../helpers/sheet'
|
||
import {
|
||
FIXTURES_DIR,
|
||
ONE_PX_PNG,
|
||
readLinks,
|
||
recordDrawing,
|
||
snapshotViews,
|
||
type ImageEntry,
|
||
type LinkEntry,
|
||
type TextEntry,
|
||
} from '../helpers/goldens'
|
||
|
||
// Same fixed raster for every view and logo: images then depend on the
|
||
// branding and the layout only.
|
||
vi.mock('@/pdf/browserRasterizer', async () => {
|
||
const { TWO_BY_ONE_PNG } = await import('../helpers/goldens')
|
||
return { browserRasterizer: vi.fn(async () => TWO_BY_ONE_PNG) }
|
||
})
|
||
// The bundled branding, unless a scenario swaps the deployment's logos.
|
||
const branding = vi.hoisted(() => ({ current: null as Branding | null }))
|
||
vi.mock('@/pdf/branding', async (importOriginal) => {
|
||
const real = await importOriginal<typeof import('@/pdf/branding')>()
|
||
return {
|
||
loadBranding: vi.fn(async () => branding.current ?? real.loadBranding()),
|
||
}
|
||
})
|
||
|
||
let sheet: SheetIndex
|
||
let demo: Report
|
||
|
||
beforeAll(async () => {
|
||
sheet = await loadTestSheet()
|
||
demo = ReportSchema.parse(
|
||
JSON.parse(
|
||
readFileSync(resolve(FIXTURES_DIR, 'report-v3.demo.json'), 'utf8'),
|
||
),
|
||
)
|
||
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
|
||
})
|
||
afterEach(() => {
|
||
branding.current = null
|
||
vi.unstubAllGlobals()
|
||
})
|
||
afterAll(() => vi.restoreAllMocks())
|
||
|
||
interface Facts {
|
||
texts: TextEntry[]
|
||
images: ImageEntry[]
|
||
links: LinkEntry[]
|
||
}
|
||
|
||
/** Runs an export under the drawing recorder and reads the links back. */
|
||
async function recorded<T extends { pdf: Uint8Array }>(
|
||
run: () => Promise<T>,
|
||
): Promise<T & Facts> {
|
||
const recording = recordDrawing()
|
||
try {
|
||
const result = await run()
|
||
const { texts, images } = recording
|
||
return { ...result, texts, images, links: await readLinks(result.pdf) }
|
||
} finally {
|
||
recording.stop()
|
||
}
|
||
}
|
||
|
||
const facts = ({ texts, images, links }: Facts): Facts => ({
|
||
texts,
|
||
images,
|
||
links,
|
||
})
|
||
|
||
/** The app export path, bundled logos, under the recorder. */
|
||
function appExport(index: SheetIndex): Promise<Facts & { pdf: Uint8Array }> {
|
||
return recorded(() => exportReportToPdf(demo, index, { locale: 'fr' }))
|
||
}
|
||
|
||
async function archiveOf(pdf: Uint8Array): Promise<ReportArchiveEnvelope> {
|
||
const outcome = await readReportArchive(pdf, sheet.sheet.migrationMap)
|
||
expect(outcome.origin).toBe('envelope_v1')
|
||
return outcome.archive!
|
||
}
|
||
|
||
const REEXPORT = {
|
||
locale: 'fr' as const,
|
||
rasterizer: browserRasterizer,
|
||
exportedAt: new Date('2027-01-01T00:00:00.000Z'),
|
||
}
|
||
|
||
function renameNodes(nodes: (FindingCategory | FindingNode)[]): void {
|
||
for (const node of nodes) {
|
||
node.name = {
|
||
fr: `${node.name.fr} (révisé)`,
|
||
en: `${node.name.en} (revised)`,
|
||
}
|
||
renameNodes(node.children)
|
||
}
|
||
}
|
||
|
||
/** The catalog after a revision: every label changed, the hip marker moved. */
|
||
function revisedSheet(source: LoadedSheet): LoadedSheet {
|
||
const next = structuredClone(source)
|
||
for (const a of next.document.annotations) {
|
||
if (a.name) {
|
||
a.name = { fr: `${a.name.fr} (révisé)`, en: `${a.name.en} (revised)` }
|
||
}
|
||
if (a.id === 'hip' && a.geometry?.type === 'circle') a.geometry.cx += 100
|
||
}
|
||
for (const zone of next.document.finding_zones) {
|
||
zone.name = {
|
||
fr: `${zone.name.fr} (révisé)`,
|
||
en: `${zone.name.en} (revised)`,
|
||
}
|
||
renameNodes(zone.categories)
|
||
}
|
||
return next
|
||
}
|
||
|
||
describe('the archive survives its resources', () => {
|
||
it('T2 catalog changed: the envelope re-exports the archived sheet, not the revised one', async () => {
|
||
const first = await appExport(sheet)
|
||
const archive = await archiveOf(first.pdf)
|
||
|
||
// The revision is visible through the app path (labels and geometry)...
|
||
const revised = buildSheetIndex(revisedSheet(sheet.sheet))
|
||
const onRevised = await appExport(revised)
|
||
expect(onRevised.texts).not.toEqual(first.texts)
|
||
const revisedViews = snapshotViews(
|
||
buildSheetSnapshot(revised.sheet, demo),
|
||
demo,
|
||
)
|
||
const archivedViews = snapshotViews(archive.sheetSnapshot, demo)
|
||
expect(revisedViews).not.toEqual(archivedViews)
|
||
|
||
// ...and absent from a re-export of the envelope alone.
|
||
const again = await recorded(() => exportFromEnvelope(archive, REEXPORT))
|
||
expect(again.warnings).toEqual([])
|
||
expect(facts(again)).toEqual(facts(first))
|
||
expect(
|
||
snapshotViews(again.envelope.sheetSnapshot, again.envelope.report),
|
||
).toEqual(archivedViews)
|
||
}, 60_000)
|
||
|
||
it('scenario 3 branding changed: the envelope re-exports the archived branding and never fetches', async () => {
|
||
const first = await appExport(sheet)
|
||
const archive = await archiveOf(first.pdf)
|
||
|
||
// The deployment now ships other logos: a 1×1 PNG everywhere, another partner SVG.
|
||
const onePx = {
|
||
kind: 'png' as const,
|
||
base64: Buffer.from(ONE_PX_PNG).toString('base64'),
|
||
}
|
||
const otherSvg =
|
||
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><rect width="10" height="10"/></svg>'
|
||
branding.current = {
|
||
...archive.branding,
|
||
headerLogo: onePx,
|
||
footerLogo: onePx,
|
||
poweredBy: { ...archive.branding.poweredBy!, logo: onePx },
|
||
partner: {
|
||
...archive.branding.partner!,
|
||
logo: { kind: 'svg', svg: otherSvg },
|
||
},
|
||
}
|
||
const current = await loadBranding()
|
||
expect(current.headerLogo).not.toBeNull()
|
||
expect(current).not.toEqual(archive.branding)
|
||
const fetchSpy = vi.fn()
|
||
vi.stubGlobal('fetch', fetchSpy)
|
||
|
||
const again = await recorded(() => exportFromEnvelope(archive, REEXPORT))
|
||
expect(fetchSpy).not.toHaveBeenCalled()
|
||
expect(again.warnings).toEqual([])
|
||
expect(facts(again)).toEqual(facts(first))
|
||
expect(again.envelope.branding).toEqual(archive.branding)
|
||
}, 60_000)
|
||
})
|