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

@@ -1,8 +1,10 @@
/**
* T15 (STEP6 §7, §16): the home route on Step 6 archives, with the byte
* scan (`readAttachment`) mocked to hand over the attachment; the layered
* read and the rebase run in the core (R2). Every alert opens with a headline: complete
* restoration or partial recovery. Same sheet: archive kept in the store and
* T15 (STEP6 §7, §16): the home route on Step 6 archives, each envelope
* written into a minimal Faunosteo PDF (`helpers/pdfBytes`) and read by the
* import worker (R4.3: the real worker logic in-process on the shipped
* module): the byte scan, the layered read and the rebase all run in the
* core. Every alert opens with a headline: complete restoration or partial
* recovery. Same sheet: archive kept in the store and
* mirrored, complete + archive_ok. Other sheet: the report is rebased on the
* loaded sheet (unresolved flags, labels from the archived snapshot, sheet
* ref updated) and the alert says degraded with the count; a rebase that
@@ -32,21 +34,17 @@ import type {
ReportArchiveEnvelope,
} from '@faunosteo/core-wasm/types'
import HomeRoute from '@/app/routes/HomeRoute'
import { ImportError, readAttachment } from '@/pdf/import'
import { useReportStore } from '@/state/reportStore'
import { ARCHIVE_KEY } from '@/state/persistence'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { SheetIndex } from '@/services/sheetIndex'
import { useInProcessImportWorker } from '../helpers/importWorker'
import { archivePdf, pdfFile, strippedPdf } from '../helpers/pdfBytes'
import { archiveEnvelope, loadTestSheet, minimalReport } from '../helpers/sheet'
vi.mock('@/pdf/import', async (importOriginal) => ({
...(await importOriginal<typeof import('@/pdf/import')>()),
readAttachment: vi.fn(),
}))
const readMock = vi.mocked(readAttachment)
let sheet: SheetIndex
let restoreSpawn: () => void
beforeAll(async () => {
sheet = await loadTestSheet()
@@ -60,9 +58,10 @@ beforeEach(() => {
activeObservationIndex: 0,
})
vi.stubGlobal('alert', vi.fn())
readMock.mockReset()
restoreSpawn = useInProcessImportWorker().restore
})
afterEach(() => {
restoreSpawn()
cleanup()
vi.unstubAllGlobals()
})
@@ -125,7 +124,8 @@ function staleReportAndArchive(): {
return { report, archive }
}
async function mountAndPickPdf(): Promise<void> {
/** Home mounted, then `pdf` picked: a Faunosteo PDF the worker reads in full. */
async function mountAndPickPdf(pdf: Uint8Array<ArrayBuffer>): Promise<void> {
render(
<MemoryRouter initialEntries={['/']}>
<Routes>
@@ -137,12 +137,13 @@ async function mountAndPickPdf(): Promise<void> {
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
await waitFor(() => expect(button).toBeEnabled())
const input = document.querySelector('input[type="file"]')!
const file = new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
type: 'application/pdf',
})
fireEvent.change(input, { target: { files: [file] } })
fireEvent.change(input, { target: { files: [pdfFile(pdf)] } })
}
/** The PDF the app exports for `archive`: the envelope as its attachment. */
const pdfOf = (archive: ReportArchiveEnvelope) =>
archivePdf(JSON.stringify(archive))
const alertMock = () => vi.mocked(window.alert)
/** The alert split on its blank lines: headline first, then the detail lines. */
const alertLines = () => (alertMock().mock.calls[0]![0] as string).split('\n\n')
@@ -152,8 +153,7 @@ describe('HomeRoute import of a Step 6 archive', () => {
it('same sheet: keeps the archive in the store and its mirror, alerts archive_ok, no rebase', async () => {
const report = reportWithHip()
const archive = archiveEnvelope(sheet, report)
readMock.mockResolvedValue(archive)
await mountAndPickPdf()
await mountAndPickPdf(pdfOf(archive))
await screen.findByTestId('editor-route')
const state = useReportStore.getState()
expect(state.report).toEqual(report)
@@ -172,8 +172,7 @@ describe('HomeRoute import of a Step 6 archive', () => {
const { report, archive } = staleReportAndArchive()
// The attachment is the envelope of the stale report (read by the core).
expect(archive.report).toBe(report)
readMock.mockResolvedValue(archive)
await mountAndPickPdf()
await mountAndPickPdf(pdfOf(archive))
await screen.findByTestId('editor-route')
const opened = useReportStore.getState().report!
expect(opened.sheet).toEqual(sheet.sheet.ref)
@@ -205,8 +204,7 @@ describe('HomeRoute import of a Step 6 archive', () => {
})
const archive = archiveEnvelope(sheet, report)
archive.sheetSnapshot.ref.documentSha256 = OTHER_SHA
readMock.mockResolvedValue(archive)
await mountAndPickPdf()
await mountAndPickPdf(pdfOf(archive))
await screen.findByTestId('editor-route')
const opened = useReportStore.getState().report!
expect(opened.sheet).toEqual(sheet.sheet.ref)
@@ -224,8 +222,7 @@ describe('HomeRoute import of a Step 6 archive', () => {
// A label over the 512-character cap: the core drops the snapshot ('caps').
const archive = archiveEnvelope(sheet, report)
archive.sheetSnapshot.document.name.fr = 'x'.repeat(513)
readMock.mockResolvedValue(archive)
await mountAndPickPdf()
await mountAndPickPdf(pdfOf(archive))
await screen.findByTestId('editor-route')
expect(useReportStore.getState().report).toEqual(report)
expect(useReportStore.getState().archive).toBeNull()
@@ -241,8 +238,8 @@ describe('HomeRoute import of a Step 6 archive', () => {
})
it('attachment_stripped → its error message, nothing opened', async () => {
readMock.mockRejectedValue(new ImportError('attachment_stripped'))
await mountAndPickPdf()
// The stamp says Faunosteo, the embedded file is gone (a tool rewrote the PDF).
await mountAndPickPdf(strippedPdf())
await waitFor(() => expect(errorText()).not.toBeNull())
expect(errorText()!.textContent).toBe(
fr['home.import.error_attachment_stripped'],