137 lines
4.3 KiB
TypeScript
137 lines
4.3 KiB
TypeScript
import type {
|
|
Branding,
|
|
FindingCategory,
|
|
FindingNode,
|
|
Observation,
|
|
Report,
|
|
ReportArchiveEnvelope,
|
|
SheetSnapshot,
|
|
} from '@faunosteo/core-wasm/types'
|
|
import { defaultPdfLabels } from '@/pdf/labels'
|
|
import type { SheetIndexData } from '@faunosteo/core-wasm/types'
|
|
import type { ReportMigrationMap } from '@faunosteo/core-wasm/n-1'
|
|
import { coreRuntime } from '@/core/runtime'
|
|
import { SheetOps } from '@/core/sheetOps'
|
|
import { bundledSheetProvider, type BundledSheetData } from '@/sheets/provider'
|
|
import { loadSheetIndex } from '@/sheets/useSheet'
|
|
import { SheetIndex } from '@/services/sheetIndex'
|
|
|
|
/** The bundled reference draft (vitest runs the sheet-bundle plugin in draft mode). */
|
|
export async function loadTestSheet(): Promise<SheetIndex> {
|
|
const index = await loadSheetIndex('equine')
|
|
if (!index) throw new Error('bundled sheet missing: run pnpm import:equine')
|
|
return index
|
|
}
|
|
|
|
export function emptyObservation(id = 'obs-1'): Observation {
|
|
return { id, zones: [], findings: [], freeTextFindings: [], comment: '' }
|
|
}
|
|
|
|
export function minimalReport(
|
|
sheet: SheetIndex,
|
|
patch: Partial<Report> = {},
|
|
): Report {
|
|
return {
|
|
version: 3,
|
|
id: 'r1',
|
|
createdAt: '2026-10-10T10:00:00Z',
|
|
date: '2026-10-10',
|
|
practitioner: { name: 'Dr. Dupont' },
|
|
animal: { speciesId: 'equine', name: 'Mistral' },
|
|
owner: { name: 'Jean Martin' },
|
|
sheet: sheet.sheet.ref,
|
|
observations: [],
|
|
conclusion: '',
|
|
recommendations: '',
|
|
localAnnotations: [],
|
|
...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',
|
|
},
|
|
}
|
|
}
|
|
|
|
/** The snapshot of `sheet` an archive of `report` carries, built by the core. */
|
|
export function sheetSnapshot(
|
|
sheet: SheetIndex,
|
|
report: Report,
|
|
): SheetSnapshot {
|
|
const outcome = sheet.ops.snapshot(report)
|
|
if (!outcome.ok) throw new Error(`snapshot failed: ${outcome.kind}`)
|
|
return outcome.value
|
|
}
|
|
|
|
/** A valid Step 6 envelope for `report`, snapshotted on `sheet` (no logos). */
|
|
export function archiveEnvelope(
|
|
sheet: SheetIndex,
|
|
report: Report,
|
|
): ReportArchiveEnvelope {
|
|
return {
|
|
format: 'faunosteo-report-archive',
|
|
envelopeVersion: 1,
|
|
exportedAt: '2026-10-11T12:00:00.000Z',
|
|
locale: 'fr',
|
|
report,
|
|
sheetSnapshot: sheetSnapshot(sheet, report),
|
|
branding: nologosBranding(),
|
|
labels: defaultPdfLabels,
|
|
}
|
|
}
|
|
|
|
/** The bundled migration map, for the frozen N-1 readers (the core keeps its own copy). */
|
|
export async function bundledMigrationMap(): Promise<ReportMigrationMap> {
|
|
return (await bundledSheet()).migrationMap as ReportMigrationMap
|
|
}
|
|
|
|
/**
|
|
* The category or node at a path of a finding zone (first match at each level, as the
|
|
* core's walk), read from the parsed document: the tests' view of what `hasNode` answers.
|
|
*/
|
|
export function findingNodeAt(
|
|
sheet: SheetIndex,
|
|
zoneId: string,
|
|
path: readonly string[],
|
|
): FindingCategory | FindingNode | undefined {
|
|
if (path.length === 0) return undefined
|
|
let nodes: readonly (FindingCategory | FindingNode)[] =
|
|
sheet.findingZone(zoneId)?.categories ?? []
|
|
let node: FindingCategory | FindingNode | undefined
|
|
for (const segment of path) {
|
|
node = nodes.find((n) => n.id === segment)
|
|
if (!node) return undefined
|
|
nodes = node.children
|
|
}
|
|
return node
|
|
}
|
|
|
|
/** The bundled payload as the provider hands it to the core. */
|
|
export async function bundledSheet(): Promise<BundledSheetData> {
|
|
const bundle = await bundledSheetProvider.getReferenceSheet('equine')
|
|
if (!bundle) throw new Error('bundled sheet missing: run pnpm import:equine')
|
|
return bundle
|
|
}
|
|
|
|
/** Another sheet loaded in the core (a revised catalog), outside the provider's cache. */
|
|
export function sheetFromBundle(bundle: BundledSheetData): SheetIndex {
|
|
const session = coreRuntime().sheet(JSON.stringify(bundle))
|
|
const data = session.call<SheetIndexData>('index_data', '')
|
|
if (!data.ok) throw new Error(`sheet refused by the core: ${data.kind}`)
|
|
return new SheetIndex(data.value, new SheetOps(session))
|
|
}
|