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

@@ -0,0 +1,280 @@
/**
* R4.3: the page side of one import (plan §B.1.5 items 1, 3, 5): the worker created in a
* try/catch, listeners before the post, the start timer until `ready`, the task timer
* after, settle once on the first of result / failed / error event / timer / cancel, the
* worker terminated in every case, later messages ignored, timers paused while hidden.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
runImportTask,
type ImportSettlement,
type WorkerLike,
} from '@/core/worker/client'
import { PROTOCOL_VERSION } from '@/core/worker/protocol'
import type { Visibility } from '@/core/worker/timers'
/** A scripted worker: the test drives its messages and events. */
function scriptedWorker() {
const worker: WorkerLike & { terminated: number; sent: unknown[] } = {
terminated: 0,
sent: [],
onmessage: null,
onerror: null,
onmessageerror: null,
postMessage: (m) => worker.sent.push(m),
terminate: () => {
worker.terminated += 1
},
}
const emit = (data: unknown) => worker.onmessage?.({ data } as MessageEvent)
return {
worker,
ready: () => emit({ v: PROTOCOL_VERSION, type: 'ready' }),
result: (out = '{"ok":true}') =>
emit({
v: PROTOCOL_VERSION,
type: 'result',
out,
diag: { ms: 1, linearBytes: 0, chunks: 1 },
}),
failed: (phase: 'start' | 'task', kind: string) =>
emit({
v: PROTOCOL_VERSION,
type: 'failed',
phase,
kind,
detail: `${kind}!`,
}),
error: (message: string) => worker.onerror?.({ message } as ErrorEvent),
}
}
function visibility(): Visibility & { set: (hidden: boolean) => void } {
let hidden = false
const listeners = new Set<() => void>()
return {
hidden: () => hidden,
onChange: (listener) => {
listeners.add(listener)
return () => listeners.delete(listener)
},
set: (next) => {
hidden = next
for (const listener of listeners) listener()
},
}
}
const file = new File([new Uint8Array([1])], 'a.pdf')
const input = { task: 'pdf' as const, file, sheet: '{}', module: null }
async function settled(run: {
settled: Promise<ImportSettlement>
}): Promise<ImportSettlement> {
await vi.advanceTimersByTimeAsync(0)
return run.settled
}
describe('import client', () => {
beforeEach(() => vi.useFakeTimers())
afterEach(() => vi.useRealTimers())
it('posts the task after installing the listeners, then settles on the result and terminates', async () => {
const s = scriptedWorker()
const run = runImportTask(input, {
spawn: () => s.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
expect(s.worker.onmessage).not.toBeNull()
expect(s.worker.sent).toEqual([{ v: 1, type: 'task', ...input }])
s.ready()
s.result('{"ok":true}')
expect(await settled(run)).toMatchObject({
kind: 'result',
out: '{"ok":true}',
})
expect(s.worker.terminated).toBe(1)
})
it('is worker_unavailable when the spawn throws, fails before ready, or never answers ready', async () => {
const thrown = runImportTask(input, {
spawn: () => {
throw new DOMException('blocked', 'SecurityError')
},
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
expect(await settled(thrown)).toEqual({
kind: 'failed',
reason: 'worker_unavailable',
detail: 'SecurityError',
})
const early = scriptedWorker()
const failedEarly = runImportTask(input, {
spawn: () => early.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
early.failed('start', 'other')
expect(await settled(failedEarly)).toMatchObject({
kind: 'failed',
reason: 'worker_unavailable',
})
const silent = scriptedWorker()
const timedOut = runImportTask(input, {
spawn: () => silent.worker,
startTimeoutMs: 100,
taskTimeoutMs: 1000,
})
await vi.advanceTimersByTimeAsync(99)
expect(silent.worker.terminated).toBe(0)
await vi.advanceTimersByTimeAsync(2)
expect(await timedOut.settled).toMatchObject({
kind: 'failed',
reason: 'worker_unavailable',
})
expect(silent.worker.terminated).toBe(1)
})
it('maps task failures: trap → crashed, read → read, sheet/other → engine, timer → timeout', async () => {
for (const [kind, reason] of [
['trap', 'crashed'],
['read', 'read'],
['sheet', 'engine'],
['other', 'engine'],
]) {
const s = scriptedWorker()
const run = runImportTask(input, {
spawn: () => s.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
s.ready()
s.failed('task', kind!)
expect(await settled(run)).toEqual({
kind: 'failed',
reason,
detail: `${kind}!`,
})
expect(s.worker.terminated).toBe(1)
}
const slow = scriptedWorker()
const run = runImportTask(input, {
spawn: () => slow.worker,
startTimeoutMs: 50,
taskTimeoutMs: 200,
})
await vi.advanceTimersByTimeAsync(40)
slow.ready()
await vi.advanceTimersByTimeAsync(199)
expect(slow.worker.terminated).toBe(0)
await vi.advanceTimersByTimeAsync(2)
expect(await run.settled).toMatchObject({
kind: 'failed',
reason: 'timeout',
})
})
it('settles once: a result after the timeout, a failure after the result, are ignored', async () => {
const s = scriptedWorker()
const run = runImportTask(input, {
spawn: () => s.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
s.ready()
await vi.advanceTimersByTimeAsync(101)
s.result('{"ok":true}')
expect(await run.settled).toMatchObject({
kind: 'failed',
reason: 'timeout',
})
expect(s.worker.terminated).toBe(1)
const t = scriptedWorker()
const second = runImportTask(input, {
spawn: () => t.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
t.ready()
t.result('{"ok":true}')
t.failed('task', 'trap')
t.error('late')
expect(await settled(second)).toMatchObject({ kind: 'result' })
expect(t.worker.terminated).toBe(1)
})
it('never masks an error event: worker_unavailable before ready, crashed after', async () => {
const before = scriptedWorker()
const first = runImportTask(input, {
spawn: () => before.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
before.error('script failed')
expect(await settled(first)).toEqual({
kind: 'failed',
reason: 'worker_unavailable',
detail: 'script failed',
})
const after = scriptedWorker()
const second = runImportTask(input, {
spawn: () => after.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
after.ready()
after.error('boom')
expect(await settled(second)).toEqual({
kind: 'failed',
reason: 'crashed',
detail: 'boom',
})
})
it('cancel terminates the worker and settles cancelled, once', async () => {
const s = scriptedWorker()
const run = runImportTask(input, {
spawn: () => s.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
})
s.ready()
run.cancel()
run.cancel()
s.result()
expect(await settled(run)).toMatchObject({
kind: 'failed',
reason: 'cancelled',
})
expect(s.worker.terminated).toBe(1)
})
it('pauses the task timer while the page is hidden', async () => {
const s = scriptedWorker()
const vis = visibility()
const run = runImportTask(input, {
spawn: () => s.worker,
startTimeoutMs: 100,
taskTimeoutMs: 100,
visibility: vis,
})
s.ready()
await vi.advanceTimersByTimeAsync(50)
vis.set(true)
await vi.advanceTimersByTimeAsync(10_000)
expect(s.worker.terminated).toBe(0)
vis.set(false)
await vi.advanceTimersByTimeAsync(49)
expect(s.worker.terminated).toBe(0)
await vi.advanceTimersByTimeAsync(2)
expect(await run.settled).toMatchObject({
kind: 'failed',
reason: 'timeout',
})
})
})
@@ -0,0 +1,211 @@
/**
* R4.3: the import worker's logic in-process on the shipped module (plan §B.1.5 items 2
* and 4): one task, `ready` then one `result` or one `failed` with its phase and kind;
* the file streamed into the module's memory; the caps checked before any read; every
* throwable classified (trap, read, sheet, other) and the start phase kept apart.
*/
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, expect, it } from 'vitest'
import {
createWorkerHandler,
MAX_COPY_BYTES,
MAX_PDF_BYTES,
} from '@/core/worker/handler'
import { PROTOCOL_VERSION, type WorkerMessage } from '@/core/worker/protocol'
import { bundledSheetProvider } from '@/sheets/provider'
import { FIXTURES_DIR } from '../helpers/goldens'
import { nodeEngineLoader, throwingEngineLoader } from '../helpers/importWorker'
const fixture = (name: string) => readFileSync(resolve(FIXTURES_DIR, name))
async function sheetText(): Promise<string> {
const bundle = await bundledSheetProvider.getReferenceSheet('equine')
if (!bundle) throw new Error('bundled sheet missing')
return JSON.stringify(bundle)
}
interface Run {
messages: WorkerMessage[]
}
async function run(
task: 'pdf' | 'copy',
file: File,
load = nodeEngineLoader,
sheet?: string,
): Promise<Run> {
const messages: WorkerMessage[] = []
const handle = createWorkerHandler(load, (m) => messages.push(m))
await handle({
v: PROTOCOL_VERSION,
type: 'task',
task,
file,
sheet: sheet ?? (await sheetText()),
module: null,
})
return { messages }
}
function envelope(messages: WorkerMessage[]): {
ok: boolean
kind?: string
value?: unknown
} {
const result = messages.find((m) => m.type === 'result')
if (!result || result.type !== 'result')
throw new Error(`no result: ${JSON.stringify(messages)}`)
return JSON.parse(result.out)
}
describe('import worker handler (in-process, shipped module)', () => {
it('answers ready then a result for a Faunosteo PDF, streamed in chunks', async () => {
const file = new File([fixture('report-v3.bare.pdf')], 'bilan.pdf')
const { messages } = await run('pdf', file)
expect(messages.map((m) => m.type)).toEqual(['ready', 'result'])
const out = envelope(messages)
expect(out.ok).toBe(true)
expect(
(out.value as { rebase: { report: { version: number } } }).rebase.report
.version,
).toBe(3)
const result = messages[1]
if (result?.type !== 'result') throw new Error('no result')
expect(result.diag.chunks).toBeGreaterThanOrEqual(1)
expect(result.diag.linearBytes).toBeGreaterThan(0)
})
it('reads a rescue copy through the core (strict UTF-8, no JSON wrapping)', async () => {
const file = new File([fixture('report-v3.demo.json')], 'copie.json')
const { messages } = await run('copy', file)
expect(envelope(messages).ok).toBe(true)
const bad = new File([new Uint8Array([0x7b, 0xff, 0x7d])], 'copie.json')
expect(envelope((await run('copy', bad)).messages)).toMatchObject({
ok: false,
kind: 'invalid',
})
})
it('refuses an oversized file before reading a byte', async () => {
const huge = {
size: MAX_PDF_BYTES + 1,
stream: () => {
throw new Error('read')
},
} as unknown as File
expect(envelope((await run('pdf', huge)).messages)).toMatchObject({
ok: false,
kind: 'invalid_pdf',
})
const copy = {
size: MAX_COPY_BYTES + 1,
stream: () => {
throw new Error('read')
},
} as unknown as File
expect(envelope((await run('copy', copy)).messages)).toMatchObject({
ok: false,
kind: 'invalid',
})
})
it('gives the core kinds for a foreign PDF and garbage', async () => {
const garbage = new File([new TextEncoder().encode('hello')], 'x.pdf')
expect(envelope((await run('pdf', garbage)).messages)).toMatchObject({
ok: false,
kind: 'invalid_pdf',
})
const stripped = new File(
[new TextEncoder().encode('%PDF-1.7\n/Keywords (faunosteo-archive/1)')],
'x.pdf',
)
expect(envelope((await run('pdf', stripped)).messages)).toMatchObject({
ok: false,
kind: 'attachment_stripped',
})
})
it('classifies a trap during the task as failed/task/trap', async () => {
const file = new File([fixture('report-v3.bare.pdf')], 'bilan.pdf')
const load = throwingEngineLoader(
new WebAssembly.RuntimeError('unreachable'),
)
const { messages } = await run('pdf', file, load)
expect(messages).toEqual([
{ v: 1, type: 'ready' },
{
v: 1,
type: 'failed',
phase: 'task',
kind: 'trap',
detail: 'RuntimeError: unreachable',
},
])
const overflow = throwingEngineLoader(
new RangeError('Maximum call stack size exceeded'),
)
expect((await run('pdf', file, overflow)).messages[1]).toMatchObject({
kind: 'trap',
})
})
it('classifies a file that cannot be read as failed/task/read', async () => {
const error = new DOMException(
'The file could not be read',
'NotReadableError',
)
const unreadable = {
size: 10,
stream: () => ({
getReader: () => ({
read: () => Promise.reject(error),
cancel: async () => {},
}),
}),
} as unknown as File
const { messages } = await run('pdf', unreadable)
expect(messages[1]).toMatchObject({
type: 'failed',
phase: 'task',
kind: 'read',
})
})
it('classifies a refused sheet as failed/task/sheet and a load failure as start', async () => {
const file = new File([fixture('report-v3.bare.pdf')], 'bilan.pdf')
const refused = await run(
'pdf',
file,
nodeEngineLoader,
'{"status":"draft"}',
)
expect(refused.messages[1]).toMatchObject({
type: 'failed',
phase: 'task',
kind: 'sheet',
})
const broken = await run('pdf', file, () =>
Promise.reject(new TypeError('no glue')),
)
expect(broken.messages).toEqual([
{
v: 1,
type: 'failed',
phase: 'start',
kind: 'other',
detail: 'TypeError: no glue',
},
])
})
it('ignores a message that is not a task of this protocol', async () => {
const messages: WorkerMessage[] = []
const handle = createWorkerHandler(nodeEngineLoader, (m) =>
messages.push(m),
)
await handle({ v: 2, type: 'task' })
await handle('nope')
expect(messages).toEqual([])
})
})
@@ -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'],
@@ -1,11 +1,15 @@
/**
* Step 6 Phase 0 characterisation of the import flow on the home route, with
* the two import steps mocked (R2: `readAttachment`, the engine's byte scan,
* then `importAttachment`, the core's layered read and rebase on the loaded
* sheet): the migration alert (degraded headline, then the count), the three
* ImportError kinds plus the generic error mapped to the FR messages, and the
* navigation to /editor on success; `lone_surrogate` (L1) has its own message.
* Step 6 Phase 0 characterisation of the import flow on the home route, through the
* import worker (R4.3: the real worker logic in-process on the shipped module, the
* files real fixtures or minimal PDFs from `helpers/pdfBytes`): the picked file reaches
* the worker unread by the page, with the page's sheet and module, and the core's
* result hydrates the store on the loaded sheet; the migration alert (degraded
* headline, then the count), the core's refusal kinds plus a worker trap mapped to the
* FR messages, and the navigation to /editor on success; `lone_surrogate` (L1) has its
* own message.
*/
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import {
describe,
it,
@@ -25,82 +29,89 @@ import {
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import type { ArchiveImport } from '@faunosteo/core-wasm/types'
import HomeRoute from '@/app/routes/HomeRoute'
import {
ImportError,
importAttachment,
MAX_PDF_BYTES,
readAttachment,
} from '@/pdf/import'
import { MAX_PDF_BYTES, type EngineLoader } from '@/core/worker/handler'
import type { ImportTaskMessage } from '@/core/worker/protocol'
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'
vi.mock('@/pdf/import', async (importOriginal) => ({
...(await importOriginal<typeof import('@/pdf/import')>()),
readAttachment: vi.fn(),
importAttachment: vi.fn(),
}))
const readMock = vi.mocked(readAttachment)
const importMock = vi.mocked(importAttachment)
const ATTACHMENT = { version: 3 }
import { FIXTURES_DIR } from '../helpers/goldens'
import {
throwingEngineLoader,
useInProcessImportWorker,
type FakeWorker,
} from '../helpers/importWorker'
import { archivePdf, foreignPdf, pdfFile } from '../helpers/pdfBytes'
import { bundledSheet, loadTestSheet, minimalReport } from '../helpers/sheet'
let sheet: SheetIndex
let workers: FakeWorker[]
let restoreSpawn: () => void
beforeAll(async () => {
sheet = await loadTestSheet()
})
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
// A fresh tab: the previous import's draft would be a held text.
sessionStorage.clear()
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
})
vi.stubGlobal('alert', vi.fn())
readMock.mockReset()
readMock.mockResolvedValue(ATTACHMENT)
importMock.mockReset()
;({ workers, restore: restoreSpawn } = useInProcessImportWorker())
})
afterEach(() => {
restoreSpawn()
cleanup()
vi.unstubAllGlobals()
vi.restoreAllMocks()
})
/** What the import resolves with: the read (bare report, no archive) and no rebase. */
function outcome(patch: {
migrated: boolean
unresolvedZones?: number
unresolvedFindings?: number
}): ArchiveImport {
const report = minimalReport(sheet)
return {
read: {
report,
origin: patch.migrated ? 'v2' : 'bare_v3',
archive: null,
unresolvedZones: 0,
unresolvedFindings: 0,
...patch,
},
rebase: {
report,
unresolvedZones: 0,
unresolvedFindings: 0,
rebased: false,
},
const fixture = (name: string) =>
new Uint8Array(readFileSync(resolve(FIXTURES_DIR, name)))
/** The demo report exported bare by the Step 6 code: same sheet, no archive. */
const barePdf = () => pdfFile(fixture('report-v3.bare.pdf'))
/** A Report v2 holding one zone and one finding the migration map does not know. */
function lossyV2Pdf(): File {
const v2 = JSON.parse(
readFileSync(resolve(FIXTURES_DIR, 'report-v2.minimal.json'), 'utf8'),
)
const observation = {
id: 'obs-lost',
zones: [{ boneId: 'point-gone' }],
findings: [
{
zoneId: 'zone-gone',
path: ['zone-gone-dysfunctions', 'gone'],
labelFrSnapshot: 'Constat disparu',
labelEnSnapshot: 'Gone finding',
},
],
freeTextFindings: [],
comment: '',
}
const report = { ...v2, observations: [observation] }
return pdfFile(archivePdf(JSON.stringify(report)))
}
/** A 4-byte "%PDF" file; the import is mocked, the bytes only travel. */
function pdfFile(): File {
return new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
type: 'application/pdf',
})
/** The core's import outcome the worker answered (the result message, parsed). */
function resultOf(worker: FakeWorker): ArchiveImport {
const result = worker.posted.find((m) => m.type === 'result')
if (result?.type !== 'result') throw new Error('the worker gave no result')
const envelope = JSON.parse(result.out) as {
ok: boolean
value: ArchiveImport
}
if (!envelope.ok) throw new Error(`the core refused: ${result.out}`)
return envelope.value
}
async function mountAndPickPdf(file: File = pdfFile()): Promise<void> {
async function mountAndPickPdf(file: File = barePdf()): Promise<void> {
render(
<MemoryRouter initialEntries={['/']}>
<Routes>
@@ -120,25 +131,44 @@ const errorText = () => screen.queryByText(/./, { selector: 'p.text-red-700' })
describe('HomeRoute import: success paths', () => {
it('passes the file bytes to the scan and the attachment to the loaded sheet, hydrates, navigates to /editor', async () => {
importMock.mockReturnValue(outcome({ migrated: false }))
await mountAndPickPdf()
await screen.findByTestId('editor-route')
expect(readMock).toHaveBeenCalledTimes(1)
expect(readMock.mock.calls[0]![0]).toBeInstanceOf(Uint8Array)
expect(importMock).toHaveBeenCalledTimes(1)
const [attachment, index] = importMock.mock.calls[0]!
expect(attachment).toBe(ATTACHMENT)
// Since R4.3 the scan runs in the worker: the page hands it the file unread.
const file = barePdf()
const read = vi.spyOn(file, 'arrayBuffer')
const text = vi.spyOn(file, 'text')
const real = useReportStore.getState().hydrateImport
const hydrate = vi.fn(real)
useReportStore.setState({ hydrateImport: hydrate })
try {
await mountAndPickPdf(file)
await screen.findByTestId('editor-route')
} finally {
useReportStore.setState({ hydrateImport: real })
}
expect(workers).toHaveLength(1)
const task = workers[0]!.received[0] as ImportTaskMessage
expect(task).toMatchObject({ type: 'task', task: 'pdf' })
expect(task.file).toBe(file)
expect(task.sheet).toBe(JSON.stringify(await bundledSheet()))
expect(task.module).toBeInstanceOf(WebAssembly.Module)
expect(read).not.toHaveBeenCalled()
expect(text).not.toHaveBeenCalled()
const imported = resultOf(workers[0]!)
expect(hydrate).toHaveBeenCalledTimes(1)
const [session, index] = hydrate.mock.calls[0]!
expect(session).toEqual({ report: imported.rebase.report, archive: null })
expect(index).toBe(sheet)
expect(useReportStore.getState().report?.id).toBe('r1')
expect(useReportStore.getState().report?.id).toBe('fixture-demo-v3')
expect(alertMock()).not.toHaveBeenCalled()
})
it('alerts "degraded" then the unresolved count (zones + findings) after a lossy migration', async () => {
importMock.mockReturnValue(
outcome({ migrated: true, unresolvedZones: 1, unresolvedFindings: 1 }),
)
await mountAndPickPdf()
await mountAndPickPdf(lossyV2Pdf())
await screen.findByTestId('editor-route')
expect(resultOf(workers[0]!).read).toMatchObject({
migrated: true,
unresolvedZones: 1,
unresolvedFindings: 1,
})
expect(alertMock()).toHaveBeenCalledTimes(1)
const message = alertMock().mock.calls[0]![0] as string
const [headline, ...lines] = message.split('\n\n')
@@ -147,49 +177,66 @@ describe('HomeRoute import: success paths', () => {
})
it('stays silent on a migration that lost nothing', async () => {
importMock.mockReturnValue(outcome({ migrated: true }))
await mountAndPickPdf()
// The Step 5 v2 export: every zone and finding has its place on the draft.
await mountAndPickPdf(pdfFile(fixture('report-v2.full.pdf')))
await screen.findByTestId('editor-route')
expect(resultOf(workers[0]!).read).toMatchObject({
migrated: true,
unresolvedZones: 0,
unresolvedFindings: 0,
})
expect(alertMock()).not.toHaveBeenCalled()
})
})
describe('HomeRoute import: error paths', () => {
const cases: Array<[string, Error, string]> = [
const cases: Array<[string, () => File, string, EngineLoader?]> = [
[
'incompatible_version → message with the found version',
new ImportError('incompatible_version', 7),
() =>
pdfFile(
archivePdf(JSON.stringify({ ...minimalReport(sheet), version: 7 })),
),
fr['home.import.error_incompatible_version']!.replace('{version}', '7'),
],
[
'invalid_pdf → unreadable PDF message',
new ImportError('invalid_pdf'),
() => pdfFile(new TextEncoder().encode('not a PDF at all')),
fr['home.import.error_invalid_pdf']!,
],
[
'no_attachment → not a Faunosteo PDF message',
new ImportError('no_attachment'),
() => pdfFile(foreignPdf()),
fr['home.import.error_no_attachment']!,
],
[
'lone_surrogate → malformed text message, file untouched',
new ImportError('lone_surrogate'),
() =>
pdfFile(
archivePdf(
JSON.stringify(minimalReport(sheet, { conclusion: 'a\ud800b' })),
),
),
fr['home.import.error_lone_surrogate']!,
],
[
'generic Error → generic message',
new Error('disk on fire'),
// A trap of the worker's engine: never shown as a corrupt PDF (OD-5).
barePdf,
fr['home.import.error_generic']!,
throwingEngineLoader(new WebAssembly.RuntimeError('unreachable')),
],
]
it.each(cases)('%s', async (_label, error, expected) => {
importMock.mockImplementation(() => {
throw error
})
await mountAndPickPdf()
it.each(cases)('%s', async (_label, file, expected, load) => {
// Another engine replaces the seam; afterEach restores the default.
if (load) {
;({ workers, restore: restoreSpawn } = useInProcessImportWorker(load))
}
await mountAndPickPdf(file())
await waitFor(() => expect(errorText()).not.toBeNull())
expect(errorText()!.textContent).toBe(expected)
expect(workers).toHaveLength(1)
expect(screen.queryByTestId('editor-route')).toBeNull()
expect(useReportStore.getState().report).toBeNull()
expect(alertMock()).not.toHaveBeenCalled()
@@ -198,15 +245,16 @@ describe('HomeRoute import: error paths', () => {
describe('HomeRoute import: size gate', () => {
it('refuses a file over MAX_PDF_BYTES before reading it: unreadable PDF message', async () => {
const file = pdfFile()
const file = barePdf()
Object.defineProperty(file, 'size', { value: MAX_PDF_BYTES + 1 })
const read = vi.spyOn(file, 'arrayBuffer')
const stream = vi.spyOn(file, 'stream')
await mountAndPickPdf(file)
await waitFor(() => expect(errorText()).not.toBeNull())
expect(errorText()!.textContent).toBe(fr['home.import.error_invalid_pdf'])
expect(read).not.toHaveBeenCalled()
expect(readMock).not.toHaveBeenCalled()
expect(importMock).not.toHaveBeenCalled()
expect(stream).not.toHaveBeenCalled()
expect(workers).toHaveLength(0)
expect(screen.queryByTestId('editor-route')).toBeNull()
expect(useReportStore.getState().report).toBeNull()
})
@@ -214,12 +262,13 @@ describe('HomeRoute import: size gate', () => {
describe('HomeRoute import: a core failure reading the tab draft', () => {
it('shows the engine error line, not the held-session alert', async () => {
importMock.mockReturnValue(outcome({ migrated: false }))
const real = useReportStore.getState().hydrateImport
useReportStore.setState({ hydrateImport: () => ({ ok: false }) })
try {
await mountAndPickPdf()
await screen.findByText(fr['home.import.error_core_unavailable']!)
// The worker read the file: the failure is the store's, after the result.
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
expect(alertMock()).not.toHaveBeenCalled()
expect(screen.queryByTestId('editor-route')).toBeNull()
} finally {
@@ -228,12 +277,14 @@ describe('HomeRoute import: a core failure reading the tab draft', () => {
})
it('still alerts and opens the editor for a held session text', async () => {
importMock.mockReturnValue(outcome({ migrated: false }))
const real = useReportStore.getState().hydrateImport
useReportStore.setState({ hydrateImport: () => ({ ok: false, reason: 'held' }) })
useReportStore.setState({
hydrateImport: () => ({ ok: false, reason: 'held' }),
})
try {
await mountAndPickPdf()
await screen.findByTestId('editor-route')
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
expect(alertMock()).toHaveBeenCalledWith(fr['session.held_blocked'])
} finally {
useReportStore.setState({ hydrateImport: real })
@@ -0,0 +1,264 @@
/**
* R4.3: the Home import's message on every file of the frozen corpora, through the real
* worker logic in-process on the shipped module (plan §B.1.8, OD-5). O8 (the full R3
* path, 51 PDFs: expected outcome from `tolerance.json`, the declared OD-3 / OD-4
* differences applied), O7 (the stored reader inputs: expected from `readReportArchive`),
* the O2 PDFs of the R3 build, the O3 envelopes and the Step 5 fixtures (all open the
* editor). A technical failure of the worker is never shown as a corrupt PDF: a trap, a
* timeout and an unreadable file give « Erreur de lecture du PDF. », a worker that
* cannot start the engine message.
*/
import { readdirSync, readFileSync } from 'node:fs'
import { join, resolve } from 'node:path'
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import {
cleanup,
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react'
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import HomeRoute from '@/app/routes/HomeRoute'
import { coreRuntime } from '@/core/runtime'
import { setImportWorkerSpawn } from '@/core/worker/controller'
import { useReportStore } from '@/state/reportStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import { FIXTURES_DIR } from '../helpers/goldens'
import {
throwingEngineLoader,
useInProcessImportWorker,
} from '../helpers/importWorker'
const ROOT = resolve(FIXTURES_DIR, '../../../..')
const oracle = (name: string) =>
JSON.parse(
readFileSync(join(ROOT, 'crates/core/tests/oracles/r4', name), 'utf8'),
)
/** The message of a failure kind (plan §B.1.8), `editor` for a success. */
function expectedOf(outcome: {
ok: boolean
kind?: string
foundVersion?: number
}): string {
if (outcome.ok) return 'editor'
switch (outcome.kind) {
case 'incompatible_version':
return fr['home.import.error_incompatible_version']!.replace(
'{version}',
String(outcome.foundVersion),
)
case 'invalid_pdf':
return fr['home.import.error_invalid_pdf']!
case 'attachment_stripped':
return fr['home.import.error_attachment_stripped']!
case 'no_attachment':
return fr['home.import.error_no_attachment']!
case 'lone_surrogate':
return fr['home.import.error_lone_surrogate']!
default:
throw new Error(`unexpected kind ${outcome.kind}`)
}
}
/** Rust outcomes that differ from the R3 path on purpose (RUST.md §10.3). */
const DECLARED: Record<string, { ok: boolean; kind?: string }> = {
'values-1m': { ok: false, kind: 'attachment_stripped' },
'surrogate-content-overwritten': { ok: false, kind: 'lone_surrogate' },
'surrogate-version-overwritten': { ok: false, kind: 'lone_surrogate' },
'two-files-decoy-first': { ok: false, kind: 'lone_surrogate' },
}
const ERRORS = [
'home.import.error_incompatible_version',
'home.import.error_invalid_pdf',
'home.import.error_attachment_stripped',
'home.import.error_no_attachment',
'home.import.error_lone_surrogate',
'home.import.error_invalid_copy',
'home.import.error_generic',
'home.import.error_core_unavailable',
] as const
function renderHome() {
return render(
<MemoryRouter initialEntries={['/']}>
<Routes>
<Route path="/" element={<HomeRoute />} />
<Route path="/editor" element={<p data-testid="editor-route" />} />
</Routes>
</MemoryRouter>,
)
}
/** Picks `file` on a fresh Home and returns `editor` or the error line shown. */
async function outcomeOf(file: File): Promise<string> {
cleanup()
// A fresh tab for every file: the previous import's draft would be a held text.
sessionStorage.clear()
useReportStore.getState().clearTrapped()
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
})
renderHome()
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
await waitFor(() => expect(button).toBeEnabled())
fireEvent.change(document.querySelector('input[type="file"]')!, {
target: { files: [file] },
})
const shown = await waitFor(
() => {
if (screen.queryByTestId('editor-route')) return 'editor'
// A message with a placeholder is matched on its text before it.
const error = ERRORS.map((key) => fr[key]!.split('{')[0]!).find((text) =>
screen.queryByText(text, { exact: false }),
)
if (!error) throw new Error(`pending (${file.name})`)
return screen.getByText(error, { exact: false }).textContent ?? error
},
{ timeout: 10_000 },
)
return shown
}
const pdfFile = (path: string, name = path.split('/').pop() ?? 'bilan.pdf') =>
new File([readFileSync(path)], name, { type: 'application/pdf' })
beforeAll(() => {
useInProcessImportWorker()
})
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
sessionStorage.clear()
vi.stubGlobal('alert', vi.fn())
})
afterEach(() => {
cleanup()
vi.unstubAllGlobals()
})
describe('Home import messages on the frozen corpora (real worker logic, shipped module)', () => {
it('O8: every tolerance case gives the R3 message, outside the declared differences', async () => {
const cases = oracle('tolerance.json').cases as Array<{
id: string
pdf: string
outcome: { ok: boolean; kind?: string; foundVersion?: number }
}>
expect(cases).toHaveLength(51)
const mismatches: string[] = []
for (const c of cases) {
const expected = expectedOf(DECLARED[c.id] ?? c.outcome)
const shown = await outcomeOf(pdfFile(join(ROOT, c.pdf)))
if (shown !== expected)
mismatches.push(`${c.id}: expected ${expected}, shown ${shown}`)
}
expect(mismatches).toEqual([])
}, 120_000)
it('O7: every stored reader input gives the reader outcome message', async () => {
const inputs = oracle('read.json').inputs as Array<{
file: string
kind: string
stored: string
outcomes: {
readReportArchive?: {
ok: boolean
kind?: string
foundVersion?: number
}
}
}>
const mismatches: string[] = []
let count = 0
for (const input of inputs) {
const expected = input.outcomes.readReportArchive
if (input.kind !== 'pdf' || input.stored !== 'bytes' || !expected)
continue
count += 1
const shown = await outcomeOf(
pdfFile(join(FIXTURES_DIR, 'pdf/r4/read', input.file)),
)
if (shown !== expectedOf(expected)) {
mismatches.push(
`${input.file}: expected ${expectedOf(expected)}, shown ${shown}`,
)
}
}
expect(mismatches).toEqual([])
expect(count).toBeGreaterThan(130)
}, 180_000)
it('O2, O3 and the Step 5 fixtures open the editor', async () => {
const files = [
join(FIXTURES_DIR, 'report-v2.full.pdf'),
join(FIXTURES_DIR, 'report-v2.minimal.pdf'),
join(FIXTURES_DIR, 'report-v3.bare.pdf'),
...readdirSync(join(FIXTURES_DIR, 'pdf/r3'), {
recursive: true,
encoding: 'utf8',
})
.filter((n) => n.endsWith('.pdf'))
.map((n) => join(FIXTURES_DIR, 'pdf/r3', n)),
...readdirSync(join(FIXTURES_DIR, 'pdf/step6'))
.filter((n) => n.endsWith('.pdf'))
.map((n) => join(FIXTURES_DIR, 'pdf/step6', n)),
]
expect(files.length).toBeGreaterThanOrEqual(20)
for (const file of files) {
expect(await outcomeOf(pdfFile(file)), file).toBe('editor')
expect(useReportStore.getState().report?.version).toBe(3)
}
}, 60_000)
it('a worker trap or an unreadable file is « Erreur de lecture du PDF. », never a corrupt-PDF message', async () => {
const trapping = useInProcessImportWorker(
throwingEngineLoader(new WebAssembly.RuntimeError('unreachable')),
)
try {
expect(
await outcomeOf(pdfFile(join(FIXTURES_DIR, 'report-v3.bare.pdf'))),
).toBe(fr['home.import.error_generic'])
// The worker's trap never touches the page's own runtime (plan §B.3 isolation).
expect(coreRuntime().status).toBe('ready')
} finally {
trapping.restore()
useInProcessImportWorker()
}
const unreadable = pdfFile(join(FIXTURES_DIR, 'report-v3.bare.pdf'))
Object.defineProperty(unreadable, 'stream', {
value: () => ({
getReader: () => ({
read: () =>
Promise.reject(new DOMException('gone', 'NotReadableError')),
cancel: async () => {},
}),
}),
})
expect(await outcomeOf(unreadable)).toBe(fr['home.import.error_generic'])
})
it('a worker that cannot start gives the engine message', async () => {
setImportWorkerSpawn(() => {
throw new DOMException('blocked', 'SecurityError')
})
try {
expect(
await outcomeOf(pdfFile(join(FIXTURES_DIR, 'report-v3.bare.pdf'))),
).toBe(fr['home.import.error_core_unavailable'])
} finally {
useInProcessImportWorker()
}
})
})
@@ -1,22 +1,45 @@
/**
* The rescue copy (`JSON.stringify(report)`, the downloaded format) imported from Home:
* routed by content from the first KiB, read by the core's draft restore, opened as the
* original report; every refusal leaves the store and the tab's slots untouched.
* routed by content from the first KiB, read in the import worker by the core's copy
* restore (strict UTF-8, one BOM dropped; the real worker logic in-process on the shipped
* module, R4.3), opened as the original report; every refusal leaves the store and the
* tab's slots untouched.
*/
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 { MemoryRouter, Route, Routes } from 'react-router-dom'
import HomeRoute from '@/app/routes/HomeRoute'
import { MAX_ATTACHMENT_BYTES } from '@/pdf/import'
import { MAX_COPY_BYTES } from '@/core/worker/handler'
import { DRAFT_KEY, QUARANTINE_KEYS } from '@/state/persistence'
import { useReportStore } from '@/state/reportStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { SheetIndex } from '@/services/sheetIndex'
import { buildGoldenReports, GOLDEN_REPORTS } from '../helpers/goldenReports'
import {
answeringEngineLoader,
useInProcessImportWorker,
type FakeWorker,
} from '../helpers/importWorker'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
let sheet: SheetIndex
let workers: FakeWorker[]
let restoreSpawn: () => void
beforeAll(async () => {
sheet = await loadTestSheet()
@@ -24,11 +47,17 @@ beforeAll(async () => {
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
sessionStorage.clear()
useReportStore.setState({ report: null, archive: null, activeObservationIndex: 0 })
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
})
useReportStore.getState().clearTrapped()
vi.stubGlobal('alert', vi.fn())
;({ workers, restore: restoreSpawn } = useInProcessImportWorker())
})
afterEach(() => {
restoreSpawn()
cleanup()
vi.unstubAllGlobals()
vi.restoreAllMocks()
@@ -54,13 +83,16 @@ const copyFile = (text: string, name = 'faunosteo-brouillon.json') =>
new File([text], name, { type: 'application/json' })
describe('opening a rescue copy', () => {
it.each(GOLDEN_REPORTS)('reopens the %s report equal to the original', async (name) => {
const { reports } = buildGoldenReports(sheet)
await pick(copyFile(JSON.stringify(reports[name])))
await screen.findByTestId('editor-route')
expect(useReportStore.getState().report).toEqual(reports[name])
expect(useReportStore.getState().archive).toBeNull()
})
it.each(GOLDEN_REPORTS)(
'reopens the %s report equal to the original',
async (name) => {
const { reports } = buildGoldenReports(sheet)
await pick(copyFile(JSON.stringify(reports[name])))
await screen.findByTestId('editor-route')
expect(useReportStore.getState().report).toEqual(reports[name])
expect(useReportStore.getState().archive).toBeNull()
},
)
it('routes by content: no .json name, no type, leading blanks and a BOM', async () => {
const report = minimalReport(sheet, { conclusion: 'sniffed' })
@@ -91,7 +123,11 @@ describe('opening a rescue copy', () => {
})
it('still sends a PDF down the PDF path', async () => {
await pick(new File(['%PDF-1.7 not really'], 'bilan.pdf', { type: 'application/pdf' }))
await pick(
new File(['%PDF-1.7 not really'], 'bilan.pdf', {
type: 'application/pdf',
}),
)
await screen.findByText(fr['home.import.error_no_attachment']!)
expect(screen.queryByText(fr['home.import.error_invalid_copy']!)).toBeNull()
})
@@ -104,7 +140,10 @@ describe('refusing a file', () => {
['an unsupported version', () => copyFile(JSON.stringify({ version: 1 }))],
[
'a lone surrogate',
() => copyFile(JSON.stringify(minimalReport(sheet, { conclusion: 'a\ud800b' }))),
() =>
copyFile(
JSON.stringify(minimalReport(sheet, { conclusion: 'a\ud800b' })),
),
],
[
'invalid UTF-8',
@@ -121,29 +160,44 @@ describe('refusing a file', () => {
expect(screen.queryByTestId('editor-route')).toBeNull()
}
it.each(refused)('%s: error line, nothing changed, nothing held', async (_label, make) => {
sessionStorage.setItem(DRAFT_KEY, unreadable)
await pick(make())
await expectUntouched()
})
it.each(refused)(
'%s: error line, nothing changed, nothing held',
async (_label, make) => {
sessionStorage.setItem(DRAFT_KEY, unreadable)
await pick(make())
await expectUntouched()
expect(workers).toHaveLength(1)
},
)
it('an over-cap file is refused before any full read', async () => {
sessionStorage.setItem(DRAFT_KEY, unreadable)
const file = copyFile('{"version":3}')
Object.defineProperty(file, 'size', { value: MAX_ATTACHMENT_BYTES + 1 })
Object.defineProperty(file, 'size', { value: MAX_COPY_BYTES + 1 })
const read = vi.spyOn(file, 'arrayBuffer')
const stream = vi.spyOn(file, 'stream')
await pick(file)
await expectUntouched()
expect(read).not.toHaveBeenCalled()
expect(stream).not.toHaveBeenCalled()
expect(workers).toHaveLength(0)
})
it.each(['internal', 'rebase_failed'])(
'the core kind %s takes the engine message path, not the copy message',
async (kind) => {
vi.spyOn(sheet.ops, 'restoreDraft').mockReturnValue({ ok: false, kind } as never)
// The worker's core answers the kind (the seam replaced; afterEach restores it).
const refusal = JSON.stringify({ ok: false, kind, detail: '' })
;({ workers, restore: restoreSpawn } = useInProcessImportWorker(
answeringEngineLoader(refusal),
))
await pick(copyFile(JSON.stringify(minimalReport(sheet))))
await screen.findByText(fr['home.import.error_core_unavailable']!)
expect(screen.queryByText(fr['home.import.error_invalid_copy']!)).toBeNull()
// The worker answered: the line is the core's kind, not a worker failure.
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
expect(
screen.queryByText(fr['home.import.error_invalid_copy']!),
).toBeNull()
expect(useReportStore.getState().report).toBeNull()
},
)
+108 -32
View File
@@ -2,10 +2,30 @@
* The engine stopped for good (three recoveries spent, owner decision 2026-10-15): the
* stopped notice on every core page, its two variants (tab copy saved: reload; tab storage
* full: download a copy), no retry anywhere, the editor's save banner with its copy button,
* and the stopped alerts (the beforeunload guard has its own hook test).
* and the stopped alerts (the beforeunload guard has its own hook test). The Home import
* refuses up front once the engine stopped (no import worker spawned); while it runs, the
* worker reads the file (in-process, R4.3) and the page's own index failing after it
* accepted gives the engine line.
*/
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import {
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
vi,
} from 'vitest'
import {
act,
cleanup,
fireEvent,
render,
screen,
waitFor,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter } from 'react-router-dom'
import EditorRoute from '@/app/routes/EditorRoute'
@@ -14,23 +34,25 @@ import NewReportRoute from '@/app/routes/NewReportRoute'
import CoreUnavailable from '@/components/report/CoreUnavailable'
import { coreRuntime } from '@/core/runtime'
import { exportReportToPdf } from '@/pdf/export'
import { readAttachment } from '@/pdf/import'
import { loadSheetIndex } from '@/sheets/useSheet'
import { useReportStore } from '@/state/reportStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { SheetIndex } from '@/services/sheetIndex'
import { controlStopped } from '../helpers/core'
import { FIXTURES_DIR } from '../helpers/goldens'
import {
useInProcessImportWorker,
type FakeWorker,
} from '../helpers/importWorker'
import { loadTestSheet, minimalReport } from '../helpers/sheet'
vi.mock('@/pdf/export', () => ({ exportReportToPdf: vi.fn() }))
vi.mock('@/pdf/import', async (importOriginal) => ({
...(await importOriginal<typeof import('@/pdf/import')>()),
readAttachment: vi.fn(),
}))
vi.mock('@/sheets/useSheet', async (importOriginal) => ({
...(await importOriginal<typeof import('@/sheets/useSheet')>()),
loadSheetIndex: vi.fn((await importOriginal<typeof import('@/sheets/useSheet')>()).loadSheetIndex),
loadSheetIndex: vi.fn(
(await importOriginal<typeof import('@/sheets/useSheet')>()).loadSheetIndex,
),
}))
let sheet: SheetIndex
@@ -46,8 +68,14 @@ beforeEach(() => {
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.getState().hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
useReportStore.setState({
report: null,
archive: null,
activeObservationIndex: 0,
})
useReportStore
.getState()
.hydrateImport({ report: minimalReport(sheet), archive: null }, sheet)
setStopped = controlStopped(coreRuntime(), true)
blobs.length = 0
URL.createObjectURL = vi.fn((blob: Blob | MediaSource) => {
@@ -55,7 +83,9 @@ beforeEach(() => {
return 'blob:faunosteo-test'
})
URL.revokeObjectURL = vi.fn()
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => undefined)
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(
() => undefined,
)
})
afterEach(() => {
setStopped(false)
@@ -74,9 +104,12 @@ function fillStorage() {
expect(useReportStore.getState().saveFailed).toBe(true)
}
const retryButton = () => screen.queryByRole('button', { name: fr['sheet.core_retry'] })
const reloadButton = () => screen.queryByRole('button', { name: fr['core.reload'] })
const copyButton = () => screen.queryByRole('button', { name: fr['session.held.download'] })
const retryButton = () =>
screen.queryByRole('button', { name: fr['sheet.core_retry'] })
const reloadButton = () =>
screen.queryByRole('button', { name: fr['core.reload'] })
const copyButton = () =>
screen.queryByRole('button', { name: fr['session.held.download'] })
function expectSavedVariant() {
expect(screen.getByText(fr['core.stopped']!)).toBeInTheDocument()
@@ -92,7 +125,9 @@ async function expectUnsavedVariant() {
await userEvent.setup().click(copyButton()!)
expect(blobs).toHaveLength(1)
expect(blobs[0]!.type).toBe('application/json')
expect(await blobs[0]!.text()).toBe(JSON.stringify(useReportStore.getState().report))
expect(await blobs[0]!.text()).toBe(
JSON.stringify(useReportStore.getState().report),
)
}
const inRouter = (node: React.ReactNode) =>
@@ -164,22 +199,34 @@ describe('EditorRoute', () => {
await screen.findByText(fr['editor.save_failed']!)
expect(screen.queryByText(fr['core.stopped_unsaved']!)).toBeNull()
await userEvent.setup().click(copyButton()!)
expect(await blobs[0]!.text()).toBe(JSON.stringify(useReportStore.getState().report))
expect(await blobs[0]!.text()).toBe(
JSON.stringify(useReportStore.getState().report),
)
})
it('says the stopped text when the export fails once the engine stopped', async () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
vi.mocked(exportReportToPdf).mockRejectedValue(new Error('core_unavailable'))
vi.mocked(exportReportToPdf).mockRejectedValue(
new Error('core_unavailable'),
)
mountEditor()
await userEvent.setup().click(await screen.findByRole('button', { name: fr['editor.export']! }))
await waitFor(() => expect(window.alert).toHaveBeenCalledWith(fr['core.stopped']))
await userEvent
.setup()
.click(await screen.findByRole('button', { name: fr['editor.export']! }))
await waitFor(() =>
expect(window.alert).toHaveBeenCalledWith(fr['core.stopped']),
)
})
it('says the unsaved stopped text for an export that fails with a full storage', async () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
vi.mocked(exportReportToPdf).mockRejectedValue(new Error('core_unavailable'))
vi.mocked(exportReportToPdf).mockRejectedValue(
new Error('core_unavailable'),
)
mountEditor()
const button = await screen.findByRole('button', { name: fr['editor.export']! })
const button = await screen.findByRole('button', {
name: fr['editor.export']!,
})
fillStorage()
await userEvent.setup().click(button)
await waitFor(() =>
@@ -192,7 +239,9 @@ describe('EditorRoute', () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
vi.mocked(exportReportToPdf).mockRejectedValue(new Error('render exploded'))
mountEditor()
await userEvent.setup().click(await screen.findByRole('button', { name: fr['editor.export']! }))
await userEvent
.setup()
.click(await screen.findByRole('button', { name: fr['editor.export']! }))
await waitFor(() =>
expect(window.alert).toHaveBeenCalledWith(fr['editor.export_failed']),
)
@@ -201,17 +250,25 @@ describe('EditorRoute', () => {
describe('the stopped text on the Home import line and the New report alert', () => {
const realLoad = vi.mocked(loadSheetIndex).getMockImplementation()!
let workers: FakeWorker[]
let restoreSpawn: () => void
beforeEach(() => {
// The page's own index fails: reached only once the worker accepted the file.
vi.mocked(loadSheetIndex).mockResolvedValue(null)
vi.mocked(readAttachment).mockResolvedValue({ version: 3 })
;({ workers, restore: restoreSpawn } = useInProcessImportWorker())
})
afterEach(() => {
restoreSpawn()
vi.mocked(loadSheetIndex).mockImplementation(realLoad)
})
afterEach(() => vi.mocked(loadSheetIndex).mockImplementation(realLoad))
/** Picks a valid Faunosteo PDF: a refusal is never the file's. */
async function pickPdf() {
inRouter(<HomeRoute />)
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
await waitFor(() => expect(button).toBeEnabled())
const file = new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf')
const bytes = readFileSync(resolve(FIXTURES_DIR, 'report-v3.bare.pdf'))
const file = new File([bytes], 'bilan.pdf', { type: 'application/pdf' })
fireEvent.change(document.querySelector('input[type="file"]')!, {
target: { files: [file] },
})
@@ -220,37 +277,56 @@ describe('the stopped text on the Home import line and the New report alert', ()
async function submitNewReport() {
const user = userEvent.setup()
inRouter(<NewReportRoute />)
await user.type(screen.getByLabelText(fr['form.practitioner.name']!), 'Dr X')
await user.type(
screen.getByLabelText(fr['form.practitioner.name']!),
'Dr X',
)
await user.type(screen.getByLabelText(fr['form.animal.name']!), 'Rex')
await user.type(screen.getByLabelText(fr['form.owner.name']!), 'Y')
const submit = screen.getByRole('button', { name: fr['new_report.submit']! })
const submit = screen.getByRole('button', {
name: fr['new_report.submit']!,
})
await waitFor(() => expect(submit).toBeEnabled())
await user.click(submit)
// A report is in progress (beforeEach): replacing it is confirmed first.
await user.click(await screen.findByRole('button', { name: fr['session.replace.confirm']! }))
await user.click(
await screen.findByRole('button', {
name: fr['session.replace.confirm']!,
}),
)
}
it('Home import: the engine error line says the stopped text', async () => {
await pickPdf()
await screen.findByText(fr['core.stopped']!, { selector: 'p.text-red-700' })
expect(screen.queryByText(fr['home.import.error_core_unavailable']!)).toBeNull()
expect(
screen.queryByText(fr['home.import.error_core_unavailable']!),
).toBeNull()
expect(workers).toHaveLength(0)
})
it('Home import: the unsaved stopped text when storage is full', async () => {
fillStorage()
await pickPdf()
await screen.findByText(fr['core.stopped_unsaved']!, { selector: 'p.text-red-700' })
await screen.findByText(fr['core.stopped_unsaved']!, {
selector: 'p.text-red-700',
})
expect(workers).toHaveLength(0)
})
it('Home import: keeps the engine error line while the engine is not stopped', async () => {
setStopped(false)
await pickPdf()
await screen.findByText(fr['home.import.error_core_unavailable']!)
// The worker read the file: the line is the page's index failing after it.
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
})
it('New report: the alert says the stopped text', async () => {
await submitNewReport()
await waitFor(() => expect(window.alert).toHaveBeenCalledWith(fr['core.stopped']))
await waitFor(() =>
expect(window.alert).toHaveBeenCalledWith(fr['core.stopped']),
)
})
it('New report: the unsaved stopped text when storage is full', async () => {
@@ -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'] }),
+13 -12
View File
@@ -2,8 +2,11 @@
* R2 plan rev 2 (robustness 7): a store action whose core call fails changes
* nothing and answers `{ ok: false }`, and the callers stop: the new-report
* page does not open the editor, the home page shows the import error instead
* of opening a report.
* of opening a report (R4.3: after the import worker, which loads its own
* module, read the file).
*/
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import {
afterEach,
beforeAll,
@@ -11,6 +14,7 @@ import {
describe,
expect,
it,
onTestFinished,
vi,
} from 'vitest'
import {
@@ -25,7 +29,6 @@ import { MemoryRouter, Route, Routes } from 'react-router-dom'
import HomeRoute from '@/app/routes/HomeRoute'
import NewReportRoute from '@/app/routes/NewReportRoute'
import { setCoreRuntime } from '@/core/runtime'
import { readAttachment } from '@/pdf/import'
import { bundledSheetProvider } from '@/sheets/provider'
import { setSheetProvider } from '@/sheets/useSheet'
import { useReportStore } from '@/state/reportStore'
@@ -33,18 +36,14 @@ import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { SheetIndex } from '@/services/sheetIndex'
import { testCoreRuntime, unavailableCoreRuntime } from '../helpers/core'
import { FIXTURES_DIR } from '../helpers/goldens'
import { useInProcessImportWorker } from '../helpers/importWorker'
import {
emptyObservation,
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
beforeAll(async () => {
sheet = await loadTestSheet()
@@ -152,7 +151,8 @@ describe('callers stop on a failed core call', () => {
})
it('the home page shows the import error when the core cannot load', async () => {
readMock.mockResolvedValue({ version: 3 })
const { workers, restore } = useInProcessImportWorker()
onTestFinished(restore)
setCoreRuntime(unavailableCoreRuntime())
setSheetProvider(bundledSheetProvider)
render(
@@ -165,13 +165,14 @@ describe('callers stop on a failed core call', () => {
)
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
await waitFor(() => expect(button).toBeEnabled())
const file = new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
type: 'application/pdf',
})
const bytes = readFileSync(resolve(FIXTURES_DIR, 'report-v3.bare.pdf'))
const file = new File([bytes], 'bilan.pdf', { type: 'application/pdf' })
fireEvent.change(document.querySelector('input[type="file"]')!, {
target: { files: [file] },
})
await screen.findByText(fr['home.import.error_core_unavailable']!)
// The worker read the file; the page's core, needed to open it, did not load.
expect(workers[0]!.posted.map((m) => m.type)).toEqual(['ready', 'result'])
expect(screen.queryByTestId('editor-route')).toBeNull()
expect(useReportStore.getState().report).toBeNull()
})