/** * A document boot script failing must never disable interactive filling — * the invariant behind the i-140 class of bugs (Adobe's `!ADBE::…VersChk…` * boilerplate calling APIs we don't emulate). A boot error degrades to a * `script-error` diagnostic; the user's own commit still applies. Boot-phase * UI effects are tagged so embedders can suppress doc-open nags. */ import { describe, expect, it, vi } from 'vitest'; import { toPageRef, type DocumentHandle, type ChangeOp, type FormSnapshot, } from '@embedpdf/engine-core/runtime'; import type { ScriptSandbox } from '@embedpdf/core-js-sandbox'; import type { ScriptInput, ScriptOutput } from '@embedpdf/core-acrojs'; import { engineScripts } from './helpers/engine-scripts'; import { standaloneRealm } from './helpers/standalone-realm'; const okEvent = (value: unknown = '') => ({ rc: true, value: value as never, change: '', selStart: 0, selEnd: 0, }); const output = (over: Partial = {}): ScriptOutput => ({ event: okEvent(), formEffects: [], annotEffects: [], uiEffects: [], diagnostics: [], ...over, }); /** A sandbox whose boot throws (script called an API we don't emulate). */ function failingBootSandbox(): ScriptSandbox { return { disposed: false, boot: () => output({ uiEffects: [{ type: 'alert', message: 'This PDF requires a newer version…', icon: 1 }], error: { kind: 'exception', message: 'not a function' }, }), run: (_source: string, input: ScriptInput) => output({ event: okEvent((input.event as { value?: unknown }).value) }), dispose: () => {}, } as unknown as ScriptSandbox; } function textField(objnum: number, name: string) { return { ref: { kind: 'objectNumber' as const, objectNumber: objnum }, name, kind: 'text' as const, origin: 'acroform' as const, readOnly: false, required: false, noExport: false, alternateName: null, mappingName: null, createdBy: null, createdAt: null, filledBy: null, filledByName: null, filledAt: null, importedBy: null, widgets: [{ objectNumber: objnum + 100, page: toPageRef(3) }], value: '', defaultValue: '', valueEntry: { kind: 'none' as const }, defaultValueEntry: { kind: 'none' as const }, maxLength: null, multiline: false, password: false, comb: false, }; } function makeDoc(applied: ChangeOp[][]) { return { id: 'diag-doc', security: { identity: null }, actions: { get: async () => ({ nameTreeScripts: [ { name: '!ADBE::VersChk', action: { incomplete: false, root: { type: 'javascript', subtype: 'JavaScript', script: 'app.findComponent({cType:"Plugin"});', next: [], }, }, }, ], }), }, apply: vi.fn(async ({ ops }: { ops: ChangeOp[] }) => { applied.push(ops); return { items: [], meta: { undoable: true } as never }; }), forms: { list: async () => snapshot() }, } as unknown as DocumentHandle; } const snapshot = (): FormSnapshot => ({ formKind: 'acroform', needsAppearances: false, calculationOrder: [], fields: [textField(7, 'First_Name')], widgets: [], }) as unknown as FormSnapshot; describe('FormScriptingController — boot failures degrade, never brick', () => { it('a throwing boot script still lets the user commit (and again after)', async () => { const applied: ChangeOp[][] = []; const doc = makeDoc(applied); const realm = standaloneRealm(doc, () => null, { sandboxFactory: async () => failingBootSandbox(), }); const controller = engineScripts({ doc, document: () => null, transaction: realm.transaction, budget: realm.budget, }); const first = await controller.commit( { kind: 'objectNumber', objectNumber: 7 }, { value: 'HELLO' }, ); expect(first.status).toBe('applied'); expect(applied[0]).toEqual([ { type: 'forms.setValue', field: { kind: 'objectNumber', objectNumber: 7 }, value: { value: 'HELLO' }, }, ]); // The failure is surfaced, not swallowed — and not fatal. expect(first.diagnostics.some((diagnostic) => diagnostic.code === 'script-error')).toBe(true); // The bogus doc-open alert is tagged as boot-phase (suppressible). expect(first.uiEffects).toEqual([expect.objectContaining({ type: 'alert', phase: 'boot' })]); // Boot ran once; the next commit neither retries nor fails. const second = await controller.commit( { kind: 'objectNumber', objectNumber: 7 }, { value: 'WORLD' }, ); expect(second.status).toBe('applied'); expect(second.diagnostics.some((diagnostic) => diagnostic.code === 'script-error')).toBe(false); controller.dispose(); }); it('a failing doc.actions.get also degrades to a diagnostic', async () => { const applied: ChangeOp[][] = []; const doc = makeDoc(applied); // The fake's `read` is a plain async function, not an AbortablePromise. (doc.actions as unknown as { get: () => Promise }).get = async () => { throw new Error('actions unavailable'); }; const realm = standaloneRealm(doc, () => null, { sandboxFactory: async () => failingBootSandbox(), }); const controller = engineScripts({ doc, document: () => null, transaction: realm.transaction, budget: realm.budget, }); const result = await controller.commit( { kind: 'objectNumber', objectNumber: 7 }, { value: 'HELLO' }, ); expect(result.status).toBe('applied'); expect(result.diagnostics.some((diagnostic) => diagnostic.code === 'script-error')).toBe(true); controller.dispose(); }); });