/** * R2 plan rev 2 (robustness BLOCKER 1): when the core cannot run, a reload of * the editor shows the degraded notice with a retry, never redirects to the * home page, and never reads, rewrites or removes the tab's draft. A sheet * payload the core refuses (`invalid_sheet`) shows the same notice. Once the * module loads, the retry restores the draft. */ 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 { setCoreRuntime } from '@/core/runtime' import { bundledSheetProvider } from '@/sheets/provider' import { setSheetProvider } from '@/sheets/useSheet' import { useReportStore } from '@/state/reportStore' import { DRAFT_KEY } from '@/state/persistence' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' import type { SheetIndex } from '@/services/sheetIndex' import { testCoreRuntime, unavailableCoreRuntime } from '../helpers/core' import { bundledSheet, loadTestSheet, minimalReport } from '../helpers/sheet' let sheet: SheetIndex let draft: string beforeAll(async () => { sheet = await loadTestSheet() draft = JSON.stringify(minimalReport(sheet, { conclusion: 'unsaved work' })) }) beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) sessionStorage.clear() sessionStorage.setItem('osteo:disclaimer-shown', '1') window.scrollTo = vi.fn() as unknown as typeof window.scrollTo vi.stubGlobal('alert', vi.fn()) useReportStore.setState({ report: null, archive: null, activeObservationIndex: 0, }) // After the reset (an empty store mirrors no draft): the tab as a reload finds it. sessionStorage.setItem(DRAFT_KEY, draft) }) afterEach(async () => { cleanup() vi.unstubAllGlobals() setCoreRuntime(await testCoreRuntime()) setSheetProvider(bundledSheetProvider) }) function mount() { return render( } /> } /> , ) } async function expectDegraded() { await screen.findByText(fr['sheet.core_unavailable']!) expect( screen.getByRole('button', { name: fr['sheet.core_retry'] }), ).toBeInTheDocument() expect(screen.queryByTestId('home-route')).toBeNull() expect(sessionStorage.getItem(DRAFT_KEY)).toBe(draft) expect(useReportStore.getState().report).toBeNull() } describe('EditorRoute while the core cannot run', () => { it('shows the degraded notice, stays on the editor and leaves the draft byte-identical', async () => { setCoreRuntime(unavailableCoreRuntime()) setSheetProvider(bundledSheetProvider) mount() await expectDegraded() expect(window.alert).not.toHaveBeenCalled() }) it('restores the draft once a retry finds the module', async () => { const user = userEvent.setup() setCoreRuntime(unavailableCoreRuntime()) setSheetProvider(bundledSheetProvider) mount() await expectDegraded() // The module is reachable again (network back, CSP fixed). setCoreRuntime(await testCoreRuntime()) await user.click( screen.getByRole('button', { name: fr['sheet.core_retry'] }), ) await waitFor(() => expect(useReportStore.getState().report?.conclusion).toBe('unsaved work'), ) expect(screen.queryByTestId('home-route')).toBeNull() }) it('shows the same notice for a sheet payload the core refuses (invalid_sheet)', async () => { const bundle = await bundledSheet() setSheetProvider({ getReferenceSheet: async () => ({ ...bundle, document: { schema_version: 2, broken: true }, }), }) mount() await expectDegraded() }) })