/** * R2 maintenance B, interface (owner choice 2026-10-14): a session text that * could not be set aside shows the resolution panel in the editor (copy to * download, deletion or replacement only after a confirmation, retry); a new * report started from /new is refused and sends the practitioner there; a * refused draft write shows the storage-full banner. */ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi, } from 'vitest' import { cleanup, render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { MemoryRouter, Route, Routes } from 'react-router-dom' import EditorRoute from '@/app/routes/EditorRoute' import NewReportRoute from '@/app/routes/NewReportRoute' import { useReportStore } from '@/state/reportStore' import { DRAFT_KEY, QUARANTINE_KEYS } from '@/state/persistence' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' import type { SheetIndex } from '@/services/sheetIndex' import { loadTestSheet, minimalReport } from '../helpers/sheet' let sheet: SheetIndex const UNREADABLE = JSON.stringify({ version: 7 }) beforeAll(async () => { sheet = await loadTestSheet() }) beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) useReportStore.setState({ report: null, archive: null, activeObservationIndex: 0, saveFailed: false, }) sessionStorage.clear() localStorage.clear() sessionStorage.setItem('osteo:disclaimer-shown', '1') window.scrollTo = vi.fn() as unknown as typeof window.scrollTo vi.stubGlobal('alert', vi.fn()) }) afterEach(() => { cleanup() vi.unstubAllGlobals() vi.restoreAllMocks() }) function quarantineFull() { const setItem = sessionStorage.setItem.bind(sessionStorage) vi.spyOn(sessionStorage, 'setItem').mockImplementation((key, value) => { if (key.startsWith('osteo:quarantine:')) { throw new DOMException('full', 'QuotaExceededError') } setItem(key, value) }) } function mount(path: '/editor' | '/new') { return render( } /> } /> } /> , ) } describe('the resolution panel', () => { it('lists the held draft, deletes it only after the confirmation, then leaves', async () => { const user = userEvent.setup() sessionStorage.setItem(DRAFT_KEY, UNREADABLE) quarantineFull() mount('/editor') await screen.findByText(fr['session.held.notice']!) expect(screen.getByTestId('held-draft')).toHaveTextContent( fr['session.held.draft_unreadable']!.replace('{size}', '1 Ko'), ) await user.click(screen.getByRole('button', { name: fr['session.held.discard'] })) const dialog = screen.getByRole('dialog') expect(dialog).toHaveTextContent(fr['session.held.discard_body']!) await user.click(screen.getByRole('button', { name: fr['common.cancel'] })) expect(sessionStorage.getItem(DRAFT_KEY)).toBe(UNREADABLE) await user.click(screen.getByRole('button', { name: fr['session.held.discard'] })) await user.click( screen.getByRole('button', { name: fr['session.held.discard_confirm'] }), ) expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull() await screen.findByTestId('home-route') }) it('downloads the stored text verbatim', async () => { const user = userEvent.setup() sessionStorage.setItem(DRAFT_KEY, UNREADABLE) quarantineFull() const blobs: Blob[] = [] URL.createObjectURL = vi.fn((blob: Blob) => { blobs.push(blob) return 'blob:held' }) URL.revokeObjectURL = vi.fn() const clicks = vi .spyOn(HTMLAnchorElement.prototype, 'click') .mockImplementation(() => undefined) mount('/editor') await user.click( await screen.findByRole('button', { name: fr['session.held.download'] }), ) expect(clicks).toHaveBeenCalledOnce() expect(await blobs[0]!.text()).toBe(UNREADABLE) expect(sessionStorage.getItem(DRAFT_KEY)).toBe(UNREADABLE) }) it('opens the adapted draft only after the replacement is confirmed', async () => { const user = userEvent.setup() const stale = JSON.stringify( minimalReport(sheet, { conclusion: 'work in progress', sheet: { ...sheet.sheet.ref, documentSha256: 'c'.repeat(64) }, observations: [ { id: 'o', zones: [{ annotationId: 'point-gone', viewId: 'main' }], findings: [], freeTextFindings: [], comment: '', }, ], }), ) sessionStorage.setItem(DRAFT_KEY, stale) quarantineFull() mount('/editor') await user.click( await screen.findByRole('button', { name: fr['session.held.open_adapted'] }), ) expect(sessionStorage.getItem(DRAFT_KEY)).toBe(stale) await user.click( screen.getByRole('button', { name: fr['session.held.open_confirm'] }), ) await waitFor(() => expect(useReportStore.getState().report?.conclusion).toBe('work in progress'), ) expect(screen.queryByText(fr['session.held.notice']!)).toBeNull() }) it('retries the set-aside once the storage accepts it', async () => { const user = userEvent.setup() sessionStorage.setItem(DRAFT_KEY, UNREADABLE) quarantineFull() mount('/editor') await screen.findByText(fr['session.held.notice']!) vi.restoreAllMocks() await user.click(screen.getByRole('button', { name: fr['session.held.retry'] })) await screen.findByTestId('home-route') expect(sessionStorage.getItem(QUARANTINE_KEYS.draft)).toBe(UNREADABLE) }) }) describe('a new report over a held draft', () => { it('is refused and sends the practitioner to the resolution panel', async () => { const user = userEvent.setup() sessionStorage.setItem(DRAFT_KEY, UNREADABLE) quarantineFull() mount('/new') await user.type(screen.getByLabelText(fr['form.practitioner.name']!), 'Dr. Dupont') await user.type(screen.getByLabelText(fr['form.animal.name']!), 'Mistral') await user.type(screen.getByLabelText(fr['form.owner.name']!), 'Jean Martin') const submit = screen.getByRole('button', { name: fr['new_report.submit'] }) await waitFor(() => expect(submit).toBeEnabled()) await user.click(submit) await waitFor(() => expect(window.alert).toHaveBeenCalledWith(fr['session.held_blocked']), ) await screen.findByText(fr['session.held.notice']!) expect(sessionStorage.getItem(DRAFT_KEY)).toBe(UNREADABLE) expect(useReportStore.getState().report).toBeNull() }) }) describe('the storage-full banner', () => { it('shows while the draft mirror is refused and goes once a write succeeds', async () => { useReportStore .getState() .hydrateImport({ report: minimalReport(sheet), archive: null }, sheet) mount('/editor') await screen.findByTestId('sheet-status') expect(screen.queryByText(fr['editor.save_failed']!)).toBeNull() const setItem = sessionStorage.setItem.bind(sessionStorage) const spy = vi .spyOn(sessionStorage, 'setItem') .mockImplementation((key, value) => { if (key === DRAFT_KEY) throw new DOMException('full', 'QuotaExceededError') setItem(key, value) }) useReportStore.getState().setConclusion('lost on reload') await screen.findByText(fr['editor.save_failed']!) spy.mockRestore() useReportStore.getState().setConclusion('saved') await waitFor(() => expect(screen.queryByText(fr['editor.save_failed']!)).toBeNull(), ) }) })