Files
faunosteo/apps/app/tests/unit/components.FindingsTreePicker.test.tsx
T
bchanot 74fcb37564 feat(app): R2 switch — the application runs its domain rules in the Rust core
Consumers now fed by the core (crates/core through @faunosteo/core-wasm):
- the sheet: the bundled payload is parsed and indexed by the core
  (SheetHandle, SheetIndexData); SheetIndex reads precomputed tables only;
- the store: new report, add / remove / move observation, draft restore
  (v2 migration + rebase) through the core, actions answering { ok };
- the editor click chain (zone.click), laterality and vertebra pickers
  (zone.add_sides, vertebra.initial / sync / select), the findings picker
  (finding.toggle by clicked leaf position), zone removal;
- the import: engine byte scan (TypeScript until R4), then the core's
  layered reading and rebase in one call; the export: the core builds the
  sheet snapshot, the frozen TypeScript engine writes the PDF (R4).
Lazy core: Home and the new-report form read the sheet availability from
the bundle and load the module on import / submit only; the auth shell
never references it. Degraded states (core_unavailable, invalid_sheet)
show the validated notice with a retry and never redirect or touch the
draft; an unreadable draft or archive is set aside under
osteo:quarantine:* (only content kinds; a failed write keeps it in place
and the editor stays), a rebase that flags items keeps the raw pre-rebase
draft. Four strings, wording validated by the owner.
Deleted: services/zoneInteraction.ts and its suite (13 cases ported to
cargo), the TypeScript Finding builder and vertebra selection rules of the
pickers, store rules, persistence parsing, hydrateReport. ESLint forbids
@faunosteo/domain in apps/app/src; the report:compare CLI keeps the frozen
domain until R4 (scripts/compare/sheet.ts). App suite 313/313 on the real
core (24 new tests: quarantine, degraded editor, core failures, N-1
reader); step6 goldens and the frozen envelopes unchanged.
2026-10-14 00:41:34 +02:00

246 lines
7.5 KiB
TypeScript

