/** * R4.1 freeze O13 (R4 plan B.5.1, B.4): the synthetic large reports L (60 observations) and * CAP (500, the engine's observation cap), built from the demo report. Every observation * carries zones on each view of the draft sheet (main and head), so an export rasterises two * views per observation: 120 and 1,000 views, the memory classes the baseline (O12) and R4.6 * measure. Zones and findings are the demo's, cycled per view; the texts follow the main * view's source observation. `MAKE_R4_ORACLES=1` writes `tests/fixtures/r4/reports/`; * without it the reports are rebuilt and compared with the committed files. */ import { beforeAll, describe, expect, it } from 'vitest' import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' import { resolve } from 'node:path' import type { Finding, Observation, Report, ReportZone, } from '@faunosteo/core-wasm/types' import { isObservable, type SheetIndex } from '@/services/sheetIndex' import { FIXTURES_DIR } from '../helpers/goldens' import { loadTestSheet, sheetSnapshot } from '../helpers/sheet' const MAKE = process.env.MAKE_R4_ORACLES === '1' const OUT_DIR = resolve(FIXTURES_DIR, 'r4/reports') const SIZES = { L: 60, CAP: 500 } as const type SizeName = keyof typeof SIZES const NAMES = Object.keys(SIZES) as SizeName[] const EXPECTED_VIEWS: Record = { L: 120, CAP: 1000 } /** `PAGE_CAPS` of the engine at the freeze (now `crates/core/src/pdf/write/input.rs`). */ const PAGE_CAPS = { observations: 500, viewsPerObservation: 8 } as const /** The zones a demo observation holds on one view, with the findings of their finding zones. */ interface ViewPart { source: Observation zones: ReportZone[] findings: Finding[] } const readJson = (path: string): unknown => JSON.parse(readFileSync(path, 'utf8')) const observationId = (index: number): string => `00000000-0000-4000-8000-${String(index + 1).padStart(12, '0')}` function findingZoneOf(sheet: SheetIndex, annotationId: string): string | null { const annotation = sheet.annotation(annotationId) return isObservable(annotation) ? annotation.finding_zone_id : null } function partOn( sheet: SheetIndex, source: Observation, viewId: string, ): ViewPart | null { const zones = source.zones.filter( (z) => sheet.annotation(z.annotationId)?.view_id === viewId, ) if (zones.length === 0) return null const zoneIds = new Set( zones.map((z) => findingZoneOf(sheet, z.annotationId)), ) const findings = source.findings.filter((f) => zoneIds.has(f.findingZoneId)) return { source, zones, findings } } /** One list of demo parts per view of the sheet, in document order (main first). */ function partsByView(sheet: SheetIndex, demo: Report): ViewPart[][] { return sheet.sheet.document.views.map((view) => demo.observations.flatMap((o) => partOn(sheet, o, view.id) ?? []), ) } function observationAt(parts: ViewPart[][], index: number): Observation { const picks = parts.map((list) => list[index % list.length]!) return { ...picks[0]!.source, id: observationId(index), zones: picks.flatMap((p) => p.zones), findings: picks.flatMap((p) => p.findings), } } function syntheticReport( demo: Report, parts: ViewPart[][], name: SizeName, ): Report { return { ...demo, id: `fixture-r4-${name}`, observations: Array.from({ length: SIZES[name] }, (_, i) => observationAt(parts, i), ), } } /** Per observation, the views the engine rasterises (`observationViewIds` at the freeze; the core's `views.rs`). */ function rasterisedViews(report: Report, snapshotViews: Set): number[] { return report.observations.map( (o) => [...new Set(o.zones.map((z) => z.viewId))].filter((id) => snapshotViews.has(id), ).length, ) } let sheet: SheetIndex let demo: Report let reports: Record beforeAll(async () => { sheet = await loadTestSheet() demo = readJson(resolve(FIXTURES_DIR, 'report-v3.demo.json')) as Report const parts = partsByView(sheet, demo) reports = { L: syntheticReport(demo, parts, 'L'), CAP: syntheticReport(demo, parts, 'CAP'), } }) describe('the draft sheet the reports are built on', () => { it('has two views, main first, and the demo has zones on both', () => { const views = sheet.sheet.document.views.map((v) => v.id) expect(views).toEqual(['main', 'head']) expect(sheet.mainViewId).toBe('main') for (const list of partsByView(sheet, demo)) { expect(list.length).toBeGreaterThan(0) } }) }) describe.each(NAMES)('synthetic report %s', (name) => { const file = resolve(OUT_DIR, `${name}.json`) it('matches the committed file', () => { if (MAKE) { mkdirSync(OUT_DIR, { recursive: true }) writeFileSync(file, `${JSON.stringify(reports[name], null, 2)}\n`) } expect(readJson(file)).toEqual(reports[name]) }) it('comes back byte for byte through the draft restore (draft.restore)', () => { const report = reports[name] // The text the store writes to osteo:draft (persistence.ts) and reads back on reload. const outcome = sheet.ops.restoreDraft(JSON.stringify(report), null) if (!outcome.ok) throw new Error(`draft.restore refused: ${outcome.kind}`) const { rebase, archive, archiveRejected } = outcome.value expect(rebase.rebased).toBe(false) expect(rebase.unresolvedZones + rebase.unresolvedFindings).toBe(0) expect(archive).toBeNull() expect(archiveRejected).toBe(false) expect(rebase.report).toStrictEqual(report) expect(JSON.stringify(rebase.report)).toBe(JSON.stringify(report)) }) it('rasterises two views per observation', () => { const report = reports[name] const snapshot = sheetSnapshot(sheet, report) const views = new Set(snapshot.document.views.map((v) => v.id)) const perObservation = rasterisedViews(report, views) expect(new Set(perObservation)).toEqual(new Set([2])) const total = perObservation.reduce((sum, n) => sum + n, 0) expect(total).toBe(EXPECTED_VIEWS[name]) }) it('stays within the export caps', () => { const { observations } = reports[name] expect(observations).toHaveLength(SIZES[name]) expect(observations.length).toBeLessThanOrEqual(PAGE_CAPS.observations) for (const o of observations) { const views = new Set(o.zones.map((z) => z.viewId)).size expect(views).toBeLessThanOrEqual(PAGE_CAPS.viewsPerObservation) } }) })