/** * The editor route on the bundled sheet: the click chain end to end (DOM * click on a generated marker → service → store), the detail view by its * localized name, the sheet status line, the disclaimer once per session, * the draft restore (silent on the same sheet, one alert after a rebase). */ import { describe, it, expect, vi, beforeAll, beforeEach, afterEach, } from 'vitest' import { render, screen, waitFor, within, cleanup, } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { MemoryRouter } from 'react-router-dom' import EditorRoute from '@/app/routes/EditorRoute' import { useReportStore } from '@/state/reportStore' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' import type { SheetIndex } from '@/services/sheetIndex' import { loadTestSheet, minimalReport } from '../helpers/sheet' const DISCLAIMER_FLAG = 'osteo:disclaimer-shown' let sheet: SheetIndex beforeAll(async () => { sheet = await loadTestSheet() }) beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) sessionStorage.clear() sessionStorage.setItem(DISCLAIMER_FLAG, '1') window.scrollTo = vi.fn() as unknown as typeof window.scrollTo vi.stubGlobal('alert', vi.fn()) useReportStore.setState({ report: null, activeObservationIndex: 0 }) useReportStore.getState().hydrateImport({ report: minimalReport(sheet, { observations: [ { id: 'o1', zones: [], findings: [], freeTextFindings: [], comment: '', }, ], }), archive: null, }, sheet) }) afterEach(() => { cleanup() vi.restoreAllMocks() vi.unstubAllGlobals() }) const zonesOf = (i = 0) => useReportStore.getState().report!.observations[i]!.zones async function mount() { const utils = render( , ) await waitFor(() => expect( document.querySelector('[data-anno-id="head-trigger"]'), ).not.toBeNull(), ) return utils } function clickZone(id: string, scope: ParentNode = document) { const els = scope.querySelectorAll(`[data-anno-id="${id}"]`) expect(els.length, id).toBeGreaterThan(0) els[0]!.dispatchEvent(new Event('click', { bubbles: true })) } describe('EditorRoute on the canonical sheet', () => { it('shows the sheet status line and toggles a plain zone on the active observation', async () => { await mount() expect(screen.getByTestId('sheet-status').textContent).toBe( fr['sheet.status.draft'], ) clickZone('c7-t1') expect(zonesOf()).toEqual([ { annotationId: 'c7-t1', viewId: 'main', labelFrSnapshot: 'Charnière C7-T1 (cervico-thoracique)', labelEnSnapshot: 'C7-T1 junction (cervicothoracic)', }, ]) clickZone('c7-t1') expect(zonesOf()).toEqual([]) }) it('a click with no active observation only activates the last one', async () => { useReportStore.getState().setActiveObservation(-1) await mount() clickZone('c7-t1') expect(useReportStore.getState().activeObservationIndex).toBe(0) expect(zonesOf()).toEqual([]) }) it('a bilateral zone opens the side picker and adds the chosen side; both sides then clear on re-click', async () => { const user = userEvent.setup() await mount() clickZone('hip') const dialog = await screen.findByRole('dialog', { name: 'Hanche (coxo-fémorale)', }) await user.click( within(dialog).getByRole('button', { name: fr['lateral.both'] }), ) expect(zonesOf().map((z) => z.side)).toEqual(['left', 'right']) clickZone('hip') expect(zonesOf()).toEqual([]) }) it('a spine zone is added and opens the vertebra picker; confirming syncs vertebrae by region', async () => { const user = userEvent.setup() await mount() clickZone('cervical') const dialog = await screen.findByRole('dialog', { name: /Vertèbres cervicales/, }) expect(zonesOf()[0]!.annotationId).toBe('cervical') expect( within(dialog).getByRole('tab', { name: 'Cervicales' }), ).toHaveAttribute('aria-selected', 'true') await user.click(within(dialog).getByText('C3')) await user.click(within(dialog).getByRole('tab', { name: 'Lombaires' })) await user.click(within(dialog).getByText('L2')) await user.click( within(dialog).getByRole('button', { name: fr['common.confirm'] }), ) expect(zonesOf().map((z) => [z.annotationId, z.vertebrae])).toEqual([ ['cervical', [{ region: 'C', index: 3 }]], ['lumbar', [{ region: 'L', index: 2 }]], ]) }) it('cancelling the vertebra picker keeps the spine zone without vertebrae', async () => { const user = userEvent.setup() await mount() clickZone('cervical') const dialog = await screen.findByRole('dialog', { name: /Vertèbres cervicales/, }) await user.click(within(dialog).getByText('C3')) await user.click( within(dialog).getByRole('button', { name: fr['common.cancel'] }), ) expect(zonesOf().map((z) => [z.annotationId, z.vertebrae])).toEqual([ ['cervical', undefined], ]) }) it('the head trigger opens the detail view by its name and its zones carry the head view id', async () => { await mount() clickZone('head-trigger') const dialog = await screen.findByRole('dialog', { name: 'Tête (vue latérale gauche)', }) await waitFor(() => expect(dialog.querySelector('[data-anno-id="atm"]')).not.toBeNull(), ) expect(dialog.querySelector('svg')!.getAttribute('viewBox')).toBe( '60 100 500 380', ) clickZone('atm', dialog) expect(zonesOf()).toEqual([ expect.objectContaining({ annotationId: 'atm', viewId: 'head' }), ]) expect( screen.getByRole('dialog', { name: 'Tête (vue latérale gauche)' }), ).toBeInTheDocument() }) it('shows the scope disclaimer once per session, from the sheet', async () => { sessionStorage.removeItem(DISCLAIMER_FLAG) const user = userEvent.setup() await mount() const dialog = await screen.findByRole('dialog', { name: fr['editor.disclaimer_title'], }) expect(dialog.textContent).toContain('R. 243-6') await user.click( within(dialog).getByRole('button', { name: fr['common.understood'] }), ) expect(sessionStorage.getItem(DISCLAIMER_FLAG)).toBe('1') }) it('restores the per-tab draft when the store is empty, silently on the same sheet', async () => { const report = useReportStore.getState().report! useReportStore.setState({ report: null }) sessionStorage.setItem( 'osteo:draft', JSON.stringify({ ...report, version: 3 }), ) await mount() expect(useReportStore.getState().report?.id).toBe(report.id) expect(window.alert).not.toHaveBeenCalled() }) it('a draft from another sheet is rebased, its lost zone flagged, and announced once with the count', async () => { const report = useReportStore.getState().report! useReportStore.setState({ report: null }) const foreign = { ...report, sheet: { ...report.sheet, documentSha256: '0'.repeat(64) }, observations: [ { ...report.observations[0]!, zones: [ { annotationId: 'ghost-zone', viewId: 'main', labelFrSnapshot: 'Zone disparue', labelEnSnapshot: 'Vanished zone', }, ], }, ], } sessionStorage.setItem('osteo:draft', JSON.stringify(foreign)) await mount() const restored = useReportStore.getState().report! expect(restored.sheet).toEqual(sheet.sheet.ref) expect(restored.observations[0]!.zones[0]).toMatchObject({ annotationId: 'ghost-zone', unresolved: true, }) expect(window.alert).toHaveBeenCalledTimes(1) expect(window.alert).toHaveBeenCalledWith( fr['editor.draft_rebased'].replace('{count}', '1'), ) }) })