import {
beforeAll,
describe,
expect,
it,
vi,
afterEach,
beforeEach,
} from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { Finding, LateralSide } from '@faunosteo/core-wasm/types'
import FindingsTreePicker from '@/components/observation/FindingsTreePicker'
import { useLocaleStore } from '@/state/localeStore'
import type { SheetIndex } from '@/services/sheetIndex'
import { loadTestSheet } from '../helpers/sheet'
let sheet: SheetIndex
beforeAll(async () => {
sheet = await loadTestSheet()
})
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
afterEach(cleanup)
/**
* The picker reports the clicked leaf; the core builds or removes the finding
* (R2), as ObservationCard wires it. `onChange` receives the observation's
* findings after the toggle, which is what the picker handed out before R2.
*/
function CorePicker({
side,
selected,
onChange,
}: {
side?: LateralSide
selected: Finding[]
onChange: (next: Finding[]) => void
}) {
return (
<FindingsTreePicker
zone={sheet.findingZone('hip')!}
side={side}
selected={selected}
onToggle={(path, leafPosition) => {
const out = sheet.ops.toggleFinding({
observation: {
id: 'obs',
zones: [],
findings: selected,
freeTextFindings: [],
comment: '',
},
findingZoneId: 'hip',
path,
leafPosition,
...(side ? { side } : {}),
})
if (!out.ok) throw new Error(out.kind)
onChange(out.value.findings)
}}
/>
)
}
describe('FindingsTreePicker on a sheet finding zone', () => {
it('drills down the hip zone and selects a leaf as a v3 finding with snapshots and side', async () => {
const user = userEvent.setup()
const onChange = vi.fn()
render(<CorePicker side="left" selected={[]} onChange={onChange} />)
expect(screen.getByText('Hanche (coxo-fémorale)')).toBeInTheDocument()
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Dysfonctions ostéopathiques',
}),
)
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Restrictions de mobilité articulaire',
}),
)
await user.click(
screen.getByRole('checkbox', { name: 'Restriction en flexion' }),
)
expect(onChange).toHaveBeenCalledWith([
{
findingZoneId: 'hip',
path: ['hip-dysfunctions', 'articular', 'restriction-flexion'],
labelFrSnapshot: 'Restriction en flexion',
labelEnSnapshot: 'Flexion restriction',
side: 'left',
},
])
})
it('shows descriptions in the active locale, the vet marker, counts, and unselects', async () => {
const user = userEvent.setup()
useLocaleStore.setState({ locale: 'en' })
const selected: Finding[] = [
{
findingZoneId: 'hip',
path: ['hip-dysfunctions', 'articular', 'hypermobility'],
labelFrSnapshot: 'x',
labelEnSnapshot: 'x',
requiresVetReferral: true,
},
]
const onChange = vi.fn()
render(<CorePicker selected={selected} onChange={onChange} />)
expect(screen.getByLabelText('1 finding selected')).toBeInTheDocument()
await user.click(
screen.getByRole('button', { name: 'Open Osteopathic dysfunctions' }),
)
await user.click(
screen.getByRole('button', {
name: 'Open Articular mobility restrictions',
}),
)
expect(
screen.getByText(/Perceived laxity may indicate a ligament injury/),
).toBeInTheDocument()
expect(
screen.getAllByLabelText('Refer to a veterinarian').length,
).toBeGreaterThan(0)
const box = screen.getByRole('checkbox', {
name: 'Hypermobility / perceived laxity',
})
expect(box).toBeChecked()
await user.click(box)
expect(onChange).toHaveBeenCalledWith([])
await user.click(
screen.getByRole('button', { name: 'Back to previous level' }),
)
expect(
screen.getByRole('button', {
name: 'Open Articular mobility restrictions',
}),
).toBeInTheDocument()
})
it('flags a vet-referral leaf and omits the key on other leaves', async () => {
const user = userEvent.setup()
const onChange = vi.fn()
render(<CorePicker selected={[]} onChange={onChange} />)
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Dysfonctions ostéopathiques',
}),
)
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Restrictions de mobilité articulaire',
}),
)
await user.click(
screen.getByRole('checkbox', { name: 'Hypermobilité / laxité perçue' }),
)
expect(onChange.mock.calls[0]![0][0].requiresVetReferral).toBe(true)
onChange.mockClear()
await user.click(
screen.getByRole('checkbox', { name: 'Hypomobilité globale' }),
)
expect(onChange.mock.calls[0]![0][0]).not.toHaveProperty(
'requiresVetReferral',
)
})
describe('count badges and scoping', () => {
const finding = (
path: string[],
extra: Partial<Finding> = {},
): Finding => ({
findingZoneId: 'hip',
path,
labelFrSnapshot: 'x',
labelEnSnapshot: 'x',
side: 'left',
...extra,
})
const base = [
finding(['hip-dysfunctions', 'articular', 'hypomobility']),
finding(['hip-dysfunctions', 'articular', 'hypermobility']),
finding(['hip-dysfunctions', 'muscular', 'trigger-points']),
]
function renderPicker(selected: Finding[]) {
render(<CorePicker side="left" selected={selected} onChange={vi.fn()} />)
}
it('counts selected findings per level, none on a clean sibling', async () => {
const user = userEvent.setup()
renderPicker(base)
expect(
screen.getByLabelText('3 constats sélectionnés'),
).toBeInTheDocument()
expect(screen.getAllByLabelText(/constats? sélectionnés?$/)).toHaveLength(
1,
)
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Dysfonctions ostéopathiques',
}),
)
expect(
screen.getByLabelText('2 constats sélectionnés'),
).toBeInTheDocument()
expect(screen.getByLabelText('1 constat sélectionné')).toBeInTheDocument()
expect(screen.getAllByLabelText(/constats? sélectionnés?$/)).toHaveLength(
2,
)
})
it('ignores findings of another zone or another side', async () => {
const user = userEvent.setup()
renderPicker([
finding(['hip-dysfunctions', 'articular', 'hypomobility'], {
findingZoneId: 'stifle',
}),
finding(['hip-dysfunctions', 'articular', 'hypomobility'], {
side: 'right',
}),
])
expect(screen.queryByLabelText(/sélectionnés?$/)).toBeNull()
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Dysfonctions ostéopathiques',
}),
)
await user.click(
screen.getByRole('button', {
name: 'Ouvrir Restrictions de mobilité articulaire',
}),
)
expect(
screen.getByRole('checkbox', { name: 'Hypomobilité globale' }),
).not.toBeChecked()
})
})
it('the findings list carries no permanent compositing hint (text stays crisp)', () => {
render(<CorePicker selected={[]} onChange={() => {}} />)
const ul = document.querySelector('ul') as HTMLElement
expect(ul.getAttribute('style') ?? '').not.toContain('will-change')
expect(ul.style.willChange).toBe('')
})
})