feat(app): R4.3b/c import worker, client, controller; HomeRoute on the disposable worker

Protocol v1 (task → ready → result | failed with phase and kind), the worker entry with
the glue bundled statically and the page's compiled Module posted, the file streamed into
the module's memory; client with start and task timers (paused while hidden), settle-once,
terminate always, error events never masked; module-level controller (one import at a
time, cancel on leaving Home, worker chunk prefetched once the engine is loaded); the
settlement mapped to the B.1.8 messages (OD-5: a trap, a timeout or an unreadable file is
« Erreur de lecture du PDF. », never a corrupt PDF). HomeRoute keeps its routing and size
pre-checks, loads its own index after acceptance, drops a result when the session changed.
Tests: handler and client in-process, the corpus message test (O8 51, O7 stored inputs,
O2/O3/Step 5), the six existing suites migrated to the real worker logic.
This commit is contained in:
bchanot committed 2026-10-18 18:10:42 +02:00
1 parent 52471dbbc0
commit 6d1cce6e06
26 files changed
+2164 -247

No files matched your search

@@ -4,10 +4,26 @@
* while the engine is not stopped; New report and Import answer the engine message then
* open the panel; the trapping (draft, archive) pair is read once (one trap across mount,
* New report, Import and remount); only the panel's Retry reads it again; a confirmed
* discard removes the draft and clears the memory.
* discard removes the draft and clears the memory. The Import step reads its file in the
* import worker (R4.3, the real worker logic in-process): the trap is the page's own
* `draft.restore` of the tab draft, reached through `hydrateImport`.
*/
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import {
cleanup,
fireEvent,
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'
@@ -22,6 +38,7 @@ import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { SheetIndex } from '@/services/sheetIndex'
import { testCoreRuntime, trappingCoreRuntime } from '../helpers/core'
import { useInProcessImportWorker } from '../helpers/importWorker'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
const MARKER = 'trap-me'
@@ -30,6 +47,7 @@ let draft: string
let traps: { count: number }
let engineStopped: () => boolean
let engineReady: () => Promise<boolean>
let restoreSpawn: () => void
beforeAll(async () => {
sheet = await loadTestSheet()
@@ -42,7 +60,11 @@ beforeEach(async () => {
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 })
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
})
useReportStore.getState().clearTrapped()
const trapping = await trappingCoreRuntime(MARKER)
traps = trapping.traps
@@ -50,10 +72,12 @@ beforeEach(async () => {
engineReady = () => trapping.runtime.ready()
setCoreRuntime(trapping.runtime)
setSheetProvider(bundledSheetProvider)
restoreSpawn = useInProcessImportWorker().restore
// After the reset (an empty store mirrors no draft): the tab as a reload finds it.
sessionStorage.setItem(DRAFT_KEY, draft)
})
afterEach(async () => {
restoreSpawn()
cleanup()
vi.unstubAllGlobals()
vi.restoreAllMocks()
@@ -76,9 +100,10 @@ const routes = (initial: string) =>
async function expectEnginePanel() {
await screen.findByText(fr['session.held.notice_engine']!)
expect(screen.getByTestId('held-draft')).toHaveTextContent(
fr['session.held.draft_engine_failure']!.replace('{size}', /\d+ Ko/.exec(
screen.getByTestId('held-draft').textContent ?? '',
)![0]),
fr['session.held.draft_engine_failure']!.replace(
'{size}',
/\d+ Ko/.exec(screen.getByTestId('held-draft').textContent ?? '')![0],
),
)
expect(screen.queryByText(fr['sheet.core_unavailable']!)).toBeNull()
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(draft)
@@ -100,13 +125,17 @@ async function pickCopy(user: ReturnType<typeof userEvent.setup>) {
const pick = screen.getByRole('button', { name: fr['home.import.cta']! })
await waitFor(() => expect(pick).toBeEnabled())
await user.click(pick)
vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(() => undefined)
vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(
() => undefined,
)
await user.click(
await screen.findByRole('button', { name: fr['session.replace.confirm']! }),
)
const copy = JSON.stringify(minimalReport(sheet, { id: 'other' }))
fireEvent.change(document.querySelector('input[type="file"]')!, {
target: { files: [new File([copy], 'copie.json', { type: 'application/json' })] },
target: {
files: [new File([copy], 'copie.json', { type: 'application/json' })],
},
})
}
@@ -128,7 +157,9 @@ describe('a draft the engine traps on', () => {
routes('/new')
await submitNewReport(user)
await waitFor(() =>
expect(window.alert).toHaveBeenCalledWith(fr['session.held_blocked_engine']),
expect(window.alert).toHaveBeenCalledWith(
fr['session.held_blocked_engine'],
),
)
await expectEnginePanel()
cleanup()
@@ -136,7 +167,9 @@ describe('a draft the engine traps on', () => {
routes('/')
await pickCopy(user)
await waitFor(() =>
expect(window.alert).toHaveBeenCalledWith(fr['session.held_blocked_engine']),
expect(window.alert).toHaveBeenCalledWith(
fr['session.held_blocked_engine'],
),
)
await expectEnginePanel()
cleanup()
@@ -154,7 +187,9 @@ describe('a draft the engine traps on', () => {
await expectEnginePanel()
// The instance that trapped is replaced in the background: retry on the fresh one.
await engineReady()
await user.click(screen.getByRole('button', { name: fr['session.held.retry'] }))
await user.click(
screen.getByRole('button', { name: fr['session.held.retry'] }),
)
await waitFor(() => expect(traps.count).toBe(2))
await expectEnginePanel()
})
@@ -163,7 +198,9 @@ describe('a draft the engine traps on', () => {
const user = userEvent.setup()
routes('/editor')
await expectEnginePanel()
await user.click(screen.getByRole('button', { name: fr['session.held.discard'] }))
await user.click(
screen.getByRole('button', { name: fr['session.held.discard'] }),
)
expect(sessionStorage.getItem(DRAFT_KEY)).toBe(draft)
await user.click(
screen.getByRole('button', { name: fr['session.held.discard_confirm'] }),