Files
faunosteo/apps/app/tests/unit/migration.leaves.test.ts
T

73 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* The 612 legacy leaves through the application itself: migrated with the
* bundled sheet's map, resolved by the sheet index the editor and the PDF
* use, exported to a v3 PDF and read back without loss.
*/
import { describe, it, expect, vi, beforeAll } from 'vitest'
import {
readReportDocument,
type Finding,
type Report,
} from '@faunosteo/domain'
import { exportReportToPdf } from '@/pdf/export'
import { importReportFromPdf } from '@/pdf/import'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
import {
leavesAsV2Report,
legacyLeaves,
loadLegacyBaseline,
} from '../helpers/legacyLeaves'
// The factory runs when the engine first loads the adapter: fixed 1×1 PNG.
vi.mock('@/pdf/browserRasterizer', async () => {
const { ONE_PX_PNG } = await import('../helpers/goldens')
return { browserRasterizer: vi.fn(async () => ONE_PX_PNG) }
})
let sheet: SheetIndex
let report: Report
let findings: Finding[]
let unresolvedFindings: number
beforeAll(async () => {
// No logo served: the branding loader leaves every slot empty.
globalThis.fetch = (async () =>
new Response('not found', { status: 404 })) as typeof fetch
sheet = await loadTestSheet()
const leaves = legacyLeaves(loadLegacyBaseline())
const v2 = leavesAsV2Report(leaves)
const outcome = readReportDocument(v2, sheet.sheet.migrationMap)
report = outcome.report
findings = report.observations.flatMap((o) => o.findings)
unresolvedFindings = outcome.unresolvedFindings
vi.spyOn(console, 'warn').mockImplementation(() => undefined)
})
describe('the 612 legacy leaves in the application', () => {
it('resolves every migrated leaf to a leaf node of the bundled sheet', () => {
expect(findings).toHaveLength(612)
expect(unresolvedFindings).toBe(29)
for (const f of findings) {
const node = sheet.nodeAt(f.findingZoneId, f.path)
const where = `${f.findingZoneId}/${f.path.join('/')}`
if (f.unresolved) expect(node, where).toBeUndefined()
else {
expect(node, where).toBeDefined()
expect(node!.children, where).toHaveLength(0)
}
}
})
it('keeps every finding, resolved or not, through an archive PDF export and import', async () => {
const bytes = await exportReportToPdf(report, sheet, { locale: 'fr' })
const back = await importReportFromPdf(bytes, sheet.sheet.migrationMap)
expect(back.migrated).toBe(false)
expect(back.origin).toBe('envelope_v1')
expect(back.archive).not.toBeNull()
expect(back.report).toEqual(report)
const again = back.report.observations.flatMap((o) => o.findings)
expect(again.filter((f) => f.unresolved)).toHaveLength(29)
}, 30_000)
})