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:
1 parent
52471dbbc0
commit
6d1cce6e06
26 files changed
+2164
-247
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useRef, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type {
|
||||
ArchiveReadOutcome,
|
||||
RebaseOutcome,
|
||||
@@ -10,21 +10,23 @@ import { CoreStoppedBanner } from '@/components/report/CoreStoppedNotice'
|
||||
import ReplaceSessionDialog from '@/components/report/ReplaceSessionDialog'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Card from '@/components/ui/Card'
|
||||
import {
|
||||
importAttachment,
|
||||
ImportError,
|
||||
MAX_ATTACHMENT_BYTES,
|
||||
MAX_PDF_BYTES,
|
||||
readAttachment,
|
||||
readRescueCopy,
|
||||
} from '@/pdf/import'
|
||||
import { coreRuntime, coreStopped } from '@/core/runtime'
|
||||
import type { ImportSettlement } from '@/core/worker/client'
|
||||
import { cancelImport, startImport } from '@/core/worker/controller'
|
||||
import { MAX_COPY_BYTES, MAX_PDF_BYTES } from '@/core/worker/handler'
|
||||
import { copyRestoreOf, ImportError, pdfImportOf } from '@/core/worker/outcome'
|
||||
import type { ImportTaskKind } from '@/core/worker/protocol'
|
||||
import {
|
||||
isSessionInProgress,
|
||||
useReportStore,
|
||||
type ImportedSession,
|
||||
} from '@/state/reportStore'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadSheetIndex, useSheetAvailability } from '@/sheets/useSheet'
|
||||
import {
|
||||
loadSheetBundleText,
|
||||
loadSheetIndex,
|
||||
useSheetAvailability,
|
||||
} from '@/sheets/useSheet'
|
||||
import { DEFAULT_SPECIES_ID } from './NewReportRoute'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { coreFailureMessage } from '@/core/failure'
|
||||
@@ -119,6 +121,8 @@ function importErrorMessage(err: unknown, t: Translate): string {
|
||||
return t('home.import.error_invalid_copy')
|
||||
case 'core_unavailable':
|
||||
return coreFailureMessage(t, 'home.import.error_core_unavailable')
|
||||
case 'generic':
|
||||
return t('home.import.error_generic')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,20 +147,39 @@ function firstContentByte(head: Uint8Array): number | undefined {
|
||||
* is JSON (an object) or is named as such; anything else goes the PDF path and its error.
|
||||
*/
|
||||
function isRescueCopy(file: File, head: Uint8Array): boolean {
|
||||
const start = new TextDecoder('latin1').decode(head.slice(0, PDF_MAGIC.length))
|
||||
const start = new TextDecoder('latin1').decode(
|
||||
head.slice(0, PDF_MAGIC.length),
|
||||
)
|
||||
if (start === PDF_MAGIC) return false
|
||||
if (firstContentByte(head) === 0x7b) return true
|
||||
return file.name.toLowerCase().endsWith('.json') || file.type === 'application/json'
|
||||
return (
|
||||
file.name.toLowerCase().endsWith('.json') ||
|
||||
file.type === 'application/json'
|
||||
)
|
||||
}
|
||||
|
||||
/** The copy's text, refused (never altered) when it is over the cap or not valid UTF-8. */
|
||||
async function readCopyText(file: File): Promise<string> {
|
||||
if (file.size > MAX_ATTACHMENT_BYTES) throw new ImportError('invalid_copy')
|
||||
try {
|
||||
return new TextDecoder('utf-8', { fatal: true }).decode(await file.arrayBuffer())
|
||||
} catch {
|
||||
throw new ImportError('invalid_copy')
|
||||
/**
|
||||
* One task in the disposable import worker (R4.3, plan §B.1.5): the page refuses up front
|
||||
* when its own engine stopped, hands the file (never read here), the sheet payload and
|
||||
* its compiled module to the worker, and reads the settlement. Diagnostics in DEV only.
|
||||
*/
|
||||
async function runImportTask(
|
||||
task: ImportTaskKind,
|
||||
file: File,
|
||||
): Promise<ImportSettlement> {
|
||||
if (coreStopped()) throw new ImportError('core_unavailable')
|
||||
const sheet = await loadSheetBundleText(DEFAULT_SPECIES_ID)
|
||||
if (!sheet) throw new ImportError('core_unavailable')
|
||||
const module = await (
|
||||
coreRuntime().compiledModule() ?? Promise.resolve(null)
|
||||
).catch(() => null)
|
||||
const settlement = await startImport({ task, file, sheet, module })
|
||||
// Diagnostics only (never the outcome: it holds the bilan's names), DEV console only.
|
||||
if (import.meta.env.DEV) {
|
||||
const diag = settlement.kind === 'result' ? settlement.diag : settlement
|
||||
console.debug('[import]', task, diag)
|
||||
}
|
||||
return settlement
|
||||
}
|
||||
|
||||
export default function HomeRoute() {
|
||||
@@ -169,48 +192,64 @@ export default function HomeRoute() {
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [confirmingReplace, setConfirmingReplace] = useState(false)
|
||||
const t = useT()
|
||||
// Leaving Home cancels a running import: its worker is terminated, its result dropped.
|
||||
useEffect(() => cancelImport, [])
|
||||
|
||||
/** A Faunosteo PDF: its attachment, read and rebased by the core on the loaded sheet. */
|
||||
async function readPdf(file: File): Promise<ImportRead> {
|
||||
// The engine refuses the same size; checking first keeps a huge file out of memory.
|
||||
if (file.size > MAX_PDF_BYTES) throw new ImportError('invalid_pdf')
|
||||
const bytes = new Uint8Array(await file.arrayBuffer())
|
||||
const attachment = await readAttachment(bytes)
|
||||
/** The page's own index, loaded after the worker accepted the file, for `hydrateImport`. */
|
||||
async function mainIndex(): Promise<SheetIndex> {
|
||||
const index = await loadSheetIndex(DEFAULT_SPECIES_ID)
|
||||
if (!index) throw new ImportError('core_unavailable')
|
||||
const { read: outcome, rebase } = importAttachment(attachment, index)
|
||||
return index
|
||||
}
|
||||
|
||||
/** A Faunosteo PDF: its attachment, read and rebased by the worker's core on the sheet. */
|
||||
async function readPdf(file: File): Promise<ImportRead> {
|
||||
// The worker and the core refuse the same size; checking first reads nothing.
|
||||
if (file.size > MAX_PDF_BYTES) throw new ImportError('invalid_pdf')
|
||||
const { read: outcome, rebase } = pdfImportOf(
|
||||
await runImportTask('pdf', file),
|
||||
)
|
||||
return {
|
||||
session: { report: rebase.report, archive: outcome.archive },
|
||||
index,
|
||||
index: await mainIndex(),
|
||||
lines: importMessages(outcome, rebase, t),
|
||||
}
|
||||
}
|
||||
|
||||
/** A rescue copy: read before any session change, so a refusal leaves the tab untouched. */
|
||||
async function readCopy(file: File): Promise<ImportRead> {
|
||||
const text = await readCopyText(file)
|
||||
const index = await loadSheetIndex(DEFAULT_SPECIES_ID)
|
||||
if (!index) throw new ImportError('core_unavailable')
|
||||
const rebase = readRescueCopy(text, index)
|
||||
if (file.size > MAX_COPY_BYTES) throw new ImportError('invalid_copy')
|
||||
const { rebase } = copyRestoreOf(await runImportTask('copy', file))
|
||||
const count = String(rebase.unresolvedZones + rebase.unresolvedFindings)
|
||||
return {
|
||||
session: { report: rebase.report, archive: null },
|
||||
index,
|
||||
lines: rebase.rebased ? [t('editor.draft_rebased').replace('{count}', count)] : [],
|
||||
index: await mainIndex(),
|
||||
lines: rebase.rebased
|
||||
? [t('editor.draft_rebased').replace('{count}', count)]
|
||||
: [],
|
||||
}
|
||||
}
|
||||
|
||||
/** Read, rebase onto the loaded sheet, open; the editor always works on that sheet. */
|
||||
async function importFile(file: File) {
|
||||
const head = new Uint8Array(await file.slice(0, SNIFF_BYTES).arrayBuffer())
|
||||
const read = isRescueCopy(file, head) ? await readCopy(file) : await readPdf(file)
|
||||
const reportAtPick = useReportStore.getState().report
|
||||
const read = isRescueCopy(file, head)
|
||||
? await readCopy(file)
|
||||
: await readPdf(file)
|
||||
// The session changed under the import (another tab action): its result is dropped.
|
||||
if (useReportStore.getState().report !== reportAtPick) return
|
||||
const opened = hydrateImport(read.session, read.index)
|
||||
// A tab text that could not be set aside would be overwritten: resolve it first.
|
||||
if (!opened.ok) {
|
||||
// A core failure reading the tab's draft is an engine error, not a held text.
|
||||
if (opened.reason !== 'held') throw new ImportError('core_unavailable')
|
||||
window.alert(
|
||||
t(opened.engine ? 'session.held_blocked_engine' : 'session.held_blocked'),
|
||||
t(
|
||||
opened.engine
|
||||
? 'session.held_blocked_engine'
|
||||
: 'session.held_blocked',
|
||||
),
|
||||
)
|
||||
navigate('/editor')
|
||||
return
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
import {
|
||||
isWorkerMessage,
|
||||
PROTOCOL_VERSION,
|
||||
type ImportDiag,
|
||||
type ImportTaskKind,
|
||||
} from './protocol'
|
||||
import { PausableTimer, type Visibility, documentVisibility } from './timers'
|
||||
|
||||
/**
|
||||
* The page side of one import (R4.3, plan §B.1.5 items 1, 3 and 5): creates the worker
|
||||
* in a try/catch (a `SecurityError` is synchronous), installs the listeners before posting,
|
||||
* arms the start timer until `ready` then the task timer, settles once on the first of
|
||||
* `result`, `failed`, an `error` event, a timer, or a cancel, and terminates the worker in
|
||||
* every case. Everything after the first settlement is ignored.
|
||||
*/
|
||||
|
||||
/** What `new Worker(…)` gives, narrowed to what the client uses (a test fake fits). */
|
||||
export interface WorkerLike {
|
||||
postMessage(message: unknown): void
|
||||
terminate(): void
|
||||
onmessage: ((event: MessageEvent) => void) | null
|
||||
onerror: ((event: ErrorEvent) => void) | null
|
||||
onmessageerror: ((event: MessageEvent) => void) | null
|
||||
}
|
||||
|
||||
export interface ImportTaskInput {
|
||||
task: ImportTaskKind
|
||||
file: File
|
||||
sheet: string
|
||||
module: WebAssembly.Module | null
|
||||
}
|
||||
|
||||
/**
|
||||
* `worker_unavailable`: the worker could not be created, failed before `ready`, or did not
|
||||
* answer `ready` in time; `crashed`: a trap or an `error` event during the task;
|
||||
* `timeout`: the task timer; `read`: the file could not be read; `engine`: the module
|
||||
* refused the sheet or failed otherwise during the task; `cancelled`: the page gave up.
|
||||
*/
|
||||
export type ImportFailureReason =
|
||||
| 'worker_unavailable'
|
||||
| 'crashed'
|
||||
| 'timeout'
|
||||
| 'read'
|
||||
| 'engine'
|
||||
| 'cancelled'
|
||||
|
||||
export type ImportSettlement =
|
||||
| { kind: 'result'; out: string; diag: ImportDiag }
|
||||
| { kind: 'failed'; reason: ImportFailureReason; detail: string }
|
||||
|
||||
export interface ImportClientOptions {
|
||||
spawn: () => WorkerLike
|
||||
/** Until `ready`: the module's fetch, compile and instantiation. */
|
||||
startTimeoutMs: number
|
||||
/** From `ready` to the result (plan §B.3: 20 s hypothesis, set at J1). */
|
||||
taskTimeoutMs: number
|
||||
visibility?: Visibility
|
||||
}
|
||||
|
||||
export interface ImportRun {
|
||||
settled: Promise<ImportSettlement>
|
||||
/** Terminates the worker and settles `cancelled` unless already settled. */
|
||||
cancel(): void
|
||||
}
|
||||
|
||||
/** The failure reason of a `failed` message, per §B.1.8. */
|
||||
function reasonOf(phase: 'start' | 'task', kind: string): ImportFailureReason {
|
||||
if (phase === 'start') return 'worker_unavailable'
|
||||
if (kind === 'trap') return 'crashed'
|
||||
if (kind === 'read') return 'read'
|
||||
return 'engine'
|
||||
}
|
||||
|
||||
export function runImportTask(
|
||||
input: ImportTaskInput,
|
||||
options: ImportClientOptions,
|
||||
): ImportRun {
|
||||
let worker: WorkerLike | null = null
|
||||
let timer: PausableTimer | null = null
|
||||
let ready = false
|
||||
let done = false
|
||||
let resolve!: (settlement: ImportSettlement) => void
|
||||
const settled = new Promise<ImportSettlement>((r) => {
|
||||
resolve = r
|
||||
})
|
||||
const visibility = options.visibility ?? documentVisibility
|
||||
|
||||
const settle = (settlement: ImportSettlement) => {
|
||||
if (done) return
|
||||
done = true
|
||||
timer?.clear()
|
||||
timer = null
|
||||
worker?.terminate()
|
||||
worker = null
|
||||
resolve(settlement)
|
||||
}
|
||||
const fail = (reason: ImportFailureReason, detail: string) =>
|
||||
settle({ kind: 'failed', reason, detail })
|
||||
const arm = (ms: number, reason: ImportFailureReason) => {
|
||||
timer?.clear()
|
||||
timer = new PausableTimer(
|
||||
ms,
|
||||
() => fail(reason, `${reason} after ${ms} ms`),
|
||||
visibility,
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
worker = options.spawn()
|
||||
} catch (error) {
|
||||
fail(
|
||||
'worker_unavailable',
|
||||
error instanceof Error ? error.name : 'spawn failed',
|
||||
)
|
||||
return { settled, cancel: () => fail('cancelled', 'cancelled') }
|
||||
}
|
||||
worker.onmessage = (event) => {
|
||||
const data: unknown = event.data
|
||||
if (!isWorkerMessage(data)) return
|
||||
if (data.type === 'ready') {
|
||||
ready = true
|
||||
arm(options.taskTimeoutMs, 'timeout')
|
||||
} else if (data.type === 'result') {
|
||||
settle({ kind: 'result', out: data.out, diag: data.diag })
|
||||
} else {
|
||||
fail(reasonOf(data.phase, data.kind), data.detail)
|
||||
}
|
||||
}
|
||||
// An `error` event is never masked: it settles the import (and reaches the console).
|
||||
worker.onerror = (event) =>
|
||||
fail(ready ? 'crashed' : 'worker_unavailable', event.message)
|
||||
worker.onmessageerror = () =>
|
||||
fail(ready ? 'engine' : 'worker_unavailable', 'messageerror')
|
||||
arm(options.startTimeoutMs, 'worker_unavailable')
|
||||
worker.postMessage({ v: PROTOCOL_VERSION, type: 'task', ...input })
|
||||
return { settled, cancel: () => fail('cancelled', 'cancelled') }
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import workerUrl from './import.worker?worker&url'
|
||||
import {
|
||||
runImportTask,
|
||||
type ImportRun,
|
||||
type ImportSettlement,
|
||||
type ImportTaskInput,
|
||||
} from './client'
|
||||
import type { WorkerLike } from './client'
|
||||
|
||||
/**
|
||||
* One import at a time for the page (R4.3, plan §B.1.5 items 6 and 7): a new start
|
||||
* cancels the running one, leaving Home cancels it, and the worker script is prefetched
|
||||
* once the editor has loaded the engine so that a later offline import can start.
|
||||
*/
|
||||
|
||||
/** Until `ready`: a cold fetch, compile and instantiation on a slow device. */
|
||||
export const IMPORT_START_TIMEOUT_MS = 10_000
|
||||
/** From `ready` to the result (plan §B.3: 20 s hypothesis, confirmed or moved at J1). */
|
||||
export const IMPORT_TIMEOUT_MS = 20_000
|
||||
|
||||
type Spawn = () => WorkerLike
|
||||
|
||||
const spawnWorker: Spawn = () =>
|
||||
new Worker(workerUrl, { type: 'module', name: 'faunosteo-import' })
|
||||
|
||||
let spawn: Spawn = spawnWorker
|
||||
let running: ImportRun | null = null
|
||||
let prefetched = false
|
||||
|
||||
/** Starts an import; a running one is cancelled first (its result dropped). */
|
||||
export function startImport(input: ImportTaskInput): Promise<ImportSettlement> {
|
||||
running?.cancel()
|
||||
const run = runImportTask(input, {
|
||||
spawn,
|
||||
startTimeoutMs: IMPORT_START_TIMEOUT_MS,
|
||||
taskTimeoutMs: IMPORT_TIMEOUT_MS,
|
||||
})
|
||||
running = run
|
||||
void run.settled.then(() => {
|
||||
if (running === run) running = null
|
||||
})
|
||||
return run.settled
|
||||
}
|
||||
|
||||
/** Cancels the running import, if any (leaving Home, a second pick). */
|
||||
export function cancelImport(): void {
|
||||
running?.cancel()
|
||||
running = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Warms the HTTP cache with the worker chunk (an immutable `/assets/` file) so that an
|
||||
* import works offline once the editor has loaded the engine, as it does today. Best
|
||||
* effort: a failure changes nothing.
|
||||
*/
|
||||
export function prefetchImportWorker(): void {
|
||||
if (
|
||||
prefetched ||
|
||||
typeof Worker === 'undefined' ||
|
||||
typeof fetch !== 'function'
|
||||
)
|
||||
return
|
||||
prefetched = true
|
||||
fetch(workerUrl, { credentials: 'same-origin' })
|
||||
.then((response) => response.body?.cancel())
|
||||
.catch(() => undefined)
|
||||
}
|
||||
|
||||
/** Test seam: run the worker logic in-process instead of a real `Worker`. */
|
||||
export function setImportWorkerSpawn(next: Spawn | null): void {
|
||||
spawn = next ?? spawnWorker
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import type { GlueFileBytes, GlueSheetHandle } from '@faunosteo/core-wasm'
|
||||
import { closedRefusal } from '@faunosteo/core-wasm'
|
||||
import {
|
||||
describeError,
|
||||
failureKindOf,
|
||||
PROTOCOL_VERSION,
|
||||
type ImportTaskKind,
|
||||
type ImportTaskMessage,
|
||||
type WorkerFailurePhase,
|
||||
type WorkerMessage,
|
||||
} from './protocol'
|
||||
|
||||
/**
|
||||
* The import worker's logic (R4.3, plan §B.1.5 items 2 and 4), apart from the worker
|
||||
* global so that vitest runs it in-process on the Node module: the engine is injected.
|
||||
* One task per worker: instantiate, answer `ready`, build the sheet handle, stream the
|
||||
* file into the module's memory (the file lives once, never as an ArrayBuffer here), run
|
||||
* the core, answer `result`; every throwable becomes one `failed` with its phase and kind.
|
||||
*/
|
||||
|
||||
/** What the glue gives once instantiated. */
|
||||
export interface WorkerEngine {
|
||||
SheetHandle: new (bundle: string) => GlueSheetHandle
|
||||
FileBytes: new (expected: number) => GlueFileBytes
|
||||
/** The instance's linear memory, for the diag. */
|
||||
memory: WebAssembly.Memory | null
|
||||
}
|
||||
|
||||
export type EngineLoader = (
|
||||
module: WebAssembly.Module | null,
|
||||
) => Promise<WorkerEngine>
|
||||
|
||||
/** The caps the worker checks before reading a byte (the core enforces the same). */
|
||||
export const MAX_PDF_BYTES = 50 * 1024 * 1024
|
||||
export const MAX_COPY_BYTES = 16 * 1024 * 1024
|
||||
|
||||
/** The core's failure envelope, for the caps the worker applies itself. */
|
||||
function refusal(kind: string, detail: string): string {
|
||||
return JSON.stringify({ ok: false, kind, detail })
|
||||
}
|
||||
|
||||
/** The sheet handle, or an error named `SheetRefused` carrying the module's refusal. */
|
||||
function sheetHandle(engine: WorkerEngine, sheet: string): GlueSheetHandle {
|
||||
try {
|
||||
return new engine.SheetHandle(sheet)
|
||||
} catch (error) {
|
||||
const detail = closedRefusal(error)
|
||||
if (detail === null) throw error
|
||||
const refused = new Error(detail)
|
||||
refused.name = 'SheetRefused'
|
||||
throw refused
|
||||
}
|
||||
}
|
||||
|
||||
interface Gathered {
|
||||
bytes: GlueFileBytes
|
||||
chunks: number
|
||||
}
|
||||
|
||||
/** Streams the file into the module's memory, one chunk live at a time. */
|
||||
async function gather(
|
||||
engine: WorkerEngine,
|
||||
file: File,
|
||||
): Promise<Gathered | null> {
|
||||
const bytes = new engine.FileBytes(file.size)
|
||||
const reader = file.stream().getReader()
|
||||
let chunks = 0
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) return { bytes, chunks }
|
||||
chunks += 1
|
||||
// A push refused means the file is not the size it announced: not a Faunosteo file.
|
||||
if (!bytes.push(value)) {
|
||||
await reader.cancel()
|
||||
bytes.free()
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The task's outcome envelope text and the chunk count. */
|
||||
async function runTask(
|
||||
engine: WorkerEngine,
|
||||
handle: GlueSheetHandle,
|
||||
task: ImportTaskKind,
|
||||
file: File,
|
||||
): Promise<{ out: string; chunks: number }> {
|
||||
const cap = task === 'pdf' ? MAX_PDF_BYTES : MAX_COPY_BYTES
|
||||
const kind = task === 'pdf' ? 'invalid_pdf' : 'invalid'
|
||||
if (file.size > cap)
|
||||
return { out: refusal(kind, 'over the size cap'), chunks: 0 }
|
||||
const gathered = await gather(engine, file)
|
||||
if (!gathered)
|
||||
return { out: refusal(kind, 'size changed while reading'), chunks: 0 }
|
||||
const out =
|
||||
task === 'pdf'
|
||||
? handle.import_pdf_bytes(gathered.bytes)
|
||||
: handle.restore_copy_bytes(gathered.bytes)
|
||||
return { out, chunks: gathered.chunks }
|
||||
}
|
||||
|
||||
/**
|
||||
* The handler of the task message: `load` instantiates the module (the posted one, or
|
||||
* fetched), `post` sends the answers. Resolves once the task has settled either way.
|
||||
*/
|
||||
export function createWorkerHandler(
|
||||
load: EngineLoader,
|
||||
post: (message: WorkerMessage) => void,
|
||||
): (data: unknown) => Promise<void> {
|
||||
const fail = (phase: WorkerFailurePhase, error: unknown) =>
|
||||
post({
|
||||
v: PROTOCOL_VERSION,
|
||||
type: 'failed',
|
||||
phase,
|
||||
kind: failureKindOf(error),
|
||||
detail: describeError(error),
|
||||
})
|
||||
return async (data) => {
|
||||
if (!isTaskMessage(data)) return
|
||||
let phase: WorkerFailurePhase = 'start'
|
||||
try {
|
||||
const engine = await load(data.module)
|
||||
post({ v: PROTOCOL_VERSION, type: 'ready' })
|
||||
phase = 'task'
|
||||
const started = performance.now()
|
||||
const handle = sheetHandle(engine, data.sheet)
|
||||
const { out, chunks } = await runTask(
|
||||
engine,
|
||||
handle,
|
||||
data.task,
|
||||
data.file,
|
||||
)
|
||||
const linearBytes = engine.memory?.buffer.byteLength ?? null
|
||||
const diag = { ms: performance.now() - started, linearBytes, chunks }
|
||||
post({ v: PROTOCOL_VERSION, type: 'result', out, diag })
|
||||
} catch (error) {
|
||||
fail(phase, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isTaskMessage(data: unknown): data is ImportTaskMessage {
|
||||
if (typeof data !== 'object' || data === null) return false
|
||||
const record = data as { v?: unknown; type?: unknown; task?: unknown }
|
||||
return (
|
||||
record.v === PROTOCOL_VERSION &&
|
||||
record.type === 'task' &&
|
||||
(record.task === 'pdf' || record.task === 'copy')
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/// <reference lib="webworker" />
|
||||
import init, {
|
||||
FileBytes,
|
||||
SheetHandle,
|
||||
} from '@faunosteo/core-wasm/pkg/faunosteo_wasm.js'
|
||||
import wasmUrl from '@faunosteo/core-wasm/pkg/faunosteo_wasm_bg.wasm?url'
|
||||
import { createWorkerHandler, type WorkerEngine } from './handler'
|
||||
import { describeError, PROTOCOL_VERSION } from './protocol'
|
||||
|
||||
/**
|
||||
* The import worker entry (R4.3, plan §B.1.5 item 2): the glue is imported statically and
|
||||
* bundled into this chunk (a same-origin hashed asset the production CSP allows, no
|
||||
* `import()`, no recovery logic); the module is the compiled one the page posts, else the
|
||||
* asset fetched here. The listener is installed before any await; the whole life runs
|
||||
* inside the handler's try/catch, with `unhandledrejection` as a backstop: every throwable
|
||||
* reaches the page as one `failed` message. The page terminates the worker in every case.
|
||||
*/
|
||||
|
||||
const scope = self as unknown as DedicatedWorkerGlobalScope
|
||||
|
||||
const loadEngine = async (
|
||||
module: WebAssembly.Module | null,
|
||||
): Promise<WorkerEngine> => {
|
||||
const exports = (await init({ module_or_path: module ?? wasmUrl })) as {
|
||||
memory?: WebAssembly.Memory
|
||||
}
|
||||
return { SheetHandle, FileBytes, memory: exports.memory ?? null }
|
||||
}
|
||||
|
||||
const handle = createWorkerHandler(loadEngine, (message) =>
|
||||
scope.postMessage(message),
|
||||
)
|
||||
|
||||
scope.onmessage = (event: MessageEvent<unknown>) => {
|
||||
void handle(event.data)
|
||||
}
|
||||
|
||||
scope.onunhandledrejection = (event: PromiseRejectionEvent) => {
|
||||
scope.postMessage({
|
||||
v: PROTOCOL_VERSION,
|
||||
type: 'failed',
|
||||
phase: 'task',
|
||||
kind: 'other',
|
||||
detail: describeError(event.reason),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import type { ArchiveImport, DraftRestore } from '@faunosteo/core-wasm/types'
|
||||
import type { ImportSettlement, ImportTaskInput } from './client'
|
||||
|
||||
/**
|
||||
* What the import worker's settlement means for the page (R4.3, plan §B.1.8): the core's
|
||||
* outcome envelope mapped to the page's import error kinds, and the worker's own failures
|
||||
* classified so that a technical error, a resource overflow or a timeout is never shown as
|
||||
* a corrupt PDF (OD-5: `generic` for a PDF crash or timeout, `core_unavailable` for a copy).
|
||||
*/
|
||||
|
||||
export type ImportErrorKind =
|
||||
| 'invalid_pdf'
|
||||
| 'no_attachment'
|
||||
| 'attachment_stripped'
|
||||
| 'incompatible_version'
|
||||
| 'lone_surrogate'
|
||||
| 'invalid_copy'
|
||||
| 'core_unavailable'
|
||||
| 'generic'
|
||||
|
||||
export class ImportError extends Error {
|
||||
constructor(
|
||||
readonly kind: ImportErrorKind,
|
||||
/** The report version found, for `incompatible_version`. */
|
||||
readonly foundVersion: number | null = null,
|
||||
/** The technical reason, for the DEV console only. */
|
||||
readonly detail = '',
|
||||
) {
|
||||
super(kind)
|
||||
this.name = 'ImportError'
|
||||
}
|
||||
}
|
||||
|
||||
interface Envelope<T> {
|
||||
ok: boolean
|
||||
value?: T
|
||||
kind?: string
|
||||
found?: number
|
||||
detail?: string
|
||||
}
|
||||
|
||||
/** The envelope text, or `core_unavailable` when it is not one (never a trusted shape). */
|
||||
function parseEnvelope<T>(out: string): Envelope<T> {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(out)
|
||||
if (typeof parsed === 'object' && parsed !== null && 'ok' in parsed) {
|
||||
return parsed as Envelope<T>
|
||||
}
|
||||
} catch {
|
||||
// Not JSON: the module answered garbage, treated as an engine failure below.
|
||||
}
|
||||
throw new ImportError('core_unavailable', null, 'unreadable envelope')
|
||||
}
|
||||
|
||||
/** `SheetHandle.import_pdf` failures, one to one with today's kinds. */
|
||||
function pdfError(envelope: Envelope<unknown>): ImportError {
|
||||
switch (envelope.kind) {
|
||||
case 'invalid_pdf':
|
||||
case 'no_attachment':
|
||||
case 'attachment_stripped':
|
||||
case 'lone_surrogate':
|
||||
return new ImportError(envelope.kind, null, envelope.detail)
|
||||
case 'incompatible_version':
|
||||
return new ImportError('incompatible_version', envelope.found ?? null)
|
||||
case 'invalid':
|
||||
// The rebase refused the loaded sheet's reference: today's generic message.
|
||||
return new ImportError('generic', null, envelope.detail)
|
||||
default:
|
||||
return new ImportError('core_unavailable', null, envelope.detail)
|
||||
}
|
||||
}
|
||||
|
||||
/** Kinds that mean the copy itself cannot be read (`reportStore.UNREADABLE_DRAFT_KINDS`). */
|
||||
const UNREADABLE_COPY = new Set([
|
||||
'invalid',
|
||||
'unsupported_version',
|
||||
'lone_surrogate',
|
||||
])
|
||||
|
||||
function copyError(envelope: Envelope<unknown>): ImportError {
|
||||
if (UNREADABLE_COPY.has(envelope.kind ?? '')) {
|
||||
return new ImportError('invalid_copy', null, envelope.detail)
|
||||
}
|
||||
return new ImportError('core_unavailable', null, envelope.detail)
|
||||
}
|
||||
|
||||
/** The worker's own failures, by task (OD-5). */
|
||||
function workerError(
|
||||
task: ImportTaskInput['task'],
|
||||
settlement: ImportSettlement,
|
||||
): ImportError {
|
||||
if (settlement.kind !== 'failed') throw new Error('not a failure')
|
||||
const { reason, detail } = settlement
|
||||
if (reason === 'read') return new ImportError('generic', null, detail)
|
||||
if (task === 'pdf' && (reason === 'crashed' || reason === 'timeout')) {
|
||||
return new ImportError('generic', null, detail)
|
||||
}
|
||||
return new ImportError('core_unavailable', null, detail)
|
||||
}
|
||||
|
||||
/** The imported archive, or the ImportError the page shows. */
|
||||
export function pdfImportOf(settlement: ImportSettlement): ArchiveImport {
|
||||
if (settlement.kind === 'failed') throw workerError('pdf', settlement)
|
||||
const envelope = parseEnvelope<ArchiveImport>(settlement.out)
|
||||
if (!envelope.ok || envelope.value === undefined) throw pdfError(envelope)
|
||||
return envelope.value
|
||||
}
|
||||
|
||||
/** The restored copy, or the ImportError the page shows. */
|
||||
export function copyRestoreOf(settlement: ImportSettlement): DraftRestore {
|
||||
if (settlement.kind === 'failed') throw workerError('copy', settlement)
|
||||
const envelope = parseEnvelope<DraftRestore>(settlement.out)
|
||||
if (!envelope.ok || envelope.value === undefined) throw copyError(envelope)
|
||||
return envelope.value
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Messages between the page and the import worker (R4.3, plan §B.1.5), tagged `v: 1`.
|
||||
* The page posts one task; the worker answers `ready` once its instance runs, then one
|
||||
* `result` or one `failed`. Anything after the first settlement is ignored by the page.
|
||||
*/
|
||||
|
||||
export const PROTOCOL_VERSION = 1
|
||||
|
||||
export type ImportTaskKind = 'pdf' | 'copy'
|
||||
|
||||
export interface ImportTaskMessage {
|
||||
v: typeof PROTOCOL_VERSION
|
||||
type: 'task'
|
||||
task: ImportTaskKind
|
||||
/** The picked file, never read on the page's thread. */
|
||||
file: File
|
||||
/** The bundled sheet payload text the page's runtime builds its handle from. */
|
||||
sheet: string
|
||||
/** The compiled module of the page's runtime when it holds one; else the worker fetches. */
|
||||
module: WebAssembly.Module | null
|
||||
}
|
||||
|
||||
export interface WorkerReadyMessage {
|
||||
v: typeof PROTOCOL_VERSION
|
||||
type: 'ready'
|
||||
}
|
||||
|
||||
export interface WorkerResultMessage {
|
||||
v: typeof PROTOCOL_VERSION
|
||||
type: 'result'
|
||||
/** The outcome envelope text of the core (`{"ok":true,…}` or `{"ok":false,"kind":…}`). */
|
||||
out: string
|
||||
diag: ImportDiag
|
||||
}
|
||||
|
||||
/** Printed in DEV only, never sent anywhere. */
|
||||
export interface ImportDiag {
|
||||
ms: number
|
||||
linearBytes: number | null
|
||||
chunks: number
|
||||
}
|
||||
|
||||
export type WorkerFailurePhase = 'start' | 'task'
|
||||
|
||||
/**
|
||||
* `trap`: a `WebAssembly.RuntimeError`, or an engine stack overflow (`RangeError`,
|
||||
* `InternalError`); `read`: the file could not be read; `sheet`: the bundled sheet was
|
||||
* refused by the module; `other`: anything else (a glue or network failure at start).
|
||||
*/
|
||||
export type WorkerFailureKind = 'trap' | 'read' | 'sheet' | 'other'
|
||||
|
||||
export interface WorkerFailedMessage {
|
||||
v: typeof PROTOCOL_VERSION
|
||||
type: 'failed'
|
||||
phase: WorkerFailurePhase
|
||||
kind: WorkerFailureKind
|
||||
/** Error name and first message line, for the DEV console only. */
|
||||
detail: string
|
||||
}
|
||||
|
||||
export type WorkerMessage =
|
||||
| WorkerReadyMessage
|
||||
| WorkerResultMessage
|
||||
| WorkerFailedMessage
|
||||
|
||||
/** True for a message of this protocol, from a worker of the same build. */
|
||||
export function isWorkerMessage(data: unknown): data is WorkerMessage {
|
||||
if (typeof data !== 'object' || data === null) return false
|
||||
const record = data as { v?: unknown; type?: unknown }
|
||||
return (
|
||||
record.v === PROTOCOL_VERSION &&
|
||||
(record.type === 'ready' ||
|
||||
record.type === 'result' ||
|
||||
record.type === 'failed')
|
||||
)
|
||||
}
|
||||
|
||||
/** Error name and first message line, bounded, for a `failed` message. */
|
||||
export function describeError(error: unknown): string {
|
||||
const name = error instanceof Error ? error.name : 'Error'
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
return `${name}: ${message}`.split('\n')[0]!.slice(0, 200)
|
||||
}
|
||||
|
||||
/** The failure kind of a throwable, per §B.1.5 item 2. */
|
||||
export function failureKindOf(error: unknown): WorkerFailureKind {
|
||||
if (error instanceof WebAssembly.RuntimeError) return 'trap'
|
||||
if (!(error instanceof Error)) return 'other'
|
||||
if (error.name === 'RangeError' || error.name === 'InternalError')
|
||||
return 'trap'
|
||||
if (error.name === 'NotReadableError' || error.name === 'NotFoundError')
|
||||
return 'read'
|
||||
if (error.name === 'SheetRefused') return 'sheet'
|
||||
return 'other'
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* A one-shot timer that stops counting while the page is hidden (R4.3, plan §B.1.5 item
|
||||
* 3): a phone that backgrounds the tab must not time an import out on return. The
|
||||
* visibility source is injected so tests drive it.
|
||||
*/
|
||||
|
||||
export interface Visibility {
|
||||
hidden(): boolean
|
||||
/** Calls `listener` on every change; returns the unsubscribe function. */
|
||||
onChange(listener: () => void): () => void
|
||||
}
|
||||
|
||||
export const documentVisibility: Visibility = {
|
||||
hidden: () =>
|
||||
typeof document !== 'undefined' && document.visibilityState === 'hidden',
|
||||
onChange: (listener) => {
|
||||
if (typeof document === 'undefined') return () => {}
|
||||
document.addEventListener('visibilitychange', listener)
|
||||
return () => document.removeEventListener('visibilitychange', listener)
|
||||
},
|
||||
}
|
||||
|
||||
export class PausableTimer {
|
||||
private remainingMs: number
|
||||
private startedAt: number | null = null
|
||||
private handle: ReturnType<typeof setTimeout> | null = null
|
||||
private readonly unsubscribe: () => void
|
||||
|
||||
constructor(
|
||||
durationMs: number,
|
||||
private readonly onElapsed: () => void,
|
||||
private readonly visibility: Visibility = documentVisibility,
|
||||
private readonly now: () => number = () => performance.now(),
|
||||
) {
|
||||
this.remainingMs = durationMs
|
||||
this.unsubscribe = visibility.onChange(() => this.sync())
|
||||
this.sync()
|
||||
}
|
||||
|
||||
/** Stops the timer for good; a timer never fires after it. */
|
||||
clear(): void {
|
||||
this.pause()
|
||||
this.unsubscribe()
|
||||
this.remainingMs = Number.POSITIVE_INFINITY
|
||||
}
|
||||
|
||||
private sync(): void {
|
||||
if (this.visibility.hidden()) this.pause()
|
||||
else this.resume()
|
||||
}
|
||||
|
||||
private resume(): void {
|
||||
if (this.handle !== null || !Number.isFinite(this.remainingMs)) return
|
||||
this.startedAt = this.now()
|
||||
this.handle = setTimeout(() => {
|
||||
this.handle = null
|
||||
this.remainingMs = 0
|
||||
this.unsubscribe()
|
||||
this.onElapsed()
|
||||
}, this.remainingMs)
|
||||
}
|
||||
|
||||
private pause(): void {
|
||||
if (this.handle === null) return
|
||||
clearTimeout(this.handle)
|
||||
this.handle = null
|
||||
if (this.startedAt !== null) {
|
||||
this.remainingMs = Math.max(
|
||||
0,
|
||||
this.remainingMs - (this.now() - this.startedAt),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import type { ArchiveImport, RebaseOutcome } from '@faunosteo/core-wasm/types'
|
||||
import type { ArchiveImport } from '@faunosteo/core-wasm/types'
|
||||
import {
|
||||
ArchiveReadError,
|
||||
readAttachedDocument,
|
||||
type ArchiveReadErrorKind,
|
||||
} from '@faunosteo/pdf'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { UNREADABLE_DRAFT_KINDS } from '@/state/reportStore'
|
||||
|
||||
export { MAX_ATTACHMENT_BYTES, MAX_PDF_BYTES } from '@faunosteo/pdf'
|
||||
|
||||
@@ -87,17 +86,3 @@ export async function importReportFromPdf(
|
||||
): Promise<ArchiveImport> {
|
||||
return importAttachment(await readAttachment(pdfBytes), index)
|
||||
}
|
||||
|
||||
/**
|
||||
* A rescue copy (`JSON.stringify(report)`, the byte format of the tab's draft)
|
||||
* read by the core's draft restore, no archive beside it. Content the core
|
||||
* refuses is `invalid_copy`; any other failure is the engine's or the sheet's.
|
||||
*/
|
||||
export function readRescueCopy(text: string, index: SheetIndex): RebaseOutcome {
|
||||
const outcome = index.ops.restoreDraft(text, null)
|
||||
if (outcome.ok) return outcome.value.rebase
|
||||
if (UNREADABLE_DRAFT_KINDS.has(outcome.kind)) {
|
||||
throw new ImportError('invalid_copy')
|
||||
}
|
||||
throw new ImportError('core_unavailable')
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import type { SheetIndexData } from '@faunosteo/core-wasm/types'
|
||||
import { coreRuntime } from '@/core/runtime'
|
||||
import { prefetchImportWorker } from '@/core/worker/controller'
|
||||
import { SheetOps } from '@/core/sheetOps'
|
||||
import { SheetIndex } from '@/services/sheetIndex'
|
||||
import {
|
||||
@@ -50,10 +51,13 @@ async function loadIntoCore(speciesId: string): Promise<SheetLoad> {
|
||||
const data = session.call<SheetIndexData>('index_data', '')
|
||||
if (data.ok) {
|
||||
const index = new SheetIndex(data.value, new SheetOps(session))
|
||||
// The engine is loaded: warm the import worker's chunk for a later offline import.
|
||||
prefetchImportWorker()
|
||||
return { status: 'ready', index }
|
||||
}
|
||||
return {
|
||||
status: data.kind === 'invalid_sheet' ? 'invalid_sheet' : 'core_unavailable',
|
||||
status:
|
||||
data.kind === 'invalid_sheet' ? 'invalid_sheet' : 'core_unavailable',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +77,17 @@ export function loadSheet(speciesId: string): Promise<SheetLoad> {
|
||||
return pending
|
||||
}
|
||||
|
||||
/**
|
||||
* The bundled payload as the text the core's `SheetHandle` reads, for the import worker
|
||||
* (R4.3): the same text the page's own session was built from. Null without a sheet.
|
||||
*/
|
||||
export async function loadSheetBundleText(
|
||||
speciesId: string,
|
||||
): Promise<string | null> {
|
||||
const bundle = await provider.getReferenceSheet(speciesId)
|
||||
return bundle ? JSON.stringify(bundle) : null
|
||||
}
|
||||
|
||||
/** The ready index, or null (tests and callers that need the sheet or nothing). */
|
||||
export async function loadSheetIndex(
|
||||
speciesId: string,
|
||||
@@ -99,7 +114,8 @@ export function useSheet(speciesId: string | undefined): SheetState {
|
||||
void loadSheet(speciesId).then((load) => {
|
||||
if (cancelled) return
|
||||
if (load.status === 'ready' || load.status === 'none') setState(load)
|
||||
else if (load.status === 'invalid_sheet') setState({ status: load.status, retry })
|
||||
else if (load.status === 'invalid_sheet')
|
||||
setState({ status: load.status, retry })
|
||||
else setState({ status: 'core_unavailable', retry })
|
||||
})
|
||||
return () => {
|
||||
|
||||
@@ -55,6 +55,7 @@ export async function trappingCoreRuntime(marker: string) {
|
||||
core_version: glue.core_version,
|
||||
call: glue.call,
|
||||
SheetHandle: Trapping,
|
||||
FileBytes: glue.FileBytes,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import { nodeCoreSource } from '@faunosteo/core-wasm/node'
|
||||
import type { WorkerLike } from '@/core/worker/client'
|
||||
import { setImportWorkerSpawn } from '@/core/worker/controller'
|
||||
import {
|
||||
createWorkerHandler,
|
||||
type EngineLoader,
|
||||
type WorkerEngine,
|
||||
} from '@/core/worker/handler'
|
||||
import type { WorkerMessage } from '@/core/worker/protocol'
|
||||
|
||||
/**
|
||||
* The import worker run in-process (happy-dom has no `Worker`): a `WorkerLike` whose
|
||||
* `postMessage` feeds the worker handler and whose answers come back through `onmessage`
|
||||
* on a later tick, as a real worker's would; `terminate` drops every later answer. The
|
||||
* engine is the Node module by default; a test passes its own loader for failures.
|
||||
*/
|
||||
|
||||
let instances = 0
|
||||
|
||||
/** The shipped module instantiated afresh (one instance per worker, as in the browser). */
|
||||
export const nodeEngineLoader: EngineLoader = async (module) => {
|
||||
const source = nodeCoreSource('pkg')
|
||||
instances += 1
|
||||
const glue = await source.importGlue(1000 + instances)
|
||||
const compiled = module ?? (await source.compile())
|
||||
const exports = (await glue.default({ module_or_path: compiled })) as {
|
||||
memory?: WebAssembly.Memory
|
||||
}
|
||||
return {
|
||||
SheetHandle: glue.SheetHandle,
|
||||
FileBytes: glue.FileBytes,
|
||||
memory: exports.memory ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
export interface FakeWorker extends WorkerLike {
|
||||
terminated: boolean
|
||||
/** Every message the page posted to the worker (the task, as the worker receives it). */
|
||||
received: unknown[]
|
||||
/** Every message the worker posted, dropped or delivered. */
|
||||
posted: WorkerMessage[]
|
||||
}
|
||||
|
||||
export function inProcessWorker(
|
||||
load: EngineLoader = nodeEngineLoader,
|
||||
): FakeWorker {
|
||||
const worker: FakeWorker = {
|
||||
terminated: false,
|
||||
received: [],
|
||||
posted: [],
|
||||
onmessage: null,
|
||||
onerror: null,
|
||||
onmessageerror: null,
|
||||
terminate() {
|
||||
worker.terminated = true
|
||||
},
|
||||
postMessage(message) {
|
||||
worker.received.push(message)
|
||||
void handle(message)
|
||||
},
|
||||
}
|
||||
const handle = createWorkerHandler(load, (message) => {
|
||||
worker.posted.push(message)
|
||||
setTimeout(() => {
|
||||
if (!worker.terminated)
|
||||
worker.onmessage?.({ data: message } as MessageEvent)
|
||||
}, 0)
|
||||
})
|
||||
return worker
|
||||
}
|
||||
|
||||
/** Routes the page's imports through in-process workers; returns the spawned ones. */
|
||||
export function useInProcessImportWorker(
|
||||
load: EngineLoader = nodeEngineLoader,
|
||||
): { workers: FakeWorker[]; restore: () => void } {
|
||||
const workers: FakeWorker[] = []
|
||||
setImportWorkerSpawn(() => {
|
||||
const worker = inProcessWorker(load)
|
||||
workers.push(worker)
|
||||
return worker
|
||||
})
|
||||
return { workers, restore: () => setImportWorkerSpawn(null) }
|
||||
}
|
||||
|
||||
/** An engine whose `SheetHandle` throws `error` on every import call (a trap, say). */
|
||||
export function throwingEngineLoader(error: Error): EngineLoader {
|
||||
return async (module) => {
|
||||
const engine = await nodeEngineLoader(module)
|
||||
class Throwing extends engine.SheetHandle {
|
||||
import_pdf_bytes(): string {
|
||||
throw error
|
||||
}
|
||||
restore_copy_bytes(): string {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
return { ...engine, SheetHandle: Throwing } as WorkerEngine
|
||||
}
|
||||
}
|
||||
|
||||
/** An engine whose import calls answer the envelope text `out` (a core refusal, say). */
|
||||
export function answeringEngineLoader(out: string): EngineLoader {
|
||||
return async (module) => {
|
||||
const engine = await nodeEngineLoader(module)
|
||||
class Answering extends engine.SheetHandle {
|
||||
import_pdf_bytes(): string {
|
||||
return out
|
||||
}
|
||||
restore_copy_bytes(): string {
|
||||
return out
|
||||
}
|
||||
}
|
||||
return { ...engine, SheetHandle: Answering } as WorkerEngine
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Minimal PDFs for the import tests, byte for byte as `pdfWith` of
|
||||
* `scripts/browser/lib/hostile-classes.mjs` writes them (uncompressed streams, no
|
||||
* padding): the core's scanner reads the objects in clear, so an archive is a
|
||||
* `/Type /EmbeddedFile` stream, a `/Filespec` naming it, and the
|
||||
* `/Keywords (faunosteo-archive/1)` stamp.
|
||||
*/
|
||||
|
||||
const ATTACHMENT_NAME = 'osteo-report.json'
|
||||
const STAMP = '<< /Keywords (faunosteo-archive/1) >>'
|
||||
const OTHER_PRODUCER = '<< /Producer (another tool) >>'
|
||||
|
||||
const utf8 = (text: string) => new TextEncoder().encode(text)
|
||||
|
||||
/** Bytes a `File` accepts as a part (never a shared buffer). */
|
||||
type Bytes = Uint8Array<ArrayBuffer>
|
||||
|
||||
interface EmbeddedStream {
|
||||
bytes: Bytes
|
||||
/** The Filespec name; none leaves the stream unnamed. */
|
||||
name?: string
|
||||
}
|
||||
|
||||
function concat(parts: Bytes[]): Bytes {
|
||||
const out = new Uint8Array(parts.reduce((sum, p) => sum + p.byteLength, 0))
|
||||
let at = 0
|
||||
for (const part of parts) {
|
||||
out.set(part, at)
|
||||
at += part.byteLength
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** One EmbeddedFile object (number `n`) and its Filespec (`n + 1`) when named. */
|
||||
function streamObjects(stream: EmbeddedStream, n: number): Bytes[] {
|
||||
const length = stream.bytes.byteLength
|
||||
const parts = [
|
||||
utf8(`${n} 0 obj\n<< /Type /EmbeddedFile /Length ${length}`),
|
||||
utf8(' >>\nstream\n'),
|
||||
stream.bytes,
|
||||
utf8('\nendstream\nendobj\n'),
|
||||
]
|
||||
if (stream.name) {
|
||||
parts.push(
|
||||
utf8(`${n + 1} 0 obj\n<< /Type /Filespec /F (${stream.name})`),
|
||||
utf8(` /EF << /F ${n} 0 R >> >>\nendobj\n`),
|
||||
)
|
||||
}
|
||||
return parts
|
||||
}
|
||||
|
||||
/** A PDF holding `streams`, then the Faunosteo stamp (or another producer's info). */
|
||||
export function pdfWith(
|
||||
streams: EmbeddedStream[],
|
||||
{ stamp = true }: { stamp?: boolean } = {},
|
||||
): Bytes {
|
||||
const parts = [utf8('%PDF-1.7\n%\xE2\xE3\xCF\xD3\n')]
|
||||
let n = 1
|
||||
for (const stream of streams) {
|
||||
parts.push(...streamObjects(stream, n))
|
||||
n += 2
|
||||
}
|
||||
const info = stamp ? STAMP : OTHER_PRODUCER
|
||||
parts.push(utf8(`${n} 0 obj\n${info}\nendobj\n%%EOF\n`))
|
||||
return concat(parts)
|
||||
}
|
||||
|
||||
/** A Faunosteo PDF whose `osteo-report.json` attachment holds `json`. */
|
||||
export function archivePdf(json: string): Bytes {
|
||||
return pdfWith([{ bytes: utf8(json), name: ATTACHMENT_NAME }])
|
||||
}
|
||||
|
||||
/** The stamp without its attachment: a Faunosteo PDF a tool stripped. */
|
||||
export function strippedPdf(): Bytes {
|
||||
return pdfWith([])
|
||||
}
|
||||
|
||||
/** A PDF of another producer: no stamp, no attachment. */
|
||||
export function foreignPdf(): Bytes {
|
||||
return pdfWith([], { stamp: false })
|
||||
}
|
||||
|
||||
/** `bytes` as the file the practitioner picks. */
|
||||
export function pdfFile(bytes: Bytes, name = 'bilan.pdf'): File {
|
||||
return new File([bytes], name, { type: 'application/pdf' })
|
||||
}
|
||||
@@ -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()
|
||||
},
|
||||
)
|
||||
|
||||
@@ -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'] }),
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -36,4 +36,6 @@ export default defineConfig(({ command }) => ({
|
||||
emptyOutDir: true,
|
||||
sourcemap: false,
|
||||
},
|
||||
// The import worker (R4.3) is a module worker: the core glue it bundles is ESM.
|
||||
worker: { format: 'es' },
|
||||
}))
|
||||
@@ -8,8 +8,10 @@
|
||||
export {
|
||||
CoreRuntime,
|
||||
SheetSession,
|
||||
closedRefusal,
|
||||
type CoreSource,
|
||||
type CoreStatus,
|
||||
type GlueFileBytes,
|
||||
type GlueModule,
|
||||
type GlueSheetHandle,
|
||||
} from './runtime'
|
||||
|
||||
@@ -16,7 +16,9 @@ export const CORE_UNAVAILABLE: CoreFailure = {
|
||||
}
|
||||
|
||||
export function failure(kind: string, detail?: string): CoreFailure {
|
||||
return detail === undefined ? { ok: false, kind } : { ok: false, kind, detail }
|
||||
return detail === undefined
|
||||
? { ok: false, kind }
|
||||
: { ok: false, kind, detail }
|
||||
}
|
||||
|
||||
/** Parses the JSON text a core call returned. */
|
||||
|
||||
@@ -144,6 +144,15 @@ export class CoreRuntime {
|
||||
if (this.restartable) void this.retry()
|
||||
}
|
||||
|
||||
/**
|
||||
* The compiled module of this runtime, when a load has started: posted to the import
|
||||
* worker (R4.3, structured clone) so an import needs no second fetch, offline included;
|
||||
* `null` before the first load, the worker then fetches the asset itself.
|
||||
*/
|
||||
compiledModule(): Promise<WebAssembly.Module> | null {
|
||||
return this.compiled
|
||||
}
|
||||
|
||||
/** Runs a free operation. */
|
||||
call<T>(op: string, input: string): CoreOutcome<T> {
|
||||
return this.invoke((glue) => parseOutcome<T>(glue.call(op, input)))
|
||||
@@ -246,7 +255,7 @@ export class CoreRuntime {
|
||||
* `JsError` worded `<kind>: <detail>`), without its `invalid_sheet: ` kind; `null` when the
|
||||
* error is anything else.
|
||||
*/
|
||||
function closedRefusal(error: unknown): string | null {
|
||||
export function closedRefusal(error: unknown): string | null {
|
||||
if (!(error instanceof Error) || error.name !== 'Error') return null
|
||||
const match = /^([a-z_]+): /.exec(error.message)
|
||||
if (!match) return null
|
||||
|
||||
Reference in new issue
Block a user